Communicates with the WiFi - be very careful when using as it is tempremental.

Dependencies:   mbed

Revision:
9:8cf34e4f9ca0
Parent:
8:e09edf050cca
Child:
10:e5eee7bc1a7d
--- a/initiateWifi.cpp	Thu May 25 15:06:50 2017 +0000
+++ b/initiateWifi.cpp	Fri May 26 09:47:27 2017 +0000
@@ -1,5 +1,7 @@
 #include "mbed.h"
 #include "RawSerial.h"
+#include "string.h"
+#include "stdio.h"
 
 //setup the other wifi pins
 DigitalOut rstPin (PTE29);
@@ -16,39 +18,66 @@
 
 char input; //character to store inputs/outputs in communication
 
-char * getRequest = "GET /HTTP/1.1\r\nHost:172.217.23.19\r\n\r\n";
+//char * getRequest = "GET / HTTP/1.1\r\nHost: time.jsontest.com\r\nConnection: keep-alive\r\nCache-Control: max-age=0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nUser-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36\r\nAccept-Encoding: gzip,deflate,sdch\r\nAccept-Language: en-US,en;q=0.8";
+//char * getRequest = "GET / HTTP/1.1\r\nHost: time.jsontest.com\r\n\r\n";
+//char buf[20];
+//sprintf(buf, "AT+CIPSEND=1,%d", sizeof(getRequest));
 
-char* startCommands[10]= {
+char* startCommands[5]= {
     "AT+RST",
     "AT+CWMODE=1",
     //"AT+CWJAP=\"Hotspot\",\"password\"", skip signing in for debugging purposes
     "AT+CIPMUX=1",
-    "AT+CIPSTART=1,\"TCP\",\"172.217.23.19\",80", // change to 172.217.23.19\", \"80\"",
-    "AT+CIPSEND=1,45",
-    getRequest
+    "AT+CIPSTART=1,\"TCP\",\"time.jsontest.com\",80" //time.jsontest.com
 };
 
 
-int numberOfCommands;
+void sendCommand(char* command, int commandNumber)
+{
+    pc.printf("\r\ncommand %d",commandNumber);  //identify the command number
+    pc.printf("\r\n");
+
+    esp.printf("%s\r\n",command);    //send command
+
+}
+
+
+void getTime()
+{
+    int * timeDate [2]; //stores time and date
+
+    pc.printf("Syncing time with internet\r\n");
+    char *msg=(char *)malloc(50);
+    char *msg2= "GET / HTTP/1.1\r\nhost: time.jsontest.com\r\n\r\n";
+
+    sprintf(msg,"AT+CIPSEND=1,%d",strlen(msg2));
+
+    pc.printf("%s\r\n",msg);
+    esp.printf("%s\r\n",msg);
+    pc.printf("%s\r\n",msg2);
+    esp.printf("%s\r\n",msg2);
+    
+    
+
+}
+char * buffer [128];
+char * nextBuff;
+
 
 int k = 0; //counter for comm numb
 
 
+
+
 void start()
 {
     pc.printf("Initialising start sequence\r\n");
 
-    //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]);
-    numberOfCommands = 10;
-
-    for (int i = 0; i<numberOfCommands; i++) { //replace the 9 with sizeOf start commands
+    int numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]); // check if this works
+    for (int i = 0; i<numberOfCommands; i++) {
 
-        pc.printf("\r\ncommand %d",i);  //identify the command number
-        pc.printf("\r\n");
-
-        esp.printf("%s\r\n",startCommands[i]);    //send command
-        esp.putc(10); //send the command by pressing enter
-
+        sendCommand(startCommands[i],i);
+        wait (6); //wait an arbitrary time between each command
 
     }
 }
@@ -61,14 +90,16 @@
     pc.printf("Reset complete\r\n");
 
 }
+
 void esp_recv()
 {
     redLED = !redLED;
     while(esp.readable()) {
         pc.putc(esp.getc());
-        //wait_us(1);
     }
 }
+
+
 void pc_recv()
 {
     char c;
@@ -77,20 +108,24 @@
         c=pc.getc();
         esp.putc(c);
 
+        /*
+        /////////////////////////////////////////////////////////////////////////////////////this section is for debugging purposes only remove when done
+                if(c=='x') {
+                    pc.printf("\r\ncommand %d",k);  //identify the command number
+                    pc.printf("\r\n");
 
-        if(c=='x') {
-            pc.printf("\r\ncommand %d",k);  //identify the command number
-            pc.printf("\r\n");
+                    esp.printf("%s\r\n",startCommands[k]);    //send command
+                    pc.putc(10); //ie makes enter perform as expected
 
-            esp.printf("%s\r\n",startCommands[k]);    //send command
-            pc.putc(10); //ie makes enter perform as expected
+                    if (k <10) {
+                        k++;   //reset k if required
+                    } else {
+                        k=0;
+                    }
+                }
+        *////////////////////////////////////////////////////////////////////////////////////
 
-            if (k <10) {
-                k++;   //reset k if required
-            } else {
-                k=0;
-            }
-        } else if(c==13) {
+        if(c==13) {
             pc.putc(10); //ie makes enter perform as expected
             esp.putc(10);
         }
@@ -111,7 +146,17 @@
     pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
     esp.attach(&esp_recv, Serial::RxIrq);
 
-    //wait(10);
-    //start();
+    wait(10);
+    start();
 
-}
\ No newline at end of file
+    wait (3);
+    getTime();
+    /*
+    pc.printf("%d",dateTime[0]);
+    pc.printf("%d",dateTime[1]);
+    */
+}
+
+
+
+