4180 Final Project
Dependencies: 4DGL-uLCD-SE mbed Servo
wifi.cpp
- Committer:
- ecarrick
- Date:
- 2016-04-18
- Revision:
- 6:50a82641d07b
- Child:
- 25:2e95a5731412
File content as of revision 6:50a82641d07b:
#include "wifi.h" Serial esp(p28, p27); // tx, rx Timer t; int count,ended,timeout; char ssid[32] = "fishes"; // enter WiFi router ssid inside the quotes char pwd [32] = "1Fishesonfire23"; // enter WiFi router password inside the quotes char buf[2024]; char snd[1024]; // Private Functions void SendCMD() { //pc.printf("%s", snd); esp.printf("%s", snd); } void flush(){ while(esp.readable()) { char tmp = esp.getc(); //data += tmp; //pc.putc(tmp); } } void getreply() { memset(buf, '\0', sizeof(buf)); t.start(); ended=0; count=0; while(!ended) { if(esp.readable()) { buf[count] = esp.getc(); count++; } if(t.read() > timeout) { ended = 1; t.stop(); t.reset(); } } } void ESPsetbaudrate() { strcpy(snd, "uart.setup(0, 9600, 8, 0, 1, 0)\r\n"); // change the numeric value to the required baudrate SendCMD(); timeout=2; getreply(); //pc.printf(buf); } // Public Functions void setup_wifi() { //pc.printf("---------- Reset & get Firmware ----------\r\n"); strcpy(snd,"node.restart()\r\n"); SendCMD(); timeout=2; getreply(); //pc.printf(buf); ESPsetbaudrate(); //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=3; getreply(); //pc.printf(buf); wait(5); /* pc.printf("\n---------- Get IP's ----------\r\n"); strcpy(snd, "print(wifi.sta.getip())\r\n"); SendCMD(); timeout=2; getreply(); pc.printf(buf); wait(1); pc.printf("\n---------- Get Connection Status ----------\r\n"); strcpy(snd, "print(wifi.sta.status())\r\n"); pc.printf("%s", snd); SendCMD(); timeout=2; getreply(); pc.printf(buf); */ } void setup_ntp(){ strcpy(snd, "timezone = -4\r\n"); SendCMD(); wait(2); strcpy(snd, "request=string.char(227,0,6,236,0,0,0,0,0,0,0,0,49,78,49,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\r\n"); SendCMD(); wait(4); strcpy(snd, "time = 0\r\n"); SendCMD(); wait(1); strcpy(snd, "sk=net.createConnection(net.UDP, 0)\r\n"); SendCMD(); wait(1); strcpy(snd, "sk:on(\"receive\", function(sck, payload)\r\n"); SendCMD(); wait(1); strcpy(snd, "local highw,loww,ntpstamp\r\n"); SendCMD(); wait(1); strcpy(snd, "highw = payload:byte(41) * 256 + payload:byte(42)\r\n"); SendCMD(); wait(1); strcpy(snd, "loww = payload:byte(43) * 256 + payload:byte(44)\r\n"); SendCMD(); wait(1); strcpy(snd, "ntpstamp=( highw * 65536 + loww ) + ( timezone* 3600)\r\n"); SendCMD(); wait(1); strcpy(snd, "time = ntpstamp - 1104494400 - 1104494400\r\n"); SendCMD(); wait(1); strcpy(snd, "print(time)\r\n"); SendCMD(); wait(1); strcpy(snd, "sck:close()\r\n"); SendCMD(); wait(1); strcpy(snd, "end )\r\n"); SendCMD(); wait(1); flush(); wait(5); } void update_ntp(){ flush(); strcpy(snd, "sk:connect(123,\"104.232.3.3\")\r\n"); SendCMD(); wait(3); strcpy(snd, "sk:send(request)\r\n"); SendCMD(); wait(4); flush(); strcpy(snd, "print(time)\r\n"); SendCMD(); wait(1); timeout=5; getreply(); //pc.printf(buf); int utime = atoi(buf); if(utime != NULL) set_time(utime); time_t seconds = time(NULL); //pc.printf("Time as a basic string = %s", ctime(&seconds)); }