![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
init
Fork of iot_example by
Diff: esp8266.cpp
- Revision:
- 3:06080fa094a0
- Parent:
- 2:b10925c474fc
- Child:
- 6:629e300b6c3b
diff -r b10925c474fc -r 06080fa094a0 esp8266.cpp --- a/esp8266.cpp Tue Nov 07 04:09:57 2017 +0000 +++ b/esp8266.cpp Mon Nov 13 20:25:48 2017 +0800 @@ -13,10 +13,10 @@ #include <cstdarg> #include <cstring> +#include <stdint.h> #include "mbed.h" - -//#define DEBUG +extern Serial ser2usb; static int ser_baud = 9600; @@ -41,15 +41,38 @@ void Esp8266::gotResponse(char *token, char *param) { - printf("gotResponse %s %s\r\n", token, param); + ser2usb.printf("gotResponse %s %s\r\n", token, param); if(strcmp(token, "connected") == 0) mqtt_start = true; + else if(strcmp(token, "control") == 0){ + if(!control_cmd){ + strncpy(control_buf, param, sizeof(control_buf)); + control_cmd = true; + } + } else if(strcmp(token, "wifi") == 0){ if(*param == '5') network_start = true; } } +bool Esp8266::get_control_cmd(char* actuator, char* value) +{ + if(!control_cmd) + return false; + + char* plus = strchr(control_buf, '+'); + if(!plus){ + control_cmd = false; + return false; + } + *plus = '\0'; + strcpy(actuator, control_buf); + strcpy(value, plus+1); + control_cmd = false; + return true; +} + // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 // 数据格式约定: #token+data void Esp8266::esp8266_rxCallback() { @@ -101,13 +124,11 @@ Esp8266::Esp8266(PinName TX, PinName RX, const char *wifi_ssid, const char *wifi_passwd) //定义类的函数 - : network_start(false), mqtt_start(false), esp_buf_ready(false), ser2esp8266(TX, RX) + : network_start(false), mqtt_start(false), control_cmd(false), esp_buf_ready(false), ser2esp8266(TX, RX) { // serial to esp8266 init ser2esp8266.baud(ser_baud); -#ifndef DEBUG ser2esp8266.attach(callback(this,&Esp8266::esp8266_rxCallback), Serial::RxIrq); -#endif //if (mode == 0) { // client mode this->reset(); this->connect_wifi(wifi_ssid, wifi_passwd); @@ -147,35 +168,71 @@ return true; } -bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数 +void Esp8266::buildCapability(char *out, const char* infoList[][2]) +{ + out[0] = '\0'; + for (int i = 0; infoList[i][0]; ++i) + { + strcat(out, infoList[i][0]); + strcat(out, ","); + strcat(out, infoList[i][1]); + strcat(out, "\\n"); + } +} + +bool Esp8266::connect_mqtt_broker(char *ip, const char* sensors[][2], const char* actuator[][2]) { //定义类的函数 + ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip); do{ wait(0.5); }while(!mqtt_start); + + ESP_CMD("m:on(\"message\", function(conn, topic, data)"); + ESP_CMD("if topic:find(\"^/control/\") then"); + ESP_CMD("local tok = topic:match(\"^/control/.+/(.+)\")"); + ESP_CMD("if tok then print(\"\\035\"..tok..\"+\"..data) end"); + ESP_CMD("end"); + ESP_CMD("end)"); + + ESP_CMD("m:publish('/events/i_'..node.chipid()..'/online','',1,0)"); + wait(0.1); + + char * capabilities = new char[512]; + + if(sensors){ + buildCapability(capabilities, sensors); + ESP_CMD("m:publish('/capability/i_'..node.chipid()..'/values','%s',1,1)", capabilities); + wait(0.1); + } + if(actuator){ + buildCapability(capabilities, actuator); + ESP_CMD("m:publish('/capability/i_'..node.chipid()..'/control','%s',1,1)", capabilities); + wait(0.1); + for (int i = 0; actuator[i][0]; ++i) + subscribe_control(actuator[i][0]); + } + + delete[ ] capabilities; + return true; } -bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数 +bool Esp8266::publish_value(const char *topic, const char *data) { //定义类的函数 //if (mqtt_start) { - ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,0)", topic, data); + ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,1)", topic, data); + wait(0.1); //} - wait(0.1); return true; } -bool Esp8266::subscribe_poll(char *topic, char *data, int size) { //定义类的函数 +bool Esp8266::subscribe_control(const char *topic, const char *data) { //定义类的函数 //if (mqtt_start) { - ESP_CMD("m:subscribe(\"%s\", 0)", topic); + ESP_CMD("m:subscribe('/control/i_'..node.chipid()..'/%s', 0)", topic); wait(0.1); //} - ESP_CMD("m:on(\"message\", function(conn, topic, data)"); - ESP_CMD("if topic == \"%s\" then", topic); - ESP_CMD("print(\"\\035\"..topic..\"+\"..data)"); - ESP_CMD("end"); - ESP_CMD("end)"); - ESP_CMD("m:unsubscribe(\"%s\")", topic); + // ESP_CMD("m:unsubscribe(\"%s\")", topic); return true; } \ No newline at end of file