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

Files at this revision

API Documentation at this revision

Comitter:
AlanPig
Date:
Thu Dec 14 00:30:12 2017 +0000
Parent:
2:747b84e54088
Commit message:
Newly created program.

Changed in this revision

Car.h Show diff for this revision Revisions of this file
HALLFX_ENCODER.lib Show diff for this revision Revisions of this file
Light.h Show annotated file Show diff for this revision Revisions of this file
Motor.lib Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 747b84e54088 -r 99303dc07632 Car.h
--- a/Car.h	Tue Dec 13 16:00:55 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-#include "mbed_rpc.h"
-
-void carTurnRight(Arguments *in, Reply *out);
-void carTurnLeft(Arguments *in, Reply *out);
-void carTurn(Arguments *in, Reply *out);
-void carMoveForwardDistance(Arguments *in, Reply *out);
-void carMoveBackwardDistance(Arguments *in, Reply *out);
-void carStop();
-void carResetEncoders();
\ No newline at end of file
diff -r 747b84e54088 -r 99303dc07632 HALLFX_ENCODER.lib
--- a/HALLFX_ENCODER.lib	Tue Dec 13 16:00:55 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/electromotivated/code/HALLFX_ENCODER/#f10558519825
diff -r 747b84e54088 -r 99303dc07632 Light.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Light.h	Thu Dec 14 00:30:12 2017 +0000
@@ -0,0 +1,4 @@
+#include "mbed_rpc.h"
+
+void lightOff(Arguments *in, Reply *out);
+void lightOn(Arguments *in, Reply *out);
\ No newline at end of file
diff -r 747b84e54088 -r 99303dc07632 Motor.lib
--- a/Motor.lib	Tue Dec 13 16:00:55 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/simon/code/Motor/#f265e441bcd9
diff -r 747b84e54088 -r 99303dc07632 Servo.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Thu Dec 14 00:30:12 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 747b84e54088 -r 99303dc07632 main.cpp
--- a/main.cpp	Tue Dec 13 16:00:55 2016 +0000
+++ b/main.cpp	Thu Dec 14 00:30:12 2017 +0000
@@ -1,15 +1,14 @@
 #include "mbed.h"
-#include "Car.h"
+#include "Light.h"
 #include "mywifi.h"
-#include "Motor.h"
-#include "HALLFX_ENCODER.h"
+#include "Servo.h"
 #include "mbed_rpc.h"
 
-Serial pc(USBTX, USBRX);
 Serial esp(p28, p27);
-char ssid[32] = "DelosLobbyWifi";
-char pwd[32] = "freezeallmotorfunctions";
-char port[32] = "1035"; // must be port forwarded
+Servo myservo(p22);
+char ssid[32] = "GTother";
+char pwd[32] = "GeorgeP@1927";
+char port[32] = "80"; // must be port forwarded
 char timeout[32] = "28800"; // 28800 is max
 volatile int tx_in=0;
 volatile int tx_out=0;
@@ -27,28 +26,17 @@
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
 
-float left_speed = .5;
-float right_speed = .5;
-float clicksTravelled;
-Motor left_motor(p26,p24,p25);    // pwm, fwd, rev
-Motor right_motor(p21, p23, p22); // pwm, fwd, rev
-HALLFX_ENCODER right_hall(p13);    // left hall effect encoder
-HALLFX_ENCODER left_hall(p14);   // left hall effect encoder
-float encoderFactor = 40;
-RPCFunction rpcTurnRight(&carTurnRight, "turnRight");
-RPCFunction rpcTurnLeft(&carTurnLeft, "turnLeft");
-RPCFunction rpcMoveForward(&carMoveForwardDistance, "moveForward");
-RPCFunction rpcMoveBackward(&carMoveBackwardDistance, "moveBackward");
-RPCFunction rpcTurn(&carTurn, "turn");
+float range = 0.0005;
+float position = 0.5;
+RPCFunction rpcLightOn(&lightOn, "on");
+RPCFunction rpcLightOff(&lightOff, "off");
 
 int main() {
-    clicksTravelled = 0.0;
-    pc.baud(9600);
     esp.baud(9600);
     led1=0,led2=0,led3=0,led4=0;
     esp.attach(&Rx_interrupt, Serial::RxIrq);
     esp.attach(&Tx_interrupt, Serial::TxIrq);
-    wait(5);
+    wait(2);
     connectToNetwork();
     char rpc_in[256];
     char rpc_out[256];
@@ -57,13 +45,15 @@
         memset(&rpc_in[0], 0, sizeof(rpc_in));
         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
+    //    pc.printf(" the length is %d, and rpc_in is %s\n",length,rpc_in);
         if (length > 0 && length < 256) {
             for (int i = 0; i < length; i++) {
                 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);
+         //   pc.printf("rpc _out is %s\n", rpc_out);
         }
+         
         // 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"); 
@@ -87,7 +77,6 @@
         wait(.5);
         getReply();
         strcpy(cmdbuff, "conn:send('");
-        strcat(cmdbuff, reportStatus());
         strcat(cmdbuff, "')\r\n");
         sendCMD();
         wait(.5);
@@ -102,179 +91,104 @@
         getReply();
         strcpy(cmdbuff, "end)\r\n");
         sendCMD();
+        led4 = 1;
         wait(.5);
+        led4 = 0;
         getReply();
     }
