a Hello world to connect to The Things Network

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_LoRa_Connect_Example by MultiTech

Files at this revision

API Documentation at this revision

Comitter:
mfiore
Date:
Wed Jun 24 22:22:07 2015 +0000
Child:
1:0edc781de3cb
Commit message:
initial commit

Changed in this revision

libmDot.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-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed-src.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libmDot.lib	Wed Jun 24 22:22:07 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/MultiTech/code/libmDot/#5e805b567124
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 24 22:22:07 2015 +0000
@@ -0,0 +1,71 @@
+#include "mbed.h"
+#include "mDot.h"
+#include <string>
+#include <vector>
+
+// these options must match the settings on your Conduit in
+// /var/config/lora/lora-network-server.conf
+static std::string config_network_name = "<lora network id>";
+static std::string config_network_pass = "<lora network key>";
+static uint8_t config_frequency_sub_band = 1;
+
+void log_error(mDot* dot, const char* msg, int32_t retval);
+
+int main() {
+    int32_t ret;
+    mDot* dot;
+    std::vector<uint8_t> data;
+    std::string data_str = "hello world!";
+
+    // get a mDot handle
+    dot = mDot::getInstance();
+
+    // reset to default config so we know what state we're in
+    dot->resetConfig();
+    
+    // print library version information
+    printf("version: %s\r\n", dot->getId().c_str());
+
+    // set up the mDot with our network information
+    printf("setting frequency sub band\r\n");
+    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
+        log_error(dot, "failed to set frequency sub band", ret);
+    }
+    printf("setting network name\r\n");
+    if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
+        log_error(dot, "failed to set network name", ret);
+    }
+    printf("setting network password\r\n");
+    if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
+        log_error(dot, "failed to set network password", ret);
+    }
+
+    // attempt to join the network
+    printf("joining network\r\n");
+    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
+        log_error(dot, "failed to join network", ret);
+        wait(2);
+    }
+
+    // format data for sending to the gateway
+    for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
+        data.push_back((uint8_t) *it);
+
+    while (true) {
+        // send the data
+        // ACKs are enabled by default, so we're expecting to get one back
+        if ((ret = dot->send(data)) != mDot::MDOT_OK) {
+            log_error(dot, "failed to send", ret);
+        } else {
+            printf("successfully sent data to gateway\r\n");
+        }
+
+        wait(5);
+    }
+
+    return 0;
+}
+
+void log_error(mDot* dot, const char* msg, int32_t retval) {
+    printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Wed Jun 24 22:22:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#ef0a22cdf839
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-src.lib	Wed Jun 24 22:22:07 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-src/#30f9462b5296