ECE 4180 Lab 2 Part 15

Dependencies:   mbed

Fork of huzzah_helloWorld by ECE 4180 Team Who

Committer:
abraha2d
Date:
Tue Oct 09 00:52:50 2018 +0000
Revision:
1:44f210b11755
Parent:
0:57cec3469a80
Save point

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhunter029 0:57cec3469a80 1 #include "mbed.h"
abraha2d 1:44f210b11755 2
jhunter029 0:57cec3469a80 3 Serial pc(USBTX, USBRX);
abraha2d 1:44f210b11755 4 Serial esp(p13, p14); // tx, rx
jhunter029 0:57cec3469a80 5 DigitalOut reset(p26);
jhunter029 0:57cec3469a80 6 DigitalOut led1(LED1);
jhunter029 0:57cec3469a80 7 DigitalOut led4(LED4);
jhunter029 0:57cec3469a80 8 Timer t;
abraha2d 1:44f210b11755 9
jhunter029 0:57cec3469a80 10 int count,ended,timeout;
jhunter029 0:57cec3469a80 11 char buf[2024];
jhunter029 0:57cec3469a80 12 char snd[1024];
abraha2d 1:44f210b11755 13
abraha2d 1:44f210b11755 14 char ssid[32] = "SM-N910V"; // enter WiFi router ssid inside the quotes
abraha2d 1:44f210b11755 15 char pwd [32] = "KevinAbraham"; // enter WiFi router password inside the quotes
abraha2d 1:44f210b11755 16
jhunter029 0:57cec3469a80 17 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
abraha2d 1:44f210b11755 18 void dev_recv()
jhunter029 0:57cec3469a80 19 {
jhunter029 0:57cec3469a80 20 led1 = !led1;
jhunter029 0:57cec3469a80 21 while(esp.readable()) {
jhunter029 0:57cec3469a80 22 pc.putc(esp.getc());
jhunter029 0:57cec3469a80 23 }
jhunter029 0:57cec3469a80 24 }
abraha2d 1:44f210b11755 25
jhunter029 0:57cec3469a80 26 void pc_recv()
jhunter029 0:57cec3469a80 27 {
jhunter029 0:57cec3469a80 28 led4 = !led4;
jhunter029 0:57cec3469a80 29 while(pc.readable()) {
jhunter029 0:57cec3469a80 30 esp.putc(pc.getc());
jhunter029 0:57cec3469a80 31 }
jhunter029 0:57cec3469a80 32 }
abraha2d 1:44f210b11755 33
abraha2d 1:44f210b11755 34
jhunter029 0:57cec3469a80 35 int main()
jhunter029 0:57cec3469a80 36 {
jhunter029 0:57cec3469a80 37 reset=0; //hardware reset for 8266
jhunter029 0:57cec3469a80 38 pc.baud(9600); // set what you want here depending on your terminal program speed
jhunter029 0:57cec3469a80 39 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
jhunter029 0:57cec3469a80 40 wait(0.5);
jhunter029 0:57cec3469a80 41 reset=1;
jhunter029 0:57cec3469a80 42 timeout=2;
jhunter029 0:57cec3469a80 43 getreply();
abraha2d 1:44f210b11755 44
jhunter029 0:57cec3469a80 45 esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time.
abraha2d 1:44f210b11755 46
jhunter029 0:57cec3469a80 47 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ******************
abraha2d 1:44f210b11755 48
jhunter029 0:57cec3469a80 49 ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
abraha2d 1:44f210b11755 50
abraha2d 1:44f210b11755 51
abraha2d 1:44f210b11755 52
jhunter029 0:57cec3469a80 53 pc.attach(&pc_recv, Serial::RxIrq);
jhunter029 0:57cec3469a80 54 esp.attach(&dev_recv, Serial::RxIrq);
abraha2d 1:44f210b11755 55
jhunter029 0:57cec3469a80 56 // continuosly get AP list and IP
jhunter029 0:57cec3469a80 57 while(1) {
jhunter029 0:57cec3469a80 58 sleep();
jhunter029 0:57cec3469a80 59 }
abraha2d 1:44f210b11755 60
jhunter029 0:57cec3469a80 61 }
abraha2d 1:44f210b11755 62
jhunter029 0:57cec3469a80 63 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
jhunter029 0:57cec3469a80 64 void ESPsetbaudrate()
jhunter029 0:57cec3469a80 65 {
jhunter029 0:57cec3469a80 66 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
jhunter029 0:57cec3469a80 67 SendCMD();
jhunter029 0:57cec3469a80 68 }
abraha2d 1:44f210b11755 69
jhunter029 0:57cec3469a80 70 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
jhunter029 0:57cec3469a80 71 void ESPconfig()
jhunter029 0:57cec3469a80 72 {
jhunter029 0:57cec3469a80 73
jhunter029 0:57cec3469a80 74 wait(5);
jhunter029 0:57cec3469a80 75 pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
abraha2d 1:44f210b11755 76 strcpy(snd,".\r\n.\r\n");
jhunter029 0:57cec3469a80 77 SendCMD();
abraha2d 1:44f210b11755 78 wait(1);
jhunter029 0:57cec3469a80 79 pc.printf("---------- Reset & get Firmware ----------\r\n");
jhunter029 0:57cec3469a80 80 strcpy(snd,"node.restart()\r\n");
jhunter029 0:57cec3469a80 81 SendCMD();
jhunter029 0:57cec3469a80 82 timeout=5;
jhunter029 0:57cec3469a80 83 getreply();
jhunter029 0:57cec3469a80 84 pc.printf(buf);
abraha2d 1:44f210b11755 85
jhunter029 0:57cec3469a80 86 wait(2);
abraha2d 1:44f210b11755 87
jhunter029 0:57cec3469a80 88 pc.printf("\n---------- Get Version ----------\r\n");
jhunter029 0:57cec3469a80 89 strcpy(snd,"print(node.info())\r\n");
jhunter029 0:57cec3469a80 90 SendCMD();
jhunter029 0:57cec3469a80 91 timeout=4;
jhunter029 0:57cec3469a80 92 getreply();
jhunter029 0:57cec3469a80 93 pc.printf(buf);
abraha2d 1:44f210b11755 94
jhunter029 0:57cec3469a80 95 wait(3);
abraha2d 1:44f210b11755 96
jhunter029 0:57cec3469a80 97 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
jhunter029 0:57cec3469a80 98 pc.printf("\n---------- Setting Mode ----------\r\n");
jhunter029 0:57cec3469a80 99 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
jhunter029 0:57cec3469a80 100 SendCMD();
jhunter029 0:57cec3469a80 101 timeout=4;
jhunter029 0:57cec3469a80 102 getreply();
jhunter029 0:57cec3469a80 103 pc.printf(buf);
abraha2d 1:44f210b11755 104
jhunter029 0:57cec3469a80 105 wait(2);
abraha2d 1:44f210b11755 106
abraha2d 1:44f210b11755 107
abraha2d 1:44f210b11755 108
jhunter029 0:57cec3469a80 109 pc.printf("\n---------- Listing Access Points ----------\r\n");
jhunter029 0:57cec3469a80 110 strcpy(snd, "function listap(t)\r\n");
abraha2d 1:44f210b11755 111 SendCMD();
abraha2d 1:44f210b11755 112 wait(1);
abraha2d 1:44f210b11755 113 strcpy(snd, "for k,v in pairs(t) do\r\n");
abraha2d 1:44f210b11755 114 SendCMD();
abraha2d 1:44f210b11755 115 wait(1);
abraha2d 1:44f210b11755 116 strcpy(snd, "print(k..\" : \"..v)\r\n");
abraha2d 1:44f210b11755 117 SendCMD();
abraha2d 1:44f210b11755 118 wait(1);
abraha2d 1:44f210b11755 119 strcpy(snd, "end\r\n");
jhunter029 0:57cec3469a80 120 SendCMD();
jhunter029 0:57cec3469a80 121 wait(1);
abraha2d 1:44f210b11755 122 strcpy(snd, "end\r\n");
abraha2d 1:44f210b11755 123 SendCMD();
abraha2d 1:44f210b11755 124 wait(1);
abraha2d 1:44f210b11755 125 strcpy(snd, "wifi.sta.getap(listap)\r\n");
abraha2d 1:44f210b11755 126 SendCMD();
abraha2d 1:44f210b11755 127 wait(1);
abraha2d 1:44f210b11755 128 timeout=15;
jhunter029 0:57cec3469a80 129 getreply();
jhunter029 0:57cec3469a80 130 pc.printf(buf);
abraha2d 1:44f210b11755 131
jhunter029 0:57cec3469a80 132 wait(2);
abraha2d 1:44f210b11755 133
jhunter029 0:57cec3469a80 134 pc.printf("\n---------- Connecting to AP ----------\r\n");
jhunter029 0:57cec3469a80 135 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
jhunter029 0:57cec3469a80 136 strcpy(snd, "wifi.sta.config(\"");
jhunter029 0:57cec3469a80 137 strcat(snd, ssid);
jhunter029 0:57cec3469a80 138 strcat(snd, "\",\"");
jhunter029 0:57cec3469a80 139 strcat(snd, pwd);
jhunter029 0:57cec3469a80 140 strcat(snd, "\")\r\n");
jhunter029 0:57cec3469a80 141 SendCMD();
jhunter029 0:57cec3469a80 142 timeout=10;
jhunter029 0:57cec3469a80 143 getreply();
jhunter029 0:57cec3469a80 144 pc.printf(buf);
abraha2d 1:44f210b11755 145
jhunter029 0:57cec3469a80 146 wait(5);
abraha2d 1:44f210b11755 147
jhunter029 0:57cec3469a80 148 pc.printf("\n---------- Get IP's ----------\r\n");
jhunter029 0:57cec3469a80 149 strcpy(snd, "print(wifi.sta.getip())\r\n");
jhunter029 0:57cec3469a80 150 SendCMD();
jhunter029 0:57cec3469a80 151 timeout=3;
jhunter029 0:57cec3469a80 152 getreply();
jhunter029 0:57cec3469a80 153 pc.printf(buf);
abraha2d 1:44f210b11755 154
jhunter029 0:57cec3469a80 155 wait(1);
abraha2d 1:44f210b11755 156
jhunter029 0:57cec3469a80 157 pc.printf("\n---------- Get Connection Status ----------\r\n");
jhunter029 0:57cec3469a80 158 strcpy(snd, "print(wifi.sta.status())\r\n");
jhunter029 0:57cec3469a80 159 SendCMD();
jhunter029 0:57cec3469a80 160 timeout=5;
jhunter029 0:57cec3469a80 161 getreply();
jhunter029 0:57cec3469a80 162 pc.printf(buf);
abraha2d 1:44f210b11755 163
jhunter029 0:57cec3469a80 164 pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
jhunter029 0:57cec3469a80 165 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
jhunter029 0:57cec3469a80 166 pc.printf(" It saves the SSID and password settings internally\r\n");
jhunter029 0:57cec3469a80 167 wait(10);
abraha2d 1:44f210b11755 168
abraha2d 1:44f210b11755 169
abraha2d 1:44f210b11755 170 pc.printf("\n---------- Setting up http server ----------\r\n");
jhunter029 0:57cec3469a80 171 strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
abraha2d 1:44f210b11755 172 SendCMD();
abraha2d 1:44f210b11755 173 wait(1);
abraha2d 1:44f210b11755 174 strcpy(snd, "srv:listen(80,function(conn)\r\n");
abraha2d 1:44f210b11755 175 SendCMD();
abraha2d 1:44f210b11755 176 wait(1);
abraha2d 1:44f210b11755 177 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
abraha2d 1:44f210b11755 178 SendCMD();
abraha2d 1:44f210b11755 179 wait(1);
abraha2d 1:44f210b11755 180 strcpy(snd, "print(payload)\r\n");
abraha2d 1:44f210b11755 181 SendCMD();
abraha2d 1:44f210b11755 182 wait(1);
abraha2d 1:44f210b11755 183
abraha2d 1:44f210b11755 184 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
abraha2d 1:44f210b11755 185 SendCMD();
abraha2d 1:44f210b11755 186 wait(1);
abraha2d 1:44f210b11755 187
abraha2d 1:44f210b11755 188 strcpy(snd, "conn:send(\"<html>\")\r\n");
jhunter029 0:57cec3469a80 189 SendCMD();
jhunter029 0:57cec3469a80 190 wait(1);
abraha2d 1:44f210b11755 191
abraha2d 1:44f210b11755 192 strcpy(snd, "conn:send(\"<h1> What up, bruh?</h1>\")\r\n");
jhunter029 0:57cec3469a80 193 SendCMD();
jhunter029 0:57cec3469a80 194 wait(1);
abraha2d 1:44f210b11755 195
abraha2d 1:44f210b11755 196 strcpy(snd, "conn:send(\"<h2> This be NodeMCU</h2>\")\r\n");
abraha2d 1:44f210b11755 197 SendCMD();
abraha2d 1:44f210b11755 198 wait(1);
abraha2d 1:44f210b11755 199
abraha2d 1:44f210b11755 200 strcpy(snd, "conn:send(\"</html>\")\r\n");
jhunter029 0:57cec3469a80 201 SendCMD();
jhunter029 0:57cec3469a80 202 wait(1);
abraha2d 1:44f210b11755 203
abraha2d 1:44f210b11755 204 strcpy(snd, "end)\r\n");
abraha2d 1:44f210b11755 205 SendCMD();
abraha2d 1:44f210b11755 206 wait(1);
abraha2d 1:44f210b11755 207
abraha2d 1:44f210b11755 208 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
jhunter029 0:57cec3469a80 209 SendCMD();
jhunter029 0:57cec3469a80 210 wait(1);
abraha2d 1:44f210b11755 211 strcpy(snd, "end)\r\n");
abraha2d 1:44f210b11755 212 SendCMD();
abraha2d 1:44f210b11755 213 wait(1);
abraha2d 1:44f210b11755 214 timeout=17;
jhunter029 0:57cec3469a80 215 getreply();
jhunter029 0:57cec3469a80 216 pc.printf(buf);
abraha2d 1:44f210b11755 217 pc.printf("\r\nDONE");
jhunter029 0:57cec3469a80 218 }
abraha2d 1:44f210b11755 219
jhunter029 0:57cec3469a80 220 void SendCMD()
jhunter029 0:57cec3469a80 221 {
jhunter029 0:57cec3469a80 222 esp.printf("%s", snd);
jhunter029 0:57cec3469a80 223 }
abraha2d 1:44f210b11755 224
jhunter029 0:57cec3469a80 225 void getreply()
jhunter029 0:57cec3469a80 226 {
jhunter029 0:57cec3469a80 227 memset(buf, '\0', sizeof(buf));
jhunter029 0:57cec3469a80 228 t.start();
jhunter029 0:57cec3469a80 229 ended=0;
jhunter029 0:57cec3469a80 230 count=0;
jhunter029 0:57cec3469a80 231 while(!ended) {
jhunter029 0:57cec3469a80 232 if(esp.readable()) {
jhunter029 0:57cec3469a80 233 buf[count] = esp.getc();
jhunter029 0:57cec3469a80 234 count++;
jhunter029 0:57cec3469a80 235 }
jhunter029 0:57cec3469a80 236 if(t.read() > timeout) {
jhunter029 0:57cec3469a80 237 ended = 1;
jhunter029 0:57cec3469a80 238 t.stop();
jhunter029 0:57cec3469a80 239 t.reset();
jhunter029 0:57cec3469a80 240 }
jhunter029 0:57cec3469a80 241 }
jhunter029 0:57cec3469a80 242 }
abraha2d 1:44f210b11755 243