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

I have made Arduino UNO as a slave and Raspberry Pi 2 as a master. 我已经将Arduino UNO作为从属设备,并将Raspberry Pi 2作为主设备。 The code running on Arduino UNO is as follows : 在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);
  }

The Raspberry Pi 2 code is written in c#. Raspberry Pi 2代码是用c#编写的。 It is as follows : 如下:

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);

        }

    }
}

Connections done between Raspberry Pi 2 and Arduino UNO : Raspberry Pi 2和Arduino UNO之间完成的连接:

  1. Analog pin of Arduino UNO (A4-SDA) connected to pin 5(SCL) of Raspberry Pi 2 Arduino UNO(A4-SDA)的模拟引脚连接到Raspberry Pi 2的引脚5(SCL)
  2. Analog pin of Arduino UNO (A5-SCL) connected to pin 3(SDA) of Raspberry Pi 2 Arduino UNO(A5-SCL)的模拟引脚连接到Raspberry Pi 2的引脚3(SDA)
  3. Used a voltage divider circuit with resistances 1K ohm and 2k ohm to give 3.3V to Pi instead of 5V. 使用电阻为1K ohm和2k ohm的分压器电路为Pi提供3.3V电压,而不是5V。

  4. DHT11 sensor connections with Arduino UNO. DHT11传感器与Arduino UNO的连接。

Problem : I have deployed Universal windows app from Visual Studio on Pi code written in c# but I am getting an Exception in the code. 问题:我已经用c#编写的Pi代码从Visual Studio部署了通用Windows应用程序,但是代码中出现异常。 The exception and error is as follows : 异常和错误如下:

Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll 引发异常: mscorlib.ni.dll中的“ System.Runtime.InteropServices.COMException”

WinRT information: Failed to apply connection settings to the device. WinRT信息:无法将连接设置应用于设备。

Additional information: A device attached to the system is not functioning. 附加信息:连接到系统的设备不起作用。

Requirement : I have searched on Internet everything regarding this exception but didn't found any solution and Raspberry Pi 2 is unable to communicate with Arduino UNO.Don't know whether it is problem from Arduino side or Raspberry Pi side. 要求:我已经在Internet上搜索了有关此异常的所有信息,但未找到任何解决方案,并且Raspberry Pi 2无法与Arduino UNO通信。不知道这是Arduino方面还是Raspberry Pi方面的问题。

Please help me solve this problem. 请帮我解决这个问题。

It seems Your lines are wrong. 看来您的台词是错误的。 Usually SCL pin connects to SCL and SDA connects to SDA. 通常,SCL引脚连接到SCL,SDA连接到SDA。 It's not reversed like on UART. 它没有像UART那样反转。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM