Example to connect to wifi automatically or start smartconfig if it is not connected

Dependencies:   mbed ESP8266

Files at this revision

API Documentation at this revision

Comitter:
Thrillex13
Date:
Sun May 12 16:54:48 2019 +0000
Commit message:
Example to connect to wifi automatically or start smartconfig if it is not connected

Changed in this revision

ESP8266.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f4f503a32dca ESP8266.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266.lib	Sun May 12 16:54:48 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Thrillex13/code/ESP8266/#d52701d66b62
diff -r 000000000000 -r f4f503a32dca main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun May 12 16:54:48 2019 +0000
@@ -0,0 +1,93 @@
+#include "mbed.h"
+#include <string>
+#include "ESP8266.h"
+ 
+// Objects
+Serial pc(USBTX, USBRX);
+ESP8266 wifi(PTC17, PTC16, 115200);
+
+DigitalOut led_rojo(LED_RED);
+DigitalOut led_verde(LED_GREEN);
+DigitalOut led_azul(LED_BLUE);
+
+#define ON  0;
+#define OFF  1;
+
+// Global variables
+char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies
+
+void wifiInit(void);
+bool isConnectedToWifi(void);
+void wifiConnect(void);
+ 
+int main() {
+    led_azul = OFF;
+    led_rojo = OFF;
+    led_verde = OFF;
+    
+    pc.baud(115200);
+    wifi.Quit();
+    wifiInit();
+    wifiConnect();
+    
+    
+
+    while(1);
+}
+
+void wifiInit(void){
+    pc.printf("Gateway Sistema de Control de Cotos\r\n");
+    pc.printf("Resetting WiFi\r\n");
+    wifi.Reset();
+    wait(2);
+    wifi.DisableEcho();
+    pc.printf("Set mode to Station\r\n");
+    wifi.SetMode(1);
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s", rcv);
+    wait(2);
+}
+
+void wifiConnect(void){
+    Timer t;
+    if(isConnectedToWifi()){
+        pc.printf("Gateway is already connected to wifi with the following IP address\r\n");
+        wifi.GetIP(rcv);
+        pc.printf("%s", rcv);
+        led_azul = OFF;
+        led_verde = ON;
+        wait(2);      
+    }else{
+        t.start();
+        pc.printf("Starting Smart Config\r\n");
+        led_azul = ON;
+        wifi.StartSmartConfig();
+        wifi.RcvReply(rcv, 5000);
+        pc.printf("%s", rcv);
+        wait(5);
+        while(!isConnectedToWifi()){
+            if(t.read_ms() > 30000) {
+                led_azul = OFF;
+                led_rojo = ON;
+                pc.printf("No se pudo conectar al Wifi");
+                break;
+            }          
+        }          
+    }   
+}
+
+bool isConnectedToWifi(void){
+    bool status;
+    wifi.GetConnStatus(rcv);
+    //pc.printf("%s", rcv);
+    if(strcmp(rcv,"STATUS:2\r")==0){
+        led_azul = OFF;
+        led_rojo = OFF;
+        led_verde = ON;
+        status=true;
+    }else{
+        status=false;
+    }
+    return status;
+    
+}
\ No newline at end of file
diff -r 000000000000 -r f4f503a32dca mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun May 12 16:54:48 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file