Version1

Dependencies:   mbed SDFileSystem

Committer:
sacehtia
Date:
Sun Feb 06 14:27:16 2022 +0000
Revision:
1:56e56d910f78
Parent:
0:57cec3469a80
V1

Who changed what in which revision?

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