RS485传感器组网采集数据通过WiFi上传到onenet实验代码
时间: 2024-03-25 10:41:52 浏览: 112
RS485转wifi
好的,下面是一个示例代码,可以帮助您实现RS485传感器组网并将数据通过WiFi上传到OneNet平台。请注意,这只是一个示例代码,您需要根据您的实际情况进行修改。
```C++
#include <SoftwareSerial.h>
#include <WiFiEsp.h>
#include <Wire.h>
SoftwareSerial RS485Serial(2, 3); //RS485总线连接到Arduino的2号和3号引脚
WiFiEspClient wifiClient;
String ssid = "your_SSID"; //WiFi网络的名称
String password = "your_password"; //WiFi网络的密码
String apiKey = "your_apiKey"; //OneNet平台的apiKey
String deviceId = "your_deviceId"; //OneNet平台的设备ID
void setup() {
Serial.begin(115200);
RS485Serial.begin(9600);
WiFi.init(&Serial);
connectWiFi();
}
void loop() {
//读取传感器数据
float temperature = readTemperature(); //读取温度传感器数据
float humidity = readHumidity(); //读取湿度传感器数据
//将数据上传到OneNet平台
String data = "{\"temperature\":" + String(temperature) + ",\"humidity\":" + String(humidity) + "}";
if (uploadData(data)) {
Serial.println("Upload data successfully!");
} else {
Serial.println("Upload data failed!");
}
delay(10000); //每隔10秒上传一次数据
}
//连接WiFi网络
void connectWiFi() {
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("Connecting to ");
Serial.print(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
}
//读取温度传感器数据
float readTemperature() {
//发送读取温度传感器数据的命令到RS485总线
RS485Serial.write(0x01); //传感器地址
RS485Serial.write(0x03); //读取命令
RS485Serial.write(0x00); //数据起始地址
RS485Serial.write(0x01); //数据长度
RS485Serial.write(0xC5); //CRC校验码
delay(100); //等待传感器响应
//读取传感器返回的数据
byte data[8];
int i = 0;
while (RS485Serial.available()) {
data[i] = RS485Serial.read();
i++;
}
//计算温度值
int rawValue = (data[3] << 8) | data[4];
float temperature = rawValue / 16.0;
return temperature;
}
//读取湿度传感器数据
float readHumidity() {
//发送读取湿度传感器数据的命令到RS485总线
RS485Serial.write(0x02); //传感器地址
RS485Serial.write(0x03); //读取命令
RS485Serial.write(0x00); //数据起始地址
RS485Serial.write(0x01); //数据长度
RS485Serial.write(0x9A); //CRC校验码
delay(100); //等待传感器响应
//读取传感器返回的数据
byte data[8];
int i = 0;
while (RS485Serial.available()) {
data[i] = RS485Serial.read();
i++;
}
//计算湿度值
int rawValue = (data[3] << 8) | data[4];
float humidity = rawValue / 10.0;
return humidity;
}
//将数据上传到OneNet平台
bool uploadData(String data) {
if (wifiClient.connect("api.heclouds.com", 80)) {
String postString = "POST /devices/" + deviceId + "/datapoints HTTP/1.1\r\n";
postString += "Host: api.heclouds.com\r\n";
postString += "Content-Type: application/json\r\n";
postString += "api-key: " + apiKey + "\r\n";
postString += "Content-Length: " + String(data.length()) + "\r\n\r\n";
postString += data;
wifiClient.print(postString);
if (wifiClient.connected()) {
while (wifiClient.available()) {
String response = wifiClient.readStringUntil('\r');
if (response.indexOf("success") > 0) {
wifiClient.stop();
return true;
}
}
} else {
Serial.println("Connection failed");
wifiClient.stop();
return false;
}
} else {
Serial.println("Connection failed");
wifiClient.stop();
return false;
}
}
```
这个示例代码假设您使用了两个传感器,分别是温度传感器和湿度传感器,它们的地址分别为1和2。您需要根据您实际使用的传感器进行修改。当然,您还需要将WiFi网络的名称、密码、OneNet平台的apiKey和设备ID修改为您自己的。
阅读全文