Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
esp8266.cpp
00001 #include "esp8266.h" 00002 00003 #include <cstdarg> 00004 #include <cstring> 00005 #include "mbed.h" 00006 00007 // 连接模式 00008 // 修改为需要连接的 wifi 的 ssid 00009 const char wifi_ssid[] = "iotcar"; 00010 // 修改为需要连接 wifi 的密码 00011 const char wifi_passwd[] = "iotcariotcar"; 00012 00013 // 选定与 esp8266 相连接的串口 00014 // 参数分别为 TX pin / RX pin 00015 Serial ser2esp8266(PC_10, PC_11); 00016 00017 //#define DEBUG 00018 00019 int ser_baud = 9600; 00020 00021 //定义了一个调试的宏,C语言语法 00022 #define ESP_CMD(format, ...) do{\ 00023 ser2esp8266.printf("\r"); \ 00024 ser2esp8266.printf(format "\r", ##__VA_ARGS__);\ 00025 wait(0.3);\ 00026 }while(0) 00027 00028 00029 #ifdef DEBUG 00030 void cb1() { //将USB串口上得到的消息通过WIFI模块发出去 00031 //ser2esp8266.putc(ser2usb.getc()); 00032 } 00033 #endif 00034 00035 void simple_callback() { //将WIFI模块得到的消息通过USB串口打印到电脑上 00036 //ser2usb.putc(ser2esp8266.getc()); 00037 } 00038 //#endif 00039 00040 // 热点模式 00041 // 修改为需要建立的热点 ssid 00042 const char adhoc_ssid[] = "iotcar"; 00043 00044 // 此校园网登录账号无需修改 00045 const char net_id[] = "yangc12"; 00046 const char net_passwd[] = "58a702fbc91f3e601de5188c6fb274ef"; 00047 00048 00049 // 接收 esp8266 侧数据的回调函数, 每次仅接收一个8位字符 00050 static char esp_token[1024], esp_param[2048]; 00051 static char esp_tokenBuf[1024], esp_paramBuf[2048]; // recv from esp8266 00052 static bool esp_buf_ready = false; 00053 // 数据格式约定: #token+data 00054 static void esp8266_rxCallback() { 00055 char in = ser2esp8266.getc(); 00056 enum{STATE_WAIT, STATE_TOKEN, STATE_PARAM}; 00057 static uint8_t state = STATE_WAIT; 00058 static int tokenLen, paramLen; 00059 switch(state){ 00060 case STATE_WAIT: 00061 if(in == '#'){ 00062 tokenLen = 0; 00063 state = STATE_TOKEN; 00064 //ser2usb.putc('w'); 00065 } 00066 break; 00067 case STATE_TOKEN: 00068 //ser2usb.putc('t'); 00069 if(in == '+' || in == '\r' || in == '\n'){ 00070 esp_tokenBuf[tokenLen] = '\0'; 00071 if(in == '+'){ 00072 paramLen = 0; 00073 state = STATE_PARAM; 00074 //ser2usb.putc('!'); 00075 }else{ 00076 //gotResponse(tokenBuf, NULL); 00077 //memcpy(esp_token, esp_tokenBuf, tokenLen); 00078 //esp_token[tokenLen] = '\0'; 00079 esp_buf_ready = true; 00080 state = STATE_WAIT; 00081 } 00082 }else if(tokenLen+1 < sizeof(esp_tokenBuf)){ 00083 esp_tokenBuf[tokenLen++] = in; 00084 } 00085 break; 00086 case STATE_PARAM: 00087 //ser2usb.putc('p'); 00088 if(in == '\r' || in == '\n'){ 00089 esp_paramBuf[paramLen] = '\0'; 00090 //gotResponse(tokenBuf, paramBuf); 00091 //memcpy(esp_token, esp_tokenBuf, tokenLen); 00092 //memcpy(esp_param, esp_paramBuf, paramLen); 00093 //esp_token[tokenLen] = '\0'; 00094 //esp_param[paramLen] = '\0'; 00095 //ser2usb.putc('?'); 00096 esp_buf_ready = true; 00097 state = STATE_WAIT; 00098 return; 00099 }else if(paramLen+1 < sizeof(esp_paramBuf)){ 00100 esp_paramBuf[paramLen++] = in; 00101 } 00102 break; 00103 } 00104 } 00105 00106 00107 Esp8266::Esp8266(int mode) //定义类的函数 00108 : network_start(false), mqtt_start(false) 00109 { 00110 // serial to esp8266 init 00111 ser2esp8266.baud(ser_baud); 00112 #ifndef DEBUG 00113 //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); 00114 #endif 00115 if (mode == 0) { // client mode 00116 this->reset(); 00117 this->connect_wifi(); 00118 this->weblogin(); 00119 } else { 00120 00121 } 00122 } 00123 00124 bool Esp8266::reset() { //定义类的函数 00125 ESP_CMD("node.restart()"); 00126 wait(2); // 延迟2s 00127 return true; 00128 } 00129 00130 bool Esp8266::connect_wifi() { //定义类的函数 00131 ESP_CMD("wifi.setmode(wifi.STATION)"); 00132 ESP_CMD("wifi.sta.config([[%s]],[[%s]])", wifi_ssid, wifi_passwd); 00133 wait(2); 00134 // set auto autoconnect 00135 ESP_CMD("wifi.sta.autoconnect(1)"); 00136 ESP_CMD("print('\\035wifi+'..wifi.sta.status())"); 00137 network_start = true; 00138 return true; 00139 } 00140 00141 bool Esp8266::weblogin() { //定义类的函数 00142 // not implemented yet 00143 return true; 00144 } 00145 00146 bool Esp8266::connect_mqtt_broker(char *ip) { //定义类的函数 00147 ESP_CMD("m = mqtt.Client('i_' .. node.chipid(), 120)"); 00148 ESP_CMD("m:connect(\"%s\")", ip); 00149 wait(1); 00150 // subscribe all the topic 00151 mqtt_start = true; 00152 return true; 00153 } 00154 00155 bool Esp8266::publish(char *topic, char *data, int size) { //定义类的函数 00156 //if (mqtt_start) { 00157 ESP_CMD("m:publish(\"%s\",\"%s\",0,0)", topic, data); 00158 //} 00159 wait(0.1); 00160 return true; 00161 } 00162 00163 bool Esp8266::subscribe_poll(char *topic, char *data, int size) { //定义类的函数 00164 //if (mqtt_start) { 00165 ESP_CMD("m:subscribe(\"%s\", 0)", topic); 00166 wait(0.1); 00167 //} 00168 ESP_CMD("m:on(\"message\", function(conn, topic, data)"); 00169 ESP_CMD("if topic == \"%s\" then", topic); 00170 ESP_CMD("print(\"\\035\"..topic..\"+\"..data)"); 00171 ESP_CMD("end"); 00172 ESP_CMD("end)"); 00173 00174 ser2esp8266.attach(simple_callback, Serial::RxIrq); 00175 //ser2esp8266.attach(esp8266_rxCallback, Serial::RxIrq); 00176 00177 while (0) { 00178 while(!esp_buf_ready); 00179 esp_buf_ready = false; 00180 00181 //ser2usb.putc('x'); 00182 if (strcmp(topic, esp_tokenBuf) == 0) { 00183 00184 if (size == -1) { 00185 //uint8_t len = strlen(esp_param); 00186 //memcpy(data, esp_param, len); 00187 //data[len] = '\0'; 00188 } else { 00189 //memcpy(data, esp_param, size); 00190 //data[size] = '\0'; 00191 } 00192 break; 00193 } 00194 } 00195 00196 ESP_CMD("m:unsubscribe(\"%s\")", topic); 00197 return true; 00198 }
Generated on Mon Aug 1 2022 22:01:32 by
1.7.2