esp8266 用sdk发送温湿度给Onenet云平台(EDP协议)
时间: 2023-06-26 08:09:13 浏览: 113
ESP8266可以通过AT指令或使用官方提供的SDK发送数据到Onenet云平台。以下是使用SDK发送温湿度数据的示例代码:
1. 首先,需要在Onenet平台创建一个设备和数据流。
2. 在ESP8266中使用WiFi连接到网络。
3. 在ESP8266中初始化SDK并连接到Onenet云平台:
```
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "espconn.h"
#include "mem.h"
#include "gpio.h"
#include "driver/uart.h"
#include "user_config.h"
#define ONENET_HOST "api.heclouds.com" // Onenet平台API域名
#define ONENET_PORT 80 // Onenet平台API端口号
#define ONENET_DEVICEID "xxxxxxxxxx" // 设备ID
#define ONENET_APIKEY "xxxxxxxxxxxxxxxx" // 设备鉴权信息
LOCAL struct espconn user_tcp_conn;
LOCAL esp_tcp user_tcp;
LOCAL os_timer_t onenet_timer;
void onenet_send_data(void *arg)
{
char buf[128];
uint16_t len;
// 读取温湿度数据
float temperature = 25.0;
float humidity = 50.0;
// 构造JSON格式的数据
len = sprintf(buf, "{\"datastreams\":[{\"id\":\"temperature\",\"datapoints\":[{\"value\":\"%.2f\"}]},"\
"{\"id\":\"humidity\",\"datapoints\":[{\"value\":\"%.2f\"}]}]}",
temperature, humidity);
// 发送数据
espconn_secure_send(&user_tcp_conn, buf, len);
}
void onenet_connect_cb(void *arg)
{
struct espconn *pespconn = (struct espconn *)arg;
os_timer_disarm(&onenet_timer);
// 发送注册包
char buf[256];
uint16_t len = sprintf(buf, "POST /register_de?device_id=%s HTTP/1.1\r\n"\
"api-key:%s\r\n"\
"Host: %s:%d\r\n"\
"Connection: keep-alive\r\n"\
"Content-Type: application/x-www-form-urlencoded\r\n"\
"Content-Length: 0\r\n\r\n", ONENET_DEVICEID, ONENET_APIKEY, ONENET_HOST, ONENET_PORT);
espconn_secure_send(pespconn, buf, len);
os_timer_setfn(&onenet_timer, onenet_send_data, NULL); // 设置定时器回调函数
os_timer_arm(&onenet_timer, 10000, true); // 每10秒钟发送数据
}
void onenet_reconnect_cb(void *arg, sint8_t err)
{
os_printf("Onenet reconnect error %d\n", err);
}
void onenet_disconnect_cb(void *arg)
{
os_printf("Onenet disconnected\n");
}
void onenet_recv_cb(void *arg, char *pdata, unsigned short len)
{
os_printf("Onenet received: %s\n", pdata);
}
void onenet_sent_cb(void *arg)
{
os_printf("Onenet sent\n");
}
void onenet_init()
{
user_tcp.remote_port = ONENET_PORT;
user_tcp_conn.proto.tcp = &user_tcp;
user_tcp_conn.type = ESPCONN_TCP;
user_tcp_conn.state = ESPCONN_NONE;
user_tcp_conn.proto.tcp->remote_ip[0] = 183;
user_tcp_conn.proto.tcp->remote_ip[1] = 230;
user_tcp_conn.proto.tcp->remote_ip[2] = 40;
user_tcp_conn.proto.tcp->remote_ip[3] = 40;
espconn_regist_connectcb(&user_tcp_conn, onenet_connect_cb);
espconn_regist_reconcb(&user_tcp_conn, onenet_reconnect_cb);
espconn_regist_disconcb(&user_tcp_conn, onenet_disconnect_cb);
espconn_regist_recvcb(&user_tcp_conn, onenet_recv_cb);
espconn_regist_sentcb(&user_tcp_conn, onenet_sent_cb);
espconn_secure_set_size(ESPCONN_CLIENT, 5120);
espconn_secure_connect(&user_tcp_conn);
}
void user_init(void)
{
uart_init(BIT_RATE_115200, BIT_RATE_115200);
os_delay_us(1000000);
wifi_set_opmode(STATION_MODE);
struct station_config stationConf;
os_memset(&stationConf, 0, sizeof(struct station_config));
os_sprintf(stationConf.ssid, "your_wifi_ssid");
os_sprintf(stationConf.password, "your_wifi_password");
wifi_station_set_config(&stationConf);
onenet_init(); // 初始化Onenet连接
}
```
4. 在定时器回调函数中发送数据,可以根据需求设置定时器的时间间隔。
5. 在Onenet平台上可以查看温湿度数据流的实时数据和历史数据。
阅读全文