Library for Firebase, PUT, PATCH, POST, GET, DELETE operations supported, (others are available, todo). Based on Mbed's https-example. Tested on STM32F767 using ETHERNET and ESP8266 WIFI interfaces and STM32F446 using ESP8266 WIFI interface.

Dependents:   Firebase-Example

Revision:
1:4f2c1fcc6fb6
Parent:
0:768ae9838086
Child:
2:cde6181a8320
diff -r 768ae9838086 -r 4f2c1fcc6fb6 Firebase.h
--- a/Firebase.h	Thu Jan 23 22:18:11 2020 +0000
+++ b/Firebase.h	Sun Mar 08 14:43:48 2020 +0000
@@ -12,38 +12,6 @@
  */
 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"
-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JpL2fQPMcpAkkduW7sFG8mLRh14mSBsWqAusj99nAKt6NJhxZewhp\n"
-    "JKwbcawanW+w2DUzFr0FauVxNIs4xPxcgkCNAGeUj8ZI+cDcSaMOY4WxNrjOgbbI\n"
-    "feOxHsCmIWxch6/vNVxh+54BZFCy723DL8Fo/tDRU73b+gxPJusJva4ABuXSqSFT\n"
-    "EDaRGzdj8AKkoDmRkWLcEg/6a/2dfw8VV7fIAlqVxNQwZWZLtIsBGazxZ5xflrVz\n"
-    "I5AHejNR7guDaWAimhkSOlG11W8BzPtdAo/xJ6JyI4K5kPGIXucniVRmQfmojxc=\n"
-    "-----END CERTIFICATE-----\n"
-    "-----BEGIN CERTIFICATE-----\n"
     "MIIESjCCAzKgAwIBAgINAeO0mqGNiqmBJWlQuDANBgkqhkiG9w0BAQsFADBMMSAw\n"
     "HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n"
     "U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n"
@@ -82,12 +50,26 @@
     strcat(FirebaseUrl, FirebaseAuth);              // Firebase account authorisation key
 */
 
+/*
 // set Firebase project information..
 char    FirebaseUrl[300];   // dimension to suit required character space
 const char FirebaseID[100]  = "https://projectID.firebaseio.com";   // project ID 
 const char FirebaseAuth[100]= "Web API key";                        // web API key
+*/
 
-char* getData;
+// set Firebase project information..
+char    FirebaseUrl[300];   // dimension to suit required character space
+const char FirebaseID[100]  = "https://mbed-a5059.firebaseio.com";   // project ID 
+const char FirebaseAuth[100]= "YPXm66X4m82sdy3srbPwcPYvpdNwrNUCAJ9xsZsL"; // web API key
+
+char*   getData;
+int     TLSfailText,httpfail;
+/* Port number used to connect to the server */
+const int server_port = 443;
+
+#define IP         "192.168.1.180"
+#define GATEWAY    "192.168.1.1"
+#define NETMASK    "255.255.255.0" 
 
 /*
   Connects to the network using the mbed_app.json ESP8266 WIFI networking interface,
@@ -95,43 +77,55 @@
   if you use ETHERNET: change mbed_app.json "target.network-default-interface-type" : "ETHERNET",
 */
  
-NetworkInterface*   network;
+NetworkInterface*   net;
 TLSSocket* socket = new TLSSocket();
