Kevin Rhyne / Whiffy

Dependencies:   4DGL-uLCD-SE mbed

Revision:
14:8d31d304eeb3
Parent:
13:ef68cbcb4e9a
Child:
15:469db0f84fd5
--- a/src/WifiControl.cpp	Mon Oct 19 22:25:18 2015 +0000
+++ b/src/WifiControl.cpp	Tue Oct 20 09:10:31 2015 +0000
@@ -2,60 +2,63 @@
 #include <stdio.h>
 #include "WifiControl.h"
 
-WifiControl::WifiControl(){
+WifiControl::WifiControl() : pc(USBTX, USBRX), esp(p28, p27) {
     
-    pc =  new Serial(USBTX, USBRX);
-    esp = new Serial(p28, p27); // tx, rx
-
-    pc->baud(115200); 
-    pc->printf("SERIAL WIFI OPENED\n");
-
-    strcpy(snd, "AT+CIOBAUD=115200\r\n");
-    SendCMD();
     
     DigitalOut reset(p26);
     reset=0; //hardware reset for 8266
-    pc->baud(115200);  // set what you want here depending on your terminal program speed
-    pc->printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
+    pc.baud(115200);  // set what you want here depending on your terminal program speed
+    pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
     wait(0.5);
     reset=1;
     timeout=2;
     getreply();
  
-    esp->baud(115200); 
+    esp.baud(115200);   // change this to the new ESP8266 baudrate if it is changed at any time.
+ 
+    //ESPsetbaudrate();   //******************  include this routine to set a different ESP8266 baudrate  ******************
+ 
+    //ESPconfig();        //******************  include Config to set the ESP8266 configuration  ***********************
 
     
     
 }
 
+void WifiControl::quickConfig() {
 
-void WifiControl::pollAP(char * results){
+    
+    
+    }
+
+string WifiControl::pollAP(){
         
-        pc->printf("\n---------- Polling APs privately ----------\r\n");
-        strcpy(snd, "AT+CWLAP\r\n");
-        SendCMD();
-        timeout=15;
-        getreply();
-        strcpy(results, buf);
+    pc.printf("\n---------- Privately polling APs ----------\r\n");
+    strcpy(snd, "AT+CWLAP\r\n");
+    SendCMD();
+    timeout=15;
+    getreply();
+    string results(buf);
+    pc.printf(" "); //Serial weirdness.
+    return results;
     
             
 }
 
 void WifiControl::getIPMAC() {
     
-    pc->printf("\n---------- Get IP and MAC ----------\r\n");
+    pc.printf("\n---------- Get IP and MAC ----------\r\n");
         strcpy(snd, "AT+CIFSR\r\n");
         SendCMD();
         timeout=10;
         getreply();
-        pc->printf(buf);
+        pc.printf(buf);
         wait(2);
 }
 
 void WifiControl::connect(char * ssid, char * pwd) { //32 byte chars
 
-    pc->printf("\n---------- Connecting to AP ----------\r\n");
-        pc->printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
+    pc.printf("\n---------- Connecting to AP ----------\r\n");
+        pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
         strcpy(snd, "AT+CWJAP=\"");
         strcat(snd, ssid);
         strcat(snd, "\",\"");
@@ -64,7 +67,7 @@
         SendCMD();
         timeout=10;
         getreply();
-        pc->printf(buf);
+        //pc.printf(buf);
         
 }
 
@@ -87,6 +90,7 @@
     
     strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
     SendCMD();
+    
     wait(5);
     strcpy(snd,"AT\r\n");
     SendCMD();
@@ -99,52 +103,52 @@
     timeout=1;
     getreply();
     wait(1);
-    pc->printf("\f---------- Starting ESP Config ----------\r\n\n");
+    pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
  
-    pc->printf("---------- Reset & get Firmware ----------\r\n");
+    pc.printf("---------- Reset & get Firmware ----------\r\n");
     strcpy(snd,"AT+RST\r\n");
     SendCMD();
     timeout=5;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(2);
  
-    pc->printf("\n---------- Get Version ----------\r\n");
+    pc.printf("\n---------- Get Version ----------\r\n");
     strcpy(snd,"AT+GMR\r\n");
     SendCMD();
     timeout=4;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(3);
  
     // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
-    pc->printf("\n---------- Setting Mode ----------\r\n");
+    pc.printf("\n---------- Setting Mode ----------\r\n");
     strcpy(snd, "AT+CWMODE=1\r\n");
     SendCMD();
     timeout=4;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(2);
  
     // set CIPMUX to 0=Single,1=Multi
-    pc->printf("\n---------- Setting Connection Mode ----------\r\n");
+    pc.printf("\n---------- Setting Connection Mode ----------\r\n");
     strcpy(snd, "AT+CIPMUX=1\r\n");
     SendCMD();
     timeout=4;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(2);
  
-    pc->printf("\n---------- Listing Access Points ----------\r\n");
+    pc.printf("\n---------- Listing Access Points ----------\r\n");
     strcpy(snd, "AT+CWLAP\r\n");
     SendCMD();
     timeout=15;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(2);
     
@@ -155,31 +159,31 @@
 
     wait(5);
  
-    pc->printf("\n---------- Get IP's ----------\r\n");
+    pc.printf("\n---------- Get IP's ----------\r\n");
     strcpy(snd, "AT+CIFSR\r\n");
     SendCMD();
     timeout=3;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
     wait(1);
  
-    pc->printf("\n---------- Get Connection Status ----------\r\n");
+    pc.printf("\n---------- Get Connection Status ----------\r\n");
     strcpy(snd, "AT+CIPSTATUS\r\n");
     SendCMD();
     timeout=5;
     getreply();
-    pc->printf(buf);
+    pc.printf(buf);
  
-    pc->printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
-    pc->printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
-    pc->printf("  It saves the SSID and password settings internally\r\n");
+    pc.printf("\n\n\n  If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
+    pc.printf("  Run this if you want to reconfig the ESP8266 at any time.\r\n");
+    pc.printf("  It saves the SSID and password settings internally\r\n");
     wait(10);
 }
  
 void WifiControl::SendCMD()
 {
-    esp->printf("%s", snd);
+    esp.printf("%s", snd);
 }
  
 void WifiControl::getreply()
@@ -189,8 +193,8 @@
     ended=0;
     count=0;
     while(!ended) {
-        if(esp->readable()) {
-            buf[count] = esp->getc();
+        if(esp.readable()) {
+            buf[count] = esp.getc();
             count++;
         }
         if(t.read() > timeout) {