![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Gateway con agregar controles mediante firebase
Dependencies: mbed mbed-http ESP01 Pulse RFDecoder
main.cpp@0:f4f503a32dca, 2019-05-12 (annotated)
- Committer:
- Thrillex13
- Date:
- Sun May 12 16:54:48 2019 +0000
- Revision:
- 0:f4f503a32dca
- Child:
- 1:cf6a75de25ce
Example to connect to wifi automatically or start smartconfig if it is not connected
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thrillex13 | 0:f4f503a32dca | 1 | #include "mbed.h" |
Thrillex13 | 0:f4f503a32dca | 2 | #include <string> |
Thrillex13 | 0:f4f503a32dca | 3 | #include "ESP8266.h" |
Thrillex13 | 0:f4f503a32dca | 4 | |
Thrillex13 | 0:f4f503a32dca | 5 | // Objects |
Thrillex13 | 0:f4f503a32dca | 6 | Serial pc(USBTX, USBRX); |
Thrillex13 | 0:f4f503a32dca | 7 | ESP8266 wifi(PTC17, PTC16, 115200); |
Thrillex13 | 0:f4f503a32dca | 8 | |
Thrillex13 | 0:f4f503a32dca | 9 | DigitalOut led_rojo(LED_RED); |
Thrillex13 | 0:f4f503a32dca | 10 | DigitalOut led_verde(LED_GREEN); |
Thrillex13 | 0:f4f503a32dca | 11 | DigitalOut led_azul(LED_BLUE); |
Thrillex13 | 0:f4f503a32dca | 12 | |
Thrillex13 | 0:f4f503a32dca | 13 | #define ON 0; |
Thrillex13 | 0:f4f503a32dca | 14 | #define OFF 1; |
Thrillex13 | 0:f4f503a32dca | 15 | |
Thrillex13 | 0:f4f503a32dca | 16 | // Global variables |
Thrillex13 | 0:f4f503a32dca | 17 | char snd[255], rcv[1000]; // Strings for sending and receiving commands / data / replies |
Thrillex13 | 0:f4f503a32dca | 18 | |
Thrillex13 | 0:f4f503a32dca | 19 | void wifiInit(void); |
Thrillex13 | 0:f4f503a32dca | 20 | bool isConnectedToWifi(void); |
Thrillex13 | 0:f4f503a32dca | 21 | void wifiConnect(void); |
Thrillex13 | 0:f4f503a32dca | 22 | |
Thrillex13 | 0:f4f503a32dca | 23 | int main() { |
Thrillex13 | 0:f4f503a32dca | 24 | led_azul = OFF; |
Thrillex13 | 0:f4f503a32dca | 25 | led_rojo = OFF; |
Thrillex13 | 0:f4f503a32dca | 26 | led_verde = OFF; |
Thrillex13 | 0:f4f503a32dca | 27 | |
Thrillex13 | 0:f4f503a32dca | 28 | pc.baud(115200); |
Thrillex13 | 0:f4f503a32dca | 29 | wifi.Quit(); |
Thrillex13 | 0:f4f503a32dca | 30 | wifiInit(); |
Thrillex13 | 0:f4f503a32dca | 31 | wifiConnect(); |
Thrillex13 | 0:f4f503a32dca | 32 | |
Thrillex13 | 0:f4f503a32dca | 33 | |
Thrillex13 | 0:f4f503a32dca | 34 | |
Thrillex13 | 0:f4f503a32dca | 35 | while(1); |
Thrillex13 | 0:f4f503a32dca | 36 | } |
Thrillex13 | 0:f4f503a32dca | 37 | |
Thrillex13 | 0:f4f503a32dca | 38 | void wifiInit(void){ |
Thrillex13 | 0:f4f503a32dca | 39 | pc.printf("Gateway Sistema de Control de Cotos\r\n"); |
Thrillex13 | 0:f4f503a32dca | 40 | pc.printf("Resetting WiFi\r\n"); |
Thrillex13 | 0:f4f503a32dca | 41 | wifi.Reset(); |
Thrillex13 | 0:f4f503a32dca | 42 | wait(2); |
Thrillex13 | 0:f4f503a32dca | 43 | wifi.DisableEcho(); |
Thrillex13 | 0:f4f503a32dca | 44 | pc.printf("Set mode to Station\r\n"); |
Thrillex13 | 0:f4f503a32dca | 45 | wifi.SetMode(1); |
Thrillex13 | 0:f4f503a32dca | 46 | wifi.RcvReply(rcv, 1000); |
Thrillex13 | 0:f4f503a32dca | 47 | pc.printf("%s", rcv); |
Thrillex13 | 0:f4f503a32dca | 48 | wait(2); |
Thrillex13 | 0:f4f503a32dca | 49 | } |
Thrillex13 | 0:f4f503a32dca | 50 | |
Thrillex13 | 0:f4f503a32dca | 51 | void wifiConnect(void){ |
Thrillex13 | 0:f4f503a32dca | 52 | Timer t; |
Thrillex13 | 0:f4f503a32dca | 53 | if(isConnectedToWifi()){ |
Thrillex13 | 0:f4f503a32dca | 54 | pc.printf("Gateway is already connected to wifi with the following IP address\r\n"); |
Thrillex13 | 0:f4f503a32dca | 55 | wifi.GetIP(rcv); |
Thrillex13 | 0:f4f503a32dca | 56 | pc.printf("%s", rcv); |
Thrillex13 | 0:f4f503a32dca | 57 | led_azul = OFF; |
Thrillex13 | 0:f4f503a32dca | 58 | led_verde = ON; |
Thrillex13 | 0:f4f503a32dca | 59 | wait(2); |
Thrillex13 | 0:f4f503a32dca | 60 | }else{ |
Thrillex13 | 0:f4f503a32dca | 61 | t.start(); |
Thrillex13 | 0:f4f503a32dca | 62 | pc.printf("Starting Smart Config\r\n"); |
Thrillex13 | 0:f4f503a32dca | 63 | led_azul = ON; |
Thrillex13 | 0:f4f503a32dca | 64 | wifi.StartSmartConfig(); |
Thrillex13 | 0:f4f503a32dca | 65 | wifi.RcvReply(rcv, 5000); |
Thrillex13 | 0:f4f503a32dca | 66 | pc.printf("%s", rcv); |
Thrillex13 | 0:f4f503a32dca | 67 | wait(5); |
Thrillex13 | 0:f4f503a32dca | 68 | while(!isConnectedToWifi()){ |
Thrillex13 | 0:f4f503a32dca | 69 | if(t.read_ms() > 30000) { |
Thrillex13 | 0:f4f503a32dca | 70 | led_azul = OFF; |
Thrillex13 | 0:f4f503a32dca | 71 | led_rojo = ON; |
Thrillex13 | 0:f4f503a32dca | 72 | pc.printf("No se pudo conectar al Wifi"); |
Thrillex13 | 0:f4f503a32dca | 73 | break; |
Thrillex13 | 0:f4f503a32dca | 74 | } |
Thrillex13 | 0:f4f503a32dca | 75 | } |
Thrillex13 | 0:f4f503a32dca | 76 | } |
Thrillex13 | 0:f4f503a32dca | 77 | } |
Thrillex13 | 0:f4f503a32dca | 78 | |
Thrillex13 | 0:f4f503a32dca | 79 | bool isConnectedToWifi(void){ |
Thrillex13 | 0:f4f503a32dca | 80 | bool status; |
Thrillex13 | 0:f4f503a32dca | 81 | wifi.GetConnStatus(rcv); |
Thrillex13 | 0:f4f503a32dca | 82 | //pc.printf("%s", rcv); |
Thrillex13 | 0:f4f503a32dca | 83 | if(strcmp(rcv,"STATUS:2\r")==0){ |
Thrillex13 | 0:f4f503a32dca | 84 | led_azul = OFF; |
Thrillex13 | 0:f4f503a32dca | 85 | led_rojo = OFF; |
Thrillex13 | 0:f4f503a32dca | 86 | led_verde = ON; |
Thrillex13 | 0:f4f503a32dca | 87 | status=true; |
Thrillex13 | 0:f4f503a32dca | 88 | }else{ |
Thrillex13 | 0:f4f503a32dca | 89 | status=false; |
Thrillex13 | 0:f4f503a32dca | 90 | } |
Thrillex13 | 0:f4f503a32dca | 91 | return status; |
Thrillex13 | 0:f4f503a32dca | 92 | |
Thrillex13 | 0:f4f503a32dca | 93 | } |