liang brain
/
my_example_esp8266
8266
esp8266.cpp@0:63af4719467f, 2017-10-11 (annotated)
- Committer:
- brainliang
- Date:
- Wed Oct 11 08:40:04 2017 +0000
- Revision:
- 0:63af4719467f
esp8266
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brainliang | 0:63af4719467f | 1 | #include "esp8266.h" |
brainliang | 0:63af4719467f | 2 | |
brainliang | 0:63af4719467f | 3 | #include <cstdarg> |
brainliang | 0:63af4719467f | 4 | #include <cstring> |
brainliang | 0:63af4719467f | 5 | #include "mbed.h" |
brainliang | 0:63af4719467f | 6 | |
brainliang | 0:63af4719467f | 7 | // 连接模式 |
brainliang | 0:63af4719467f | 8 | // 修改为需要连接的 wifi 的 ssid |
brainliang | 0:63af4719467f | 9 | const char wifi_ssid[] = "iotcar"; |
brainliang | 0:63af4719467f | 10 | // 修改为需要连接 wifi 的密码 |
brainliang | 0:63af4719467f | 11 | const char wifi_passwd[] = "iotcariotcar"; |
brainliang | 0:63af4719467f | 12 | |
brainliang | 0:63af4719467f | 13 | // 选定与 esp8266 相连接的串口 |
brainliang | 0:63af4719467f | 14 | // 参数分别为 TX pin / RX pin |
brainliang | 0:63af4719467f | 15 | Serial ser2esp8266(PC_10, PC_11); |
brainliang | 0:63af4719467f | 16 | |
brainliang | 0:63af4719467f | 17 | //#define DEBUG |
brainliang | 0:63af4719467f | 18 | |
brainliang | 0:63af4719467f | 19 | int ser_baud = 9600; |
brainliang | 0:63af4719467f | 20 | |
brainliang | 0:63af4719467f | 21 | //定义了一个调试的宏,C语言语法 |
brainliang | 0:63af4719467f | 22 | #define ESP_CMD(format, ...) do{\ |
brainliang | 0:63af4719467f | 23 | ser2esp8266.printf("\r"); \ |
brainliang | 0:63af4719467f | 24 | ser2esp8266.printf(format "\r", ##__VA_ARGS__);\ |
brainliang | 0:63af4719467f | 25 | wait(0.3);\ |
brainliang | 0:63af4719467f | 26 | }while(0) |
brainliang | 0:63af4719467f | 27 | |
brainliang | 0:63af4719467f | 28 | |
brainliang | 0:63af4719467f | 29 | #ifdef DEBUG |
brainliang | 0:63af4719467f | 30 | void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 |
brainliang | 0:63af4719467f | 31 | //ser2esp8266.putc(ser2usb.getc()); |
brainliang | 0:63af4719467f | 32 | } |
brainliang | 0:63af4719467f | 33 | #endif |
brainliang | 0:63af4719467f | 34 | |
brainliang | 0:63af4719467f | 35 | void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 |
brainliang | 0:63af4719467f | 36 | //ser2usb.putc(ser2esp8266.getc()); |
brainliang | 0:63af4719467f | 37 | } |
brainliang | 0:63af4719467f | 38 | //#endif |
brainliang | 0:63af4719467f | 39 | |
brainliang | 0:63af4719467f | 40 | // 热点模式 |
brainliang | 0:63af4719467f | 41 | // 修改为需要建立的热点 ssid |
brainliang | 0:63af4719467f | 42 | const char adhoc_ssid[] = "iotcar"; |
brainliang | 0:63af4719467f | 43 | |
brainliang | 0:63af4719467f | 44 | // 此校园网登录账号无需修改 |
brainliang | 0:63af4719467f | 45 | const char net_id[] = "yangc12"; |
brainliang | 0:63af4719467f | 46 | const char net_passwd[] = "58a702fbc91f3e601de5188c6fb274ef"; |
brainliang | 0:63af4719467f | 47 | |
brainliang | 0:63af4719467f | 48 | |
brainliang | 0:63af4719467f | 49 | // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 |
brainliang | 0:63af4719467f | 50 | static char esp_token[1024], esp_param[2048]; |
brainliang | 0:63af4719467f | 51 | static char esp_tokenBuf[1024], esp_paramBuf[2048]; // recv from esp8266 |
brainliang | 0:63af4719467f | 52 | static bool esp_buf_ready = false; |
brainliang | 0:63af4719467f | 53 | // 数据格式约定: #token+data |
brainliang | 0:63af4719467f | 54 | static void esp8266_rxCallback() { |
brainliang | 0:63af4719467f | 55 | char in = ser2esp8266.getc(); |
brainliang | 0:63af4719467f | 56 | enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM}; |
brainliang | 0:63af4719467f | 57 | static uint8_t state = STATE_WAIT; |
brainliang | 0:63af4719467f | 58 | static int tokenLen, paramLen; |
brainliang | 0:63af4719467f | 59 | switch(state){ |
brainliang | 0:63af4719467f | 60 | case STATE_WAIT: |
brainliang | 0:63af4719467f | 61 | if(in == '#'){ |
brainliang | 0:63af4719467f | 62 | tokenLen = 0; |
brainliang | 0:63af4719467f | 63 | state = STATE_TOKEN; |
brainliang | 0:63af4719467f | 64 | //ser2usb.putc('w'); |
brainliang | 0:63af4719467f | 65 | } |
brainliang | 0:63af4719467f | 66 | break; |
brainliang | 0:63af4719467f | 67 | case STATE_TOKEN: |
brainliang | 0:63af4719467f | 68 | //ser2usb.putc('t'); |
brainliang | 0:63af4719467f | 69 | if(in == '+' || in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 70 | esp_tokenBuf[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 71 | if(in == '+'){ |
brainliang | 0:63af4719467f | 72 | paramLen = 0; |
brainliang | 0:63af4719467f | 73 | state = STATE_PARAM; |
brainliang | 0:63af4719467f | 74 | //ser2usb.putc('!'); |
brainliang | 0:63af4719467f | 75 | }else{ |
brainliang | 0:63af4719467f | 76 | //gotResponse(tokenBuf, NULL); |
brainliang | 0:63af4719467f | 77 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 78 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 79 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 80 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 81 | } |
brainliang | 0:63af4719467f | 82 | }else if(tokenLen+1 < sizeof(esp_tokenBuf)){ |
brainliang | 0:63af4719467f | 83 | esp_tokenBuf[tokenLen++] = in; |
brainliang | 0:63af4719467f | 84 | } |
brainliang | 0:63af4719467f | 85 | break; |
brainliang | 0:63af4719467f | 86 | case STATE_PARAM: |
brainliang | 0:63af4719467f | 87 | //ser2usb.putc('p'); |
brainliang | 0:63af4719467f | 88 | if(in == '\r' || in == '\n'){ |
brainliang | 0:63af4719467f | 89 | esp_paramBuf[paramLen] = '\0'; |
brainliang | 0:63af4719467f | 90 | //gotResponse(tokenBuf, paramBuf); |
brainliang | 0:63af4719467f | 91 | //memcpy(esp_token, esp_tokenBuf, tokenLen); |
brainliang | 0:63af4719467f | 92 | //memcpy(esp_param, esp_paramBuf, paramLen); |
brainliang | 0:63af4719467f | 93 | //esp_token[tokenLen] = '\0'; |
brainliang | 0:63af4719467f | 94 | //esp_param[paramLen] = '\0'; |
brainliang | 0:63af4719467f | 95 | //ser2usb.putc('?'); |
brainliang | 0:63af4719467f | 96 | esp_buf_ready = true; |
brainliang | 0:63af4719467f | 97 | state = STATE_WAIT; |
brainliang | 0:63af4719467f | 98 | return; |
brainliang | 0:63af4719467f | 99 | }else if(paramLen+1 < sizeof(esp_paramBuf)){ |
brainliang | 0:63af4719467f | 100 | esp_paramBuf[paramLen++] = in; |
brainliang | 0:63af4719467f | 101 | } |
brainliang | 0:63af4719467f | 102 | break; |
brainliang | 0:63af4719467f | 103 | } |
brainliang | 0:63af4719467f | 104 | } |
brainliang | 0:63af4719467f | 105 | |
brainliang | 0:63af4719467f | 106 | |
brainliang | 0:63af4719467f | 107 | Esp8266::Esp8266(int mode) //定义类的函数 |
brainliang | 0:63af4719467f | 108 | : network_start(false), mqtt_start(false) |
brainliang | 0:63af4719467f | 109 | { |
brainliang | 0:63af4719467f | 110 | // serial to esp8266 init |
brainliang | 0:63af4719467f | 111 | ser2esp8266.baud(ser_baud); |
brainliang | 0:63af4719467f | 112 | #ifndef DEBUG |
brainliang | 0:63af4719467f | 113 | //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); |
brainliang | 0:63af4719467f | 114 | #endif |
brainliang | 0:63af4719467f | 115 | if (mode == 0) { // client mode |
brainliang | 0:63af4719467f | 116 | this->reset(); |
brainliang | 0:63af4719467f | 117 | this->connect_wifi(); |
brainliang | 0:63af4719467f | 118 | this->weblogin(); |
brainliang | 0:63af4719467f | 119 | } else { |
brainliang | 0:63af4719467f | 120 | |
brainliang | 0:63af4719467f | 121 | } |
brainliang | 0:63af4719467f | 122 | } |
brainliang | 0:63af4719467f | 123 | |
brainliang | 0:63af4719467f | 124 | bool Esp8266::reset() { //定义类的函数 |
brainliang | 0:63af4719467f | 125 | ESP_CMD("node.restart()"); |
brainliang | 0:63af4719467f | 126 | wait(2); // 延迟2s |
brainliang | 0:63af4719467f | 127 | return true; |
brainliang | 0:63af4719467f | 128 | } |
brainliang | 0:63af4719467f | 129 | |
brainliang | 0:63af4719467f | 130 | bool Esp8266::connect_wifi() { //定义类的函数 |
brainliang | 0:63af4719467f | 131 | ESP_CMD("wifi.setmode(wifi.STATION)"); |
brainliang | 0:63af4719467f | 132 | ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd); |
brainliang | 0:63af4719467f | 133 | wait(2); |
brainliang | 0:63af4719467f | 134 | // set auto autoconnect |
brainliang | 0:63af4719467f | 135 | ESP_CMD("wifi.sta.autoconnect(1)"); |
brainliang | 0:63af4719467f | 136 | ESP_CMD("print('\\035wifi+'..wifi.sta.status())"); |
brainliang | 0:63af4719467f | 137 | network_start = true; |
brainliang | 0:63af4719467f | 138 | return true; |
brainliang | 0:63af4719467f | 139 | } |
brainliang | 0:63af4719467f | 140 | |
brainliang | 0:63af4719467f | 141 | bool Esp8266::weblogin() { //定义类的函数 |
brainliang | 0:63af4719467f | 142 | // not implemented yet |
brainliang | 0:63af4719467f | 143 | return true; |
brainliang | 0:63af4719467f | 144 | } |
brainliang | 0:63af4719467f | 145 | |
brainliang | 0:63af4719467f | 146 | bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数 |
brainliang | 0:63af4719467f | 147 | ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); |
brainliang | 0:63af4719467f | 148 | ESP_CMD("m:connect(\"%s\")", ip); |
brainliang | 0:63af4719467f | 149 | wait(1); |
brainliang | 0:63af4719467f | 150 | // subscribe all the topic |
brainliang | 0:63af4719467f | 151 | mqtt_start = true; |
brainliang | 0:63af4719467f | 152 | return true; |
brainliang | 0:63af4719467f | 153 | } |
brainliang | 0:63af4719467f | 154 | |
brainliang | 0:63af4719467f | 155 | bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数 |
brainliang | 0:63af4719467f | 156 | //if (mqtt_start) { |
brainliang | 0:63af4719467f | 157 | ESP_CMD("m:publish(\"%s\",\"%s\",0,0)", topic, data); |
brainliang | 0:63af4719467f | 158 | //} |
brainliang | 0:63af4719467f | 159 | wait(0.1); |
brainliang | 0:63af4719467f | 160 | return true; |
brainliang | 0:63af4719467f | 161 | } |
brainliang | 0:63af4719467f | 162 | |
brainliang | 0:63af4719467f | 163 | bool Esp8266::subscribe_poll(char *topic, char *data, int size) { //定义类的函数 |
brainliang | 0:63af4719467f | 164 | //if (mqtt_start) { |
brainliang | 0:63af4719467f | 165 | ESP_CMD("m:subscribe(\"%s\", 0)", topic); |
brainliang | 0:63af4719467f | 166 | wait(0.1); |
brainliang | 0:63af4719467f | 167 | //} |
brainliang | 0:63af4719467f | 168 | ESP_CMD("m:on(\"message\", function(conn, topic, data)"); |
brainliang | 0:63af4719467f | 169 | ESP_CMD("if topic == \"%s\" then", topic); |
brainliang | 0:63af4719467f | 170 | ESP_CMD("print(\"\\035\"..topic..\"+\"..data)"); |
brainliang | 0:63af4719467f | 171 | ESP_CMD("end"); |
brainliang | 0:63af4719467f | 172 | ESP_CMD("end)"); |
brainliang | 0:63af4719467f | 173 | |
brainliang | 0:63af4719467f | 174 | ser2esp8266.attach(simple_callback, Serial::RxIrq); |
brainliang | 0:63af4719467f | 175 | //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); |
brainliang | 0:63af4719467f | 176 | |
brainliang | 0:63af4719467f | 177 | while (0) { |
brainliang | 0:63af4719467f | 178 | while(!esp_buf_ready); |
brainliang | 0:63af4719467f | 179 | esp_buf_ready = false; |
brainliang | 0:63af4719467f | 180 | |
brainliang | 0:63af4719467f | 181 | //ser2usb.putc('x'); |
brainliang | 0:63af4719467f | 182 | if (strcmp(topic, esp_tokenBuf) == 0) { |
brainliang | 0:63af4719467f | 183 | |
brainliang | 0:63af4719467f | 184 | if (size == -1) { |
brainliang | 0:63af4719467f | 185 | //uint8_t len = strlen(esp_param); |
brainliang | 0:63af4719467f | 186 | //memcpy(data, esp_param, len); |
brainliang | 0:63af4719467f | 187 | //data[len] = '\0'; |
brainliang | 0:63af4719467f | 188 | } else { |
brainliang | 0:63af4719467f | 189 | //memcpy(data, esp_param, size); |
brainliang | 0:63af4719467f | 190 | //data[size] = '\0'; |
brainliang | 0:63af4719467f | 191 | } |
brainliang | 0:63af4719467f | 192 | break; |
brainliang | 0:63af4719467f | 193 | } |
brainliang | 0:63af4719467f | 194 | } |
brainliang | 0:63af4719467f | 195 | |
brainliang | 0:63af4719467f | 196 | ESP_CMD("m:unsubscribe(\"%s\")", topic); |
brainliang | 0:63af4719467f | 197 | return true; |
brainliang | 0:63af4719467f | 198 | } |