![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IoT demo
Fork of my_example_esp8266 by
Diff: esp8266.cpp
- Revision:
- 8:d5e340a59c71
- Parent:
- 7:49a823b5a935
diff -r 49a823b5a935 -r d5e340a59c71 esp8266.cpp --- a/esp8266.cpp Wed Dec 13 10:09:44 2017 +0000 +++ b/esp8266.cpp Mon Mar 19 03:32:21 2018 +0000 @@ -27,25 +27,16 @@ sprintf(cmdbuf, format "\r", ##__VA_ARGS__);\ for(p=cmdbuf;*p;p++){\ ser2esp8266.putc(*p);\ - wait(0.05);\ + wait(0.02);\ }\ wait(0.3);\ }while(0) -#ifdef DEBUG -static void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 - //ser2esp8266.putc(getchar()); -} -#endif - -static void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 -// putchar(ser2esp8266.getc()); -} -//#endif void Esp8266::gotResponse(char *token, char *param) { + if(*token<'a' || *token>'z') return; ser2usb.printf("gotResponse %s %s\r\n", token, param); if(strcmp(token, "connected") == 0) mqtt_start = true; @@ -82,6 +73,7 @@ // 数据格式约定: #token+data void Esp8266::esp8266_rxCallback() { char in = ser2esp8266.getc(); +// ser2usb.putc(in); enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM}; static uint8_t state = STATE_WAIT; static int tokenLen, paramLen; @@ -185,8 +177,9 @@ } } -bool Esp8266::connect_mqtt_broker(char *ip, const char* sensors[][2], const char* actuator[][2]) { //定义类的函数 +bool Esp8266::connect_mqtt_broker(char *ip, const char *node_name, const char* sensors[][2], const char* actuator[][2]) { //定义类的函数 + ESP_CMD("node_name = '%s'", node_name); ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip); @@ -201,19 +194,19 @@ ESP_CMD("end"); ESP_CMD("end)"); - ESP_CMD("m:publish('/events/i_'..node.chipid()..'/online','',1,0)"); + ESP_CMD("m:publish('/events/'..node_name..'/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); + ESP_CMD("m:publish('/capability/'..node_name..'/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); + ESP_CMD("m:publish('/capability/'..node_name..'/control','%s',1,1)", capabilities); wait(0.1); for (int i = 0; actuator[i][0]; ++i) subscribe_control(actuator[i][0]); @@ -226,7 +219,7 @@ bool Esp8266::publish_value(const char *topic, const char *data) { //定义类的函数 //if (mqtt_start) { - ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,1)", topic, data); + ESP_CMD("m:publish('/values/'..node_name..'/%s',\"%s\",0,1)", topic, data); wait(0.1); //} return true; @@ -234,7 +227,7 @@ bool Esp8266::subscribe_control(const char *topic, const char *data) { //定义类的函数 //if (mqtt_start) { - ESP_CMD("m:subscribe('/control/i_'..node.chipid()..'/%s', 0)", topic); + ESP_CMD("m:subscribe('/control/'..node_name..'/%s', 0)", topic); wait(0.1); //}