Gateway con agregar controles mediante firebase migracion a OS5
Dependencies: mbed-http ESP01 Pulse RFDecoder
main.cpp@3:5dceee1c49fc, 2019-06-11 (annotated)
- Committer:
- Thrillex13
- Date:
- Tue Jun 11 00:24:59 2019 +0000
- Revision:
- 3:5dceee1c49fc
- Parent:
- 2:bc3973471585
- Child:
- 4:8fed3705384e
Se implemento y probo Registro de Controles y Heartbeat
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thrillex13 | 3:5dceee1c49fc | 1 | #include "main.h" |
Thrillex13 | 3:5dceee1c49fc | 2 | |
Thrillex13 | 0:f4f503a32dca | 3 | // Objects |
Thrillex13 | 0:f4f503a32dca | 4 | Serial pc(USBTX, USBRX); |
Thrillex13 | 1:cf6a75de25ce | 5 | ESP01 wifi(PTC17, PTC16, 115200); //Con este objeto implementamos los metodos que requieren comandos AT del modulo de WIFI |
Thrillex13 | 3:5dceee1c49fc | 6 | //ESP8266Interface net(PTC17, PTC16); //Con este objeto implementamos todas las funciones de red para la creacion de Sockets |
Thrillex13 | 3:5dceee1c49fc | 7 | RFDecoder decoder = RFDecoder(D2,D3); //tx rx |
Thrillex13 | 0:f4f503a32dca | 8 | |
Thrillex13 | 0:f4f503a32dca | 9 | // Global variables |
Thrillex13 | 3:5dceee1c49fc | 10 | char send[250]; // Strings for sending and receiving commands / data send / data receive / command received / status received |
Thrillex13 | 3:5dceee1c49fc | 11 | char recv[250]; |
Thrillex13 | 3:5dceee1c49fc | 12 | char command[TCPCOMMSBYTESMAX]; |
Thrillex13 | 3:5dceee1c49fc | 13 | char status[TCPSTATSBYTESMAX]; |
Thrillex13 | 3:5dceee1c49fc | 14 | char controlcode[8]; |
Thrillex13 | 3:5dceee1c49fc | 15 | |
Thrillex13 | 3:5dceee1c49fc | 16 | int main() |
Thrillex13 | 3:5dceee1c49fc | 17 | { |
Thrillex13 | 3:5dceee1c49fc | 18 | //Inicializacion de los Perifericos |
Thrillex13 | 3:5dceee1c49fc | 19 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 20 | led_rojo = OFF; |
Thrillex13 | 3:5dceee1c49fc | 21 | led_verde = OFF; |
Thrillex13 | 3:5dceee1c49fc | 22 | pc.baud(115200); |
Thrillex13 | 0:f4f503a32dca | 23 | |
Thrillex13 | 3:5dceee1c49fc | 24 | |
Thrillex13 | 3:5dceee1c49fc | 25 | //Inicializacion y Conexion al WiFi |
Thrillex13 | 3:5dceee1c49fc | 26 | wifiInit(); |
Thrillex13 | 3:5dceee1c49fc | 27 | //wifi.Quit(); |
Thrillex13 | 3:5dceee1c49fc | 28 | wifiConnect(); |
Thrillex13 | 1:cf6a75de25ce | 29 | |
Thrillex13 | 3:5dceee1c49fc | 30 | //ESP8266Interface *net = new ESP8266Interface(); //Con este objeto implementamos todas las funciones de red para la creacion de Sockets para HTTPS |
Thrillex13 | 3:5dceee1c49fc | 31 | //http_demo(net); |
Thrillex13 | 3:5dceee1c49fc | 32 | //delete net; //Necesario destruir el objeto cuando ya no se use para desocupar la interface de wifi. |
Thrillex13 | 3:5dceee1c49fc | 33 | |
Thrillex13 | 3:5dceee1c49fc | 34 | |
Thrillex13 | 3:5dceee1c49fc | 35 | //Iniciamos el servidor |
Thrillex13 | 3:5dceee1c49fc | 36 | startServer(SERVER_PORT); |
Thrillex13 | 3:5dceee1c49fc | 37 | |
Thrillex13 | 1:cf6a75de25ce | 38 | |
Thrillex13 | 1:cf6a75de25ce | 39 | |
Thrillex13 | 3:5dceee1c49fc | 40 | while(1) { |
Thrillex13 | 3:5dceee1c49fc | 41 | //Revisamos si hay un dato por TCP disponible |
Thrillex13 | 3:5dceee1c49fc | 42 | if(wifi.TCPDataAvailable(recv)) { |
Thrillex13 | 3:5dceee1c49fc | 43 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 44 | int socket = getTCPContent(recv,command,status); |
Thrillex13 | 3:5dceee1c49fc | 45 | |
Thrillex13 | 3:5dceee1c49fc | 46 | //Comando para registrar control |
Thrillex13 | 3:5dceee1c49fc | 47 | if(command[0] == NEW_CONTROL_REGISTER) { |
Thrillex13 | 3:5dceee1c49fc | 48 | Timer t; |
Thrillex13 | 3:5dceee1c49fc | 49 | t.start(); |
Thrillex13 | 3:5dceee1c49fc | 50 | LEDAMARILLO_ON; |
Thrillex13 | 3:5dceee1c49fc | 51 | while(t.read_ms() < 30000) { |
Thrillex13 | 3:5dceee1c49fc | 52 | if(decoder.available()) { |
Thrillex13 | 3:5dceee1c49fc | 53 | unsigned long numcode = decoder.getCode(); |
Thrillex13 | 3:5dceee1c49fc | 54 | pc.printf("Codigo Recibido %x \n\r", numcode); |
Thrillex13 | 3:5dceee1c49fc | 55 | |
Thrillex13 | 3:5dceee1c49fc | 56 | convertToCharArray(&controlcode[0],numcode); |
Thrillex13 | 3:5dceee1c49fc | 57 | |
Thrillex13 | 3:5dceee1c49fc | 58 | send[0] = CONTROL_REGISTER_INFO; |
Thrillex13 | 3:5dceee1c49fc | 59 | send[1] = controlcode[4]; |
Thrillex13 | 3:5dceee1c49fc | 60 | send[2] = controlcode[5]; |
Thrillex13 | 3:5dceee1c49fc | 61 | send[3] = controlcode[6]; |
Thrillex13 | 3:5dceee1c49fc | 62 | wifi.SendTCPData(socket,TCPCOMMSBYTESMAX,send); |
Thrillex13 | 3:5dceee1c49fc | 63 | wifi.SendTCPData(socket,TCPCOMMSBYTESMAX,send); |
Thrillex13 | 3:5dceee1c49fc | 64 | break; |
Thrillex13 | 3:5dceee1c49fc | 65 | } |
Thrillex13 | 3:5dceee1c49fc | 66 | } |
Thrillex13 | 3:5dceee1c49fc | 67 | |
Thrillex13 | 3:5dceee1c49fc | 68 | LEDVERDE_ON; |
Thrillex13 | 3:5dceee1c49fc | 69 | command[0] = 0x00; |
Thrillex13 | 3:5dceee1c49fc | 70 | } |
Thrillex13 | 3:5dceee1c49fc | 71 | |
Thrillex13 | 3:5dceee1c49fc | 72 | //Evaluar otros comandos |
Thrillex13 | 3:5dceee1c49fc | 73 | if(command[0] == HEARBEAT_REQUEST) { |
Thrillex13 | 3:5dceee1c49fc | 74 | send[0] = HEARBEAT_RESPONSE; |
Thrillex13 | 3:5dceee1c49fc | 75 | wifi.SendTCPData(socket,TCPCOMMSBYTESMAX,send); |
Thrillex13 | 3:5dceee1c49fc | 76 | wifi.SendTCPData(socket,TCPCOMMSBYTESMAX,send); |
Thrillex13 | 3:5dceee1c49fc | 77 | command[0] = 0x00; |
Thrillex13 | 3:5dceee1c49fc | 78 | } |
Thrillex13 | 3:5dceee1c49fc | 79 | |
Thrillex13 | 3:5dceee1c49fc | 80 | } |
Thrillex13 | 3:5dceee1c49fc | 81 | |
Thrillex13 | 3:5dceee1c49fc | 82 | } |
Thrillex13 | 3:5dceee1c49fc | 83 | } |
Thrillex13 | 3:5dceee1c49fc | 84 | |
Thrillex13 | 3:5dceee1c49fc | 85 | void wifiInit(void) |
Thrillex13 | 3:5dceee1c49fc | 86 | { |
Thrillex13 | 3:5dceee1c49fc | 87 | pc.printf("Gateway Sistema de Control de Cotos\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 88 | pc.printf("Resetting WiFi\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 89 | wifi.Reset(); |
Thrillex13 | 3:5dceee1c49fc | 90 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 91 | wifi.DisableEcho(); |
Thrillex13 | 3:5dceee1c49fc | 92 | pc.printf("Set mode to Station\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 93 | wifi.SetMode(STATION); |
Thrillex13 | 3:5dceee1c49fc | 94 | wifi.RcvReply(recv, 1000); |
Thrillex13 | 3:5dceee1c49fc | 95 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 96 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 97 | pc.printf("Configure for multiple sockets\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 98 | wifi.SetMultiple(); |
Thrillex13 | 3:5dceee1c49fc | 99 | wifi.RcvReply(recv, 1000); |
Thrillex13 | 3:5dceee1c49fc | 100 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 101 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 102 | pc.printf("Enable DHCP\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 103 | wifi.EnableDHCP(); |
Thrillex13 | 3:5dceee1c49fc | 104 | wifi.RcvReply(recv, 1000); |
Thrillex13 | 3:5dceee1c49fc | 105 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 106 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 107 | } |
Thrillex13 | 3:5dceee1c49fc | 108 | |
Thrillex13 | 3:5dceee1c49fc | 109 | void wifiConnect(void) |
Thrillex13 | 3:5dceee1c49fc | 110 | { |
Thrillex13 | 3:5dceee1c49fc | 111 | Timer t; |
Thrillex13 | 3:5dceee1c49fc | 112 | if(isConnectedToWifi()) { |
Thrillex13 | 3:5dceee1c49fc | 113 | pc.printf("Gateway is already connected to wifi with the following IP address\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 114 | wifi.GetIP(recv); |
Thrillex13 | 3:5dceee1c49fc | 115 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 116 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 117 | led_verde = ON; |
Thrillex13 | 3:5dceee1c49fc | 118 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 119 | } else { |
Thrillex13 | 3:5dceee1c49fc | 120 | pc.printf("Starting Smart Config\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 121 | led_azul = ON; |
Thrillex13 | 3:5dceee1c49fc | 122 | wifi.StartSmartConfig(); |
Thrillex13 | 3:5dceee1c49fc | 123 | wifi.RcvReply(recv, 15000); |
Thrillex13 | 3:5dceee1c49fc | 124 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 125 | wait(5); |
Thrillex13 | 3:5dceee1c49fc | 126 | t.start(); |
Thrillex13 | 3:5dceee1c49fc | 127 | while(!isConnectedToWifi()) { |
Thrillex13 | 3:5dceee1c49fc | 128 | //Timeout para hacer la conexion |
Thrillex13 | 3:5dceee1c49fc | 129 | /* |
Thrillex13 | 3:5dceee1c49fc | 130 | if(t.read_ms() > 30000) { |
Thrillex13 | 3:5dceee1c49fc | 131 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 132 | led_rojo = ON; |
Thrillex13 | 3:5dceee1c49fc | 133 | pc.printf("No se pudo conectar al Wifi\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 134 | while(1); |
Thrillex13 | 3:5dceee1c49fc | 135 | break; |
Thrillex13 | 3:5dceee1c49fc | 136 | } |
Thrillex13 | 3:5dceee1c49fc | 137 | */ |
Thrillex13 | 3:5dceee1c49fc | 138 | } |
Thrillex13 | 3:5dceee1c49fc | 139 | } |
Thrillex13 | 3:5dceee1c49fc | 140 | } |
Thrillex13 | 1:cf6a75de25ce | 141 | |
Thrillex13 | 3:5dceee1c49fc | 142 | bool isConnectedToWifi(void) |
Thrillex13 | 3:5dceee1c49fc | 143 | { |
Thrillex13 | 3:5dceee1c49fc | 144 | bool status; |
Thrillex13 | 3:5dceee1c49fc | 145 | wifi.GetConnStatusCode(recv); |
Thrillex13 | 3:5dceee1c49fc | 146 | //pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 147 | if(strcmp(recv,"STATUS:2\r")==0) { |
Thrillex13 | 3:5dceee1c49fc | 148 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 149 | led_rojo = OFF; |
Thrillex13 | 3:5dceee1c49fc | 150 | led_verde = ON; |
Thrillex13 | 3:5dceee1c49fc | 151 | status=true; |
Thrillex13 | 3:5dceee1c49fc | 152 | } else { |
Thrillex13 | 3:5dceee1c49fc | 153 | status=false; |
Thrillex13 | 3:5dceee1c49fc | 154 | } |
Thrillex13 | 3:5dceee1c49fc | 155 | return status; |
Thrillex13 | 3:5dceee1c49fc | 156 | |
Thrillex13 | 3:5dceee1c49fc | 157 | } |
Thrillex13 | 3:5dceee1c49fc | 158 | |
Thrillex13 | 3:5dceee1c49fc | 159 | void startServer(int port) |
Thrillex13 | 3:5dceee1c49fc | 160 | { |
Thrillex13 | 3:5dceee1c49fc | 161 | pc.printf("Iniciando servidor en el puerto %d\r\n",port); |
Thrillex13 | 3:5dceee1c49fc | 162 | wifi.StartServerMode(port); |
Thrillex13 | 3:5dceee1c49fc | 163 | wifi.RcvReply(recv, 1000); |
Thrillex13 | 3:5dceee1c49fc | 164 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 165 | //wait(2); |
Thrillex13 | 3:5dceee1c49fc | 166 | } |
Thrillex13 | 3:5dceee1c49fc | 167 | |
Thrillex13 | 3:5dceee1c49fc | 168 | int getTCPContent(char* espdata, char* command, char* status) |
Thrillex13 | 3:5dceee1c49fc | 169 | { |
Thrillex13 | 3:5dceee1c49fc | 170 | char i=0; |
Thrillex13 | 3:5dceee1c49fc | 171 | char offset=0; |
Thrillex13 | 3:5dceee1c49fc | 172 | char socket=0; |
Thrillex13 | 3:5dceee1c49fc | 173 | |
Thrillex13 | 3:5dceee1c49fc | 174 | if((espdata[2]=='+') && (espdata[3]=='I') && (espdata[4]=='P') && (espdata[5]=='D')) { |
Thrillex13 | 3:5dceee1c49fc | 175 | //Obtenemos en socket de conexion |
Thrillex13 | 3:5dceee1c49fc | 176 | socket = espdata[7]-'0'; |
Thrillex13 | 3:5dceee1c49fc | 177 | //Buscamos el caracter : para de ahi iniciar el mensaje |
Thrillex13 | 3:5dceee1c49fc | 178 | for(i=0; i<250; i++) { |
Thrillex13 | 3:5dceee1c49fc | 179 | if(espdata[i]==':') { |
Thrillex13 | 3:5dceee1c49fc | 180 | offset = i+1; |
Thrillex13 | 3:5dceee1c49fc | 181 | break; |
Thrillex13 | 3:5dceee1c49fc | 182 | } |
Thrillex13 | 3:5dceee1c49fc | 183 | } |
Thrillex13 | 3:5dceee1c49fc | 184 | |
Thrillex13 | 3:5dceee1c49fc | 185 | for(i=0; (i<TCPCOMMSBYTESMAX); i++) { |
Thrillex13 | 3:5dceee1c49fc | 186 | command[i] = espdata[i+offset]; |
Thrillex13 | 3:5dceee1c49fc | 187 | } |
Thrillex13 | 3:5dceee1c49fc | 188 | |
Thrillex13 | 3:5dceee1c49fc | 189 | command[TCPCOMMSBYTESMAX] = '\0'; |
Thrillex13 | 3:5dceee1c49fc | 190 | |
Thrillex13 | 3:5dceee1c49fc | 191 | status[0] = COMMANDREC; |
Thrillex13 | 3:5dceee1c49fc | 192 | } |
Thrillex13 | 3:5dceee1c49fc | 193 | |
Thrillex13 | 3:5dceee1c49fc | 194 | return socket; |
Thrillex13 | 3:5dceee1c49fc | 195 | } |
Thrillex13 | 3:5dceee1c49fc | 196 | |
Thrillex13 | 3:5dceee1c49fc | 197 | void convertToCharArray(char *arr, unsigned long number) |
Thrillex13 | 3:5dceee1c49fc | 198 | { |
Thrillex13 | 3:5dceee1c49fc | 199 | int i = 0; |
Thrillex13 | 3:5dceee1c49fc | 200 | |
Thrillex13 | 3:5dceee1c49fc | 201 | for (i = 0; i < 8; ++i) { |
Thrillex13 | 3:5dceee1c49fc | 202 | arr[i] = (char)((((unsigned long) number) >> (56 - (8*i))) & 0xFFu); |
Thrillex13 | 3:5dceee1c49fc | 203 | } |
Thrillex13 | 3:5dceee1c49fc | 204 | } |
Thrillex13 | 3:5dceee1c49fc | 205 | |
Thrillex13 | 3:5dceee1c49fc | 206 | |
Thrillex13 | 3:5dceee1c49fc | 207 | void http_demo(NetworkInterface *network) |
Thrillex13 | 3:5dceee1c49fc | 208 | { |
Thrillex13 | 3:5dceee1c49fc | 209 | TLSSocket* socket = new TLSSocket(); |
Thrillex13 | 3:5dceee1c49fc | 210 | |
Thrillex13 | 3:5dceee1c49fc | 211 | nsapi_error_t r; |
Thrillex13 | 3:5dceee1c49fc | 212 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
Thrillex13 | 3:5dceee1c49fc | 213 | r = socket->open(network); |
Thrillex13 | 3:5dceee1c49fc | 214 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
Thrillex13 | 3:5dceee1c49fc | 215 | r = socket->connect("https://cotoceiba.firebaseio.com", 443); |
Thrillex13 | 3:5dceee1c49fc | 216 | |
Thrillex13 | 3:5dceee1c49fc | 217 | printf("\n----- HTTPS GET request -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 218 | |
Thrillex13 | 3:5dceee1c49fc | 219 | HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://cotoceiba.firebaseio.com/Condominos/94.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 3:5dceee1c49fc | 220 | |
Thrillex13 | 3:5dceee1c49fc | 221 | HttpResponse* get_res = get_req->send(); |
Thrillex13 | 3:5dceee1c49fc | 222 | if (!get_res) { |
Thrillex13 | 3:5dceee1c49fc | 223 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Thrillex13 | 3:5dceee1c49fc | 224 | //return 1; |
Thrillex13 | 3:5dceee1c49fc | 225 | } |
Thrillex13 | 3:5dceee1c49fc | 226 | printf("\n----- HTTPS GET response -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 227 | dump_response(get_res); |
Thrillex13 | 3:5dceee1c49fc | 228 | delete get_req; |
Thrillex13 | 3:5dceee1c49fc | 229 | |
Thrillex13 | 3:5dceee1c49fc | 230 | printf("\n----- HTTPS POST request -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 231 | |
Thrillex13 | 3:5dceee1c49fc | 232 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://cotoceiba.firebaseio.com/Condominos/52.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 3:5dceee1c49fc | 233 | post_req->set_header("Content-Type", "application/json"); |
Thrillex13 | 3:5dceee1c49fc | 234 | |
Thrillex13 | 3:5dceee1c49fc | 235 | const char body[] = "{\"nombre\":\"Posteado por Wifi Perros\",\"numcasa\":\"64\"}"; |
Thrillex13 | 3:5dceee1c49fc | 236 | |
Thrillex13 | 3:5dceee1c49fc | 237 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
Thrillex13 | 3:5dceee1c49fc | 238 | if (!post_res) { |
Thrillex13 | 3:5dceee1c49fc | 239 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
Thrillex13 | 3:5dceee1c49fc | 240 | //return 1; |
Thrillex13 | 3:5dceee1c49fc | 241 | } |
Thrillex13 | 3:5dceee1c49fc | 242 | |
Thrillex13 | 3:5dceee1c49fc | 243 | printf("\n----- HTTPS POST response -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 244 | dump_response(post_res); |
Thrillex13 | 3:5dceee1c49fc | 245 | delete post_req; |
Thrillex13 | 3:5dceee1c49fc | 246 | } |
Thrillex13 | 3:5dceee1c49fc | 247 | |
Thrillex13 | 3:5dceee1c49fc | 248 | void dump_response(HttpResponse* res) |
Thrillex13 | 3:5dceee1c49fc | 249 | { |
Thrillex13 | 1:cf6a75de25ce | 250 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
Thrillex13 | 1:cf6a75de25ce | 251 | |
Thrillex13 | 1:cf6a75de25ce | 252 | printf("Headers:\n"); |
Thrillex13 | 1:cf6a75de25ce | 253 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Thrillex13 | 1:cf6a75de25ce | 254 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
Thrillex13 | 1:cf6a75de25ce | 255 | } |
Thrillex13 | 1:cf6a75de25ce | 256 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
Thrillex13 | 0:f4f503a32dca | 257 | } |