Connect Wi-Fi

Dependencies:   mbed

Revision:
1:c0fbff97b740
Parent:
0:3c8c53e45689
Child:
2:6738db3eb2ed
--- a/main.cpp	Fri Dec 08 06:08:42 2017 +0000
+++ b/main.cpp	Sat Dec 09 20:43:11 2017 +0000
@@ -1,25 +1,44 @@
 #include "mbed.h"
- 
-Serial esp(D8, D2);
-Serial pc(D1, D0);
+#include <string>
+#include "ESP8266.h"
+
+ESP8266 esp(D8, D2, 9600);
+Serial pc(D1, D0, 115200);
+
+char snd[255], rcv[1000];
 
-void wifi() {
-    uint8_t data;
+int main() {   
+    pc.printf("Hello!! \n");
+        
+    pc.printf("Reset ESP\r\n");
+    esp.Reset();
+    esp.RcvReply(rcv, 400);
+    pc.printf("%s", rcv);
+    wait(2);
     
-    while(1) {
-        if(esp.readable()) {
-            data = esp.getc();   
-            pc.printf("Data = %d \n",data);
-        }
-        else {
-            pc.printf("Error!! \n");
-        }
-        break;
-    }
+    pc.printf("Sending AT\r\n");
+    strcpy(snd, "AT");
+    esp.SendCMD(snd);
+    esp.RcvReply(rcv, 400);
+    pc.printf("%s", rcv);
+    wait(2);
+    
+    pc.printf("Set mode to AP\r\n");
+    esp.SetMode(3);
+    esp.RcvReply(rcv, 1000);
+    pc.printf("%s", rcv);
     
-}
-
-int main() {
-    pc.printf("Hello!! \n");
-    wifi();
+    pc.printf("Receiving Wifi List\r\n");
+    esp.GetList(rcv);
+    pc.printf("%s", rcv);
+    
+    pc.printf("Connecting to AP\r\n");
+    esp.Join("Mind", "00000000"); // Replace MyAP and MyPasswd with your SSID and password
+    esp.RcvReply(rcv, 1000);
+    pc.printf("%s", rcv);
+    wait(8);
+    
+    pc.printf("Getting IP\r\n");
+    esp.GetIP(rcv);
+    pc.printf("%s", rcv);
 }
\ No newline at end of file