![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Gateway con agregar controles mediante firebase
Dependencies: mbed mbed-http ESP01 Pulse RFDecoder
main.cpp@5:190b19129c4d, 2019-09-16 (annotated)
- Committer:
- Thrillex13
- Date:
- Mon Sep 16 17:57:38 2019 +0000
- Revision:
- 5:190b19129c4d
- Parent:
- 4:8fed3705384e
Gateway con comunicacion a la app de android mediante firebase
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thrillex13 | 3:5dceee1c49fc | 1 | #include "main.h" |
Thrillex13 | 4:8fed3705384e | 2 | #include <string> |
Thrillex13 | 3:5dceee1c49fc | 3 | |
Thrillex13 | 0:f4f503a32dca | 4 | // Objects |
Thrillex13 | 0:f4f503a32dca | 5 | Serial pc(USBTX, USBRX); |
Thrillex13 | 1:cf6a75de25ce | 6 | ESP01 wifi(PTC17, PTC16, 115200); //Con este objeto implementamos los metodos que requieren comandos AT del modulo de WIFI |
Thrillex13 | 4:8fed3705384e | 7 | //ESP8266Interface net(PTC17, PTC16); //Con este objeto implementamos todas las funciones de red para la creacion de Sockets |
Thrillex13 | 3:5dceee1c49fc | 8 | RFDecoder decoder = RFDecoder(D2,D3); //tx rx |
Thrillex13 | 4:8fed3705384e | 9 | Ticker firebasecheck; |
Thrillex13 | 4:8fed3705384e | 10 | |
Thrillex13 | 0:f4f503a32dca | 11 | |
Thrillex13 | 0:f4f503a32dca | 12 | // Global variables |
Thrillex13 | 4:8fed3705384e | 13 | char send[128]; // Strings for sending and receiving commands / data send / data receive / command received / status received |
Thrillex13 | 4:8fed3705384e | 14 | char recv[128]; |
Thrillex13 | 3:5dceee1c49fc | 15 | char command[TCPCOMMSBYTESMAX]; |
Thrillex13 | 3:5dceee1c49fc | 16 | char status[TCPSTATSBYTESMAX]; |
Thrillex13 | 3:5dceee1c49fc | 17 | char controlcode[8]; |
Thrillex13 | 3:5dceee1c49fc | 18 | |
Thrillex13 | 4:8fed3705384e | 19 | string comandofirebase; |
Thrillex13 | 4:8fed3705384e | 20 | bool firebaseTimeoutExpired = false; |
Thrillex13 | 4:8fed3705384e | 21 | |
Thrillex13 | 4:8fed3705384e | 22 | |
Thrillex13 | 4:8fed3705384e | 23 | |
Thrillex13 | 3:5dceee1c49fc | 24 | int main() |
Thrillex13 | 3:5dceee1c49fc | 25 | { |
Thrillex13 | 4:8fed3705384e | 26 | |
Thrillex13 | 3:5dceee1c49fc | 27 | //Inicializacion de los Perifericos |
Thrillex13 | 3:5dceee1c49fc | 28 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 29 | led_rojo = OFF; |
Thrillex13 | 3:5dceee1c49fc | 30 | led_verde = OFF; |
Thrillex13 | 3:5dceee1c49fc | 31 | pc.baud(115200); |
Thrillex13 | 0:f4f503a32dca | 32 | |
Thrillex13 | 4:8fed3705384e | 33 | esp01_en.mode(PullUp); |
Thrillex13 | 4:8fed3705384e | 34 | esp01_rst.mode(PullUp); |
Thrillex13 | 4:8fed3705384e | 35 | |
Thrillex13 | 3:5dceee1c49fc | 36 | |
Thrillex13 | 3:5dceee1c49fc | 37 | //Inicializacion y Conexion al WiFi |
Thrillex13 | 4:8fed3705384e | 38 | //wifiInit(); |
Thrillex13 | 4:8fed3705384e | 39 | //wifi.Quit(); |
Thrillex13 | 3:5dceee1c49fc | 40 | wifiInit(); |
Thrillex13 | 3:5dceee1c49fc | 41 | wifiConnect(); |
Thrillex13 | 1:cf6a75de25ce | 42 | |
Thrillex13 | 3:5dceee1c49fc | 43 | //ESP8266Interface *net = new ESP8266Interface(); //Con este objeto implementamos todas las funciones de red para la creacion de Sockets para HTTPS |
Thrillex13 | 3:5dceee1c49fc | 44 | //http_demo(net); |
Thrillex13 | 3:5dceee1c49fc | 45 | //delete net; //Necesario destruir el objeto cuando ya no se use para desocupar la interface de wifi. |
Thrillex13 | 3:5dceee1c49fc | 46 | |
Thrillex13 | 4:8fed3705384e | 47 | ESP8266Interface *net = new ESP8266Interface(); //Con este objeto implementamos todas las funciones de red para la creacion de Sockets para HTTPS |
Thrillex13 | 4:8fed3705384e | 48 | //delete net; //Necesario destruir el objeto cuando ya no se use para desocupar la interface de wifi. |
Thrillex13 | 3:5dceee1c49fc | 49 | |
Thrillex13 | 4:8fed3705384e | 50 | //Tarea para revisar si hay algun comando en firebase, esta tarea se ejecuta cada x segundos. |
Thrillex13 | 4:8fed3705384e | 51 | firebasecheck.attach(&firebaseCheckTimeout, INTERVALFIREBASECHECK); |
Thrillex13 | 1:cf6a75de25ce | 52 | |
Thrillex13 | 3:5dceee1c49fc | 53 | while(1) { |
Thrillex13 | 4:8fed3705384e | 54 | if(firebaseTimeoutExpired) { |
Thrillex13 | 4:8fed3705384e | 55 | //Leemos la bsae de datos de firebase para revisar si hay algun comando disponible. |
Thrillex13 | 4:8fed3705384e | 56 | comandofirebase = getFirebaseCommand(net); |
Thrillex13 | 4:8fed3705384e | 57 | |
Thrillex13 | 4:8fed3705384e | 58 | //Habilitamos el debugeo de la memoria |
Thrillex13 | 4:8fed3705384e | 59 | //print_all_thread_info(); |
Thrillex13 | 4:8fed3705384e | 60 | //print_heap_and_isr_stack_info(); |
Thrillex13 | 3:5dceee1c49fc | 61 | |
Thrillex13 | 4:8fed3705384e | 62 | if(comandofirebase.compare(AGREGARCONTROL) == 0) { |
Thrillex13 | 3:5dceee1c49fc | 63 | Timer t; |
Thrillex13 | 4:8fed3705384e | 64 | int tini; |
Thrillex13 | 3:5dceee1c49fc | 65 | t.start(); |
Thrillex13 | 4:8fed3705384e | 66 | tini = t.read_ms(); |
Thrillex13 | 4:8fed3705384e | 67 | |
Thrillex13 | 4:8fed3705384e | 68 | pc.printf("Comando Agregar Control Recibido\r\n"); |
Thrillex13 | 4:8fed3705384e | 69 | putFirebaseCommand(net, WAITING_RF_CODE, 0); |
Thrillex13 | 4:8fed3705384e | 70 | |
Thrillex13 | 3:5dceee1c49fc | 71 | LEDAMARILLO_ON; |
Thrillex13 | 4:8fed3705384e | 72 | while((t.read_ms()- tini) < MAXTIMERXCONTROL) { |
Thrillex13 | 3:5dceee1c49fc | 73 | if(decoder.available()) { |
Thrillex13 | 3:5dceee1c49fc | 74 | unsigned long numcode = decoder.getCode(); |
Thrillex13 | 3:5dceee1c49fc | 75 | pc.printf("Codigo Recibido %x \n\r", numcode); |
Thrillex13 | 4:8fed3705384e | 76 | putFirebaseCommand(net, NEW_CONTROL_RESPONSE, numcode); |
Thrillex13 | 3:5dceee1c49fc | 77 | break; |
Thrillex13 | 3:5dceee1c49fc | 78 | } |
Thrillex13 | 3:5dceee1c49fc | 79 | } |
Thrillex13 | 3:5dceee1c49fc | 80 | LEDVERDE_ON; |
Thrillex13 | 4:8fed3705384e | 81 | firebaseTimeoutExpired = false; |
Thrillex13 | 4:8fed3705384e | 82 | comandofirebase = NINGUNCOMANDO; |
Thrillex13 | 4:8fed3705384e | 83 | } else { |
Thrillex13 | 4:8fed3705384e | 84 | pc.printf("Ningun comando recibido\r\n"); |
Thrillex13 | 4:8fed3705384e | 85 | firebaseTimeoutExpired = false; |
Thrillex13 | 3:5dceee1c49fc | 86 | } |
Thrillex13 | 3:5dceee1c49fc | 87 | |
Thrillex13 | 3:5dceee1c49fc | 88 | } |
Thrillex13 | 3:5dceee1c49fc | 89 | |
Thrillex13 | 4:8fed3705384e | 90 | |
Thrillex13 | 3:5dceee1c49fc | 91 | } |
Thrillex13 | 3:5dceee1c49fc | 92 | } |
Thrillex13 | 3:5dceee1c49fc | 93 | |
Thrillex13 | 3:5dceee1c49fc | 94 | void wifiInit(void) |
Thrillex13 | 3:5dceee1c49fc | 95 | { |
Thrillex13 | 3:5dceee1c49fc | 96 | pc.printf("Gateway Sistema de Control de Cotos\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 97 | pc.printf("Resetting WiFi\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 98 | wifi.Reset(); |
Thrillex13 | 3:5dceee1c49fc | 99 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 100 | wifi.DisableEcho(); |
Thrillex13 | 3:5dceee1c49fc | 101 | pc.printf("Set mode to Station\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 102 | wifi.SetMode(STATION); |
Thrillex13 | 4:8fed3705384e | 103 | wifi.RcvReply(recv, 3000); |
Thrillex13 | 3:5dceee1c49fc | 104 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 105 | wait(2); |
Thrillex13 | 3:5dceee1c49fc | 106 | pc.printf("Configure for multiple sockets\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 107 | wifi.SetMultiple(); |
Thrillex13 | 4:8fed3705384e | 108 | wifi.RcvReply(recv, 3000); |
Thrillex13 | 3:5dceee1c49fc | 109 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 110 | wait(2); |
Thrillex13 | 4:8fed3705384e | 111 | /* |
Thrillex13 | 3:5dceee1c49fc | 112 | pc.printf("Enable DHCP\r\n"); |
Thrillex13 | 3:5dceee1c49fc | 113 | wifi.EnableDHCP(); |
Thrillex13 | 4:8fed3705384e | 114 | wifi.RcvReply(recv, 1500); |
Thrillex13 | 3:5dceee1c49fc | 115 | pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 116 | wait(2); |
Thrillex13 | 4:8fed3705384e | 117 | */ |
Thrillex13 | 3:5dceee1c49fc | 118 | } |
Thrillex13 | 3:5dceee1c49fc | 119 | |
Thrillex13 | 3:5dceee1c49fc | 120 | void wifiConnect(void) |
Thrillex13 | 3:5dceee1c49fc | 121 | { |
Thrillex13 | 4:8fed3705384e | 122 | char connCheckCounter = 0; |
Thrillex13 | 4:8fed3705384e | 123 | |
Thrillex13 | 4:8fed3705384e | 124 | while(!isConnectedToWifi()) { |
Thrillex13 | 4:8fed3705384e | 125 | if(connCheckCounter > MAXWIFICONNCHECKS) { |
Thrillex13 | 4:8fed3705384e | 126 | //pc.printf("Starting Smart Config\r\n"); |
Thrillex13 | 4:8fed3705384e | 127 | led_azul = ON; |
Thrillex13 | 4:8fed3705384e | 128 | wifi.StartSmartConfig(); |
Thrillex13 | 4:8fed3705384e | 129 | wifi.RcvReply(recv, 15000); |
Thrillex13 | 4:8fed3705384e | 130 | //pc.printf("%s", recv); |
Thrillex13 | 4:8fed3705384e | 131 | wait(5); |
Thrillex13 | 4:8fed3705384e | 132 | connCheckCounter=0; |
Thrillex13 | 3:5dceee1c49fc | 133 | } |
Thrillex13 | 4:8fed3705384e | 134 | connCheckCounter++; |
Thrillex13 | 4:8fed3705384e | 135 | pc.printf("Gateway no conectado, intento de reconexion %d\r\n",connCheckCounter); |
Thrillex13 | 4:8fed3705384e | 136 | wait(1); |
Thrillex13 | 3:5dceee1c49fc | 137 | } |
Thrillex13 | 4:8fed3705384e | 138 | |
Thrillex13 | 4:8fed3705384e | 139 | pc.printf("Gateway is already connected to wifi with the following IP address\r\n"); |
Thrillex13 | 4:8fed3705384e | 140 | wifi.GetIP(recv); |
Thrillex13 | 4:8fed3705384e | 141 | pc.printf("%s", recv); |
Thrillex13 | 4:8fed3705384e | 142 | led_azul = OFF; |
Thrillex13 | 4:8fed3705384e | 143 | led_verde = ON; |
Thrillex13 | 4:8fed3705384e | 144 | wait(2); |
Thrillex13 | 4:8fed3705384e | 145 | |
Thrillex13 | 3:5dceee1c49fc | 146 | } |
Thrillex13 | 1:cf6a75de25ce | 147 | |
Thrillex13 | 4:8fed3705384e | 148 | |
Thrillex13 | 3:5dceee1c49fc | 149 | bool isConnectedToWifi(void) |
Thrillex13 | 3:5dceee1c49fc | 150 | { |
Thrillex13 | 3:5dceee1c49fc | 151 | bool status; |
Thrillex13 | 3:5dceee1c49fc | 152 | wifi.GetConnStatusCode(recv); |
Thrillex13 | 4:8fed3705384e | 153 | //pc.printf("El estado de la conexion es %s", recv); |
Thrillex13 | 3:5dceee1c49fc | 154 | if(strcmp(recv,"STATUS:2\r")==0) { |
Thrillex13 | 3:5dceee1c49fc | 155 | led_azul = OFF; |
Thrillex13 | 3:5dceee1c49fc | 156 | led_rojo = OFF; |
Thrillex13 | 3:5dceee1c49fc | 157 | led_verde = ON; |
Thrillex13 | 3:5dceee1c49fc | 158 | status=true; |
Thrillex13 | 3:5dceee1c49fc | 159 | } else { |
Thrillex13 | 3:5dceee1c49fc | 160 | status=false; |
Thrillex13 | 3:5dceee1c49fc | 161 | } |
Thrillex13 | 3:5dceee1c49fc | 162 | return status; |
Thrillex13 | 3:5dceee1c49fc | 163 | |
Thrillex13 | 3:5dceee1c49fc | 164 | } |
Thrillex13 | 3:5dceee1c49fc | 165 | |
Thrillex13 | 3:5dceee1c49fc | 166 | void startServer(int port) |
Thrillex13 | 3:5dceee1c49fc | 167 | { |
Thrillex13 | 4:8fed3705384e | 168 | //pc.printf("Iniciando servidor en el puerto %d\r\n",port); |
Thrillex13 | 3:5dceee1c49fc | 169 | wifi.StartServerMode(port); |
Thrillex13 | 3:5dceee1c49fc | 170 | wifi.RcvReply(recv, 1000); |
Thrillex13 | 4:8fed3705384e | 171 | //pc.printf("%s", recv); |
Thrillex13 | 3:5dceee1c49fc | 172 | //wait(2); |
Thrillex13 | 3:5dceee1c49fc | 173 | } |
Thrillex13 | 3:5dceee1c49fc | 174 | |
Thrillex13 | 3:5dceee1c49fc | 175 | int getTCPContent(char* espdata, char* command, char* status) |
Thrillex13 | 3:5dceee1c49fc | 176 | { |
Thrillex13 | 3:5dceee1c49fc | 177 | char i=0; |
Thrillex13 | 3:5dceee1c49fc | 178 | char offset=0; |
Thrillex13 | 3:5dceee1c49fc | 179 | char socket=0; |
Thrillex13 | 3:5dceee1c49fc | 180 | |
Thrillex13 | 3:5dceee1c49fc | 181 | if((espdata[2]=='+') && (espdata[3]=='I') && (espdata[4]=='P') && (espdata[5]=='D')) { |
Thrillex13 | 3:5dceee1c49fc | 182 | //Obtenemos en socket de conexion |
Thrillex13 | 3:5dceee1c49fc | 183 | socket = espdata[7]-'0'; |
Thrillex13 | 3:5dceee1c49fc | 184 | //Buscamos el caracter : para de ahi iniciar el mensaje |
Thrillex13 | 3:5dceee1c49fc | 185 | for(i=0; i<250; i++) { |
Thrillex13 | 3:5dceee1c49fc | 186 | if(espdata[i]==':') { |
Thrillex13 | 3:5dceee1c49fc | 187 | offset = i+1; |
Thrillex13 | 3:5dceee1c49fc | 188 | break; |
Thrillex13 | 3:5dceee1c49fc | 189 | } |
Thrillex13 | 3:5dceee1c49fc | 190 | } |
Thrillex13 | 3:5dceee1c49fc | 191 | |
Thrillex13 | 3:5dceee1c49fc | 192 | for(i=0; (i<TCPCOMMSBYTESMAX); i++) { |
Thrillex13 | 3:5dceee1c49fc | 193 | command[i] = espdata[i+offset]; |
Thrillex13 | 3:5dceee1c49fc | 194 | } |
Thrillex13 | 3:5dceee1c49fc | 195 | |
Thrillex13 | 3:5dceee1c49fc | 196 | command[TCPCOMMSBYTESMAX] = '\0'; |
Thrillex13 | 3:5dceee1c49fc | 197 | |
Thrillex13 | 3:5dceee1c49fc | 198 | status[0] = COMMANDREC; |
Thrillex13 | 3:5dceee1c49fc | 199 | } |
Thrillex13 | 3:5dceee1c49fc | 200 | |
Thrillex13 | 3:5dceee1c49fc | 201 | return socket; |
Thrillex13 | 3:5dceee1c49fc | 202 | } |
Thrillex13 | 3:5dceee1c49fc | 203 | |
Thrillex13 | 3:5dceee1c49fc | 204 | void convertToCharArray(char *arr, unsigned long number) |
Thrillex13 | 3:5dceee1c49fc | 205 | { |
Thrillex13 | 3:5dceee1c49fc | 206 | int i = 0; |
Thrillex13 | 3:5dceee1c49fc | 207 | |
Thrillex13 | 3:5dceee1c49fc | 208 | for (i = 0; i < 8; ++i) { |
Thrillex13 | 3:5dceee1c49fc | 209 | arr[i] = (char)((((unsigned long) number) >> (56 - (8*i))) & 0xFFu); |
Thrillex13 | 3:5dceee1c49fc | 210 | } |
Thrillex13 | 3:5dceee1c49fc | 211 | } |
Thrillex13 | 3:5dceee1c49fc | 212 | |
Thrillex13 | 3:5dceee1c49fc | 213 | |
Thrillex13 | 4:8fed3705384e | 214 | string getFirebaseCommand(NetworkInterface *network) |
Thrillex13 | 4:8fed3705384e | 215 | { |
Thrillex13 | 4:8fed3705384e | 216 | TLSSocket* socket = new TLSSocket(); |
Thrillex13 | 4:8fed3705384e | 217 | string comando = NINGUNCOMANDO; |
Thrillex13 | 4:8fed3705384e | 218 | |
Thrillex13 | 4:8fed3705384e | 219 | nsapi_error_t r; |
Thrillex13 | 4:8fed3705384e | 220 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
Thrillex13 | 4:8fed3705384e | 221 | r = socket->open(network); |
Thrillex13 | 4:8fed3705384e | 222 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
Thrillex13 | 4:8fed3705384e | 223 | r = socket->connect("https://cotoceiba.firebaseio.com", 443); |
Thrillex13 | 4:8fed3705384e | 224 | |
Thrillex13 | 4:8fed3705384e | 225 | |
Thrillex13 | 4:8fed3705384e | 226 | //printf("\n----- Conectando con la base de datos de Firebase -----\n"); |
Thrillex13 | 4:8fed3705384e | 227 | //HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://cotoceiba.firebaseio.com/Comandos/AppToGateway/Request/Comando.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 4:8fed3705384e | 228 | HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://cotoceiba.firebaseio.com/Comandos/AppToGateway/Request/Comando.json?"); |
Thrillex13 | 4:8fed3705384e | 229 | |
Thrillex13 | 4:8fed3705384e | 230 | HttpResponse* get_res = get_req->send(); |
Thrillex13 | 4:8fed3705384e | 231 | if (!get_res) { |
Thrillex13 | 4:8fed3705384e | 232 | printf("HttpGetRequest failed (error code %d)\n", get_req->get_error()); |
Thrillex13 | 4:8fed3705384e | 233 | comando = NINGUNCOMANDO; |
Thrillex13 | 4:8fed3705384e | 234 | //return 1; |
Thrillex13 | 4:8fed3705384e | 235 | } else { |
Thrillex13 | 4:8fed3705384e | 236 | //printf("\n----- Respuesta de Firebase -----\n"); |
Thrillex13 | 4:8fed3705384e | 237 | //printf("Status: %d - %s\n", get_res->get_status_code(), get_res->get_status_message().c_str()); |
Thrillex13 | 4:8fed3705384e | 238 | |
Thrillex13 | 4:8fed3705384e | 239 | comando = get_res->get_body_as_string(); |
Thrillex13 | 4:8fed3705384e | 240 | //pc.printf("%s\r\n",comando.c_str()); |
Thrillex13 | 4:8fed3705384e | 241 | //printf("\nBody (%lu bytes):\n\n%s\n", get_res->get_body_length(), get_res->get_body_as_string().c_str()); |
Thrillex13 | 4:8fed3705384e | 242 | } |
Thrillex13 | 4:8fed3705384e | 243 | |
Thrillex13 | 4:8fed3705384e | 244 | |
Thrillex13 | 4:8fed3705384e | 245 | delete get_req; |
Thrillex13 | 4:8fed3705384e | 246 | socket->close(); |
Thrillex13 | 4:8fed3705384e | 247 | delete socket; |
Thrillex13 | 4:8fed3705384e | 248 | |
Thrillex13 | 4:8fed3705384e | 249 | return comando; |
Thrillex13 | 4:8fed3705384e | 250 | } |
Thrillex13 | 4:8fed3705384e | 251 | |
Thrillex13 | 4:8fed3705384e | 252 | void putFirebaseCommand(NetworkInterface *network, char comando, unsigned long numcode) |
Thrillex13 | 4:8fed3705384e | 253 | { |
Thrillex13 | 4:8fed3705384e | 254 | TLSSocket* socket = new TLSSocket(); |
Thrillex13 | 4:8fed3705384e | 255 | char body[100]; |
Thrillex13 | 4:8fed3705384e | 256 | |
Thrillex13 | 4:8fed3705384e | 257 | nsapi_error_t r; |
Thrillex13 | 4:8fed3705384e | 258 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
Thrillex13 | 4:8fed3705384e | 259 | r = socket->open(network); |
Thrillex13 | 4:8fed3705384e | 260 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
Thrillex13 | 4:8fed3705384e | 261 | r = socket->connect("https://cotoceiba.firebaseio.com", 443); |
Thrillex13 | 4:8fed3705384e | 262 | |
Thrillex13 | 4:8fed3705384e | 263 | //printf("\n----- Enviando datos a la base de datos de Firebase -----\n"); |
Thrillex13 | 4:8fed3705384e | 264 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_PATCH, "https://cotoceiba.firebaseio.com/Comandos/GatewayToApp/Response.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 4:8fed3705384e | 265 | post_req->set_header("Content-Type", "application/json"); |
Thrillex13 | 4:8fed3705384e | 266 | |
Thrillex13 | 4:8fed3705384e | 267 | sprintf(body, "{\"Comando\":\"%d\",\"Valor1\":\"%X\"}",comando,numcode); |
Thrillex13 | 4:8fed3705384e | 268 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
Thrillex13 | 4:8fed3705384e | 269 | |
Thrillex13 | 4:8fed3705384e | 270 | if (!post_res) { |
Thrillex13 | 4:8fed3705384e | 271 | printf("HttpPatchRequest failed (error code %d)\n", post_req->get_error()); |
Thrillex13 | 4:8fed3705384e | 272 | } |
Thrillex13 | 4:8fed3705384e | 273 | |
Thrillex13 | 4:8fed3705384e | 274 | // printf("\n----- HTTPS PATCH response -----\n"); |
Thrillex13 | 4:8fed3705384e | 275 | // dump_response(post_res); |
Thrillex13 | 4:8fed3705384e | 276 | socket->close(); |
Thrillex13 | 4:8fed3705384e | 277 | delete socket; |
Thrillex13 | 4:8fed3705384e | 278 | delete post_req; |
Thrillex13 | 4:8fed3705384e | 279 | } |
Thrillex13 | 4:8fed3705384e | 280 | |
Thrillex13 | 4:8fed3705384e | 281 | void firebaseCheckTimeout() |
Thrillex13 | 4:8fed3705384e | 282 | { |
Thrillex13 | 4:8fed3705384e | 283 | firebaseTimeoutExpired = true; |
Thrillex13 | 4:8fed3705384e | 284 | |
Thrillex13 | 4:8fed3705384e | 285 | } |
Thrillex13 | 4:8fed3705384e | 286 | |
Thrillex13 | 4:8fed3705384e | 287 | |
Thrillex13 | 3:5dceee1c49fc | 288 | void http_demo(NetworkInterface *network) |
Thrillex13 | 3:5dceee1c49fc | 289 | { |
Thrillex13 | 3:5dceee1c49fc | 290 | TLSSocket* socket = new TLSSocket(); |
Thrillex13 | 3:5dceee1c49fc | 291 | |
Thrillex13 | 3:5dceee1c49fc | 292 | nsapi_error_t r; |
Thrillex13 | 3:5dceee1c49fc | 293 | // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK) |
Thrillex13 | 3:5dceee1c49fc | 294 | r = socket->open(network); |
Thrillex13 | 3:5dceee1c49fc | 295 | r = socket->set_root_ca_cert(SSL_CA_PEM); |
Thrillex13 | 3:5dceee1c49fc | 296 | r = socket->connect("https://cotoceiba.firebaseio.com", 443); |
Thrillex13 | 3:5dceee1c49fc | 297 | |
Thrillex13 | 3:5dceee1c49fc | 298 | printf("\n----- HTTPS GET request -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 299 | |
Thrillex13 | 3:5dceee1c49fc | 300 | HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://cotoceiba.firebaseio.com/Condominos/94.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 3:5dceee1c49fc | 301 | |
Thrillex13 | 3:5dceee1c49fc | 302 | HttpResponse* get_res = get_req->send(); |
Thrillex13 | 3:5dceee1c49fc | 303 | if (!get_res) { |
Thrillex13 | 3:5dceee1c49fc | 304 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Thrillex13 | 3:5dceee1c49fc | 305 | //return 1; |
Thrillex13 | 3:5dceee1c49fc | 306 | } |
Thrillex13 | 3:5dceee1c49fc | 307 | printf("\n----- HTTPS GET response -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 308 | dump_response(get_res); |
Thrillex13 | 3:5dceee1c49fc | 309 | delete get_req; |
Thrillex13 | 3:5dceee1c49fc | 310 | |
Thrillex13 | 3:5dceee1c49fc | 311 | printf("\n----- HTTPS POST request -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 312 | |
Thrillex13 | 3:5dceee1c49fc | 313 | HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://cotoceiba.firebaseio.com/Condominos/52.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH"); |
Thrillex13 | 3:5dceee1c49fc | 314 | post_req->set_header("Content-Type", "application/json"); |
Thrillex13 | 3:5dceee1c49fc | 315 | |
Thrillex13 | 3:5dceee1c49fc | 316 | const char body[] = "{\"nombre\":\"Posteado por Wifi Perros\",\"numcasa\":\"64\"}"; |
Thrillex13 | 3:5dceee1c49fc | 317 | |
Thrillex13 | 3:5dceee1c49fc | 318 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
Thrillex13 | 3:5dceee1c49fc | 319 | if (!post_res) { |
Thrillex13 | 3:5dceee1c49fc | 320 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
Thrillex13 | 3:5dceee1c49fc | 321 | //return 1; |
Thrillex13 | 3:5dceee1c49fc | 322 | } |
Thrillex13 | 3:5dceee1c49fc | 323 | |
Thrillex13 | 3:5dceee1c49fc | 324 | printf("\n----- HTTPS POST response -----\n"); |
Thrillex13 | 3:5dceee1c49fc | 325 | dump_response(post_res); |
Thrillex13 | 3:5dceee1c49fc | 326 | delete post_req; |
Thrillex13 | 3:5dceee1c49fc | 327 | } |
Thrillex13 | 3:5dceee1c49fc | 328 | |
Thrillex13 | 3:5dceee1c49fc | 329 | void dump_response(HttpResponse* res) |
Thrillex13 | 3:5dceee1c49fc | 330 | { |
Thrillex13 | 1:cf6a75de25ce | 331 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
Thrillex13 | 1:cf6a75de25ce | 332 | |
Thrillex13 | 1:cf6a75de25ce | 333 | printf("Headers:\n"); |
Thrillex13 | 1:cf6a75de25ce | 334 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Thrillex13 | 1:cf6a75de25ce | 335 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
Thrillex13 | 1:cf6a75de25ce | 336 | } |
Thrillex13 | 1:cf6a75de25ce | 337 | printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
Thrillex13 | 4:8fed3705384e | 338 | } |