-nsapi_error_t r;
+SocketAddress Fbase;
+nsapi_error_t result;
  
 NetworkInterface *connect_to_default_network_interface() {
     printf("Connecting to network...\n\n");
 
-    NetworkInterface* network = NetworkInterface::get_default_instance();
-
-    if (!network) {
+    NetworkInterface* net = NetworkInterface::get_default_instance();
+    
+    // set static IP, not working on WIFI at the moment 
+    //net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY);
+    
+    if (!net) {
         printf("No network interface found, select an interface in 'mbed_app.json'\n");
         return NULL;
     }
-    nsapi_error_t connect_status = network->connect();
+    nsapi_error_t connect_status = net->connect();
     if (connect_status != NSAPI_ERROR_OK) {
         printf("Failed to connect to network (%d)\n", connect_status);
         return NULL;
     }
-    printf("Connected to the network!\n");
-    printf("IP address: %s\n", network->get_ip_address());
-    printf("MAC: %s\n", network->get_mac_address());
-    printf("Netmask: %s\n", network->get_netmask());
-    printf("Gateway: %s\n", network->get_gateway());   
-    return network;
+    SocketAddress net_addr;   
+    net->get_ip_address(&net_addr);
+    printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
+    net->get_netmask(&net_addr);
+    printf("Netmask:    %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
+    net->get_gateway(&net_addr);
+    printf("Gateway:    %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");    
+    printf("MAC:        %s\n", net->get_mac_address());  
+    return net;
 }
    
 void startTLSreusesocket(char *FirebaseID) { 
           
-    if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
-        printf("TLS socket open failed (%d)\n", r);
+    if ((result = socket->open(net)) != NSAPI_ERROR_OK) {
+        printf("TLS socket open failed (%d)\n", result);
     }
-    if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
-        printf("TLS socket set_root_ca_cert failed (%d)\n", r);
+    if ((result = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
+        printf("TLS socket set_root_ca_cert failed (%d)\n", result);
+    }   
+    net->gethostbyname(FirebaseID, &Fbase);
+    Fbase.set_port(server_port);
+    if ((result = socket->connect(Fbase)) != NSAPI_ERROR_OK) {
+        printf("Connect failure:\n%d\n", result);
+        return;
     }
-    if ((r = socket->connect(FirebaseID, 443)) != NSAPI_ERROR_OK) {
-        printf("TLS socket connect failed (%d)\n", r);
-    }
+    printf("Successfully connected to:\n%s \nat port: %u\n",
+                   FirebaseID, server_port);
 }
 
 void dump_response(HttpResponse* res) {
@@ -145,7 +139,7 @@
 
 char *getFirebase(char* FirebaseUrl)
 {       
-    //HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, FirebaseUrl);  // non socket reuse function
+    //HttpsRequest* get_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_GET, FirebaseUrl);  // non socket reuse function
     HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, FirebaseUrl);    // socket reuse function 
     HttpResponse* get_res = get_req->send();    
     if (!get_res) {       
@@ -167,7 +161,7 @@
 
 bool putFirebase(char* FirebaseUrl, char *stringToProcess)
 {
-    //HttpsRequest* put_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_PUT, FirebaseUrl);    // non socket reuse function
+    //HttpsRequest* put_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_PUT, FirebaseUrl);    // non socket reuse function
     HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, FirebaseUrl);    // socket reuse function   
     put_req->set_header("Content-Type", "application/json");
     HttpResponse* put_res = put_req->send(stringToProcess, strlen(stringToProcess));    
@@ -182,7 +176,7 @@
         return 0;                
         }
         else{
-            // dump_response(put_res);   
+            //dump_response(put_res);   
             delete put_req;     
             return put_res; 
             } 
@@ -190,7 +184,7 @@
 
 bool postFirebase(char* FirebaseUrl, char *stringToProcess)
 {  
-   // HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, FirebaseUrl);   // non socket reuse function
+   // HttpsRequest* post_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_POST, FirebaseUrl);   // non socket reuse function
     HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, FirebaseUrl);  // socket reuse function   
     post_req->set_header("Content-Type", "application/json");
     HttpResponse* post_res = post_req->send(stringToProcess, strlen(stringToProcess));    
@@ -213,7 +207,7 @@
 
 bool deleteFirebase(char* FirebaseUrl, char *stringToProcess)
 {
-    // HttpsRequest* delete_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_DELETE, FirebaseUrl);   // non socket reuse function
+    // HttpsRequest* delete_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_DELETE, FirebaseUrl);   // non socket reuse function
     HttpsRequest* delete_req = new HttpsRequest(socket, HTTP_DELETE, FirebaseUrl);  // socket reuse function   
     delete_req->set_header("Content-Type", "application/json");
     HttpResponse* delete_res = delete_req->send(stringToProcess, strlen(stringToProcess));