![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
init
Fork of iot_example by
esp8266.cpp@3:06080fa094a0, 2017-11-13 (annotated)
- Committer:
- zyx
- Date:
- Mon Nov 13 20:25:48 2017 +0800
- Revision:
- 3:06080fa094a0
- Parent:
- 2:b10925c474fc
- Child:
- 6:629e300b6c3b
complete demo for IoT
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhangyx | 1:3b487c4813a2 | 1 | /*************************************************************** |
zhangyx | 1:3b487c4813a2 | 2 | 功能 : ESP8266物联车接口函数 |
zhangyx | 1:3b487c4813a2 | 3 | 作者 : 马晓健 |
zhangyx | 1:3b487c4813a2 | 4 | 邮箱 : jeasinema[at]gmail[dot]com |
zhangyx | 1:3b487c4813a2 | 5 | 声明 : |
zhangyx | 1:3b487c4813a2 | 6 | 本程序仅供学习与交流使用,如需他用,须联系作者 |
zhangyx | 1:3b487c4813a2 | 7 | 本程序可以随意更改,但须保留本信息页 |
zhangyx | 1:3b487c4813a2 | 8 | All rights reserved |
zhangyx | 1:3b487c4813a2 | 9 | 2017.6.16 |
zhangyx | 1:3b487c4813a2 | 10 | ***************************************************************/ |
zhangyx | 1:3b487c4813a2 | 11 | |
brainliang | 0:63af4719467f | 12 | #include "esp8266.h" |
brainliang | 0:63af4719467f | 13 | |
brainliang | 0:63af4719467f | 14 | #include <cstdarg> |
brainliang | 0:63af4719467f | 15 | #include <cstring> |
zyx | 3:06080fa094a0 | 16 | #include <stdint.h> |
brainliang | 0:63af4719467f | 17 | #include "mbed.h" |
brainliang | 0:63af4719467f | 18 | |
zyx | 3:06080fa094a0 | 19 | extern Serial ser2usb; |
brainliang | 0:63af4719467f | 20 | |
zhangyx | 1:3b487c4813a2 | 21 | static int ser_baud = 9600; |
brainliang | 0:63af4719467f | 22 | |
brainliang | 0:63af4719467f | 23 | //定义了一个调试的宏,C语言语法 |
brainliang | 0:63af4719467f | 24 | #define ESP_CMD(format, ...) do{\ |
brainliang | 0:63af4719467f | 25 | ser2esp8266.printf("\r"); \ |
brainliang | 0:63af4719467f | 26 | ser2esp8266.printf(format "\r", ##__VA_ARGS__);\ |
brainliang | 0:63af4719467f | 27 | wait(0.3);\ |
brainliang | 0:63af4719467f | 28 | }while(0) |
brainliang | 0:63af4719467f | 29 | |
brainliang | 0:63af4719467f | 30 | |
brainliang | 0:63af4719467f | 31 | #ifdef DEBUG |
zhangyx | 2:b10925c474fc | 32 | static void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 |
zhangyx | 1:3b487c4813a2 | 33 | //ser2esp8266.putc(getchar()); |
brainliang | 0:63af4719467f | 34 | } |
brainliang | 0:63af4719467f | 35 | #endif |
brainliang | 0:63af4719467f | 36 | |
zhangyx | 2:b10925c474fc | 37 | static void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 |
zhangyx | 1:3b487c4813a2 | 38 | // putchar(ser2esp8266.getc()); |
brainliang | 0:63af4719467f | 39 | } |
brainliang | 0:63af4719467f | 40 | //#endif |
brainliang | 0:63af4719467f | 41 | |
zhangyx | 2:b10925c474fc | 42 | void Esp8266::gotResponse(char *token, char *param) |
zhangyx | 2:b10925c474fc | 43 | { |
zyx | 3:06080fa094a0 | 44 | ser2usb.printf("gotResponse %s %s\r\n", token, param); |
zhangyx | 2:b10925c474fc | 45 | if(strcmp(token, "connected") == 0) |
zhangyx | 2:b10925c474fc | 46 | mqtt_start = true; |
zyx | 3:06080fa094a0 | 47 | else if(strcmp(token, "control") == 0){ |
zyx | 3:06080fa094a0 | 48 | if(!control_cmd){ |
zyx | 3:06080fa094a0 | 49 | strncpy(control_buf, param, sizeof(control_buf)); |
zyx | 3:06080fa094a0 | 50 | control_cmd = true; |
zyx | 3:06080fa094a0 | 51 | } |
zyx | 3:06080fa094a0 | 52 | } |
zhangyx | 2:b10925c474fc | 53 | else if(strcmp(token, "wifi") == 0){ |
zhangyx | 2:b10925c474fc | 54 | if(*param == '5') |
zhangyx | 2:b10925c474fc | 55 | network_start = true; |
zhangyx | 2:b10925c474fc | 56 | } |
zhangyx | 2:b10925c474fc | 57 | } |
brainliang | 0:63af4719467f | 58 | |
zyx | 3:06080fa094a0 | 59 | bool Esp8266::get_control_cmd(char* actuator, char* value) |
zyx | 3:06080fa094a0 | 60 | { |
zyx | 3:06080fa094a0 | 61 | if(!control_cmd) |
zyx | 3:06080fa094a0 | 62 | return false; |
zyx | 3:06080fa094a0 | 63 | |
zyx | 3:06080fa094a0 | 64 | char* plus = strchr(control_buf, '+'); |
zyx | 3:06080fa094a0 | 65 | if(!plus){ |
zyx | 3:06080fa094a0 | 66 | control_cmd = false; |
zyx | 3:06080fa094a0 | 67 | return false; |
zyx | 3:06080fa094a0 | 68 | } |
zyx | 3:06080fa094a0 | 69 | *plus = '\0'; |
zyx | 3:06080fa094a0 | 70 | strcpy(actuator, control_buf); |
zyx | 3:06080fa094a0 | 71 | strcpy(value, plus+1); |
zyx | 3:06080fa094a0 | 72 | control_cmd = false; |
zyx | 3:06080fa094a0 | 73 | return true; |
zyx | 3:06080fa094a0 | 74 | } |
zyx | 3:06080fa094a0 | 75 | |
brainliang | 0:63af4719467f | 76 | // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 |
brainliang | 0:63af4719467f | 77 | // 数据格式约定: #token+data |
zhangyx | 1:3b487c4813a2 | 78 | void Esp8266::esp8266_rxCallback() { |
brainliang | 0:63af4719467f | 79 | char in = ser2esp8266.getc(); |
brainliang | 0:63af4719467f | 80 | enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM}; |
brainliang | 0:63af4719467f | 81 | static uint8_t state = STATE_WAIT; |
brainliang | 0:63af4719467f | 82 | static int tokenLen, paramLen; |
brainliang | 0:63af4719467f | 83 | switch(state){ |
brainliang | 0:63af4719467f | 84 | case STATE_WAIT: |
brainliang | 0:63af4719467f | 85 | if(in == '#'){ |
brainliang | 0:63af4719467f | 86 | tokenLen = 0; |
brainliang | 0:63af4719467f | 87 | state = STATE_TOKEN; |
brainliang | 0:63af4719467f | 88 | } |
brainliang | 0:63af4719467f | 89 | break; |
brainliang | 0:63af4719467f | 90 | case STATE_TOKEN: |
brainliang | 0:63af4719467f | 91 | if(in == '+' || in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 92 | esp_tokenBuf[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 93 | if(in == '+'){ |
brainliang | 0:63af4719467f | 94 | paramLen = 0; |
brainliang | 0:63af4719467f | 95 | state = STATE_PARAM; |
brainliang | 0:63af4719467f | 96 | }else{ |
zhangyx | 2:b10925c474fc | 97 | gotResponse(esp_tokenBuf, NULL); |
brainliang | 0:63af4719467f | 98 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 99 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 100 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 101 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 102 | } |
brainliang | 0:63af4719467f | 103 | }else if(tokenLen+1 < sizeof(esp_tokenBuf)){ |
brainliang | 0:63af4719467f | 104 | esp_tokenBuf[tokenLen++] = in; |
brainliang | 0:63af4719467f | 105 | } |
brainliang | 0:63af4719467f | 106 | break; |
brainliang | 0:63af4719467f | 107 | case STATE_PARAM: |
brainliang | 0:63af4719467f | 108 | if(in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 109 | esp_paramBuf[paramLen] = '\0'; |
zhangyx | 2:b10925c474fc | 110 | gotResponse(esp_tokenBuf, esp_paramBuf); |
brainliang | 0:63af4719467f | 111 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 112 | //memcpy(esp_param, esp_paramBuf, paramLen); |
brainliang | 0:63af4719467f | 113 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 114 | //esp_param[paramLen] = '\0'; |
brainliang | 0:63af4719467f | 115 | //ser2usb.putc('?'); |
brainliang | 0:63af4719467f | 116 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 117 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 118 | }else if(paramLen+1 < sizeof(esp_paramBuf)){ |
brainliang | 0:63af4719467f | 119 | esp_paramBuf[paramLen++] = in; |
brainliang | 0:63af4719467f | 120 | } |
brainliang | 0:63af4719467f | 121 | break; |
brainliang | 0:63af4719467f | 122 | } |
brainliang | 0:63af4719467f | 123 | } |
brainliang | 0:63af4719467f | 124 | |
brainliang | 0:63af4719467f | 125 | |
zhangyx | 1:3b487c4813a2 | 126 | Esp8266::Esp8266(PinName TX, PinName RX, const char *wifi_ssid, const char *wifi_passwd) //定义类的函数 |
zyx | 3:06080fa094a0 | 127 | : network_start(false), mqtt_start(false), control_cmd(false), esp_buf_ready(false), ser2esp8266(TX, RX) |
brainliang | 0:63af4719467f | 128 | { |
brainliang | 0:63af4719467f | 129 | // serial to esp8266 init |
brainliang | 0:63af4719467f | 130 | ser2esp8266.baud(ser_baud); |
zhangyx | 2:b10925c474fc | 131 | ser2esp8266.attach(callback(this,&Esp8266::esp8266_rxCallback), Serial::RxIrq); |
zhangyx | 1:3b487c4813a2 | 132 | //if (mode == 0) { // client mode |
brainliang | 0:63af4719467f | 133 | this->reset(); |
zhangyx | 1:3b487c4813a2 | 134 | this->connect_wifi(wifi_ssid, wifi_passwd); |
zhangyx | 2:b10925c474fc | 135 | while(!is_connected()){ |
zhangyx | 2:b10925c474fc | 136 | wait(0.5); |
zhangyx | 2:b10925c474fc | 137 | } |
brainliang | 0:63af4719467f | 138 | this->weblogin(); |
zhangyx | 1:3b487c4813a2 | 139 | //} else { |
zhangyx | 1:3b487c4813a2 | 140 | // |
zhangyx | 1:3b487c4813a2 | 141 | //} |
brainliang | 0:63af4719467f | 142 | } |
brainliang | 0:63af4719467f | 143 | |
brainliang | 0:63af4719467f | 144 | bool Esp8266::reset() { //定义类的函数 |
brainliang | 0:63af4719467f | 145 | ESP_CMD("node.restart()"); |
brainliang | 0:63af4719467f | 146 | wait(2); // 延迟2s |
brainliang | 0:63af4719467f | 147 | return true; |
brainliang | 0:63af4719467f | 148 | } |
brainliang | 0:63af4719467f | 149 | |
zhangyx | 1:3b487c4813a2 | 150 | bool Esp8266::connect_wifi(const char *wifi_ssid, const char *wifi_passwd) { //定义类的函数 |
brainliang | 0:63af4719467f | 151 | ESP_CMD("wifi.setmode(wifi.STATION)"); |
brainliang | 0:63af4719467f | 152 | ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd); |
brainliang | 0:63af4719467f | 153 | wait(2); |
brainliang | 0:63af4719467f | 154 | // set auto autoconnect |
brainliang | 0:63af4719467f | 155 | ESP_CMD("wifi.sta.autoconnect(1)"); |
zhangyx | 2:b10925c474fc | 156 | return true; |
zhangyx | 2:b10925c474fc | 157 | } |
zhangyx | 2:b10925c474fc | 158 | |
zhangyx | 2:b10925c474fc | 159 | bool Esp8266::is_connected() |
zhangyx | 2:b10925c474fc | 160 | { |
brainliang | 0:63af4719467f | 161 | ESP_CMD("print('\\035wifi+'..wifi.sta.status())"); |
zhangyx | 2:b10925c474fc | 162 | wait(0.4); |
zhangyx | 2:b10925c474fc | 163 | return network_start; |
brainliang | 0:63af4719467f | 164 | } |
brainliang | 0:63af4719467f | 165 | |
brainliang | 0:63af4719467f | 166 | bool Esp8266::weblogin() { //定义类的函数 |
brainliang | 0:63af4719467f | 167 | // not implemented yet |
brainliang | 0:63af4719467f | 168 | return true; |
brainliang | 0:63af4719467f | 169 | } |
brainliang | 0:63af4719467f | 170 | |
zyx | 3:06080fa094a0 | 171 | void Esp8266::buildCapability(char *out, const char* infoList[][2]) |
zyx | 3:06080fa094a0 | 172 | { |
zyx | 3:06080fa094a0 | 173 | out[0] = '\0'; |
zyx | 3:06080fa094a0 | 174 | for (int i = 0; infoList[i][0]; ++i) |
zyx | 3:06080fa094a0 | 175 | { |
zyx | 3:06080fa094a0 | 176 | strcat(out, infoList[i][0]); |
zyx | 3:06080fa094a0 | 177 | strcat(out, ","); |
zyx | 3:06080fa094a0 | 178 | strcat(out, infoList[i][1]); |
zyx | 3:06080fa094a0 | 179 | strcat(out, "\\n"); |
zyx | 3:06080fa094a0 | 180 | } |
zyx | 3:06080fa094a0 | 181 | } |
zyx | 3:06080fa094a0 | 182 | |
zyx | 3:06080fa094a0 | 183 | bool Esp8266::connect_mqtt_broker(char *ip, const char* sensors[][2], const char* actuator[][2]) { //定义类的函数 |
zyx | 3:06080fa094a0 | 184 | |
brainliang | 0:63af4719467f | 185 | ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); |
zhangyx | 2:b10925c474fc | 186 | ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip); |
zhangyx | 2:b10925c474fc | 187 | |
zhangyx | 2:b10925c474fc | 188 | do{ |
zhangyx | 2:b10925c474fc | 189 | wait(0.5); |
zhangyx | 2:b10925c474fc | 190 | }while(!mqtt_start); |
zyx | 3:06080fa094a0 | 191 | |
zyx | 3:06080fa094a0 | 192 | ESP_CMD("m:on(\"message\", function(conn, topic, data)"); |
zyx | 3:06080fa094a0 | 193 | ESP_CMD("if topic:find(\"^/control/\") then"); |
zyx | 3:06080fa094a0 | 194 | ESP_CMD("local tok = topic:match(\"^/control/.+/(.+)\")"); |
zyx | 3:06080fa094a0 | 195 | ESP_CMD("if tok then print(\"\\035\"..tok..\"+\"..data) end"); |
zyx | 3:06080fa094a0 | 196 | ESP_CMD("end"); |
zyx | 3:06080fa094a0 | 197 | ESP_CMD("end)"); |
zyx | 3:06080fa094a0 | 198 | |
zyx | 3:06080fa094a0 | 199 | ESP_CMD("m:publish('/events/i_'..node.chipid()..'/online','',1,0)"); |
zyx | 3:06080fa094a0 | 200 | wait(0.1); |
zyx | 3:06080fa094a0 | 201 | |
zyx | 3:06080fa094a0 | 202 | char * capabilities = new char[512]; |
zyx | 3:06080fa094a0 | 203 | |
zyx | 3:06080fa094a0 | 204 | if(sensors){ |
zyx | 3:06080fa094a0 | 205 | buildCapability(capabilities, sensors); |
zyx | 3:06080fa094a0 | 206 | ESP_CMD("m:publish('/capability/i_'..node.chipid()..'/values','%s',1,1)", capabilities); |
zyx | 3:06080fa094a0 | 207 | wait(0.1); |
zyx | 3:06080fa094a0 | 208 | } |
zyx | 3:06080fa094a0 | 209 | if(actuator){ |
zyx | 3:06080fa094a0 | 210 | buildCapability(capabilities, actuator); |
zyx | 3:06080fa094a0 | 211 | ESP_CMD("m:publish('/capability/i_'..node.chipid()..'/control','%s',1,1)", capabilities); |
zyx | 3:06080fa094a0 | 212 | wait(0.1); |
zyx | 3:06080fa094a0 | 213 | for (int i = 0; actuator[i][0]; ++i) |
zyx | 3:06080fa094a0 | 214 | subscribe_control(actuator[i][0]); |
zyx | 3:06080fa094a0 | 215 | } |
zyx | 3:06080fa094a0 | 216 | |
zyx | 3:06080fa094a0 | 217 | delete[ ] capabilities; |
zyx | 3:06080fa094a0 | 218 | |
brainliang | 0:63af4719467f | 219 | return true; |
brainliang | 0:63af4719467f | 220 | } |
brainliang | 0:63af4719467f | 221 | |
zyx | 3:06080fa094a0 | 222 | bool Esp8266::publish_value(const char *topic, const char *data) { //定义类的函数 |
brainliang | 0:63af4719467f | 223 | //if (mqtt_start) { |
zyx | 3:06080fa094a0 | 224 | ESP_CMD("m:publish('/values/i_'..node.chipid()..'/%s',\"%s\",0,1)", topic, data); |
zyx | 3:06080fa094a0 | 225 | wait(0.1); |
brainliang | 0:63af4719467f | 226 | //} |
brainliang | 0:63af4719467f | 227 | return true; |
brainliang | 0:63af4719467f | 228 | } |
brainliang | 0:63af4719467f | 229 | |
zyx | 3:06080fa094a0 | 230 | bool Esp8266::subscribe_control(const char *topic, const char *data) { //定义类的函数 |
brainliang | 0:63af4719467f | 231 | //if (mqtt_start) { |
zyx | 3:06080fa094a0 | 232 | ESP_CMD("m:subscribe('/control/i_'..node.chipid()..'/%s', 0)", topic); |
brainliang | 0:63af4719467f | 233 | wait(0.1); |
brainliang | 0:63af4719467f | 234 | //} |
brainliang | 0:63af4719467f | 235 | |
zyx | 3:06080fa094a0 | 236 | // ESP_CMD("m:unsubscribe(\"%s\")", topic); |
brainliang | 0:63af4719467f | 237 | return true; |
brainliang | 0:63af4719467f | 238 | } |