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

Dependencies:   mbed

Revision:
3:b593f43b7251
Parent:
2:0b9ca0830cd3
Child:
4:65f657998b7d
diff -r 0b9ca0830cd3 -r b593f43b7251 initiateWifi.cpp
--- a/initiateWifi.cpp	Wed May 24 16:17:04 2017 +0000
+++ b/initiateWifi.cpp	Thu May 25 10:37:33 2017 +0000
@@ -1,7 +1,7 @@
 #include "mbed.h"
 #include "RawSerial.h"
 
-    //setup the other wifi pins
+//setup the other wifi pins
 DigitalOut rstPin (PTE29);
 DigitalOut chpdPin (PTE23);
 
@@ -10,9 +10,12 @@
 
 Serial pc(USBTX, USBRX, 115200);
 
-DigitalOut redLED(LED1); // twp leds  
+DigitalOut redLED(LED1); // twp leds
 DigitalOut greenLED(LED2); // to allow visual check of bidirectional comms
 DigitalOut blueLED(LED3);  //to prove it's working
+char c;
+char * buffer = (char *)malloc(128); //setup a buffer 128 characters long
+char * nextBuff; // a pointer to the next character being added to the buffer
 
 char input; //character to store inputs/outputs in communication
 char* startCommands[10]= {
@@ -21,7 +24,7 @@
     "AT",
     "AT+RST",
     "AT+CWMODE=1",
-    "AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"",
+    "AT+CWJAP=\"Hotspot\",\"password\"",   //name and password of wifi
     "AT+CIFSR",
     "AT+CIPMUX=1",
     "AT+CIPSERVER=1,8080",
@@ -30,41 +33,58 @@
 
 
 int numberOfCommands;
-
-int k = 0; //counter for comm numb
+int commandsExecuted;
+int gotLine = 0; // boolean for whether we have a line
 
 
-void start() {
+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 
-        
-        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
-        
-        
+
+    commandsExecuted = 0;
+    esp.printf("%s\r\n",startCommands[commandsExecuted]);
+    while (commandsExecuted < numberOfCommands) { //loop until we have executed all the commands
+        if(gotLine) { // if esp signals that it's ready to receive a command, send the next command
+            pc.printf("\r\ncommand %d",commandsExecuted);  //identify the command number
+            pc.printf("\r\n");
+
+            esp.printf("%s\r\n",startCommands[commandsExecuted]);    //send command
+            commandsExecuted ++; //increment the counter
+            gotLine = 0;
+        }
     }
 }
 
-void reset() {   //resets the module by turning rst off for 2 seconds then on again
+void reset()     //resets the module by turning rst off for 2 seconds then on again
+{
     rstPin = 0;
     wait(2);
     rstPin = 1;
     pc.printf("Reset complete\r\n");
-    
+
 }
 void esp_recv()
 {
-    redLED = !redLED;
+    char d;
+    nextBuff = buffer; //make nextbuff point to beginning fo buffe
+
     while(esp.readable()) {
-        pc.putc(esp.getc());
-        //wait_us(1);
+        d = esp.getc(); // get character sent by the esp
+
+        pc.putc(d); // get character sent by the esp
+        * nextBuff = d; //store in nextBuff
+        nextBuff ++;
+        if (d== '\n'  ) { // if our first two characters are return and newline, then we have reached the end of the line
+            pc.putc('!');
+            nextBuff = buffer; // what does this do????????????/
+            gotLine = 1;
+        }
+
+
     }
 }
 void pc_recv()
@@ -72,57 +92,40 @@
     char c;
     greenLED = !greenLED;
     while(pc.readable()) {
-        c=pc.getc();
-        esp.putc(c);
-        
-        
-        if(c==13) {
-            pc.printf("\r\ncommand %d",k);  //identify the command number
-            pc.printf("\r\n");     
-            
-            esp.printf("%s\r\n",startCommands[k]);    //send command
+        c=pc.getc(); // get typed input
+
+
+        if(c==13) { //send enter
+
             esp.putc(10); //send the command by pressing enter
             pc.putc(10); //ie makes enter perform as expected
-            
-            if (k <10) { k++;} else {k=0;} //reset k if required
+        }
+
+        else if(c=='y') { // send the startup sequence
+            start();
+        } else {
+            esp.putc(c);
         }
     }
 }
-void sendCommand() {
-    char c;
-    while(pc.readable()) {
-        c=pc.getc();
-        //pc.putc(c); // echo back
-        int k = 0;
-        if(c==13) {
-            
-            pc.printf("\r\ncommand %d",k);  //identify the command number
-            pc.printf("\r\n");     
-            
-            esp.printf("%s\r\n",startCommands[k]);    //send command
-            esp.putc(10); //send the command by pressing enter
-            pc.putc(10); //ie makes enter perform as expected
-            
-            if (k <10) { k ++;} else {k=0;} //reset k if required
-        }
-    }   
-    
-}
 
-void initiateWifi() {
-    
+void initiateWifi()
+{
+
     //initialise the pins
     rstPin = 1;
     chpdPin = 1;
-    
+
     reset();    //perform an initial reset
-    
+
     blueLED = 1; //turn on test light
 
     pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
     esp.attach(&esp_recv, Serial::RxIrq);
-    
-    //wait(10);
-    //start();
+    for (char i=10; i>0; i--) {
+        pc.printf("%d\r\n", i);
+        wait(1);
+    }
+    start();
 
 }
\ No newline at end of file