Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

Files at this revision

API Documentation at this revision

Comitter:
xinlei
Date:
Wed Feb 25 10:06:11 2015 +0000
Parent:
75:d4535238db18
Child:
77:f6717e4eccc4
Commit message:
watchdog: added watchdog;

Changed in this revision

C027_Support.lib Show annotated file Show diff for this revision Revisions of this file
MbedAgent.cpp Show annotated file Show diff for this revision Revisions of this file
MbedAgent.h Show annotated file Show diff for this revision Revisions of this file
config/ConfigurationProperties.h Show annotated file Show diff for this revision Revisions of this file
config/ConfigurationSynchronization.cpp Show annotated file Show diff for this revision Revisions of this file
io/DeviceFeedback.cpp Show annotated file Show diff for this revision Revisions of this file
io/GPSTracker.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
measurement/AccelerationMeasurement.cpp Show annotated file Show diff for this revision Revisions of this file
measurement/AnalogMeasurement.cpp Show annotated file Show diff for this revision Revisions of this file
measurement/LocationUpdate.cpp Show annotated file Show diff for this revision Revisions of this file
measurement/SignalQualityMeasurement.cpp Show annotated file Show diff for this revision Revisions of this file
measurement/TemperatureMeasurement.cpp Show annotated file Show diff for this revision Revisions of this file
watchdog.h Show annotated file Show diff for this revision Revisions of this file
--- a/C027_Support.lib	Wed Feb 18 10:19:51 2015 +0000
+++ b/C027_Support.lib	Wed Feb 25 10:06:11 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/ublox/code/C027_Support/#d8d94b73c725
+http://mbed.org/teams/ublox/code/C027_Support/#7d7ab5314caa
--- a/MbedAgent.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/MbedAgent.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -1,6 +1,7 @@
 #include "MbedAgent.h"
 #include "rtos.h"
 #include "logging.h"
+#include "watchdog.h"
 
 MbedAgent::MbedAgent(DeviceIO& io, MDMSerial& mdm, DeviceInfo& deviceInfo, DeviceMemory& deviceMemory) :
     _io(io),
