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

Dependencies:   mbed

Revision:
5:79618eee4d54
Parent:
4:65f657998b7d
Child:
6:99841ed0c8a8
diff -r 65f657998b7d -r 79618eee4d54 initiateWifi.cpp
--- a/initiateWifi.cpp	Thu May 25 11:27:17 2017 +0000
+++ b/initiateWifi.cpp	Thu May 25 11:41:21 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,12 +10,9 @@
 
 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]= {
@@ -24,68 +21,50 @@
     "AT",
     "AT+RST",
     "AT+CWMODE=1",
-    "AT+CWJAP=\"Hotspot\",\"password\"",   //name and password of wifi
+    "AT+CWJAP=\"Hotspot\",\"password\"",
     "AT+CIFSR",
     "AT+CIPMUX=1",
-    "AT+CIPSTART=4","TCP","172.217.23.19","5005", //connect to soroush's server
-    "AT+CIPSEND=4,1"
-    "a" // send a single char
+    "AT+CIPSERVER=1,8080",
+    "AT+CIPSEND=0,"
 };
 
 
 int numberOfCommands;
-int commandsExecuted;
-int gotLine = 0; // boolean for whether we have a line
+
+int k = 0; //counter for comm numb
 
 
-void start()
-{
+void start() {
     pc.printf("Initialising start sequence\r\n");
-
-
+    
     //numberOfCommands = sizeof(startCommands)/sizeof(startCommands[0]);
     numberOfCommands = 10;
-
-    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;
-        }
+    
+    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
+        
+        
     }
 }
 
-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()
 {
-    char d;
-    nextBuff = buffer; //make nextbuff point to beginning fo buffe
-
+    redLED = !redLED;
     while(esp.readable()) {
-        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;
-        }
-
-
+        pc.putc(esp.getc());
+        //wait_us(1);
     }
 }
 void pc_recv()
@@ -93,40 +72,36 @@
     char c;
     greenLED = !greenLED;
     while(pc.readable()) {
-        c=pc.getc(); // get typed input
-
-
-        if(c==13) { //send enter
-
-            esp.putc(10); //send the command by pressing enter
+        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
             pc.putc(10); //ie makes enter perform as expected
-        }
-
-        else if(c=='y') { // send the startup sequence
-            start();
-        } else {
-            esp.putc(c);
+            
+            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);
-    for (char i=5; i>0; i--) {
-        pc.printf("%d\r\n", i);
-        wait(1);
-    }
+    
+    //wait(10);
     //start();
 
 }
\ No newline at end of file