Firebase example. demonstrating PUT, POST and GET functions. Requires target MCU with at least 280kB Flash/33kB RAM. Tested on STM32F767 and STM32F446 boards using ETHERNET (STM32F767) and ESP8266 WIFI. Edit the mbed_app.json file to change interface, currently set to ESP8266 WIFI at 460800 baud, the default is 115200 baud. however its much more responsive at 460800 if you can use the AT command AT+UART_DEF? (see Espressif instruction set). Using TLSSocket reuse function(default) or can be set to open a new socket per request. Random dummy sensor values are used, just add some real sensors and edit the getSENSORS function. The data format is .json style and need to be wrapped in curly braces. Go to: https://firebase.google.com/products/realtime-database/ and set up a real-time database, takes a couple of minutes, and get your Project ID and Web API key from the Database/settings page. These are entered in the Firebase.h file. Added NTP RTC time set function.

Dependencies:   NTPclient Firebase-https

main.cpp

Committer:
star297
Date:
2020-03-08
Revision:
1:7f0e80fcb3a0
Parent:
0:76248fdccc2a

File content as of revision 1:7f0e80fcb3a0:

#include "mbed.h"
#include "Firebase.h"
#include "trng_api.h"
#include "NTPclient.h"

DigitalOut led1(LED1,0);
DigitalOut led2(LED1,0);
DigitalOut led3(LED1,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(100);  // allow time to reset
    printf("\033[0m\033[2J\033[H\n  ----- Firebase Example -----\r\n\n\n");
    printf("Initialise!\r\n\n");  
     
    // connect to the default connection access point
    net = connect_to_default_network_interface();    
   
    // get NTP time and set RTC
    NTPclient           ntp(*net);
    printf("\nConnecting to NTP server..\n");    
    //  NTP server address, timezone offset in seconds +/-, enable DST, set RTC 
    if(ntp.getNTP("0.pool.ntp.org",0,1,1)){
        time_t seconds = time(NULL);
        printf("System time set by NTP: %s\n\n", ctime(&seconds));
        }
        else{printf("No NTP could not set RTC !!\n\n");
        }  
    
    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, "/Mill/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));           
}