C# Arduino UNO通过I2c将温度和湿度数据从DHT11传感器发送到Raspberry pi 2(运行windows iot核心)
我让Arduino UNO做奴隶,让Raspberry Pi 2做主人。在Arduino UNO上运行的代码如下: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
#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之间的连接:
请帮我解决这个问题。看来你的线路错了。 通常,SCL引脚连接到SCL,SDA连接到SDA。它不像UART那样反转