12408| 3
|
(stm32+esp8266)连接贝壳物联在线稳定性测试 |
一、设备功能 机智云gokit开发板实时上传DHT11温湿度数据,接受控制并进行反馈,保持断线重连,可以通过开发板的usb口,用串口工具调试工具,直接控制esp8266。 二、硬件 gokit开发板2.0v,esp8266网络模块。 三、实现方法 1、gokit扩展板上的esp8266刷AT固件,参见:机智云Gokit2代功能板ESP-12F直接刷AT固件透传方法 2、下载基于ebox系统的stm32,工程文件,点击下载; 3、复制下方代码,粘贴替换下载工程文件中的mian.cpp文件内容; 4、修改文件中的DEVICEID、APIKEY、temp_input_id、hum_input_id,分别为设备id,设备密码,属于该设备下的温度接口和湿度接口id; 5、编译烧录; 6、进行透传设置,直接通过gokit的usb口,使用串口调试工具,向esp8266发送命令进行透传设置,可参见:ESP8266透传设置脚本,设置成功后自动连接贝壳物联平台 四、代码 [mw_shl_code=cpp,true] //STM32 RUN IN eBox #include "ebox.h" #include "cJSON.h" #include "Dht11.h" //================================ String DEVICEID = "xxx"; String APIKEY = "xxxxxxxxx"; String temp_input_id="xxxx"; String hum_input_id="xxxx"; //==================================== Dht11 sensor(&PB3); char uart1_rx_str[1024]; int i=0; char uart2_rx_str[1024]; int j=0; uint32_t last_beat_time = 0; bool checkinok = false; String checkin="{\"M\":\"checkin\",\"ID\":\""+DEVICEID+"\",\"K\":\""+APIKEY+"\"}\n"; void say(char *toID, char *content) { uart2.printf("{\"M\":\"say\",\"ID\":\"%s\",\"C\":\"%s\"}\n",toID,content); } int processMessage(char *msg){ cJSON *jsonObj = cJSON_Parse(msg); if(!jsonObj) { uart1.printf("json string wrong!"); return 0; } cJSON *method = cJSON_GetObjectItem(jsonObj, "M"); char *m = method->valuestring; if(strncmp(m,"b",1) == 0 || strncmp(m,"WELCOME",7) == 0) { uart1.printf("sending checkin...\r\n"); uart2.print(checkin); } if(strncmp(m,"checkinok",9) == 0) { checkinok=true; } if(strncmp(m,"connected",9) == 0) { checkinok=false; uart1.printf("sending checkin...\r\n"); uart2.print(checkin); } if(strncmp(m,"checked",7) == 0) { checkinok=true; } if(strncmp(m,"login",5) == 0) { char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring; uart1.printf("saying..."); char new_content[] = "Dear friend, welcome to BIGIOT !"; say(from_id,new_content); } if(strncmp(m,"say",3) == 0) { char *content = cJSON_GetObjectItem(jsonObj, "C")->valuestring; char *from_id = cJSON_GetObjectItem(jsonObj, "ID")->valuestring; if(strncmp(content,"play",4) == 0) { //do something here.... uart1.printf("saying..."); char new_content[] = "played"; say(from_id,new_content); } else if(strncmp(content,"stop",4) == 0) { //do something here.... uart1.printf("saying..."); char new_content[] = "stoped"; say(from_id,new_content); } else{ //do something here.... uart1.printf("saying..."); char new_content[] = "I don't know what you have said..."; say(from_id,new_content); } } if(jsonObj)cJSON_Delete(jsonObj); return 1; } void uart2_rx_event() { uint16_t c; c = uart2.read(); uart1.write(c); uart2_rx_str[j]=c; uart2_rx_str[j+1] = '\0'; if (c == '\n') { if(uart2_rx_str[j-1] == '}') { processMessage(uart2_rx_str); } if(strncmp(uart2_rx_str,"ERROR",5) == 0) { checkinok = false; } j=0; }else{ j++; } } void update_dht11() { int temp,hum; switch (sensor.read()) { case Dht11::OK: temp=sensor.getTemperature(); hum=sensor.getHumidity(); uart1.printf("{\"M\":\"update\",\"ID\":\"%s\",\"V\":{\"%s\":\"%d\",\"%s\":\"%d\"}}\n",DEVICEID.c_str(),temp_input_id.c_str(),temp,hum_input_id.c_str(),hum); uart2.printf("{\"M\":\"update\",\"ID\":\"%s\",\"V\":{\"%s\":\"%d\",\"%s\":\"%d\"}}\n",DEVICEID.c_str(),temp_input_id.c_str(),temp,hum_input_id.c_str(),hum); break; case Dht11::ERROR_CHECKSUM: uart1.printf("Checksum error\r\n"); break; case Dht11::ERROR_TIMEOUT: uart1.printf("Timeout error\r\n"); break; default: uart1.printf("Unknown error\r\n"); break; } } void uart1_rx_event() { uint16_t c; c = uart1.read(); uart1_rx_str=c; uart1_rx_str[i+1] = '\0'; if (c == '\n') { uart2.printf(uart1_rx_str); i=0; }else if(strncmp(uart1_rx_str,"+++",3) == 0) { uart2.printf(uart1_rx_str); i=0; }else { i++; } } void setup() { ebox_init(); uart1.begin(115200); uart2.begin(115200); uart1.printf("Working start!\r\n"); uart1.attach(uart1_rx_event,RxIrq); uart2.attach(uart2_rx_event,RxIrq); } int main(void) { uint32_t last_update_time = 0; uint32_t last_status_time = 0; setup(); while(1) { if(millis() - last_update_time > 6310 && checkinok) { update_dht11(); last_update_time = millis(); } if(millis() - last_status_time > 120000 || last_status_time == 0) { uart2.printf("{\"M\":\"status\"}\n"); last_status_time = millis(); } } } [/mw_shl_code] 原文出自:http://www.bigiot.net/info/458.html |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed