Paul Staron
/
ESP8266-configuaration-baudrate
ESP8266 set up for AT version 1.7.0
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 Serial pc(USBTX, USBRX); 00004 Serial esp(p13,p14); // tx,rx LPC1768 00005 //Serial esp(D8,D2); // tx,rx Nucleo 00006 00007 Timer t; 00008 00009 char cmdbuff[1024]; 00010 char snd[255]; 00011 int timeout; 00012 int Baud; 00013 00014 char ssid[32] = "ssid"; // enter router ssid inside the quotes 00015 char pwd [32] = "password"; // enter router password inside the quotes 00016 00017 void SendCMD(),getreply(),config(),restore(),getESP(); 00018 void setbaud(int espbaud); 00019 00020 00021 int main() { 00022 00023 pc.baud(115200); 00024 esp.baud(115200); // Default baud is 115200, change this to match setbaud() rate. 00025 00026 pc.printf("\033[0m\033[2J\033[H\n Initialize....\r\n\n\n"); 00027 00028 // restore(); //include to restore ESP8266 to default settings at 115200 baud. 00029 // setbaud(460800); //include to set a different ESP8266 baudrate, saved in ESP8266 flash. 00030 // config(); //include Config to set SSID, PASSWORD and MODE configuration saved in ESP8266 flash. 00031 00032 strcpy(snd,"ATE0\r\n"); 00033 SendCMD(); // set no AT ESP echo 00034 timeout=5000; 00035 getreply(); 00036 pc.printf(cmdbuff); 00037 00038 pc.printf("Set Mode to station...\r\n"); 00039 strcpy(snd, "AT+CWMODE=1\r\n"); 00040 SendCMD(); 00041 timeout=1000; 00042 getreply(); 00043 pc.printf(cmdbuff); 00044 00045 pc.printf("Connect to AP settings NOT stored in flash..\r\n"); 00046 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); 00047 strcpy(snd, "AT+CWJAP_CUR=\""); 00048 strcat(snd, ssid); 00049 strcat(snd, "\",\""); 00050 strcat(snd, pwd); 00051 strcat(snd, "\"\r\n"); 00052 SendCMD(); 00053 timeout=8000; 00054 getreply(); 00055 pc.printf(cmdbuff); 00056 00057 getESP(); 00058 } 00059 00060 // contiuosly get ESP82660Firmware, AP list, IP and MAC 00061 void getESP() 00062 { 00063 while(1){ 00064 00065 pc.printf("\033[0m\033[2J\033[H ESP8266 check...\r\n\n\n"); 00066 00067 pc.printf(" Firmware Version...\r\n"); 00068 strcpy(snd,"AT+GMR\r\n"); 00069 SendCMD(); 00070 timeout=5000; 00071 getreply(); 00072 pc.printf(cmdbuff); 00073 00074 pc.printf("\n AP's...\r\n"); 00075 strcpy(snd, "AT+CWLAP\r\n"); 00076 SendCMD(); 00077 timeout=3000; 00078 getreply(); 00079 pc.printf(cmdbuff); 00080 00081 pc.printf(" IP and MAC...\r\n"); 00082 strcpy(snd, "AT+CIFSR\r\n"); 00083 SendCMD(); 00084 timeout=2000; 00085 getreply(); 00086 pc.printf(cmdbuff); 00087 wait_us(5000000); 00088 } 00089 } 00090 00091 // resturns ESP8266 back to default settings. 00092 void restore() 00093 { 00094 pc.printf("Restore ESP8266 to default...\r\n"); 00095 t.reset();t.start(); 00096 strcpy(snd, "AT+RESTORE\r\n"); 00097 SendCMD(); 00098 timeout=5000; 00099 getreply(); 00100 pc.printf(cmdbuff); 00101 wait_us(2000000); 00102 esp.baud(115200); // set baud back to ESP8266 default 00103 strcpy(snd,"ATE0\r\n"); 00104 SendCMD(); // set no ESP echo 00105 timeout=5000; 00106 getreply(); 00107 pc.printf("Remember to set esp.baud to default rate 115200\r\n"); 00108 wait_us(5000000); 00109 } 00110 00111 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed 00112 void setbaud(int espbaud) 00113 { 00114 if(espbaud!=115200 && espbaud!=230400 && espbaud!=460800 && espbaud!=921600){ 00115 return; 00116 } 00117 pc.printf("setting ESP8266 baud to: %d\r\n",espbaud); 00118 uint32_t databits=8; 00119 uint32_t stopbits=1; 00120 uint32_t parity=0; 00121 uint32_t flowcontrol=0; 00122 // default mode 8,1,0,0 00123 // <databits>: <stopbits>: <parity>: <flowcontrol>: 00124 // 5-bit data 1=1-bit stop bit 0: None 0: flow control disabled 00125 // 6-bit data 2=1.5-bit stop bit 1: Odd 1: enable RTS 00126 // 7-bit data 3=2-bit stop bit 2: Even 2: enable CTS 00127 // 8-bit data 3: enable both RTS and CTS 00128 00129 sprintf(snd,"AT+UART_DEF=%d,%d,%d,%d,%d\r\n",espbaud,databits,stopbits,parity,flowcontrol); 00130 SendCMD(); 00131 pc.printf("Remember to set esp.baud to new rate: %d\r\n",espbaud); 00132 wait_us(3000000); 00133 esp.baud(espbaud); // set baud new rate 00134 } 00135 00136 // To configure ESP8266 permanant settings in flash 00137 void config() 00138 { 00139 pc.printf("\033[0m\033[2J\033[H Starting ESP Config stored in flash\r\n\n\n"); 00140 00141 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) 00142 pc.printf("Set Mode...\r\n"); 00143 strcpy(snd, "AT+CWMODE_DEF=1\r\n"); 00144 SendCMD(); 00145 timeout=1000; 00146 getreply(); 00147 pc.printf(cmdbuff); 00148 00149 pc.printf("Listing Acces Points...\r\n"); 00150 strcpy(snd, "AT+CWLAP\r\n"); 00151 SendCMD(); 00152 timeout=3000; 00153 getreply(); 00154 pc.printf(cmdbuff); 00155 00156 pc.printf("Connecting to AP...\r\n"); 00157 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); 00158 strcpy(snd, "AT+CWJAP_DEF=\""); 00159 strcat(snd, ssid); 00160 strcat(snd, "\",\""); 00161 strcat(snd, pwd); 00162 strcat(snd, "\"\r\n"); 00163 SendCMD(); 00164 timeout=8000; 00165 getreply(); 00166 pc.printf(cmdbuff); 00167 00168 pc.printf("Get IP's...\r\n"); 00169 strcpy(snd, "AT+CIFSR\r\n"); 00170 SendCMD(); 00171 timeout=2000; 00172 getreply(); 00173 pc.printf(cmdbuff); 00174 00175 pc.printf("\n\n\n If you get a valid IP, ESP8266 has been set up.\r\n"); 00176 wait_us(5000000); 00177 00178 getESP(); 00179 } 00180 00181 void SendCMD() 00182 { 00183 esp.printf("%s", snd); 00184 } 00185 00186 void getreply() 00187 { 00188 uint32_t i=0; 00189 memset(cmdbuff, '\0', sizeof(cmdbuff)); 00190 t.reset();t.start(); 00191 while(t.read_ms()< timeout) { 00192 if(esp.readable()) { 00193 cmdbuff[i] = esp.getc();i++; 00194 } 00195 if (cmdbuff[i-2] == 0x4f && cmdbuff[i-1] == 0x4b){break;} 00196 } 00197 cmdbuff[i-2]=0x00; // remove the 'OK' from message 00198 t.stop(); 00199 } 00200
Generated on Fri Jul 15 2022 13:51:08 by 1.7.2