Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Arduino UNO通过I2c将温度和湿度数据从DHT11传感器发送到Raspberry pi 2(运行windows iot核心)_C#_Windowsiot - Fatal编程技术网

C# Arduino UNO通过I2c将温度和湿度数据从DHT11传感器发送到Raspberry pi 2(运行windows iot核心)

C# Arduino UNO通过I2c将温度和湿度数据从DHT11传感器发送到Raspberry pi 2(运行windows iot核心),c#,windowsiot,C#,Windowsiot,我让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

我让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);
  }
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之间的连接:

  • Arduino UNO(A4-SDA)的模拟引脚连接至树莓Pi 2的引脚5(SCL)
  • Arduino UNO(A5-SCL)的模拟引脚连接至树莓Pi 2的引脚3(SDA)
  • 使用电阻为1K欧姆和2k欧姆的分压器电路为Pi提供3.3V电压,而不是5V电压

  • DHT11传感器与Arduino UNO的连接

  • 问题:我已经在用c#编写的Pi代码上从Visual Studio部署了Universal windows应用程序,但代码中出现了一个异常。异常和错误如下所示:

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

    WinRT信息:无法将连接设置应用于设备。

    附加信息:连接到系统的设备不工作。

    要求:我已经在互联网上搜索了有关此异常的所有内容,但没有找到任何解决方案,并且Raspberry Pi 2无法与Arduino UNO通信。不知道是Arduino方面的问题还是Raspberry Pi方面的问题


    请帮我解决这个问题。

    看来你的线路错了。 通常,SCL引脚连接到SCL,SDA连接到SDA。它不像UART那样反转