![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IoT demo
Fork of my_example_esp8266 by
Diff: esp8266.cpp
- Revision:
- 2:b10925c474fc
- Parent:
- 1:3b487c4813a2
- Child:
- 3:06080fa094a0
--- a/esp8266.cpp Tue Nov 07 03:15:44 2017 +0000 +++ b/esp8266.cpp Tue Nov 07 04:09:57 2017 +0000 @@ -29,16 +29,26 @@ #ifdef DEBUG -void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 +static void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 //ser2esp8266.putc(getchar()); } #endif -void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 +static void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 // putchar(ser2esp8266.getc()); } //#endif +void Esp8266::gotResponse(char *token, char *param) +{ + printf("gotResponse %s %s\r\n", token, param); + if(strcmp(token, "connected") == 0) + mqtt_start = true; + else if(strcmp(token, "wifi") == 0){ + if(*param == '5') + network_start = true; + } +} // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 // 数据格式约定: #token+data @@ -61,7 +71,7 @@ paramLen = 0; state = STATE_PARAM; }else{ - //gotResponse(tokenBuf, NULL); + gotResponse(esp_tokenBuf, NULL); //memcpy(esp_token, esp_tokenBuf, tokenLen); //esp_token[tokenLen] = '\0'; esp_buf_ready = true; @@ -74,7 +84,7 @@ case STATE_PARAM: if(in == '\r' || in == '\n'){ esp_paramBuf[paramLen] = '\0'; - //gotResponse(tokenBuf, paramBuf); + gotResponse(esp_tokenBuf, esp_paramBuf); //memcpy(esp_token, esp_tokenBuf, tokenLen); //memcpy(esp_param, esp_paramBuf, paramLen); //esp_token[tokenLen] = '\0'; @@ -82,7 +92,6 @@ //ser2usb.putc('?'); esp_buf_ready = true; state = STATE_WAIT; - return; }else if(paramLen+1 < sizeof(esp_paramBuf)){ esp_paramBuf[paramLen++] = in; } @@ -97,11 +106,14 @@ // serial to esp8266 init ser2esp8266.baud(ser_baud); #ifndef DEBUG - //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); + ser2esp8266.attach(callback(this,&Esp8266::esp8266_rxCallback), Serial::RxIrq); #endif //if (mode == 0) { // client mode this->reset(); this->connect_wifi(wifi_ssid, wifi_passwd); + while(!is_connected()){ + wait(0.5); + } this->weblogin(); //} else { // @@ -120,9 +132,14 @@ wait(2); // set auto autoconnect ESP_CMD("wifi.sta.autoconnect(1)"); + return true; +} + +bool Esp8266::is_connected() +{ ESP_CMD("print('\\035wifi+'..wifi.sta.status())"); - network_start = true; - return true; + wait(0.4); + return network_start; } bool Esp8266::weblogin() { //定义类的函数 @@ -132,16 +149,17 @@ bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数 ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); - ESP_CMD("m:connect(\"%s\")", ip); - wait(1); - // subscribe all the topic - mqtt_start = true; + ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip); + + do{ + wait(0.5); + }while(!mqtt_start); return true; } bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数 //if (mqtt_start) { - ESP_CMD("m:publish(\"%s\",\"%s\",0,0)", topic, data); + ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,0)", topic, data); //} wait(0.1); return true; @@ -157,27 +175,6 @@ ESP_CMD("print(\"\\035\"..topic..\"+\"..data)"); ESP_CMD("end"); ESP_CMD("end)"); - - ser2esp8266.attach(simple_callback, Serial::RxIrq); - //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); - - while (0) { - while(!esp_buf_ready); - esp_buf_ready = false; - - if (strcmp(topic, esp_tokenBuf) == 0) { - - if (size == -1) { - //uint8_t len = strlen(esp_param); - //memcpy(data, esp_param, len); - //data[len] = '\0'; - } else { - //memcpy(data, esp_param, size); - //data[size] = '\0'; - } - break; - } - } ESP_CMD("m:unsubscribe(\"%s\")", topic); return true;