Gateway con agregar controles mediante firebase

Dependencies:   mbed mbed-http ESP01 Pulse RFDecoder

Files at this revision

API Documentation at this revision

Comitter:
Thrillex13
Date:
Sat May 25 04:59:06 2019 +0000
Parent:
0:f4f503a32dca
Child:
2:bc3973471585
Commit message:
- Se realizo exitosamente la peticiones GET y POST a la base de datos de Firebase.

Changed in this revision

ESP01.lib Show annotated file Show diff for this revision Revisions of this file
ESP8266.lib Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-http.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
mbedtls_entropy_config.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP01.lib	Sat May 25 04:59:06 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Thrillex13/code/ESP8266/#99f1cb937894
--- a/ESP8266.lib	Sun May 12 16:54:48 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/Thrillex13/code/ESP8266/#d52701d66b62
--- a/main.cpp	Sun May 12 16:54:48 2019 +0000
+++ b/main.cpp	Sat May 25 04:59:06 2019 +0000
@@ -1,10 +1,16 @@
 #include "mbed.h"
 #include <string>
-#include "ESP8266.h"
+#include "ESP01.h"
+#include "https_request.h"
+#include "mbed_trace.h"
+//#include "network-helper.h"
+#include "TCPSocket.h"
+#include "ESP8266Interface.h"
  
 // Objects
 Serial pc(USBTX, USBRX);
-ESP8266 wifi(PTC17, PTC16, 115200);
+ESP01 wifi(PTC17, PTC16, 115200);       //Con este objeto implementamos los metodos que requieren comandos AT del modulo de WIFI
+//ESP8266Interface net(PTC17, PTC16);     //Con este objeto implementamos todas las funciones de red para la creacion de Sockets
 
 DigitalOut led_rojo(LED_RED);
 DigitalOut led_verde(LED_GREEN);
@@ -19,6 +25,84 @@
 void wifiInit(void);
 bool isConnectedToWifi(void);
 void wifiConnect(void);
