王铭轩 仓库

Dependencies:   mbed

Fork of SBY_sw4stm32_nucleo_f103rb by w mx

Committer:
xmwmx
Date:
Fri Aug 03 06:22:21 2018 +0000
Revision:
3:55a34662204f
Parent:
0:7725181a2a09
?????

Who changed what in which revision?

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