Example of UART comm between mbed and ESP8266
Fork of huzzah_helloWorld by
Revision 1:3c8b9d709797, committed 2016-12-10
- Comitter:
- nyengele
- Date:
- Sat Dec 10 23:55:04 2016 +0000
- Parent:
- 0:57cec3469a80
- Commit message:
- ESP8266 EXAMPLE
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 57cec3469a80 -r 3c8b9d709797 main.cpp --- a/main.cpp Thu Oct 22 16:18:21 2015 +0000 +++ b/main.cpp Sat Dec 10 23:55:04 2016 +0000 @@ -1,244 +1,107 @@ +/** + * This example shows how the ESP8266 chip can work hand-in-hand with the mbed + * to effectively set up a dynamic web server. + * This example, however, does not implement a fully functional web server. + * Instead, it just shows how to set up a serial link between the mbed and ESP module + * so that at every page request, the mbed provides the content to respond with. + * to extend on this example, one would need to be familiar with NodeMCU and LUA programming. + * + * Some links: + * http://frederickvandenbosch.be/?p=1629 + * https://www.lua.org/pil/contents.html + * https://nodemcu.readthedocs.io/en/master/en/modules/uart/ + * https://nodemcu.readthedocs.io/en/master/#nodemcu-documentation + */ + #include "mbed.h" - + Serial pc(USBTX, USBRX); -Serial esp(p28, p27); // tx, rx +Serial esp(p28, p27); DigitalOut reset(p26); -DigitalOut led1(LED1); -DigitalOut led4(LED4); Timer t; - -int count,ended,timeout; -char buf[2024]; -char snd[1024]; - -char ssid[32] = "SwagInABag"; // enter WiFi router ssid inside the quotes -char pwd [32] = "yoloswag"; // enter WiFi router password inside the quotes - -void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); - void dev_recv() -{ - led1 = !led1; - while(esp.readable()) { - pc.putc(esp.getc()); - } -} - -void pc_recv() -{ - led4 = !led4; - while(pc.readable()) { - esp.putc(pc.getc()); - } -} - - +char rx_buf[2048]; +volatile int index = 0; +volatile int id = 48; + +void get_reply(int timeout); +void esp_handler(void); + +char SSID[] = "net-name"; +char pswd[] = "net-password"; + int main() { - reset=0; //hardware reset for 8266 - pc.baud(9600); // set what you want here depending on your terminal program speed - pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); + pc.baud(9600); + pc.printf("[mbed]\t: Resetting ESP device ... \n\r"); + reset = 0; wait(0.5); - reset=1; - timeout=2; - getreply(); - - esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time. - - //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** - - ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** - - - - pc.attach(&pc_recv, Serial::RxIrq); - esp.attach(&dev_recv, Serial::RxIrq); - - // continuosly get AP list and IP - while(1) { - sleep(); - } - -} - -// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed -void ESPsetbaudrate() -{ - strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate - SendCMD(); -} - -// +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ -void ESPconfig() -{ + reset = 1; + esp.baud(9600); + get_reply(10); + + pc.printf("\n\r[mbed]\t: Connecting to Wi-Fi network ... \n\r"); + esp.printf("global_c = nil \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("wifi.setmode(wifi.STATION) \n\r"); get_reply(7); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("wifi.sta.config(\"%s\", \"d%s\") \n\r", SSID, pswd); get_reply(7); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("tmr.delay(1000000) \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("print(\"IP addr = \" .. wifi.sta.getip()) \n\r"); get_reply(15); + pc.printf("[esp]\t: %s \n\r", rx_buf); - wait(5); - pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); - strcpy(snd,".\r\n.\r\n"); - SendCMD(); - wait(1); - pc.printf("---------- Reset & get Firmware ----------\r\n"); - strcpy(snd,"node.restart()\r\n"); - SendCMD(); - timeout=5; - getreply(); - pc.printf(buf); - - wait(2); - - pc.printf("\n---------- Get Version ----------\r\n"); - strcpy(snd,"print(node.info())\r\n"); - SendCMD(); - timeout=4; - getreply(); - pc.printf(buf); - - wait(3); - - // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) - pc.printf("\n---------- Setting Mode ----------\r\n"); - strcpy(snd, "wifi.setmode(wifi.STATION)\r\n"); - SendCMD(); - timeout=4; - getreply(); - pc.printf(buf); - - wait(2); - + pc.printf("\n\r[mbed]\t: Setting server up .. \n\r"); + esp.printf("srv = net.createServer(net.TCP) \n\r"); get_reply(10); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("srv:listen(80, function(c) \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("if global_c ~= nil then global_c:close() end \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("global_c = c \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("c:on(\"receive\", function(sck, pl) \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("uart.write(0, \"REQUEST\") \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("end) end) \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + + pc.printf("\n\r[mbed]\t: setting up serial comm ... \n\r"); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("uart.setup(0, 9600, 8, 0, 1, 1) \n\r"); get_reply(10); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("uart.on(\"data\", 0, function(data) \n\r"); get_reply(10); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("if global_c ~= nil then \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("global_c:send(\"<html><h1>\" .. data .. \"</h1></html>\") \n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); + esp.printf("global_c:close()\n\r end end, 0)\n\r"); get_reply(3); pc.printf("[esp]\t: %s \n\r", rx_buf); - - pc.printf("\n---------- Listing Access Points ----------\r\n"); - strcpy(snd, "function listap(t)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "for k,v in pairs(t) do\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "print(k..\" : \"..v)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "end\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "end\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "wifi.sta.getap(listap)\r\n"); - SendCMD(); - wait(1); - timeout=15; - getreply(); - pc.printf(buf); - - wait(2); - - pc.printf("\n---------- Connecting to AP ----------\r\n"); - pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); - strcpy(snd, "wifi.sta.config(\""); - strcat(snd, ssid); - strcat(snd, "\",\""); - strcat(snd, pwd); - strcat(snd, "\")\r\n"); - SendCMD(); - timeout=10; - getreply(); - pc.printf(buf); - - wait(5); - - pc.printf("\n---------- Get IP's ----------\r\n"); - strcpy(snd, "print(wifi.sta.getip())\r\n"); - SendCMD(); - timeout=3; - getreply(); - pc.printf(buf); - - wait(1); - - pc.printf("\n---------- Get Connection Status ----------\r\n"); - strcpy(snd, "print(wifi.sta.status())\r\n"); - SendCMD(); - timeout=5; - getreply(); - pc.printf(buf); - - pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); - pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); - pc.printf(" It saves the SSID and password settings internally\r\n"); - wait(10); - - - pc.printf("\n---------- Setting up http server ----------\r\n"); - strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "srv:listen(80,function(conn)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "print(payload)\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:send(\"<html>\")\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:send(\"<h1> Hi James, NodeMcu.</h1>\")\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:send(\"</html>\")\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "end)\r\n"); - SendCMD(); - wait(1); - - strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); - SendCMD(); - wait(1); - strcpy(snd, "end)\r\n"); - SendCMD(); - wait(1); - timeout=17; - getreply(); - pc.printf(buf); - pc.printf("\r\nDONE"); + + esp.printf("start_seq = 1 \n\r"); + get_reply(10); + memset(rx_buf, '\0', sizeof(rx_buf)); + + esp.attach(&esp_handler, Serial::RxIrq); + pc.printf("\n\r[mbed]\t: READY \n\n\r"); + + while (1) { sleep(); } + } - -void SendCMD() -{ - esp.printf("%s", snd); -} - -void getreply() + +void get_reply(int timeout) { - memset(buf, '\0', sizeof(buf)); + memset(rx_buf, '\0', sizeof(rx_buf)); t.start(); - ended=0; - count=0; - while(!ended) { - if(esp.readable()) { - buf[count] = esp.getc(); - count++; + int ended = 0; + int count = 0; + while(!ended) + { + if (esp.readable()) + { + rx_buf[count++] = esp.getc(); } if(t.read() > timeout) { ended = 1; t.stop(); t.reset(); } - } + } } - - \ No newline at end of file + +void esp_handler(void) +{ + char c = esp.getc(); + pc.putc(c); + if (c == '\0') pc.printf("\n\r"); + esp.putc(id); + id++; + if (id == 58) id = 48; + +}