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:
Thu Jan 23 22:33:09 2020 +0000
Revision:
0:76248fdccc2a
Child:
1:7f0e80fcb3a0
Initial revision

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