Paul Staron / Mbed OS Firebase-Example

Dependencies:   NTPclient Firebase-https

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Firebase.h"
00003 #include "trng_api.h"
00004 #include "NTPclient.h"
00005 
00006 DigitalOut led1(LED1,0);
00007 DigitalOut led2(LED1,0);
00008 DigitalOut led3(LED1,0);
00009 
00010 char    dataPUT[200];       // PUT data container
00011 char    dataPOST[200];      // POST data container
00012 char    timebuff[100];      // timestamp container
00013 
00014 int     PUTdata,POSTdata,GETdata,RTCsecond,Lastsecond,RTCminute,Lastminute,RTChour,Lasthour;
00015 
00016 void    getRTC(),getSENSORS();
00017 
00018 int main()
00019 {    
00020     ThisThread::sleep_for(100);  // allow time to reset
00021     printf("\033[0m\033[2J\033[H\n  ----- Firebase Example -----\r\n\n\n");
00022     printf("Initialise!\r\n\n");  
00023      
00024     // connect to the default connection access point
00025     net = connect_to_default_network_interface();    
00026    
00027     // get NTP time and set RTC
00028     NTPclient           ntp(*net);
00029     printf("\nConnecting to NTP server..\n");    
00030     //  NTP server address, timezone offset in seconds +/-, enable DST, set RTC 
00031     if(ntp.getNTP("0.pool.ntp.org",0,1,1)){
00032         time_t seconds = time(NULL);
00033         printf("System time set by NTP: %s\n\n", ctime(&seconds));
00034         }
00035         else{printf("No NTP could not set RTC !!\n\n");
00036         }  
00037     
00038     printf("\nConnecting TLS re-use socket...!\n\n");
00039     TLSSocket* socket = new TLSSocket();
00040     startTLSreusesocket((char*)FirebaseID);
00041      
00042     printf("\nReady...!\n\n");        
00043     
00044     while(1){ 
00045         led1=1;       
00046         while(Lastsecond==RTCsecond){   // synchronise data upload with RTC             
00047             getRTC();       
00048         }
00049         Lastsecond=RTCsecond;
00050         
00051         getSENSORS();   // get sensor data.
00052         led1=0;
00053                        
00054         if(RTCsecond%10==0){PUTdata=1;}                 // PUT 10 second counter
00055         if(RTCsecond==0){GETdata=1;}                    // GET minute counter
00056         if(RTCminute==0 && RTCsecond==0){POSTdata=1;}   // POST hour counter                   
00057         
00058         if(PUTdata){    // PUT data to Firebase, initialise a data block the updates it on subsiquent PUT's
00059             led3=1;            
00060             strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
00061             strcat(FirebaseUrl, "/Test/Current/.json?auth=");   // bit in the middle to send .json data and authority
00062             strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                   
00063             if(putFirebase((char*)FirebaseUrl,(char*)dataPUT)){
00064                 printf("PUT Current okay, time: %s\n",timebuff);
00065                 }                
00066             PUTdata=0;
00067             led3=0;
00068         }                           
00069         if(POSTdata){   // POST data to Firebase, adds records           
00070             led3=1;            
00071             strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
00072             strcat(FirebaseUrl, "/Test/History/.json?auth=");   // bit in the middle to send .json data and authority
00073             strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                
00074             if(postFirebase((char*)FirebaseUrl,(char*)dataPOST)){
00075                  printf("POST History okay, time: %s\n",timebuff);
00076                  }     
00077             POSTdata=0;
00078             led3=0;
00079         }             
00080         if(GETdata){    // retrieve data from Firebase
00081             led2=1;            
00082             strcpy(FirebaseUrl, FirebaseID);                    // Firebase account ID
00083             strcat(FirebaseUrl, "/Mill/Current/.json?auth=");   // bit in the middle to send .json data and authority
00084             strcat(FirebaseUrl, FirebaseAuth);                  // Firebase account authorisation key                                     
00085             printf("\nGET current...  time: %s\n",timebuff);
00086             printf("%s \n\n",getFirebase((char*)FirebaseUrl));
00087             GETdata=0;
00088             led2=0;
00089         }                                                 
00090     }       
00091 }
00092 
00093 void getSENSORS()
00094 {
00095     //  dummy sensor values, change these if you add real sensors eg, BME280, DS1820 etc. 
00096     // STM32F7 trng   
00097     //RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;  // Enable RNG clock source 
00098     //RNG->CR |= RNG_CR_RNGEN;            // RNG Peripheral enable 
00099     //while (!(RNG->SR & (RNG_SR_DRDY))); // Wait until one RNG number is ready 
00100     //int num = RNG->DR;
00101     //num = abs(num%100);
00102         
00103     int num = abs(rand()%100);    
00104     //printf("RNG: %d\n",num);
00105     
00106     char    Temp[5][7] = {"23.80","72.50","65.00","45.30","30.60"};
00107     float temp1 = (num/1.3), temp2 = (num*1.13), humidity = (num), pressure = (985+num);
00108     // POST data
00109     sprintf(dataPOST,"{\"Temp0\" : \"%s\",\"Temp1\" : \"%s\",\"Temp2\" : \"%s\",\"Temp3\" : \"%s\",\"Temp4\" : \"%s\",\"Time\" : \"%s\"}",
00110     Temp[0],Temp[1],Temp[2],Temp[3],Temp[4],timebuff);
00111     // PUT data
00112     sprintf(dataPUT,"{\"TempIn\" : \"%3.2f\", \"TempOut\" : \"%3.2f\",\"Humidity\" : \"%2.2f\",\"Pressure\" : \"%4.2f\", \"Time\" : \"%s\"}",
00113     temp1,temp2,humidity,pressure,timebuff);
00114 }
00115 
00116 void getRTC()
00117 {
00118     time_t seconds = time(NULL);                                    
00119     strftime(timebuff, 3,"%H", localtime(&seconds));
00120     RTChour = atoi(timebuff);
00121     strftime(timebuff, 3,"%M", localtime(&seconds));
00122     RTCminute = atoi(timebuff);
00123     strftime(timebuff, 3,"%S", localtime(&seconds));
00124     RTCsecond = atoi(timebuff);    
00125     strftime(timebuff,50,"%H:%M:%S %m/%d/%Y ", localtime(&seconds));           
00126 }
00127 
00128