![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
init
Fork of 仓库 by
esp8266.cpp@8:d5e340a59c71, 2018-03-19 (annotated)
- Committer:
- zhangyx
- Date:
- Mon Mar 19 03:32:21 2018 +0000
- Revision:
- 8:d5e340a59c71
- Parent:
- 7:49a823b5a935
demo with pi;
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{\ |
zhangyx | 6:629e300b6c3b | 25 | char cmdbuf[128], *p;\ |
brainliang | 0:63af4719467f | 26 | ser2esp8266.printf("\r"); \ |
zhangyx | 6:629e300b6c3b | 27 | sprintf(cmdbuf, format "\r", ##__VA_ARGS__);\ |
zhangyx | 6:629e300b6c3b | 28 | for(p=cmdbuf;*p;p++){\ |
zhangyx | 6:629e300b6c3b | 29 | ser2esp8266.putc(*p);\ |
zhangyx | 8:d5e340a59c71 | 30 | wait(0.02);\ |
zhangyx | 6:629e300b6c3b | 31 | }\ |
brainliang | 0:63af4719467f | 32 | wait(0.3);\ |
brainliang | 0:63af4719467f | 33 | }while(0) |
brainliang | 0:63af4719467f | 34 | |
brainliang | 0:63af4719467f | 35 | |
brainliang | 0:63af4719467f | 36 | |
zhangyx | 2:b10925c474fc | 37 | void Esp8266::gotResponse(char *token, char *param) |
zhangyx | 2:b10925c474fc | 38 | { |
zhangyx | 8:d5e340a59c71 | 39 | if(*token<'a' || *token>'z') return; |
zyx | 3:06080fa094a0 | 40 | ser2usb.printf("gotResponse %s %s\r\n", token, param); |
zhangyx | 2:b10925c474fc | 41 | if(strcmp(token, "connected") == 0) |
zhangyx | 2:b10925c474fc | 42 | mqtt_start = true; |
zyx | 3:06080fa094a0 | 43 | else if(strcmp(token, "control") == 0){ |
zyx | 3:06080fa094a0 | 44 | if(!control_cmd){ |
zyx | 3:06080fa094a0 | 45 | strncpy(control_buf, param, sizeof(control_buf)); |
zyx | 3:06080fa094a0 | 46 | control_cmd = true; |
zyx | 3:06080fa094a0 | 47 | } |
zyx | 3:06080fa094a0 | 48 | } |
zhangyx | 2:b10925c474fc | 49 | else if(strcmp(token, "wifi") == 0){ |
zhangyx | 2:b10925c474fc | 50 | if(*param == '5') |
zhangyx | 2:b10925c474fc | 51 | network_start = true; |
zhangyx | 2:b10925c474fc | 52 | } |
zhangyx | 2:b10925c474fc | 53 | } |
brainliang | 0:63af4719467f | 54 | |
zyx | 3:06080fa094a0 | 55 | bool Esp8266::get_control_cmd(char* actuator, char* value) |
zyx | 3:06080fa094a0 | 56 | { |
zyx | 3:06080fa094a0 | 57 | if(!control_cmd) |
zyx | 3:06080fa094a0 | 58 | return false; |
zyx | 3:06080fa094a0 | 59 | |
zyx | 3:06080fa094a0 | 60 | char* plus = strchr(control_buf, '+'); |
zyx | 3:06080fa094a0 | 61 | if(!plus){ |
zyx | 3:06080fa094a0 | 62 | control_cmd = false; |
zyx | 3:06080fa094a0 | 63 | return false; |
zyx | 3:06080fa094a0 | 64 | } |
zyx | 3:06080fa094a0 | 65 | *plus = '\0'; |
zyx | 3:06080fa094a0 | 66 | strcpy(actuator, control_buf); |
zyx | 3:06080fa094a0 | 67 | strcpy(value, plus+1); |
zyx | 3:06080fa094a0 | 68 | control_cmd = false; |
zyx | 3:06080fa094a0 | 69 | return true; |
zyx | 3:06080fa094a0 | 70 | } |
zyx | 3:06080fa094a0 | 71 | |
brainliang | 0:63af4719467f | 72 | // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 |
brainliang | 0:63af4719467f | 73 | // 数据格式约定: #token+data |
zhangyx | 1:3b487c4813a2 | 74 | void Esp8266::esp8266_rxCallback() { |
brainliang | 0:63af4719467f | 75 | char in = ser2esp8266.getc(); |
zhangyx | 8:d5e340a59c71 | 76 | // ser2usb.putc(in); |
brainliang | 0:63af4719467f | 77 | enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM}; |
brainliang | 0:63af4719467f | 78 | static uint8_t state = STATE_WAIT; |
brainliang | 0:63af4719467f | 79 | static int tokenLen, paramLen; |
brainliang | 0:63af4719467f | 80 | switch(state){ |
brainliang | 0:63af4719467f | 81 | case STATE_WAIT: |
brainliang | 0:63af4719467f | 82 | if(in == '#'){ |
brainliang | 0:63af4719467f | 83 | tokenLen = 0; |
brainliang | 0:63af4719467f | 84 | state = STATE_TOKEN; |
brainliang | 0:63af4719467f | 85 | } |
brainliang | 0:63af4719467f | 86 | break; |
brainliang | 0:63af4719467f | 87 | case STATE_TOKEN: |
brainliang | 0:63af4719467f | 88 | if(in == '+' || in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 89 | esp_tokenBuf[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 90 | if(in == '+'){ |
brainliang | 0:63af4719467f | 91 | paramLen = 0; |
brainliang | 0:63af4719467f | 92 | state = STATE_PARAM; |
brainliang | 0:63af4719467f | 93 | }else{ |
zhangyx | 2:b10925c474fc | 94 | gotResponse(esp_tokenBuf, NULL); |
brainliang | 0:63af4719467f | 95 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 96 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 97 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 98 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 99 | } |
brainliang | 0:63af4719467f | 100 | }else if(tokenLen+1 < sizeof(esp_tokenBuf)){ |
brainliang | 0:63af4719467f | 101 | esp_tokenBuf[tokenLen++] = in; |
brainliang | 0:63af4719467f | 102 | } |
brainliang | 0:63af4719467f | 103 | break; |
brainliang | 0:63af4719467f | 104 | case STATE_PARAM: |
brainliang | 0:63af4719467f | 105 | if(in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 106 | esp_paramBuf[paramLen] = '\0'; |
zhangyx | 2:b10925c474fc | 107 | gotResponse(esp_tokenBuf, esp_paramBuf); |
brainliang | 0:63af4719467f | 108 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 109 | //memcpy(esp_param, esp_paramBuf, paramLen); |
brainliang | 0:63af4719467f | 110 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 111 | //esp_param[paramLen] = '\0'; |
brainliang | 0:63af4719467f | 112 | //ser2usb.putc('?'); |
brainliang | 0:63af4719467f | 113 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 114 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 115 | }else if(paramLen+1 < sizeof(esp_paramBuf)){ |
brainliang | 0:63af4719467f | 116 | esp_paramBuf[paramLen++] = in; |
brainliang | 0:63af4719467f | 117 | } |
brainliang | 0:63af4719467f | 118 | break; |
brainliang | 0:63af4719467f | 119 | } |
brainliang | 0:63af4719467f | 120 | } |
brainliang | 0:63af4719467f | 121 | |
brainliang | 0:63af4719467f | 122 | |
zhangyx | 1:3b487c4813a2 | 123 | Esp8266::Esp8266(PinName TX, PinName RX, const char *wifi_ssid, const char *wifi_passwd) //定义类的函数 |
zyx | 3:06080fa094a0 | 124 | : network_start(false), mqtt_start(false), control_cmd(false), esp_buf_ready(false), ser2esp8266(TX, RX) |
brainliang | 0:63af4719467f | 125 | { |
brainliang | 0:63af4719467f | 126 | // serial to esp8266 init |
brainliang | 0:63af4719467f | 127 | ser2esp8266.baud(ser_baud); |
zhangyx | 2:b10925c474fc | 128 | ser2esp8266.attach(callback(this,&Esp8266::esp8266_rxCallback), Serial::RxIrq); |
zhangyx | 1:3b487c4813a2 | 129 | //if (mode == 0) { // client mode |
brainliang | 0:63af4719467f | 130 | this->reset(); |
zhangyx | 1:3b487c4813a2 | 131 | this->connect_wifi(wifi_ssid, wifi_passwd); |
zhangyx | 2:b10925c474fc | 132 | while(!is_connected()){ |
zhangyx | 2:b10925c474fc | 133 | wait(0.5); |
zhangyx | 2:b10925c474fc | 134 | } |
brainliang | 0:63af4719467f | 135 | this->weblogin(); |
zhangyx | 1:3b487c4813a2 | 136 | //} else { |
zhangyx | 1:3b487c4813a2 | 137 | // |
zhangyx | 1:3b487c4813a2 | 138 | //} |
brainliang | 0:63af4719467f | 139 | } |
brainliang | 0:63af4719467f | 140 | |
brainliang | 0:63af4719467f | 141 | bool Esp8266::reset() { //定义类的函数 |
brainliang | 0:63af4719467f | 142 | ESP_CMD("node.restart()"); |
brainliang | 0:63af4719467f | 143 | wait(2); // 延迟2s |
brainliang | 0:63af4719467f | 144 | return true; |
brainliang | 0:63af4719467f | 145 | } |
brainliang | 0:63af4719467f | 146 | |
zhangyx | 1:3b487c4813a2 | 147 | bool Esp8266::connect_wifi(const char *wifi_ssid, const char *wifi_passwd) { //定义类的函数 |
brainliang | 0:63af4719467f | 148 | ESP_CMD("wifi.setmode(wifi.STATION)"); |
brainliang | 0:63af4719467f | 149 | ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd); |
brainliang | 0:63af4719467f | 150 | wait(2); |
brainliang | 0:63af4719467f | 151 | // set auto autoconnect |
brainliang | 0:63af4719467f | 152 | ESP_CMD("wifi.sta.autoconnect(1)"); |
zhangyx | 2:b10925c474fc | 153 | return true; |
zhangyx | 2:b10925c474fc | 154 | } |
zhangyx | 2:b10925c474fc | 155 | |
zhangyx | 2:b10925c474fc | 156 | bool Esp8266::is_connected() |
zhangyx | 2:b10925c474fc | 157 | { |
brainliang | 0:63af4719467f | 158 | ESP_CMD("print('\\035wifi+'..wifi.sta.status())"); |
zhangyx | 2:b10925c474fc | 159 | wait(0.4); |
zhangyx | 2:b10925c474fc | 160 | return network_start; |
brainliang | 0:63af4719467f | 161 | } |
brainliang | 0:63af4719467f | 162 | |
brainliang | 0:63af4719467f | 163 | bool Esp8266::weblogin() { //定义类的函数 |
brainliang | 0:63af4719467f | 164 | // not implemented yet |
brainliang | 0:63af4719467f | 165 | return true; |
brainliang | 0:63af4719467f | 166 | } |
brainliang | 0:63af4719467f | 167 | |
zyx | 3:06080fa094a0 | 168 | void Esp8266::buildCapability(char *out, const char* infoList[][2]) |
zyx | 3:06080fa094a0 | 169 | { |
zyx | 3:06080fa094a0 | 170 | out[0] = '\0'; |
zyx | 3:06080fa094a0 | 171 | for (int i = 0; infoList[i][0]; ++i) |
zyx | 3:06080fa094a0 | 172 | { |
zyx | 3:06080fa094a0 | 173 | strcat(out, infoList[i][0]); |
zyx | 3:06080fa094a0 | 174 | strcat(out, ","); |
zyx | 3:06080fa094a0 | 175 | strcat(out, infoList[i][1]); |
zyx | 3:06080fa094a0 | 176 | strcat(out, "\\n"); |
zyx | 3:06080fa094a0 | 177 | } |
zyx | 3:06080fa094a0 | 178 | } |
zyx | 3:06080fa094a0 | 179 | |
zhangyx | 8:d5e340a59c71 | 180 | bool Esp8266::connect_mqtt_broker(char *ip, const char *node_name, const char* sensors[][2], const char* actuator[][2]) { //定义类的函数 |
zyx | 3:06080fa094a0 | 181 | |
zhangyx | 8:d5e340a59c71 | 182 | ESP_CMD("node_name = '%s'", node_name); |
brainliang | 0:63af4719467f | 183 | ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); |
zhangyx | 2:b10925c474fc | 184 | ESP_CMD("m:connect(\"%s\",1883,0,function(conn)print (\"\\035connected\"); end)", ip); |
zhangyx | 2:b10925c474fc | 185 | |
zhangyx | 2:b10925c474fc | 186 | do{ |
zhangyx | 2:b10925c474fc | 187 | wait(0.5); |
zhangyx | 2:b10925c474fc | 188 | }while(!mqtt_start); |
zyx | 3:06080fa094a0 | 189 | |
zyx | 3:06080fa094a0 | 190 | ESP_CMD("m:on(\"message\", function(conn, topic, data)"); |
zyx | 3:06080fa094a0 | 191 | ESP_CMD("if topic:find(\"^/control/\") then"); |
zyx | 3:06080fa094a0 | 192 | ESP_CMD("local tok = topic:match(\"^/control/.+/(.+)\")"); |
zhangyx | 7:49a823b5a935 | 193 | ESP_CMD("if tok then print(\"\\035control+\"..tok..\"+\"..data) end"); |
zyx | 3:06080fa094a0 | 194 | ESP_CMD("end"); |
zyx | 3:06080fa094a0 | 195 | ESP_CMD("end)"); |
zyx | 3:06080fa094a0 | 196 | |
zhangyx | 8:d5e340a59c71 | 197 | ESP_CMD("m:publish('/events/'..node_name..'/online','',1,0)"); |
zyx | 3:06080fa094a0 | 198 | wait(0.1); |
zyx | 3:06080fa094a0 | 199 | |
zyx | 3:06080fa094a0 | 200 | char * capabilities = new char[512]; |
zyx | 3:06080fa094a0 | 201 | |
zyx | 3:06080fa094a0 | 202 | if(sensors){ |
zyx | 3:06080fa094a0 | 203 | buildCapability(capabilities, sensors); |
zhangyx | 8:d5e340a59c71 | 204 | ESP_CMD("m:publish('/capability/'..node_name..'/values','%s',1,1)", capabilities); |
zyx | 3:06080fa094a0 | 205 | wait(0.1); |
zyx | 3:06080fa094a0 | 206 | } |
zyx | 3:06080fa094a0 | 207 | if(actuator){ |
zyx | 3:06080fa094a0 | 208 | buildCapability(capabilities, actuator); |
zhangyx | 8:d5e340a59c71 | 209 | ESP_CMD("m:publish('/capability/'..node_name..'/control','%s',1,1)", capabilities); |
zyx | 3:06080fa094a0 | 210 | wait(0.1); |
zyx | 3:06080fa094a0 | 211 | for (int i = 0; actuator[i][0]; ++i) |
zyx | 3:06080fa094a0 | 212 | subscribe_control(actuator[i][0]); |
zyx | 3:06080fa094a0 | 213 | } |
zyx | 3:06080fa094a0 | 214 | |
zyx | 3:06080fa094a0 | 215 | delete[ ] capabilities; |
zyx | 3:06080fa094a0 | 216 | |
brainliang | 0:63af4719467f | 217 | return true; |
brainliang | 0:63af4719467f | 218 | } |
brainliang | 0:63af4719467f | 219 | |
zyx | 3:06080fa094a0 | 220 | bool Esp8266::publish_value(const char *topic, const char *data) { //定义类的函数 |
brainliang | 0:63af4719467f | 221 | //if (mqtt_start) { |
zhangyx | 8:d5e340a59c71 | 222 | ESP_CMD("m:publish('/values/'..node_name..'/%s',\"%s\",0,1)", topic, data); |
zyx | 3:06080fa094a0 | 223 | wait(0.1); |
brainliang | 0:63af4719467f | 224 | //} |
brainliang | 0:63af4719467f | 225 | return true; |
brainliang | 0:63af4719467f | 226 | } |
brainliang | 0:63af4719467f | 227 | |
zyx | 3:06080fa094a0 | 228 | bool Esp8266::subscribe_control(const char *topic, const char *data) { //定义类的函数 |
brainliang | 0:63af4719467f | 229 | //if (mqtt_start) { |
zhangyx | 8:d5e340a59c71 | 230 | ESP_CMD("m:subscribe('/control/'..node_name..'/%s', 0)", topic); |
brainliang | 0:63af4719467f | 231 | wait(0.1); |
brainliang | 0:63af4719467f | 232 | //} |
brainliang | 0:63af4719467f | 233 | |
zyx | 3:06080fa094a0 | 234 | // ESP_CMD("m:unsubscribe(\"%s\")", topic); |
brainliang | 0:63af4719467f | 235 | return true; |
brainliang | 0:63af4719467f | 236 | } |