Arduino ESP8266 上传湿温度数据到 OneNet 并控制舵机(无 OneNet 库)
以下是一个基于 Arduino 和 ESP8266 的代码示例,用于上传湿温度数据到 OneNet,并获取命令以控制舵机的开关。
#include <SoftwareSerial.h>
// 定义串口
SoftwareSerial esp8266(2, 3); // RX, TX
// 定义 WiFi 信息
const char* ssid = 'your_SSID';
const char* password = 'your_PASSWORD';
// 定义 OneNet 的设备 ID 和 API Key
const char* DeviceID = 'your_DEVICE_ID';
const char* APIKey = 'your_API_KEY';
// 定义舵机控制引脚
const int servoPin = 9;
void setup() {
// 初始化串口和舵机引脚
Serial.begin(9600);
pinMode(servoPin, OUTPUT);
// 初始化 ESP8266 串口
esp8266.begin(9600);
delay(1000);
// 连接 WiFi
Serial.println();
Serial.print('Connecting to ');
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print('.');
}
Serial.println('');
Serial.println('WiFi connected');
}
void loop() {
// 读取湿温度数据
float humidity = 50.0;
float temperature = 25.0;
// 组装上传数据的 JSON 字符串
String data = '{\"humidity\":' + String(humidity) + ',\"temperature\":' + String(temperature) + '}';
// 发送 HTTP 请求上传数据到 OneNet
String cmd = 'AT+CIPSTART="TCP","api.heclouds.com",80\r\n';
esp8266.print(cmd);
delay(1000);
if (esp8266.find('OK')) {
Serial.println('TCP connection established');
}
cmd = 'POST /devices/' + String(DeviceID) + '/datapoints HTTP/1.1\r\n';
cmd += 'Host: api.heclouds.com\r\n';
cmd += 'Content-Type: application/json\r\n';
cmd += 'Content-Length: ' + String(data.length()) + '\r\n';
cmd += 'api-key: ' + String(APIKey) + '\r\n';
cmd += 'Connection: close\r\n';
cmd += '\r\n';
cmd += data;
cmd += '\r\n';
esp8266.print('AT+CIPSEND=');
esp8266.println(cmd.length());
delay(1000);
if (esp8266.find('>')) {
Serial.print('Sending data to OneNet: ');
Serial.println(data);
esp8266.print(cmd);
delay(1000);
if (esp8266.find('OK')) {
Serial.println('Data sent to OneNet');
} else {
Serial.println('Error sending data');
}
} else {
Serial.println('Error sending command');
}
// 获取命令并控制舵机
cmd = 'AT+CIPSTART="TCP","api.heclouds.com",80\r\n';
esp8266.print(cmd);
delay(1000);
if (esp8266.find('OK')) {
Serial.println('TCP connection established');
}
cmd = 'GET /devices/' + String(DeviceID) + '/cmds?limit=1 HTTP/1.1\r\n';
cmd += 'Host: api.heclouds.com\r\n';
cmd += 'api-key: ' + String(APIKey) + '\r\n';
cmd += 'Connection: close\r\n';
cmd += '\r\n';
esp8266.print('AT+CIPSEND=');
esp8266.println(cmd.length());
delay(1000);
if (esp8266.find('>')) {
Serial.println('Getting command from OneNet');
esp8266.print(cmd);
delay(1000);
if (esp8266.find('OK')) {
String response = '';
while (esp8266.available()) {
char c = esp8266.read();
response += c;
}
Serial.print('OneNet response: ');
Serial.println(response);
// 解析命令并控制舵机
int command = -1;
int index = response.indexOf('value');
if (index != -1) {
index = response.indexOf(':', index);
if (index != -1) {
int end = response.indexOf(',', index);
if (end == -1) {
end = response.indexOf('}', index);
}
if (end != -1) {
command = response.substring(index + 1, end).toInt();
}
}
}
if (command != -1) {
Serial.print('Command received: ');
Serial.println(command);
if (command == 1) {
digitalWrite(servoPin, HIGH);
} else if (command == 0) {
digitalWrite(servoPin, LOW);
}
}
} else {
Serial.println('Error getting command');
}
} else {
Serial.println('Error sending command');
}
// 断开 TCP 连接
cmd = 'AT+CIPCLOSE\r\n';
esp8266.print(cmd);
delay(1000);
}
原文地址: http://www.cveoy.top/t/topic/oK3p 著作权归作者所有。请勿转载和采集!