-    
 }
 
 
-
-
-
-
-
-/* CAR FUNCTIONS */
-
-void carStop() {
-    left_motor.speed(0);
-    right_motor.speed(0);
-}
-
-void carResetEncoders() {
-    left_hall.reset();
-    right_hall.reset();
-    wait(0.1);
-}
-
-void carMoveForwardDistance(Arguments *in, Reply *out) {
-    carResetEncoders();
-    long diff;
-    int dist = in->getArg<int>();
-    float enc_dist = dist * encoderFactor;
-    volatile float left_val= left_hall.read();
-    volatile float right_val = right_hall.read();
-    while (left_val < enc_dist || left_val < enc_dist) {
-        left_val = left_hall.read();
-        right_val = right_hall.read();
-        diff = left_val - right_val;
-        float k = 0.5;
-        if (diff < 0) { // left has moved less than right
-            left_motor.speed(left_speed);
-            right_motor.speed(k*right_speed);
-        } else if (diff > 0)  { // right has moved less than left
-            left_motor.speed(k*left_speed);
-            right_motor.speed(right_speed);
-        } else {
-            left_motor.speed(left_speed);
-            right_motor.speed(right_speed);
-        }
-    }
-    clicksTravelled += left_val;
-    left_motor.speed(0);
-    right_motor.speed(0);
+void lightOff(Arguments *in, Reply *out) {
+    led4=1;
+    position = 0.11;
+    myservo = position;
+    wait(1);
+    position = 0.5;
+    myservo = position;
+    led4=0;
 }
 
-void carMoveBackwardDistance(Arguments *in, Reply *out) {
-    int dist = in->getArg<int>();
-    if (dist > 100) {
-        dist = 100;
-    }
-    carResetEncoders();
-    long diff;
-    float enc_dist = dist * encoderFactor;
-    volatile float left_val= left_hall.read();
-    volatile float right_val = right_hall.read();
-    while (left_val < enc_dist || right_val < enc_dist) {
-        left_val = left_hall.read();
-        right_val = right_hall.read();
-        diff = left_val-right_val;
-        float k = 0.8;
-        if (diff < 0) { // left has moved less than right
-            left_motor.speed(-left_speed);
-            right_motor.speed(-k*right_speed);
-        } else if (diff > 0)  { // right has moved less than left
-            left_motor.speed(-k*left_speed);
-            right_motor.speed(-right_speed);
-        } else {
-            left_motor.speed(-left_speed);
-            right_motor.speed(-right_speed);
-        }
-    }
-    clicksTravelled += left_val;
-    carStop();
+void lightOn(Arguments *in, Reply *out) {
+    led1=1;
+    position = 0.8;
+    myservo = position;
+    wait(1);
+    position = 0.5;
+    myservo = position;
+    led1=0;
 }
 
-void carTurn(Arguments *in, Reply *out) {
-    carResetEncoders();
-    float enc_dist = in->getArg<float>();
-    volatile float left_val= left_hall.read();
-    while (left_val < enc_dist) {
-        left_val = left_hall.read();
-        left_motor.speed(left_speed);
-        right_motor.speed(-right_speed);
-    }
-    carStop();
-}
-
-void carTurnRight(Arguments *in, Reply *out) {
-    carResetEncoders();
-    float enc_dist = 170;
-    volatile float left_val= left_hall.read();
-    while (left_val < enc_dist) {
-        left_val = left_hall.read();
-        left_motor.speed(left_speed);
-        right_motor.speed(-right_speed);
-    }
-    carStop();
-    if (out != NULL) out->putData("GOT HERE");
-}
-
-void carTurnLeft(Arguments *in, Reply *out) {
-    carResetEncoders();
-    float enc_dist = 170;
-    volatile float left_val= left_hall.read();
-    while (left_val < enc_dist) {
-        left_val = left_hall.read();
-        left_motor.speed(-left_speed);
-        right_motor.speed(right_speed);
-    }
-    carStop();
-}
-
-
-
-
-
-
-
-
-
-
 /* WIFI FUNCTIONS */
 
 void connectToNetwork() {
-    pc.printf("# Resetting ESP\r\n");
+ //   pc.printf("# Resetting ESP\r\n");
     strcpy(cmdbuff,"node.restart()\r\n");
     sendCMD();
-    wait(5);
+    wait(1);
     getReply();
 
     led1=1,led2=0,led3=0;
-    pc.printf("# Setting Mode\r\n");
+ //   pc.printf("# Setting Mode\r\n");
     strcpy(cmdbuff, "wifi.setmode(wifi.STATION)\r\n");
     sendCMD();
     getReply();
+    wait(1);
+    
+    
+ //   pc.printf("\n---------- Listing Access Points ----------\r\n");
+    strcpy(cmdbuff, "function listap(t)\r\n");
+        sendCMD();
+        wait(1);
+        strcpy(cmdbuff, "for k,v in pairs(t) do\r\n");
+        sendCMD();
+        wait(1);
+        strcpy(cmdbuff, "print(k..\" : \"..v)\r\n");
+        sendCMD();
+        wait(1);
+        strcpy(cmdbuff, "end\r\n");
+        sendCMD();
+        wait(1);
+        strcpy(cmdbuff, "end\r\n");
+        sendCMD();
+        wait(1);
+        strcpy(cmdbuff, "wifi.sta.getap(listap)\r\n");
+   sendCMD();
+    wait(5);
+    getReply();
 
-    wait(2);
     led1=0,led2=1,led3=0;
-    pc.printf("# Connecting to AP\r\n");
-    pc.printf("# ssid = %s\t\tpwd = %s\r\n", ssid, pwd);
+  //  pc.printf("# Connecting to AP\r\n");
+  //  pc.printf("# ssid = %s\t\tpwd = %s\r\n", ssid, pwd);
     strcpy(cmdbuff, "wifi.sta.config(\"");
     strcat(cmdbuff, ssid);
     strcat(cmdbuff, "\",\"");
     strcat(cmdbuff, pwd);
     strcat(cmdbuff, "\")\r\n");
     sendCMD();
+    wait(12);
     getReply();
 
+
     wait(2);
     led1=0,led2=0,led3=1;
-    pc.printf("# Get IP Address\r\n");
+   // pc.printf("# Get IP Address\r\n");
     strcpy(cmdbuff, "print(wifi.sta.getip())\r\n");
     sendCMD();
+    wait(3);
     getReply();
+    
 
     wait(2);
     led1=1,led2=0,led3=0;
-    pc.printf("# Get Connection Status\r\n");
+   // pc.printf("# Get Connection Status\r\n");
     strcpy(cmdbuff, "print(wifi.sta.status())\r\n");
     sendCMD();
     getReply();
 
     wait(2);
     led1=0,led2=1,led3=0;
-    pc.printf("# Listen on Port\r\n");
+  //  pc.printf("# Listen on Port\r\n");
     strcpy(cmdbuff, "srv=net.createServer(net.TCP,");
     strcat(cmdbuff, timeout);
     strcat(cmdbuff, ")\r\n");
@@ -289,43 +203,37 @@
     sendCMD();
     getReply();
 
-    wait(2);
+    wait(.5);
     led1=1,led2=0,led3=0;
     strcpy(cmdbuff, "conn:on(\"receive\", function(conn, payload) \r\n");
     sendCMD();
     getReply();
 
-    wait(2);
+    wait(.5);
     led1=0,led2=1,led3=0;
-    strcpy(cmdbuff, "conn:send('");
-    strcat(cmdbuff, reportStatus());
-    strcat(cmdbuff, "')\r\n");
+    strcpy(cmdbuff, "conn:send(\"<h1> Hello, NodeMCU!!! </h1>\"");
+    strcat(cmdbuff, ")\r\n");
     sendCMD();
     getReply();
-
-    wait(2);
     led1=0,led2=0,led3=1;
     strcpy(cmdbuff, "print(payload)\r\n");
     sendCMD();
     getReply();
-
-    wait(2);
     led1=1,led2=0,led3=0;
     strcpy(cmdbuff, "end)\r\n");
     sendCMD();
     getReply();
-
-    wait(2);
     led1=0,led2=1,led3=0;
     strcpy(cmdbuff, "end)\r\n");
     sendCMD();
     getReply();
+    led1=0,led2=0,led3=0;
+  //  pc.printf("# Ready\r\n");
+    wait(1);
+}
 
-    wait(2);
-    //led1=1,led2=1,led3=1;
-    led1=0,led2=0,led3=0;
-    pc.printf("# Ready\r\n");
-}
+
+
 
 void sendCMD()
 {
@@ -388,12 +296,6 @@
     return;
 }
 
-char* reportStatus() {
-    char str[30];
-    float inchesTravelled = clicksTravelled / encoderFactor ;
-    sprintf(str, "%.1f", inchesTravelled);
-    return str;
-}
 
 // Interupt Routine to read in data from serial port
 void Rx_interrupt() {
@@ -403,7 +305,7 @@
     while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
         rx_buffer[rx_in] = esp.getc();
 // Uncomment to Echo to USB serial to watch data flow
-        pc.putc(rx_buffer[rx_in]);
+    //    pc.putc(rx_buffer[rx_in]);
         rx_in = (rx_in + 1) % buffer_size;
     }
     return;
@@ -412,13 +314,13 @@
 
 // Interupt Routine to write out data to serial port
 void Tx_interrupt() {
-    //led2=1;
+
 // Loop to fill more than one character in UART's transmit FIFO buffer
 // Stop if buffer empty
     while ((esp.writeable()) && (tx_in != tx_out)) {
         esp.putc(tx_buffer[tx_out]);
         tx_out = (tx_out + 1) % buffer_size;
     }
-    //led2=0;
+
     return;
 }
\ No newline at end of file