+
+void http_demo(NetworkInterface *net);
+
+
+/* List of trusted root CA certificates
+ * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
+ *
+ * To add more root certificates, just concatenate them.
+ */
+const char SSL_CA_PEM[] =  
+    "-----BEGIN CERTIFICATE-----\n"
+    "MIIFmzCCBIOgAwIBAgIRAJRuZHok8dQjAQAAAAApJGwwDQYJKoZIhvcNAQELBQAw\n"
+    "QjELMAkGA1UEBhMCVVMxHjAcBgNVBAoTFUdvb2dsZSBUcnVzdCBTZXJ2aWNlczET\n"
+    "MBEGA1UEAxMKR1RTIENBIDFPMTAeFw0xOTAzMTMyMTMwNTdaFw0yMDAzMTEyMTMw\n"
+    "NTdaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\n"
+    "Ew1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgTExDMRcwFQYDVQQDEw5m\n"
+    "aXJlYmFzZWlvLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALkd\n"
+    "/jeM+PrwIYmM6IE28iN7CjKnBWy+tqNrs5z2AJwM5iLgtgcbc+THuXGElTN7zAWB\n"
+    "SKWv+ezAGe5NiTEEUiWgX1z1VFDj85xpz+Cysn/Oi8MtoN6h8yCmYPtXv8YPInw0\n"
+    "JLkSM5e2uuWVwc/tR1Fh+awpiYQFqGm6DvMq6yAYB/nuL3Gyc4XFgRrxje+VQ+g3\n"
+    "oWE+9NVfqu4smFCutSIGlEdRhGdr/m2u+sc+dnBZNoVOqwSyyFyjpAZvMMvuE5Ip\n"
+    "3ovfA1tq57nl9eQw8fcKsdXOeeXYHc1KBUUC7Xb8F/1+upVML8a5GMULpYkClLXt\n"
+    "OKRRfCJa53YynILJdPkCAwEAAaOCAmQwggJgMA4GA1UdDwEB/wQEAwIFoDATBgNV\n"
+    "H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/vNVxh+54BZFCy723DL8Fo/tDRU73b+gxPJusJva4ABuXSqSFT\n"
+    "EDaRGzdj8AKkoDmRkWLcEg/6a/2dfw8VV7fIAlqVxNQwZWZLtIsBGazxZ5xflrVz\n"
+    "I5AHejNR7guDaWAimhkSOlG11W8BzPtdAo/xJ6JyI4K5kPGIXucniVRmQfmojxc=\n"
+    "-----END CERTIFICATE-----\n"
+    "-----BEGIN CERTIFICATE-----\n"
+    "MIIESjCCAzKgAwIBAgINAeO0mqGNiqmBJWlQuDANBgkqhkiG9w0BAQsFADBMMSAw\n"
+    "HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n"
+    "U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n"
+    "MTUwMDAwNDJaMEIxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg\n"
+    "U2VydmljZXMxEzARBgNVBAMTCkdUUyBDQSAxTzEwggEiMA0GCSqGSIb3DQEBAQUA\n"
+    "A4IBDwAwggEKAoIBAQDQGM9F1IvN05zkQO9+tN1pIRvJzzyOTHW5DzEZhD2ePCnv\n"
+    "UA0Qk28FgICfKqC9EksC4T2fWBYk/jCfC3R3VZMdS/dN4ZKCEPZRrAzDsiKUDzRr\n"
+    "mBBJ5wudgzndIMYcLe/RGGFl5yODIKgjEv/SJH/UL+dEaltN11BmsK+eQmMF++Ac\n"
+    "xGNhr59qM/9il71I2dN8FGfcddwuaej4bXhp0LcQBbjxMcI7JP0aM3T4I+DsaxmK\n"
+    "FsbjzaTNC9uzpFlgOIg7rR25xoynUxv8vNmkq7zdPGHXkxWY7oG9j+JkRyBABk7X\n"
+    "rJfoucBZEqFJJSPk7XA0LKW0Y3z5oz2D0c1tJKwHAgMBAAGjggEzMIIBLzAOBgNV\n"
+    "HQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1Ud\n"
+    "EwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFJjR+G4Q68+b7GCfGJAboOt9Cf0rMB8G\n"
+    "A1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYuMDUGCCsGAQUFBwEBBCkwJzAl\n"
+    "BggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdvb2cvZ3NyMjAyBgNVHR8EKzAp\n"
+    "MCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dzcjIvZ3NyMi5jcmwwPwYDVR0g\n"
+    "BDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly9wa2kuZ29vZy9y\n"
+    "ZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEAGoA+Nnn78y6pRjd9XlQWNa7H\n"
+    "TgiZ/r3RNGkmUmYHPQq6Scti9PEajvwRT2iWTHQr02fesqOqBY2ETUwgZQ+lltoN\n"
+    "FvhsO9tvBCOIazpswWC9aJ9xju4tWDQH8NVU6YZZ/XteDSGU9YzJqPjY8q3MDxrz\n"
+    "mqepBCf5o8mw/wJ4a2G6xzUr6Fb6T8McDO22PLRL6u3M4Tzs3A2M1j6bykJYi8wW\n"
+    "IRdAvKLWZu/axBVbzYmqmwkm5zLSDW5nIAJbELCQCZwMH56t2Dvqofxs6BBcCFIZ\n"
+    "USpxu6x6td0V7SvJCCosirSmIatj/9dSSVDQibet8q/7UK4v4ZUN80atnZz1yg==\n"
+    "-----END CERTIFICATE-----\n";
+    
+
+void dump_response(HttpResponse* res) {
+    printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
+
+    printf("Headers:\n");
+    for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
+        printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
+    }
+    printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
+}
  
 int main() {
     led_azul = OFF;
@@ -30,6 +114,11 @@
     wifiInit();
     wifiConnect();
     
+    //NextToDo: Crear un servidor y a la vez hacer get y post
+    
+    ESP8266Interface net(PTC17, PTC16);     //Con este objeto implementamos todas las funciones de red para la creacion de Sockets
+    http_demo(&net);
+    
     
 
     while(1);
@@ -41,11 +130,21 @@
     wifi.Reset();
     wait(2);
     wifi.DisableEcho();
-    pc.printf("Set mode to Station\r\n");
-    wifi.SetMode(1);
+    pc.printf("Set mode to Station+AP\r\n");
+    wifi.SetMode(3);
     wifi.RcvReply(rcv, 1000);
     pc.printf("%s", rcv);
     wait(2);
+    pc.printf("Configure for multiple sockets\r\n");
+    wifi.SetMultiple();
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s", rcv);
+    wait(2);
+    pc.printf("Enable DHCP\r\n");
+    wifi.EnableDHCP();
+    wifi.RcvReply(rcv, 1000);
+    pc.printf("%s", rcv);
+    wait(2);   
 }
 
 void wifiConnect(void){
@@ -62,7 +161,7 @@
         pc.printf("Starting Smart Config\r\n");
         led_azul = ON;
         wifi.StartSmartConfig();
-        wifi.RcvReply(rcv, 5000);
+        wifi.RcvReply(rcv, 15000);
         pc.printf("%s", rcv);
         wait(5);
         while(!isConnectedToWifi()){
@@ -90,4 +189,44 @@
     }
     return status;
     
+}
+
+void http_demo(NetworkInterface *network){
+    TLSSocket* socket = new TLSSocket();
+ 
+    nsapi_error_t r;
+    // make sure to check the return values for the calls below (should return NSAPI_ERROR_OK)
+    r = socket->open(network);
+    r = socket->set_root_ca_cert(SSL_CA_PEM);
+    r = socket->connect("https://cotoceiba.firebaseio.com", 443);
+    
+    printf("\n----- HTTPS GET request -----\n");
+
+        HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, "https://cotoceiba.firebaseio.com/Condominos/94.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH");
+
+        HttpResponse* get_res = get_req->send();
+        if (!get_res) {
+            printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+            //return 1;
+        }
+        printf("\n----- HTTPS GET response -----\n");
+        dump_response(get_res);
+        delete get_req;     
+        
+        printf("\n----- HTTPS POST request -----\n");
+
+        HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, "https://cotoceiba.firebaseio.com/Condominos/52.json?auth=ZpXLLURU9KWmW5t1kzBYD2IuBE0V7wdv5vXwDgsH");
+        post_req->set_header("Content-Type", "application/json");
+
+        const char body[] = "{\"nombre\":\"Posteado por Wifi Perros\",\"numcasa\":\"64\"}";
+
+        HttpResponse* post_res = post_req->send(body, strlen(body));
+        if (!post_res) {
+            printf("HttpRequest failed (error code %d)\n", post_req->get_error());
+            //return 1;
+        }
+
+        printf("\n----- HTTPS POST response -----\n");
+        dump_response(post_res);
+        delete post_req;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-http.lib	Sat May 25 04:59:06 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/sandbox/code/mbed-http/#6daf67a96a91
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Sat May 25 04:59:06 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Sat May 25 04:59:06 2019 +0000
@@ -0,0 +1,31 @@
+{
+    "config": {
+        "main-stack-size": {
+            "value": 8192
+        }
+    },
+    "macros": [
+        "MBEDTLS_MPI_MAX_SIZE=1024",
+        "MBEDTLS_MPI_WINDOW_SIZE=1",
+        "MBEDTLS_USER_CONFIG_FILE=\"mbedtls_entropy_config.h\"",
+        "MBEDTLS_TEST_NULL_ENTROPY",
+        "MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES",
+        "MBED_HEAP_STATS_ENABLED=1"
+    ],
+    "target_overrides": {
+        "*": {
+            "platform.stdio-baud-rate": 115200,
+            "platform.stdio-convert-newlines": true,
+            "mbed-mesh-api.6lowpan-nd-channel-page": 0,
+            "mbed-mesh-api.6lowpan-nd-channel": 12,
+            "mbed-trace.enable": null,
+            "mbed-http.http-buffer-size": 2048,
+            "nsapi.default-wifi-security": "WPA_WPA2",
+            "nsapi.default-wifi-ssid": "\"SSID\"",
+            "nsapi.default-wifi-password": "\"Password\""
+        },
+        "DISCO_L475VG_IOT01A": {
+            "target.network-default-interface-type" : "WIFI"
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbedtls_entropy_config.h	Sat May 25 04:59:06 2019 +0000
@@ -0,0 +1,28 @@
+/*
+ *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ *  This file is part of mbed TLS (https://tls.mbed.org)
+ */
+
+//#include "select-demo.h"
+
+/* Enable entropy for devices with TRNG. This means entropy is disabled for all other targets. */
+/* Do **NOT** deploy this code in production on other targets! */
+/* See https://tls.mbed.org/kb/how-to/add-entropy-sources-to-entropy-pool */
+#if defined(DEVICE_TRNG)
+#undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
+#undef MBEDTLS_TEST_NULL_ENTROPY
+#endif