Helium / Mbed OS helium_basic

Files at this revision

API Documentation at this revision

Comitter:
Marc Nijdam
Date:
Tue Sep 05 13:57:14 2017 -0700
Parent:
12:30a33936ac22
Commit message:
Update helium library and sample

Changed in this revision

helium.lib 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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- a/helium.lib	Mon Jul 17 12:20:22 2017 -0700
+++ b/helium.lib	Tue Sep 05 13:57:14 2017 -0700
@@ -1,1 +1,1 @@
-https://github.com/helium/helium-mbed/#2fa8100772fdd609aba188d2baa2036124ad32f8
+https://github.com/helium/helium-mbed/#ce7f027ae7cb58196a5ae125c5787daf94412ce8
--- a/main.cpp	Mon Jul 17 12:20:22 2017 -0700
+++ b/main.cpp	Tue Sep 05 13:57:14 2017 -0700
@@ -3,33 +3,17 @@
  * All Rights Reserved. See LICENSE.txt for license information
  */
 
+
 #include "mbed.h"
-#include "Helium.h"
 #define __STDC_FORMAT_MACROS
 #include <inttypes.h>
 
-void report_status(int status)
-{
-    if (helium_status_OK == status) {
-        printf("Succeeded\n");
-    } else {
-        printf("Failed (status %d)\n", status);
-    }
-}
+#include "Helium.h"
+#include "HeliumUtil.h"
 
-void report_status_result(int status, int result)
-{
-    if (helium_status_OK == status) {
-        if (result == 0) {
-            printf("Succeeded\n");
-        } else {
-            printf("Failed - %d", result);
-        }
-    } else {
-        printf("Failed (status %d)\n", status);
-    }
-}
-
+#define CHANNEL_NAME "Helium MQTT"
+#define CONFIG_INTERVAL_KEY "config.interval_ms"
+#define DEFAULT_INTERVAL 500
 
 #if defined(TARGET_K64F)
 Helium helium(D9, D7); // TX:D9, RX:D7
@@ -40,33 +24,59 @@
 #endif
 
 Channel channel(&helium);
+Config config(&channel);
+int32_t send_interval;
+
+void
+update_config(bool stale)
+{
+    if (stale)
+    {
+        DBG_PRINTF("Fetching Config - ");
+        int status = config.get(CONFIG_INTERVAL_KEY, &send_interval, DEFAULT_INTERVAL);
+        report_status(status);
+    }
+}
 
 int main()
 {
-    printf("Starting\n");
+    DBG_PRINTF("Starting\n");
 
     // Let the Atom start up
     wait(0.1);
 
-    printf("Info - ");
+    // Get and print the mac address
+    DBG_PRINTF("Info - ");
     struct helium_info info;
     int status = helium.info(&info);
     if (helium_status_OK == status) {
-        printf("%" PRIx64 " - ", info.mac);
+        DBG_PRINTF("%" PRIx64 " - ", info.mac);
     }
     report_status(status);
 
-    printf("Connecting - ");
-    status = helium.connect();
-    report_status(status);
+    // Tell the Atom to connect to the network. The used HeliumUtil
+    // will keep trying to connect forever.
+    helium_connect(&helium);
+
+    // Create a channel just to get it done.. The HeliumUtil
+    // implementation of this will both connect and retry channel
+    // creation.
+    channel_create(&channel, CHANNEL_NAME);
+
+    // Fetch configuration data from channel.
+    update_config(true);
 
-    printf("Creating Channel - ");
-    int8_t result;
-    status = channel.begin("Helium MQTT", &result);
-    report_status(status);
+    while(true) {
+        // Send some data. This uses the HeliumUtil utility that both
+        // re-connects to the channel, re-connects and/or re-sends
+        // under failure conditions.
+        const char *data = "Hello Helium";
+        channel_send(&channel, CHANNEL_NAME, data, strlen(data));
 
-    printf("Sending - ");
-    const char *data = "Hello Helium";
-    status = channel.send(data, strlen(data), &result);
-    report_status_result(status, result);
+        // Update configuration if the network tells us it's stale.
+        update_config(config.is_stale());
+
+        // Wait for the configured interval
+        wait_ms(send_interval);
+    }
 }
--- a/mbed-os.lib	Mon Jul 17 12:20:22 2017 -0700
+++ b/mbed-os.lib	Tue Sep 05 13:57:14 2017 -0700
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#c9e63f14085f5751ff5ead79a7c0382d50a813a2
+https://github.com/ARMmbed/mbed-os/#db4be94693c3a873cfa0c025a5ad2e62b86a8474