Paul Staron / Mbed OS Firebase-Example

Dependencies:   NTPclient Firebase-https

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Thu Jan 23 22:33:09 2020 +0000
Child:
1:7f0e80fcb3a0
Commit message:
Initial revision

Changed in this revision

Firebase-https.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp 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
mbed_lib.json Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Firebase-https.lib	Thu Jan 23 22:33:09 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/star297/code/Firebase-https/#768ae9838086
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jan 23 22:33:09 2020 +0000
@@ -0,0 +1,122 @@
+#include "mbed.h"
+#include "Firebase.h"
+#include "trng_api.h"
+
+DigitalOut led1(LED1,0);
+DigitalOut led2(LED2,0);
+DigitalOut led3(LED3,0);
+
+char    dataPUT[200];       // PUT data container
+char    dataPOST[200];      // POST data container
+char    timebuff[100];      // timestamp container
+
+int     PUTdata,POSTdata,GETdata,RTCsecond,Lastsecond,RTCminute,Lastminute,RTChour,Lasthour;
+
+void    getRTC(),getSENSORS();
+
+int main()
+{    
+    ThisThread::sleep_for(1000);  // allow time to reset
+    printf("\033[0m\033[2J\033[H\n  ----- Firebase Example -----\r\n\n\n");
+    printf("Initialise!\r\n\n");  
+    
+    // Need to set the date/time here first eg, SNTP server 
+    
+    // connect to the default connection access point
+    network = connect_to_default_network_interface();    
+    if (!network) {
+        printf("Cannot connect to the network, see serial output\n");
+        return 1;
+    }   
+    
+    printf("\nConnecting TLS re-use socket...!\n\n");
+    TLSSocket* socket = new TLSSocket();
+    startTLSreusesocket((char*)FirebaseID);
+     
+    printf("\nReady...!\n\n");        
+    
+    while(1){ 
+        led1=1;       
+        while(Lastsecond==RTCsecond){   // synchronise data upload with RTC             
+            getRTC();       
+        }
+        Lastsecond=RTCsecond;
+        
+        getSENSORS();   // get sensor data.
+        led1=0;
+                       
+        if(RTCsecond%10==0){PUTdata=1;}                 // PUT 10 second counter
+        if(RTCsecond==0){GETdata=1;}                    // GET minute counter
+        if(RTCminute==0 && RTCsecond==0){POSTdata=1;}   // POST hour counter                    
+        
+        if(PUTdata){    // PUT data to Firebase, initialise a data block the updates it on subsiquent PUT's
+            led3=1;            
+            strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
+            strcat(FirebaseUrl, "/Test/Current/.json?auth=");   // bit in the middle to send .json data and authority
+            strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                   
+            if(putFirebase((char*)FirebaseUrl,(char*)dataPUT)){
+                printf("PUT Current okay, time: %s\n",timebuff);
+                }                
+            PUTdata=0;
+            led3=0;
+        }                           
+        if(POSTdata){   // POST data to Firebase, adds records           
+            led3=1;            
+            strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
+            strcat(FirebaseUrl, "/Test/History/.json?auth=");   // bit in the middle to send .json data and authority
+            strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                
+            if(postFirebase((char*)FirebaseUrl,(char*)dataPOST)){
+                 printf("POST History okay, time: %s\n",timebuff);
+                 }     
+            POSTdata=0;
+            led3=0;
+        }             
+        if(GETdata){    // retrieve data from Firebase
+            led2=1;            
+            strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
+            strcat(FirebaseUrl, "/Test/Current/.json?auth=");   // bit in the middle to send .json data and authority
+            strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                                     
+            printf("\nGET current...  time: %s\n",timebuff);
+            printf("%s \n\n",getFirebase((char*)FirebaseUrl));
+            GETdata=0;
+            led2=0;
+        }                                                 
+    }       
+}
+
+void getSENSORS()
+{
+    //  dummy sensor values, change these if you add real sensors eg, BME280, DS1820 etc. 
+    // STM32F7 trng   
+    RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;  // Enable RNG clock source 
+    RNG->CR |= RNG_CR_RNGEN;            // RNG Peripheral enable 
+    while (!(RNG->SR & (RNG_SR_DRDY))); // Wait until one RNG number is ready 
+    int num = RNG->DR;
+    num = abs(num%100);
+        
+    //int num = abs(rand()%100);    
+    //printf("RNG: %d\n",num);
+    
+    char    Temp[5][7] = {"23.80","72.50","65.00","45.30","30.60"};
+    float temp1 = (num/1.3), temp2 = (num*1.13), humidity = (num), pressure = (985+num);
+    // POST data
+    sprintf(dataPOST,"{\"Temp0\" : \"%s\",\"Temp1\" : \"%s\",\"Temp2\" : \"%s\",\"Temp3\" : \"%s\",\"Temp4\" : \"%s\",\"Time\" : \"%s\"}",
+    Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],timebuff);
+    // PUT data
+    sprintf(dataPUT,"{\"TempIn\" : \"%3.2f\", \"TempOut\" : \"%3.2f\",\"Humidity\" : \"%2.2f\",\"Pressure\" : \"%4.2f\", \"Time\" : \"%s\"}",
+    temp1,temp2,humidity,pressure,timebuff);
+}
+
+void getRTC()
+{
+    time_t seconds = time(NULL);                                    
+    strftime(timebuff, 3,"%H", localtime(&seconds));
+    RTChour = atoi(timebuff);
+    strftime(timebuff, 3,"%M", localtime(&seconds));
+    RTCminute = atoi(timebuff);
+    strftime(timebuff, 3,"%S", localtime(&seconds));
+    RTCsecond = atoi(timebuff);    
+    strftime(timebuff,50,"%H:%M:%S %m/%d/%Y ", localtime(&seconds));           
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Jan 23 22:33:09 2020 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#430e64fce8098fd5e54f6de32f3f029f35dc705f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Thu Jan 23 22:33:09 2020 +0000
@@ -0,0 +1,55 @@
+{    
+    "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": 921600,
+            "platform.stdio-convert-newlines": true,
+            "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\""
+        },
+        "NUCLEO_F401RE": {
+            "target.network-default-interface-type" : "WIFI",
+            "esp8266.serial-baudrate"           : 460800,
+            "esp8266.tx"                        : "D8",
+            "esp8266.rx"                        : "D2",
+            "esp8266.provide-default"           : true
+        },
+        "NUCLEO_F446ZE": {
+            "target.network-default-interface-type" : "WIFI",
+            "esp8266.serial-baudrate"           : 460800,
+            "esp8266.tx"                        : "D1",
+            "esp8266.rx"                        : "D0",
+            "esp8266.provide-default"           : true
+        },
+        "NUCLEO_F767ZI": {
+            "target.network-default-interface-type" : "WIFI",
+            "esp8266.serial-baudrate"           : 460800,
+            "esp8266.tx"                        : "D1",
+            "esp8266.rx"                        : "D0",
+            "esp8266.provide-default"           : true
+        },
+        "DISCO_F746NG": {
+            "target.network-default-interface-type" : "WIFI",
+            "esp8266.serial-baudrate"           : 460800,
+            "esp8266.tx"                        : "D1",
+            "esp8266.rx"                        : "D0",
+            "esp8266.provide-default"           : true
+        }
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_lib.json	Thu Jan 23 22:33:09 2020 +0000
@@ -0,0 +1,13 @@
+{
+    "name": "mbed-http",
+    "macros": [
+        "MBEDTLS_SHA1_C"
+    ],
+    "config": {
+        "http-buffer-size": {
+            "help": "Size of the HTTP receive buffer in bytes",
+            "value": 2048,
+            "macro_name": "HTTP_RECEIVE_BUFFER_SIZE"
+        }
+    }
+}