繁体   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