簡體   English   中英

Arduino UNO通過I2c將DHT11傳感器的溫度和濕度數據發送到Raspberry Pi 2(運行Windows IoT核心)

[英]Arduino UNO sending Temperature and Humidity data from DHT11 sensor to Raspberry pi 2(running windows iot core) through I2c

我已經將Arduino UNO作為從屬設備,並將Raspberry Pi 2作為主設備。 在Arduino UNO上運行的代碼如下:

#include "DHT.h"
#include<Wire.h>
#define DHTPIN 4    // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
#define SLAVE_ADDRESS 0x29

DHT dht(DHTPIN, DHTTYPE);
int t;

void setup() {

  Serial.begin(9600); //setting baud rate for communication
  Wire.begin(SLAVE_ADDRESS); //assigning slave with i2c at defined slave address
  Wire.onRequest(sendData); //Event for sending the data through i2c
  dht.begin();
}

void loop() {

    float h = dht.readHumidity();
// Read temperature as Celsius (the default)
   t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("\n");
  Wire.onRequest(sendData); // asked to send the data 
  delay(1000);
}

void sendData(){
  Wire.write(t);
  Serial.print("in send data:"+t);
  }

Raspberry Pi 2代碼是用c#編寫的。 如下:

using System;
using Windows.UI.Xaml.Controls;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;
using System.Diagnostics;
using System.Threading;

namespace App2
{
    public sealed partial class MainPage : Page
    {

    private I2cDevice Device;
    private Timer periodicTimer;

    public MainPage()
    {
        this.InitializeComponent();
        initcomunica();
    }

    private async void initcomunica()
    {

        var settings = new I2cConnectionSettings(0x29); // Arduino address
        Debug.WriteLine(settings);
        settings.BusSpeed = I2cBusSpeed.StandardMode;
        Debug.WriteLine(settings.BusSpeed);
        string aqs = I2cDevice.GetDeviceSelector("I2C1");
        Debug.WriteLine(aqs);

        var dis = await DeviceInformation.FindAllAsync(aqs);
        Debug.WriteLine(dis);
        Debug.WriteLine(dis[0].Id);
        Device = await I2cDevice.FromIdAsync(dis[0].Id,settings );
periodicTimer = new Timer(this.TimerCallback, null, 0, 1000); // Create a timmer
    }

    private void TimerCallback(object state)
    {
        byte[] RegAddrBuf = new byte[] { 0x08 };
        byte[] ReadBuf = new byte[5];
        try
        {
            Device.Read(ReadBuf); // read the data
            Debug.WriteLine(ReadBuf);

        }
        catch (Exception f)
        {
            Debug.WriteLine("error in reading from buffer"+f.Message);
        }
// Converte  Byte to CharArray
char[] cArray = System.Text.Encoding.UTF8.GetString(ReadBuf, 0,5).ToCharArray(); 

            String c = new String(cArray);
            Debug.WriteLine(c);

        }

    }
}

Raspberry Pi 2和Arduino UNO之間完成的連接:

  1. Arduino UNO(A4-SDA)的模擬引腳連接到Raspberry Pi 2的引腳5(SCL)
  2. Arduino UNO(A5-SCL)的模擬引腳連接到Raspberry Pi 2的引腳3(SDA)
  3. 使用電阻為1K ohm和2k ohm的分壓器電路為Pi提供3.3V電壓,而不是5V。

  4. DHT11傳感器與Arduino UNO的連接。

問題:我已經用c#編寫的Pi代碼從Visual Studio部署了通用Windows應用程序,但是代碼中出現異常。 異常和錯誤如下:

引發異常: mscorlib.ni.dll中的“ System.Runtime.InteropServices.COMException”

WinRT信息:無法將連接設置應用於設備。

附加信息:連接到系統的設備不起作用。

要求:我已經在Internet上搜索了有關此異常的所有信息,但未找到任何解決方案,並且Raspberry Pi 2無法與Arduino UNO通信。不知道這是Arduino方面還是Raspberry Pi方面的問題。

請幫我解決這個問題。

看來您的台詞是錯誤的。 通常,SCL引腳連接到SCL,SDA連接到SDA。 它沒有像UART那樣反轉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM