Helium
/
helium_basic
A basic setup and connect sample for the Helium Atom
Diff: main.cpp
- Revision:
- 13:90f88e66f0aa
- Parent:
- 10:e666766643d2
--- 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); + } }