@@ -26,35 +27,35 @@
 {
     bool flag = true;
     if (!_integration.init()) {
-        aWarning("Init Device Integration failed.\r\n");
+        aError("Init Device Integration failed.\r\n");
         flag = false;
     }
     if (!_configurationSynchronization.init()) {
-        aWarning("Init Configuration Synchronization failed.\r\n");
+        aError("Init Configuration Synchronization failed.\r\n");
         flag = false;
     }
     if (!_signalQualityMeasurement.init()) {
-        aWarning("Init Signal Quality Measurement failed.\r\n");
+        aError("Init Signal Quality Measurement failed.\r\n");
         flag = false;
     }
     if (!_temperatureMeasurement.init()) {
-        aWarning("Init Temperature Measurement failed.\r\n");
+        aError("Init Temperature Measurement failed.\r\n");
         flag = false;
     }
     if (!_accelerationMeasurement.init()) {
-        aWarning("Init Acceleration Measurement failed.\r\n");
+        aError("Init Acceleration Measurement failed.\r\n");
         flag = false;
     }
     if (!_analogMeasurement.init()) {
-        aWarning("Init Analog Measurement failed.\r\n");
+        aError("Init Analog Measurement failed.\r\n");
         flag = false;
     }
     if (!_locationUpdate.init()) {
-        aWarning("Init Location Measurement failed.\r\n");
+        aError("Init Location Measurement failed.\r\n");
         flag = false;
     }
     if (!_operationSupport.init()) {
-        aWarning("Init Operation Support failed.\r\n");
+        aError("Init Operation Support failed.\r\n");
         flag = false;
     }
     return flag;
@@ -88,25 +89,23 @@
 
 void MbedAgent::loop()
 {
-    Timer timer; int interval;
-    
-    timer.start();
+    Watchdog wdt;
+    wdt.kick(60.0);    // set a 60.0 seconds timeout on watchdog hardware timer
     while (true) {
-        timer.reset();
-        
         _configurationSynchronization.run();
         _signalQualityMeasurement.run();
         _temperatureMeasurement.run();
+        _analogMeasurement.run();
         _accelerationMeasurement.run();
-        _analogMeasurement.run();
         _locationUpdate.run();
         _operationSupport.run();
         
-        if ((interval = _configurationProperties.readInterval()) == 0)
-            break;
-
-        while (timer.read() < interval) {
-            Thread::yield();
-        }
+//        if ((interval = _configurationProperties.readInterval()) < 0)
+//            break;
+//
+//        while (timer.read() < interval) {
+//            Thread::yield();
+//        }
+        wdt.kick();    // reset watchdog timer
     }
 }
\ No newline at end of file
--- a/MbedAgent.h	Wed Feb 18 10:19:51 2015 +0000
+++ b/MbedAgent.h	Wed Feb 25 10:06:11 2015 +0000
@@ -20,7 +20,8 @@
 #include "LocationUpdate.h"
 #include "OperationSupport.h"
 
-#define MBED_AGENT_HOST "developer.cumulocity.com"
+#define MBED_AGENT_HOST "management.m2m-devicecloud.com"
+// #define MBED_AGENT_HOST "developer.cumulocity.com"
 #define MBED_AGENT_PORT 80
 #define MBED_AGENT_DEVICE_IDENTIFIER "com_cumulocity_MbedAgent_1.5.1"
 
--- a/config/ConfigurationProperties.h	Wed Feb 18 10:19:51 2015 +0000
+++ b/config/ConfigurationProperties.h	Wed Feb 25 10:06:11 2015 +0000
@@ -4,7 +4,7 @@
 #include <stddef.h>
 #include "DeviceConfiguration.h"
 
-#define CONFIGURATION_PROPERTY_INTERVAL "300"
+#define CONFIGURATION_PROPERTY_INTERVAL "20"
 
 class ConfigurationProperties
 {
--- a/config/ConfigurationSynchronization.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/config/ConfigurationSynchronization.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -1,8 +1,9 @@
+#include <stdio.h>
 #include "ConfigurationSynchronization.h"
-#include <stdio.h>
 #include "ComposedRecord.h"
 #include "CharValue.h"
 #include "IntegerValue.h"
+#include "logging.h"
 
 ConfigurationSynchronization::ConfigurationSynchronization(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceMemory& deviceMemory, DeviceConfiguration& deviceConfiguration, ConfigurationProperties& configurationProperties) :
     _client(client),
@@ -24,6 +25,7 @@
     // Update Configuration
     // Usage: 130,<DEVICE/ID>,<CONFIG/STRING>,<RESPONSIBILITY>
     if (!_tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING NUMBER,\"{\"\"c8y_Configuration\"\":{\"\"config\"\":\"\"%%\"\"},\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n"))
+//    if (!_tpl.add("10,130,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED NUMBER,\"{\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":%%}}\"\r\n"))
         return false;
 
     _init = true;
@@ -87,28 +89,27 @@
         return false;
 
     if (_client.send(record) != SMARTREST_SUCCESS) {
-        puts("Send failed.");
+        aWarning("Sending configuration failed.");
         _client.stop();
         return false;
     }
 
     if (_client.receive(received) != SMARTREST_SUCCESS) {
-        puts("Update failed.");
+        aWarning("Receiving configuration failed.");
         _client.stop();
         return false;
     }
     _client.stop();
 
     if (received.values() != 3) {
-        puts("Bad received data.");
+        aWarning("Incomplete configuration data received.");
         return false;
     }
     
     if (received.value(0).integerValue() != 201) {
-        puts("Bad received data.");
+        aWarning("Bad configuration data received.");
         return false;
     }
-
     return true;
 }
 
--- a/io/DeviceFeedback.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/io/DeviceFeedback.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -84,35 +84,35 @@
             case MSG_CLOSE_RELAY:
                 if (!relayState) {
                     relayState = true;
-                    for (float i=2000.0; i<10000.0; i+=100) {
-                        _speaker.period(1.0/i);
-                        _speaker = 0.5;
-                        Thread::wait(20);
-                    }
-                    _speaker = 0.0;
+//                    for (float i=2000.0; i<10000.0; i+=100) {
+//                        _speaker.period(1.0/i);
+//                        _speaker = 0.5;
+//                        Thread::wait(20);
+//                    }
+//                    _speaker = 0.0;
                 }
                 break;
             case MSG_OPEN_RELAY:
                 if (relayState) {
                     relayState = false;
-                    for (float i=10000.0; i>2000.0; i-=100) {
-                        _speaker.period(1.0/i);
-                        _speaker = 0.5;
-                        Thread::wait(20);
-                    }
-                    _speaker = 0.0;
+//                    for (float i=10000.0; i>2000.0; i-=100) {
+//                        _speaker.period(1.0/i);
+//                        _speaker = 0.5;
+//                        Thread::wait(20);
+//                    }
+//                    _speaker = 0.0;
                 }
                 break;
             }
             _mail.free(msg);
         }
 
