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

Committer:
star297
Date:
Sun Mar 08 14:45:02 2020 +0000
Revision:
1:7f0e80fcb3a0
Parent:
0:76248fdccc2a
Added NTP set RTC function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:76248fdccc2a 1 #include "mbed.h"
star297 0:76248fdccc2a 2 #include "Firebase.h"
star297 0:76248fdccc2a 3 #include "trng_api.h"
star297 1:7f0e80fcb3a0 4 #include "NTPclient.h"
star297 0:76248fdccc2a 5
star297 0:76248fdccc2a 6 DigitalOut led1(LED1,0);
star297 1:7f0e80fcb3a0 7 DigitalOut led2(LED1,0);
star297 1:7f0e80fcb3a0 8 DigitalOut led3(LED1,0);
star297 0:76248fdccc2a 9
star297 0:76248fdccc2a 10 char dataPUT[200]; // PUT data container
star297 0:76248fdccc2a 11 char dataPOST[200]; // POST data container
star297 0:76248fdccc2a 12 char timebuff[100]; // timestamp container
star297 0:76248fdccc2a 13
star297 0:76248fdccc2a 14 int PUTdata,POSTdata,GETdata,RTCsecond,Lastsecond,RTCminute,Lastminute,RTChour,Lasthour;
star297 0:76248fdccc2a 15
star297 0:76248fdccc2a 16 void getRTC(),getSENSORS();
star297 0:76248fdccc2a 17
star297 0:76248fdccc2a 18 int main()
star297 0:76248fdccc2a 19 {
star297 1:7f0e80fcb3a0 20 ThisThread::sleep_for(100); // allow time to reset
star297 0:76248fdccc2a 21 printf("\033[0m\033[2J\033[H\n ----- Firebase Example -----\r\n\n\n");
star297 0:76248fdccc2a 22 printf("Initialise!\r\n\n");
star297 1:7f0e80fcb3a0 23
star297 0:76248fdccc2a 24 // connect to the default connection access point
star297 1:7f0e80fcb3a0 25 net = connect_to_default_network_interface();
star297 1:7f0e80fcb3a0 26
star297 1:7f0e80fcb3a0 27 // get NTP time and set RTC
star297 1:7f0e80fcb3a0 28 NTPclient ntp(*net);
star297 1:7f0e80fcb3a0 29 printf("\nConnecting to NTP server..\n");
star297 1:7f0e80fcb3a0 30 // NTP server address, timezone offset in seconds +/-, enable DST, set RTC
star297 1:7f0e80fcb3a0 31 if(ntp.getNTP("0.pool.ntp.org",0,1,1)){
star297 1:7f0e80fcb3a0 32 time_t seconds = time(NULL);
star297 1:7f0e80fcb3a0 33 printf("System time set by NTP: %s\n\n", ctime(&seconds));
star297 1:7f0e80fcb3a0 34 }
star297 1:7f0e80fcb3a0 35 else{printf("No NTP could not set RTC !!\n\n");
star297 1:7f0e80fcb3a0 36 }
star297 0:76248fdccc2a 37
star297 0:76248fdccc2a 38 printf("\nConnecting TLS re-use socket...!\n\n");
star297 0:76248fdccc2a 39 TLSSocket* socket = new TLSSocket();
star297 0:76248fdccc2a 40 startTLSreusesocket((char*)FirebaseID);
star297 0:76248fdccc2a 41
star297 0:76248fdccc2a 42 printf("\nReady...!\n\n");
star297 0:76248fdccc2a 43
star297 0:76248fdccc2a 44 while(1){
star297 0:76248fdccc2a 45 led1=1;
star297 0:76248fdccc2a 46 while(Lastsecond==RTCsecond){ // synchronise data upload with RTC
star297 0:76248fdccc2a 47 getRTC();
star297 0:76248fdccc2a 48 }
star297 0:76248fdccc2a 49 Lastsecond=RTCsecond;
star297 0:76248fdccc2a 50
star297 0:76248fdccc2a 51 getSENSORS(); // get sensor data.
star297 0:76248fdccc2a 52 led1=0;
star297 0:76248fdccc2a 53
star297 0:76248fdccc2a 54 if(RTCsecond%10==0){PUTdata=1;} // PUT 10 second counter
star297 0:76248fdccc2a 55 if(RTCsecond==0){GETdata=1;} // GET minute counter
star297 1:7f0e80fcb3a0 56 if(RTCminute==0 && RTCsecond==0){POSTdata=1;} // POST hour counter
star297 0:76248fdccc2a 57
star297 0:76248fdccc2a 58 if(PUTdata){ // PUT data to Firebase, initialise a data block the updates it on subsiquent PUT's
star297 0:76248fdccc2a 59 led3=1;
star297 0:76248fdccc2a 60 strcpy(FirebaseUrl, FirebaseID); // Firebase account ID
star297 0:76248fdccc2a 61 strcat(FirebaseUrl, "/Test/Current/.json?auth="); // bit in the middle to send .json data and authority
star297 0:76248fdccc2a 62 strcat(FirebaseUrl, FirebaseAuth); // Firebase account authorisation key
star297 0:76248fdccc2a 63 if(putFirebase((char*)FirebaseUrl,(char*)dataPUT)){
star297 0:76248fdccc2a 64 printf("PUT Current okay, time: %s\n",timebuff);
star297 0:76248fdccc2a 65 }
star297 0:76248fdccc2a 66 PUTdata=0;
star297 0:76248fdccc2a 67 led3=0;
star297 0:76248fdccc2a 68 }
star297 0:76248fdccc2a 69 if(POSTdata){ // POST data to Firebase, adds records
star297 0:76248fdccc2a 70 led3=1;
star297 0:76248fdccc2a 71 strcpy(FirebaseUrl, FirebaseID); // Firebase account ID
star297 0:76248fdccc2a 72 strcat(FirebaseUrl, "/Test/History/.json?auth="); // bit in the middle to send .json data and authority
star297 0:76248fdccc2a 73 strcat(FirebaseUrl, FirebaseAuth); // Firebase account authorisation key
star297 0:76248fdccc2a 74 if(postFirebase((char*)FirebaseUrl,(char*)dataPOST)){
star297 0:76248fdccc2a 75 printf("POST History okay, time: %s\n",timebuff);
star297 0:76248fdccc2a 76 }
star297 0:76248fdccc2a 77 POSTdata=0;
star297 0:76248fdccc2a 78 led3=0;
star297 0:76248fdccc2a 79 }
star297 0:76248fdccc2a 80 if(GETdata){ // retrieve data from Firebase
star297 0:76248fdccc2a 81 led2=1;
star297 0:76248fdccc2a 82 strcpy(FirebaseUrl, FirebaseID); // Firebase account ID
star297 1:7f0e80fcb3a0 83 strcat(FirebaseUrl, "/Mill/Current/.json?auth="); // bit in the middle to send .json data and authority
star297 0:76248fdccc2a 84 strcat(FirebaseUrl, FirebaseAuth); // Firebase account authorisation key
star297 0:76248fdccc2a 85 printf("\nGET current... time: %s\n",timebuff);
star297 0:76248fdccc2a 86 printf("%s \n\n",getFirebase((char*)FirebaseUrl));
star297 0:76248fdccc2a 87 GETdata=0;
star297 0:76248fdccc2a 88 led2=0;
star297 0:76248fdccc2a 89 }
star297 0:76248fdccc2a 90 }
star297 0:76248fdccc2a 91 }
star297 0:76248fdccc2a 92
star297 0:76248fdccc2a 93 void getSENSORS()
star297 0:76248fdccc2a 94 {
star297 0:76248fdccc2a 95 // dummy sensor values, change these if you add real sensors eg, BME280, DS1820 etc.
star297 0:76248fdccc2a 96 // STM32F7 trng
star297 1:7f0e80fcb3a0 97 //RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN; // Enable RNG clock source
star297 1:7f0e80fcb3a0 98 //RNG->CR |= RNG_CR_RNGEN; // RNG Peripheral enable
star297 1:7f0e80fcb3a0 99 //while (!(RNG->SR & (RNG_SR_DRDY))); // Wait until one RNG number is ready
star297 1:7f0e80fcb3a0 100 //int num = RNG->DR;
star297 1:7f0e80fcb3a0 101 //num = abs(num%100);
star297 0:76248fdccc2a 102
star297 1:7f0e80fcb3a0 103 int num = abs(rand()%100);
star297 0:76248fdccc2a 104 //printf("RNG: %d\n",num);
star297 0:76248fdccc2a 105
star297 0:76248fdccc2a 106 char Temp[5][7] = {"23.80","72.50","65.00","45.30","30.60"};
star297 0:76248fdccc2a 107 float temp1 = (num/1.3), temp2 = (num*1.13), humidity = (num), pressure = (985+num);
star297 0:76248fdccc2a 108 // POST data
star297 0:76248fdccc2a 109 sprintf(dataPOST,"{\"Temp0\" : \"%s\",\"Temp1\" : \"%s\",\"Temp2\" : \"%s\",\"Temp3\" : \"%s\",\"Temp4\" : \"%s\",\"Time\" : \"%s\"}",
star297 0:76248fdccc2a 110 Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],timebuff);
star297 0:76248fdccc2a 111 // PUT data
star297 0:76248fdccc2a 112 sprintf(dataPUT,"{\"TempIn\" : \"%3.2f\", \"TempOut\" : \"%3.2f\",\"Humidity\" : \"%2.2f\",\"Pressure\" : \"%4.2f\", \"Time\" : \"%s\"}",
star297 0:76248fdccc2a 113 temp1,temp2,humidity,pressure,timebuff);
star297 0:76248fdccc2a 114 }
star297 0:76248fdccc2a 115
star297 0:76248fdccc2a 116 void getRTC()
star297 0:76248fdccc2a 117 {
star297 0:76248fdccc2a 118 time_t seconds = time(NULL);
star297 0:76248fdccc2a 119 strftime(timebuff, 3,"%H", localtime(&seconds));
star297 0:76248fdccc2a 120 RTChour = atoi(timebuff);
star297 0:76248fdccc2a 121 strftime(timebuff, 3,"%M", localtime(&seconds));
star297 0:76248fdccc2a 122 RTCminute = atoi(timebuff);
star297 0:76248fdccc2a 123 strftime(timebuff, 3,"%S", localtime(&seconds));
star297 0:76248fdccc2a 124 RTCsecond = atoi(timebuff);
star297 0:76248fdccc2a 125 strftime(timebuff,50,"%H:%M:%S %m/%d/%Y ", localtime(&seconds));
star297 0:76248fdccc2a 126 }
star297 0:76248fdccc2a 127
star297 0:76248fdccc2a 128