Final version

Dependencies:   mbed SparkfunAnalogJoystick LSM9DS1_Library_cal PinDetect

Revision:
1:0ec1b59239f2
Parent:
0:5ad3d5f99603
Child:
2:da3e288789a4
--- a/main.cpp	Wed Dec 05 17:13:03 2018 +0000
+++ b/main.cpp	Wed Dec 05 20:53:45 2018 +0000
@@ -1,4 +1,4 @@
-// This mbed code creates a connection with the other mbeds server and reads from it
+// This mbed code creates a connection with the car mbeds server and reads from it
 // The car will host the server, the remote controll will send data to the server
 
 #include "mbed.h"
@@ -16,7 +16,7 @@
 
 // Push button used for testing
 PinDetect pb1(p21);
-void pushed(void) { }
+void pushed();
 
 /*
 char ssid[32] = "hsd";     // enter WiFi router ssid inside the quotes
@@ -42,7 +42,7 @@
 char replybuff[4096];
 char webdata[4096]; // This may need to be bigger depending on WEB browser used
 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
-void SendCMD(),getreply(),ReadWebData(),startserver();
+void SendCMD(),getreply();
 void getConnection();   // Sets up a connection with the car server
 char rx_line[1024];
 int port        =80;  // set server port
@@ -69,6 +69,9 @@
     update = 0;
     // Everything is interrupt driven, infinite loop
     while(1) {
+        
+        pushed();
+        
         //Send all car vals+
         if (update) {
             // Send led for testing 
@@ -81,42 +84,24 @@
     }
 }
 
-// Reads and processes GET and POST web data
-void ReadWebData()
+void pushed()
 {
-    wait_ms(200);
-    esp.attach(NULL,Serial::RxIrq);
-    DataRX=0;
-    memset(webdata, '\0', sizeof(webdata));
-    strcpy(webdata, rx_buffer);
-    memset(rx_buffer, '\0', sizeof(rx_buffer));
-    rx_in = 0;
-    rx_out = 0;
-    
-    // Modify this to check for our commands
-    // check web data for form information
-    if( strstr(webdata, "check=led1v") != NULL ) {
-        led1=!led1;
-    }
-    if( strstr(webdata, "check=led2v") != NULL ) {
-        led2=!led2;
-    }
-    if( strstr(webdata, "check=led3v") != NULL ) {
-        led3=!led3;
-    }
-    if( strstr(webdata, "check=led4v") != NULL ) {
-        led4=!led4;
-    }
-    if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
-        update=1;
-    }
-    if( strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
-        update=1;
-    }
+    strcpy(cmdbuff,"cl:send(\"flip_led1\")\r\n");
+    SendCMD();
+    //getreply();
+    wait(.1);  
 }
+
 // Sets up connection with car server
 void getConnection()
 {
+    // Reset the ESP8266
+    pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
+    strcpy(cmdbuff,"node.restart()\r\n");
+    SendCMD();
+    wait(2);
+    getreply();
+    
     // Disconncet from any potential connections
     strcpy(cmdbuff,"wifi.sta.disconnect()\r\n");
     SendCMD();
@@ -128,7 +113,7 @@
     getreply();
     wait(2); 
     // Connect to car server
-    strcpy(cmdbuff,"wifi.sta.config(\"Server\", \"fpv_car\"\)r\n");
+    strcpy(cmdbuff,"wifi.sta.config(\"Marlon's iPhone\", \"feelsbadman\")\r\n");
     SendCMD();
     getreply();
     wait(2);
@@ -136,6 +121,11 @@
     strcpy(cmdbuff,"wifi.sta.connect()\r\n");
     SendCMD();
     getreply();
+    wait(2);
+    
+    strcpy(cmdbuff,"print(\"Looking for a connection\")\r\n");
+    SendCMD();
+    getreply();
     wait(2); 
     
     strcpy(cmdbuff,"tmr.alarm(1,2000,1, function()\r\n");
@@ -143,7 +133,7 @@
     getreply();
     wait(2); 
     
-    strcpy(cmdbuff,"if(wifi.sta.getip()~=nil then\r\n");
+    strcpy(cmdbuff,"if(wifi.sta.getip()~=nil) then\r\n");
     SendCMD();
     getreply();
     wait(2); 
@@ -158,16 +148,36 @@
     getreply();
     wait(2); 
     // Print out controller ip address
-    strcpy(cmdbuff,"print(\"Client IP Address:\",wifi.sta.getip())\r\n");
+    strcpy(cmdbuff,"print(\"Client IP Address: \",wifi.sta.getip())\r\n");
+    SendCMD();
+    getreply();
+    wait(5); 
+    // Create connetion
+    strcpy(cmdbuff,"cl=net.createConnection(net.TCP, 0)\r\n");
+    SendCMD();
+    getreply();
+    wait(2);
+    // Connect the connection
+    strcpy(cmdbuff,"cl:connect(80, \"192.168.4.1\")\r\n");
     SendCMD();
     getreply();
     wait(2); 
     
-    // Print out controller ip address
-    strcpy(cmdbuff,"cl:connect(80, \"192.168.1.31\"\r\n");
-    SendCMD();
-    getreply();
-    wait(2); 
+//    strcpy(cmdbuff,"tmr.alarm(2, 5000, 1, function()\r\n");
+//    SendCMD();
+//    getreply();
+//    wait(2);
+//    
+//    strcpy(cmdbuff,"cl:send(\"Hello World!\")\r\n");
+//    SendCMD();
+//    getreply();
+//    wait(2);
+//    
+//    strcpy(cmdbuff,"end)\r\n");
+//    SendCMD();
+//    getreply();
+//    wait(2);
+    
     
     // Print out controller ip address
     strcpy(cmdbuff,"else\r\n");
@@ -183,28 +193,13 @@
     strcpy(cmdbuff,"end\r\n");
     SendCMD();
     getreply();
-    wait(2); 
+    wait(2);
     // Print out controller ip address
     strcpy(cmdbuff,"end)\r\n");
     SendCMD();
     getreply();
     wait(2); 
     
-//tmr.alarm(1, 2000, 1, function()
-//     if(wifi.sta.getip()~=nil) then
-//          tmr.stop(1)
-//          print("Connected!")
-//          print("Client IP Address:",wifi.sta.getip())
-//          cl=net.createConnection(net.TCP, 0)
-//          cl:connect(80,"192.168.4.1")
-//          tmr.alarm(2, 5000, 1, function() 
-//            cl:send("Hello World!") 
-//          end)
-//      else
-//         print("Connecting...")
-//      end
-//end)
-
 }