esp8266 用sdk编程上传数据点给Onenet云平台(EDP协议)
时间: 2023-12-03 11:45:51 浏览: 115
esp8266数据上传onenet源代码
使用 ESP8266 SDK 编写程序上传数据点给 OneNet 云平台(EDP协议)的步骤如下:
1. 注册 OneNet 账号并创建产品和设备。
2. 下载 ESP8266 SDK,并在开发环境中配置好 SDK。
3. 在你的代码中添加以下头文件:
```c
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "mem.h"
#include "espconn.h"
```
4. 定义以下变量:
```c
#define SERVER "api.heclouds.com" // OneNet 服务器地址
#define PORT 80 // OneNet 服务器端口
#define DEVICE_ID "your_device_id" // 设备 ID
#define API_KEY "your_api_key" // 设备 API Key
#define DATA_STREAM "your_data_stream" // 数据流名称
#define DATA_VALUE "your_data_value" // 数据流值
```
5. 编写连接 OneNet 服务器的函数:
```c
void ICACHE_FLASH_ATTR connect_to_server() {
struct espconn conn;
ip_addr_t ip;
espconn_create(&conn);
conn.type = ESPCONN_TCP;
conn.state = ESPCONN_NONE;
conn.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
conn.proto.tcp->local_port = espconn_port();
conn.proto.tcp->remote_port = PORT;
conn.proto.tcp->remote_ip[0] = 183;
conn.proto.tcp->remote_ip[1] = 230;
conn.proto.tcp->remote_ip[2] = 40;
conn.proto.tcp->remote_ip[3] = 39;
espconn_regist_connectcb(&conn, connect_callback);
espconn_regist_reconcb(&conn, reconnect_callback);
espconn_connect(&conn);
}
```
6. 编写连接成功回调函数:
```c
void ICACHE_FLASH_ATTR connect_callback(void *arg) {
struct espconn *conn = (struct espconn *)arg;
os_printf("Connected to server!\n");
char data[300];
os_sprintf(data, "{\"datastreams\":[{\"id\":\"%s\",\"datapoints\":[{\"value\":\"%s\"}]}]}", DATA_STREAM, DATA_VALUE);
char content_length[10];
os_sprintf(content_length, "%d", os_strlen(data));
char header[200];
os_sprintf(header, "POST /devices/%s/datapoints HTTP/1.1\r\napi-key: %s\r\nHost: %s:%d\r\nContent-Length: %s\r\nContent-Type: application/json\r\n\r\n%s", DEVICE_ID, API_KEY, SERVER, PORT, content_length, data);
espconn_sent(conn, header, os_strlen(header));
}
```
7. 编写重连回调函数:
```c
void ICACHE_FLASH_ATTR reconnect_callback(void *arg, sint8 err) {
os_printf("Reconnecting to server...\n");
connect_to_server();
}
```
8. 在你的程序中调用 `connect_to_server()` 函数即可上传数据点到 OneNet 云平台。
完整代码示例:
```c
#include "ets_sys.h"
#include "osapi.h"
#include "user_interface.h"
#include "mem.h"
#include "espconn.h"
#define SERVER "api.heclouds.com" // OneNet 服务器地址
#define PORT 80 // OneNet 服务器端口
#define DEVICE_ID "your_device_id" // 设备 ID
#define API_KEY "your_api_key" // 设备 API Key
#define DATA_STREAM "your_data_stream" // 数据流名称
#define DATA_VALUE "your_data_value" // 数据流值
void ICACHE_FLASH_ATTR connect_to_server();
void ICACHE_FLASH_ATTR connect_callback(void *arg);
void ICACHE_FLASH_ATTR reconnect_callback(void *arg, sint8 err);
void ICACHE_FLASH_ATTR user_init() {
uart_div_modify(0, UART_CLK_FREQ / 115200);
os_printf("Starting...\n");
wifi_set_opmode(STATION_MODE);
struct station_config config;
os_memset(&config, 0, sizeof(config));
os_sprintf(config.ssid, "%s", "your_wifi_ssid");
os_sprintf(config.password, "%s", "your_wifi_password");
wifi_station_set_config(&config);
wifi_station_connect();
connect_to_server();
}
void ICACHE_FLASH_ATTR connect_to_server() {
struct espconn conn;
ip_addr_t ip;
espconn_create(&conn);
conn.type = ESPCONN_TCP;
conn.state = ESPCONN_NONE;
conn.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
conn.proto.tcp->local_port = espconn_port();
conn.proto.tcp->remote_port = PORT;
conn.proto.tcp->remote_ip[0] = 183;
conn.proto.tcp->remote_ip[1] = 230;
conn.proto.tcp->remote_ip[2] = 40;
conn.proto.tcp->remote_ip[3] = 39;
espconn_regist_connectcb(&conn, connect_callback);
espconn_regist_reconcb(&conn, reconnect_callback);
espconn_connect(&conn);
}
void ICACHE_FLASH_ATTR connect_callback(void *arg) {
struct espconn *conn = (struct espconn *)arg;
os_printf("Connected to server!\n");
char data[300];
os_sprintf(data, "{\"datastreams\":[{\"id\":\"%s\",\"datapoints\":[{\"value\":\"%s\"}]}]}", DATA_STREAM, DATA_VALUE);
char content_length[10];
os_sprintf(content_length, "%d", os_strlen(data));
char header[200];
os_sprintf(header, "POST /devices/%s/datapoints HTTP/1.1\r\napi-key: %s\r\nHost: %s:%d\r\nContent-Length: %s\r\nContent-Type: application/json\r\n\r\n%s", DEVICE_ID, API_KEY, SERVER, PORT, content_length, data);
espconn_sent(conn, header, os_strlen(header));
}
void ICACHE_FLASH_ATTR reconnect_callback(void *arg, sint8 err) {
os_printf("Reconnecting to server...\n");
connect_to_server();
}
```
阅读全文