Aeris Workshop program to read Moisture sensor and send the data to AerCloud using MQTT

Dependencies:   MQTT mbed-src

Files at this revision

API Documentation at this revision

Comitter:
agoel
Date:
Wed Jun 17 00:10:49 2015 +0000
Parent:
1:4e6e185783c2
Child:
3:db681a5cf46f
Commit message:
Added time function to set RTC clock.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jun 16 22:48:20 2015 +0000
+++ b/main.cpp	Wed Jun 17 00:10:49 2015 +0000
@@ -60,11 +60,11 @@
 
 //! AerCloud API Key
 //   You can find your api key at https://aerport.aeris.com"
-char _AERCLOUD_API_KEY[] = "ff767bee-f5bf-403b-badb-fde13bc2b756";
+char _AERCLOUD_API_KEY[] = "<Set Your AerCloud Account API KEY>";          // ff767bee-f5bf-403b-badb-fde13bc2b756
 
 //! Aercloud Account Id
 //   You can find your account id at https://aerport.aeris.com
-char _AERCLOUD_ACCOUNT_ID[] = "10975";
+char _AERCLOUD_ACCOUNT_ID[] = "<Set Your AerCloud Account Id>";            // 10975
 
 // AerCloud Container
 //  The name of the AerCloud Container to write data into
@@ -154,6 +154,109 @@
     logInfo("topic: [%s]\r\npayload: [%s]", topic, payload);
 }
 
+// Function to set RTC clock using the time received from Radio 
+// This is required as the board does not have a clock so every time board gets reset the RTC clock gets reset too.
+void set_current_time (Cellular* radio)
+{
+    string response = radio->sendCommand("AT+CCLK?", 1000);
+    if (response.find("+CCLK: ") == string::npos) {
+        printf("Error : Could not retrieve time \n\n\n");
+    }
+//    printf("Output from AT Command\n%s", response.c_str());
+
+    char dateString [256];
+    char timeString [256];
+    int start = response.find(':');
+    int stop = response.find(',', start);
+    string signal = response.substr(start + 2, stop - start - 2);
+    strcpy (dateString, signal.c_str());
+    string signal2 = response.substr(stop+1);
+    strcpy (timeString, signal2.c_str());
+//    printf("***** Date String  ****** %s \n\n", dateString);
+//    printf("***** Time String  ****** %s \n\n", timeString);
+
+    char dayStr[2+1];
+    char monthStr[2+1];
+    char yearStr[2+1];
+ 
+    uint32_t dayInt;
+    uint32_t monthInt;
+    uint32_t yearInt;
+ 
+    char hourStr[2+1];
+    char minuteStr[2+1];
+    char secStr[2+1];
+ 
+    uint32_t hourInt;
+    uint32_t minuteInt;
+    uint32_t secInt;
+
+    //Step 1 - Seperate the day, month and year into their own NULL terminated string buffers...
+    yearStr[0] = dateString[1];
+    yearStr[1] = dateString[2];
+    yearStr[2] = '\0';
+
+    monthStr[0] = dateString[4];
+    monthStr[1] = dateString[5];
+    monthStr[2] = '\0';
+
+    dayStr[0] = dateString[7];
+    dayStr[1] = dateString[8];
+    dayStr[2] = '\0';
+ 
+    //Step 2 - Run atoi() to parse the strings into integer values...
+    dayInt = atoi(dayStr);
+    monthInt = atoi(monthStr);
+    yearInt = atoi(yearStr) + 2000;
+ 
+    //Step 3 - Seperate the hour, minute and sec into their own NULL terminated string buffers...
+    hourStr[0] = timeString[0];
+    hourStr[1] = timeString[1];
+    hourStr[2] = '\0';
+ 
+    minuteStr[0] = timeString[3];
+    minuteStr[1] = timeString[4];
+    minuteStr[2] = '\0';
+
+    secStr[0] = timeString[6];
+    secStr[1] = timeString[7];
+    secStr[2] = '\0';
+
+    //Step 4 - Run atoi() to parse the strings into integer values...
+    hourInt = atoi(hourStr);
+    minuteInt = atoi(minuteStr);
+    secInt = atoi(secStr);
+
+    time_t rawtime;
+    struct tm * timeinfo;
+
+    // get current timeinfo and modify it to the user's choice
+    time_t ctTime;
+    ctTime = time ( &rawtime );
+    timeinfo = localtime (&rawtime);
+
+//    printf("Time before setting is (UTC): %s\r\n", ctime(&ctTime));
+
+    //Step 5 - Build up our date data structure...this will be used by the next step...
+    timeinfo->tm_mday = dayInt;
+    timeinfo->tm_mon = monthInt - 1; //num months since January - so Jan=0, Feb=1, Mar=2, etc
+    timeinfo->tm_year = yearInt - 1900; //num years since 1900
+    timeinfo->tm_hour = hourInt;
+    timeinfo->tm_min = minuteInt;
+    timeinfo->tm_sec = secInt;
+     
+//    printf("***** Year: %s, Month: %s, Day: %s ****** \n\n\n", yearStr, monthStr, dayStr);
+//    printf("***** Year: %d, Month: %d, Day: %d ****** \n\n\n", yearInt, monthInt, dayInt);
+//    printf("***** hour: %s, minute: %s, sec: %s ****** \n\n\n", hourStr, minuteStr, secStr);
+//    printf("***** hour: %d, minute: %d, sec: %d ****** \n\n\n", hourInt, minuteInt, secInt);
+
+    time_t t1=0;
+    t1 = mktime(timeinfo);
+    set_time (t1 + 7*60*60); //offseting the TimeZone Index
+    ctTime = time (NULL);
+    printf("Time set to (UTC): %s\r\n", ctime(&ctTime));
+}
+
 int main()
 {
     bool simProvisioned = false;
@@ -224,6 +327,9 @@
     simImei = (char*) sImei.c_str();
     strcpy (_AERCLOUD_DEVICE_ID, simImei);
 
+    // set RTC clock to current time using radio time
+    set_current_time (radio);
+
     // Create http client
     HTTPClient http;