ESP8266固件编译出现undefined reference to `os_zalloc'的解决方法

undefined reference to `os_zalloc'

 

出错原因:找不到os_zalloc函数

解决办法:#include "mem.h"

增加头文件包含

 

 

 

使用 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(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值