-        if (relayState) {
-            _speaker.period(1.0/10000);
-            _speaker = 0.5;
-            Thread::wait(20);
-            _speaker = 0.0;
-        }
+//        if (relayState) {
+//            _speaker.period(1.0/10000);
+//            _speaker = 0.5;
+//            Thread::wait(20);
+//            _speaker = 0.0;
+//        }
     }
 }
 
--- a/io/GPSTracker.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/io/GPSTracker.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -1,6 +1,7 @@
-#include "GPSTracker.h"
 #include <stdlib.h>
 #include <string.h>
+#include "GPSTracker.h"
+#include "logging.h"
 
 GPSTracker::GPSTracker(GPSI2C& gps) :
     _gps(gps),
@@ -40,22 +41,29 @@
         }
         
         len = LENGTH(ret);
-        if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6))
+        if ((PROTOCOL(ret) != GPSParser::NMEA) || (len <= 6)) {
+            aWarning("GPS data is not in NMEA protocol!\r\n");
             continue;
+        }
 
-        // we're only interested in fixed GPS positions
-        // we are not interested in invalid data
-        if ((strncmp("$GPGGA", buf, 6) != 0) ||
-            (!_gps.getNmeaItem(6, buf, len, n, 10)) || (n == 0))
+        // we're only interested in fixed GPS positions, data type: GPGGA
+        if ((strncmp("$GPGGA", buf, 6) != 0)) {
             continue;
+        }
+
+        if (!_gps.getNmeaItem(6, buf, len, n, 10) || n == 0) {
+            continue;
+        }
         
         // get altitude, latitude and longitude
         if ((!_gps.getNmeaAngle(2, buf, len, latitude)) ||
             (!_gps.getNmeaAngle(4, buf, len, longitude)) ||
             (!_gps.getNmeaItem(9, buf, len, altitude)) ||
             (!_gps.getNmeaItem(10, buf, len, chr)) ||
-            (chr != 'M'))
+            (chr != 'M')) {
+            aWarning("Unable to retrieve GPS data!\r\n");
             continue;
+        }
 
         _mutex.lock();
         _position.altitude = altitude;
--- a/main.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/main.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -34,28 +34,19 @@
     GPSI2C gps;
     DeviceIO io(gps);
     
-    //mdm.setDebug(4);
+    mdm.setDebug(4);
     io.lcdPrint("Device Init");
-    if (!mdm.init(SIM_PIN, &devStatus))
+    if (!mdm.init(SIM_PIN, &devStatus)) {
         status = 1;
-    else if (!gps.init())
+        io.lcdPrint("Modem Init Failure", "No SIM card found", "Or SIM has PIN code");
+    } else if (!gps.init()) {
         status = 2;
-    
-    switch (status) {
-    case 1:
-        io.lcdPrint("Modem Init Failure", "Check SIM");
-        break;
-    case 2:
         io.lcdPrint("GPS Init Failure");
-        break;
-    }
-    
+    }    
     if (status != 0)
         goto error;
     
-    
     io.lcdPrint("Register Network", "IMEI", devStatus.imei);
-
     if (!mdm.registerNet()) {
         io.lcdPrint("Network Reg Error");
         goto error;
@@ -67,12 +58,11 @@
 #else
     if (mdm.join() == NOIP) {
 #endif
-        io.lcdPrint("Network join failure");
+        io.lcdPrint("Network join failure", "Check your APN settting,", "username or password");
         goto error;
     }
     
     {
-        uint8_t tries;
         DeviceInfo deviceInfo(mdm, devStatus);
         DeviceMemory deviceMemory(mdm);
         
@@ -86,8 +76,8 @@
 //                io.deviceFeedback().beepFailure();
                 io.lcdPrint("Reset Failure");
             }
-//            Thread::wait(1000);
-            return;
+            Thread::wait(1000);
+            return 0;
         }
 
         MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
