a
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed-rtos mbed wave_player
Wifi.cpp
00001 #include "Wifi.h" 00002 00003 Wifi::Wifi(PinName tx, PinName rx, PinName rst) 00004 : esp(tx, rx), reset(rst), led1(LED1), led2(LED2), led3(LED3), led4(LED4) { 00005 ssid = "GTother"; // enter WiFi router ssid inside the quotes 00006 pwd = "GeorgeP@1927"; // enter WiFi router password inside the quotes 00007 } 00008 void dev_recv() 00009 { 00010 led1 = !led1; 00011 while(esp.readable()) { 00012 pc.putc(esp.getc()); 00013 } 00014 } 00015 00016 void pc_recv() 00017 { 00018 led4 = !led4; 00019 while(pc.readable()) { 00020 esp.putc(pc.getc()); 00021 } 00022 } 00023 void Wifi::ESPsetbaudrate() { 00024 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate 00025 SendCMD(); 00026 } 00027 void Wifi::ESPconfig() { 00028 wait(5); 00029 pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); 00030 strcpy(snd,".\r\n.\r\n"); 00031 SendCMD(); 00032 wait(1); 00033 pc.printf("---------- Reset & get Firmware ----------\r\n"); 00034 strcpy(snd,"node.restart()\r\n"); 00035 SendCMD(); 00036 timeout=5; 00037 getreply(); 00038 pc.printf(buf); 00039 00040 wait(2); 00041 00042 pc.printf("\n---------- Get Version ----------\r\n"); 00043 strcpy(snd,"print(node.info())\r\n"); 00044 SendCMD(); 00045 timeout=4; 00046 getreply(); 00047 pc.printf(buf); 00048 00049 wait(3); 00050 00051 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) 00052 pc.printf("\n---------- Setting Mode ----------\r\n"); 00053 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n"); 00054 SendCMD(); 00055 timeout=4; 00056 getreply(); 00057 pc.printf(buf); 00058 00059 wait(2); 00060 00061 00062 00063 pc.printf("\n---------- Listing Access Points ----------\r\n"); 00064 strcpy(snd, "function listap(t)\r\n"); 00065 SendCMD(); 00066 wait(1); 00067 strcpy(snd, "for k,v in pairs(t) do\r\n"); 00068 SendCMD(); 00069 wait(1); 00070 strcpy(snd, "print(k..\" : \"..v)\r\n"); 00071 SendCMD(); 00072 wait(1); 00073 strcpy(snd, "end\r\n"); 00074 SendCMD(); 00075 wait(1); 00076 strcpy(snd, "end\r\n"); 00077 SendCMD(); 00078 wait(1); 00079 strcpy(snd, "wifi.sta.getap(listap)\r\n"); 00080 SendCMD(); 00081 wait(1); 00082 timeout=15; 00083 getreply(); 00084 pc.printf(buf); 00085 00086 wait(2); 00087 00088 pc.printf("\n---------- Connecting to AP ----------\r\n"); 00089 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); 00090 strcpy(snd, "wifi.sta.config(\""); 00091 strcat(snd, ssid); 00092 strcat(snd, "\",\""); 00093 strcat(snd, pwd); 00094 strcat(snd, "\")\r\n"); 00095 SendCMD(); 00096 timeout=10; 00097 getreply(); 00098 pc.printf(buf); 00099 00100 wait(5); 00101 00102 pc.printf("\n---------- Get IP's ----------\r\n"); 00103 strcpy(snd, "print(wifi.sta.getip())\r\n"); 00104 SendCMD(); 00105 timeout=3; 00106 getreply(); 00107 pc.printf(buf); 00108 00109 wait(1); 00110 00111 pc.printf("\n---------- Get Connection Status ----------\r\n"); 00112 strcpy(snd, "print(wifi.sta.status())\r\n"); 00113 SendCMD(); 00114 timeout=5; 00115 getreply(); 00116 pc.printf(buf); 00117 00118 pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); 00119 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); 00120 pc.printf(" It saves the SSID and password settings internally\r\n"); 00121 wait(10); 00122 00123 00124 pc.printf("\n---------- Setting up http server ----------\r\n"); 00125 strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); 00126 SendCMD(); 00127 wait(1); 00128 strcpy(snd, "srv:listen(80,function(conn)\r\n"); 00129 SendCMD(); 00130 wait(1); 00131 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n"); 00132 SendCMD(); 00133 wait(1); 00134 strcpy(snd, "print(payload)\r\n"); 00135 SendCMD(); 00136 wait(1); 00137 00138 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); 00139 SendCMD(); 00140 wait(1); 00141 00142 strcpy(snd, "conn:send(\"<html>\")\r\n"); 00143 SendCMD(); 00144 wait(1); 00145 00146 strcpy(snd, "conn:send(\"<h1> Hi James, NodeMcu.</h1>\")\r\n"); 00147 SendCMD(); 00148 wait(1); 00149 00150 strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n"); 00151 SendCMD(); 00152 wait(1); 00153 00154 strcpy(snd, "conn:send(\"</html>\")\r\n"); 00155 SendCMD(); 00156 wait(1); 00157 00158 strcpy(snd, "end)\r\n"); 00159 SendCMD(); 00160 wait(1); 00161 00162 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); 00163 SendCMD(); 00164 wait(1); 00165 strcpy(snd, "end)\r\n"); 00166 SendCMD(); 00167 wait(1); 00168 timeout=17; 00169 getreply(); 00170 pc.printf(buf); 00171 pc.printf("\r\nDONE"); 00172 } 00173 void Wifi::SendCMD() { 00174 esp.printf("%s", snd); 00175 } 00176 void Wifi::getreply() { 00177 memset(buf, '\0', sizeof(buf)); 00178 t.start(); 00179 ended=0; 00180 count=0; 00181 while(!ended) { 00182 if(esp.readable()) { 00183 buf[count] = esp.getc(); 00184 count++; 00185 } 00186 if(t.read() > timeout) { 00187 ended = 1; 00188 t.stop(); 00189 t.reset(); 00190 } 00191 } 00192 } 00193 00194 void Wifi::setupPage() { 00195 reset=0; //hardware reset for 8266 00196 pc.baud(9600); // set what you want here depending on your terminal program speed 00197 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); 00198 wait(0.5); 00199 reset=1; 00200 timeout=2; 00201 getreply(); 00202 00203 esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time. 00204 00205 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** 00206 00207 ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** 00208 00209 pc.attach(&pc_recv, Serial::RxIrq); 00210 esp.attach(&dev_recv, Serial::RxIrq); 00211 00212 // continuosly get AP list and IP 00213 } 00214
Generated on Mon Jul 18 2022 22:47:22 by 1.7.2