A program using ESP8266 Huzzah to receive message from Amazon AWS service and control a servo to flip on/off the mechanical light switch.

Dependencies:   RPCInterface Servo mbed

Fork of dotbot by Graham Miles

Revision:
2:747b84e54088
Parent:
0:1fb00e911fe6
Child:
3:99303dc07632
--- a/main.cpp	Mon Dec 12 14:44:09 2016 +0000
+++ b/main.cpp	Tue Dec 13 16:00:55 2016 +0000
@@ -7,10 +7,10 @@
 
 Serial pc(USBTX, USBRX);
 Serial esp(p28, p27);
-char ssid[32] = "ThugMansion";
-char pwd[32] = "2paclives";
+char ssid[32] = "DelosLobbyWifi";
+char pwd[32] = "freezeallmotorfunctions";
 char port[32] = "1035"; // must be port forwarded
-char timeout[32] = "28800";
+char timeout[32] = "28800"; // 28800 is max
 volatile int tx_in=0;
 volatile int tx_out=0;
 volatile int rx_in=0;
@@ -55,16 +55,18 @@
     while (1) {
         getReply();
         memset(&rpc_in[0], 0, sizeof(rpc_in));
-        memset(&rpc_in[0], 0, sizeof(rpc_out));
-        int length = (int)rx_line[3] - 48;
+        memset(&rpc_out[0], 0, sizeof(rpc_out));
+        int length = (int)rx_line[3] - 48; // bytes 0 to 2 are trash; byte 3 is length of message
         if (length > 0 && length < 256) {
             for (int i = 0; i < length; i++) {
-                rpc_in[i] = rx_line[i+4];
+                rpc_in[i] = rx_line[i+4]; // bytes 4 to length+3 are the valid data
             }
             RPC::call(rpc_in, rpc_out);
             pc.printf("%s\n", rpc_out);
         }
-        strcpy(cmdbuff, "srv:close()\r\n");
+        // lambda function is event-triggered and non-persistent
+        // after it terminates, we need to close the existing connection and start another one
+        strcpy(cmdbuff, "srv:close()\r\n"); 
         sendCMD();
         wait(.5);
         getReply();