@@ -98,7 +88,7 @@
             goto error;
         }
         
-        tries = 3;
+        uint8_t tries = 3;
         do {
             io.lcdPrint("Agent Run");
             if (agent.run())
@@ -112,9 +102,11 @@
     }
     
     mdm.disconnect();
+    mdm.powerOff();
     return 0;
 
 error:
     mdm.disconnect();
+    mdm.powerOff();
     return 1;
 }
--- a/measurement/AccelerationMeasurement.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/measurement/AccelerationMeasurement.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -85,7 +85,7 @@
 
     float t_start = sendingTimer.read();
     if (_client.send(record) != SMARTREST_SUCCESS) {
-        aError("Signal measurement failed.");
+        aWarning("Sending acceleration readings failed.");
         _client.stop();
         return false;
     }
--- a/measurement/AnalogMeasurement.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/measurement/AnalogMeasurement.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -5,7 +5,7 @@
 #include "FloatValue.h"
 #include "logging.h"
 
-#define THRESHOLD_PERCENT_ANA 0.05       // Percentage cut-off for avoiding sending similar analog sensor data.
+#define THRESHOLD_PERCENT_ANA 0.03       // Percentage cut-off for avoiding sending similar analog sensor data.
 // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds).
 #define TIME_LIMIT_ANA 900               
 
@@ -79,7 +79,7 @@
 
     float t_start = sendingTimer.read();
     if (_client.send(record) != SMARTREST_SUCCESS) {
-        aError("Signal measurement failed.");
+        aWarning("Sending analog readings failed.");
         _client.stop();
         return false;
     }
--- a/measurement/LocationUpdate.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/measurement/LocationUpdate.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -100,7 +100,7 @@
 
     float t_start = sendingTimer.read();
     if (_client.send(aggregator) != SMARTREST_SUCCESS) {
-        aError("Signal measurement failed.\r\n");
+        aWarning("Sending GPS readings failed.\r\n");
         _client.stop();
         return false;
     }
--- a/measurement/SignalQualityMeasurement.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/measurement/SignalQualityMeasurement.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -82,7 +82,7 @@
 
     float t_start = sendingTimer.read();
     if (_client.send(record) != SMARTREST_SUCCESS) {
-        aError("Signal measurement failed.\r\n");
+        aWarning("Sending signal readings failed.\r\n");
         _client.stop();
         return false;
     }
--- a/measurement/TemperatureMeasurement.cpp	Wed Feb 18 10:19:51 2015 +0000
+++ b/measurement/TemperatureMeasurement.cpp	Wed Feb 25 10:06:11 2015 +0000
@@ -5,7 +5,7 @@
 #include "FloatValue.h"
 #include "logging.h"
 
-#define THRESHOLD_PERCENT_TEMP 0.05       // Percentage cut-off for avoiding sending similar temperature sensor data.
+#define THRESHOLD_PERCENT_TEMP 0.02       // Percentage cut-off for avoiding sending similar temperature sensor data.
 // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds).
 #define TIME_LIMIT_TEMP 900               
 
@@ -78,7 +78,7 @@
 
     float t_start = sendingTimer.read();
     if (_client.send(record) != SMARTREST_SUCCESS) {
-        aError("Signal measurement failed.\r\n");
+        aWarning("Sending temperature readings failed.\r\n");
         _client.stop();
         return false;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/watchdog.h	Wed Feb 25 10:06:11 2015 +0000
@@ -0,0 +1,22 @@
+#ifndef WATCHDOG_H
+#define WATCHDOG_H
+#include "mbed.h"
+
+class Watchdog {
+public:
+// Load timeout value in watchdog timer and enable
+    void kick(float s) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+        LPC_WDT->WDTC = s * (float)clk;
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+        kick();
+    }
+// "kick" or "feed" the dog - reset the watchdog timer
+// by writing this required bit pattern
+    void kick() {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
+#endif /* WATCHDOG_H */
\ No newline at end of file