Hello World demo for the Adafruit ESP8266 Huzzah Wi-Fi module and mbed LPC1768

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

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