简体   繁体   中英

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. The code running on Arduino UNO is as follows :

#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#. 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 :

  1. Analog pin of Arduino UNO (A4-SDA) connected to pin 5(SCL) of Raspberry Pi 2
  2. Analog pin of Arduino UNO (A5-SCL) connected to pin 3(SDA) of Raspberry Pi 2
  3. Used a voltage divider circuit with resistances 1K ohm and 2k ohm to give 3.3V to Pi instead of 5V.

  4. DHT11 sensor connections with 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. The exception and error is as follows :

Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.ni.dll

WinRT information: Failed to apply connection settings to the device.

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.

Please help me solve this problem.

It seems Your lines are wrong. Usually SCL pin connects to SCL and SDA connects to SDA. It's not reversed like on UART.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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