updated

Dependencies:   MTS-Serial libmDot mbed-rtos mbed

Fork of mDot_LoRa_Street_light by HM_IOT_Demo

Revision:
2:20a5d4681abf
Parent:
1:c999b29cd8ad
Child:
6:db60638d7f9a
--- a/main.cpp	Fri Jul 15 17:50:26 2016 +0000
+++ b/main.cpp	Tue Jan 31 12:56:05 2017 +0000
@@ -1,109 +1,113 @@
 #include "mbed.h"
 #include "mDot.h"
+#include "wrapper.h"
 #include "MTSLog.h"
 #include <string>
-#include <vector>
-#include <algorithm>
+#include"Thread.h"
+//#include <vector>
+//#include <algorithm>
 
 // these options must match the settings on your Conduit
 // uncomment the following lines and edit their values to match your configuration
-static std::string config_network_name = "redelora";
-static std::string config_network_pass = "senhalora";
-static uint8_t config_frequency_sub_band = 7;
+char *config_network_name = "pathfinder";
+char *config_network_pass = "pathfinder";
+static int config_frequency_sub_band = 7;
+
+//void startLoraTask(void);
+//void startZigbeeTask(void);
+//void startBLETask(void);
 
-int main() {
-    int32_t ret;
-    mDot* dot;
-    std::vector<uint8_t> data;
-    std::string data_str = "hello!";
+//void loraTask(void const *args);
+//void zigbeeTask(void const *args);
+//void bleTask(void const *args);
+
+void receive_data_from_gateway(void const *args);
+void send_data_to_gateway(void const *args);
+
+int main() 
+{
+    printf("starting main thread\r\n");
     
-    // get a mDot handle
-    dot = mDot::getInstance();
+    int ret;
+    //printf("lora task started\r\n");
+    printf("Initializing Lora mdot lib\r\n");   
+    init_mdot();
     
-    // print library version information
-    logInfo("version: %s", dot->getId().c_str());
-
-    //*******************************************
-    // configuration
-    //*******************************************
-    // reset to default config so we know what state we're in
-    dot->resetConfig();
+    printf("Resetting Lora mdot config\r\n");
+    reset_config();
     
-    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
-
+    printf("Setting Log info lavel\r\n");
+    set_log_lavel(4);
+    
+    //set_public_network(false);
+  
     // set up the mDot with our network information: frequency sub band, network name, and network password
     // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
     
     // frequency sub band is only applicable in the 915 (US) frequency band
     // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
     // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
-    
-//    logInfo("setting to public network");
-//    if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
-//        logError("failed to set public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-//    }
-
-    logInfo("setting frequency sub band");    
-    if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
-        logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+     
+    printf("setting frequency sub band %d\r\n", config_frequency_sub_band);
+    ret = set_frequency_sub_band(config_frequency_sub_band);
+    if (ret < 0) {
+        logError("failed to set frequency sub band");
     }
     
-    logInfo("setting network name");
-    if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
-        logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    printf("setting network name: %s\r\n", config_network_name);
+    ret = set_network_name(config_network_name);
+    if (ret < 0) {
+        logError("failed to set network name");
     }
     
-    logInfo("setting network password");
-    if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
-        logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    printf("setting network password: %s\r\n", config_network_pass);
+    ret = set_network_passphrase(config_network_pass);
+    if (ret < 0) {
+        logError("failed to set network password");
     }
     
+    set_txpower(14);
+    
     // a higher spreading factor allows for longer range but lower throughput
     // in the 915 (US) frequency band, spreading factors 7 - 10 are available
     // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
-    logInfo("setting TX spreading factor");
-    if ((ret = dot->setTxDataRate(mDot::SF_10)) != mDot::MDOT_OK) {
-        logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    printf("setting TX spreading factor\r\n");
+    ret = set_tx_datarate();
+    if (ret < 0) {
+        logError("failed to set TX datarate");
     }
     
     // request receive confirmation of packets from the gateway
-    logInfo("enabling ACKs");
-    if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
-        logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+    printf("enabling ACKs\r\n");
+    ret = set_ack(true);
+    if (ret < 0) {
+        logError("failed to enable ACKs");
     }
     
     // save this configuration to the mDot's NVM
-    logInfo("saving config");
-    if (! dot->saveConfig()) {
+    printf("saving config\r\n");
+    ret = save_config();
+    if (ret < 0) {
         logError("failed to save configuration");
     }
+    
     //*******************************************
     // end of configuration
     //*******************************************
 
     // attempt to join the network
-    logInfo("joining network");
-    while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
-        logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
-        osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
+    printf("joining network\r\n");
+    ret = join_network();
+    /*if (ret < 0) {
+        printf("error in join network\r\n");
+    }*/
+    logInfo("creating thread for sending and receving data from gateway");
+  
+    Thread send_lora_thread(send_data_to_gateway);
+    Thread receive_lora_thread(receive_data_from_gateway);
+    
+    while(1) {
+            osDelay(5000);
     }
-
-    // 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 to the gateway
-        if ((ret = dot->send(data)) != mDot::MDOT_OK) {
-            logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
-        } else {
-            logInfo("successfully sent data to gateway");
-        }
-
-        // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
-        osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
-    }
-
     return 0;
 }