test demo

Dependencies:   PinDetect libmDot mbed-rtos mbed-src

Fork of mDot_LoRa_Connect_Woodstream_Demo by James Coleman

Files at this revision

API Documentation at this revision

Comitter:
tmulrooney
Date:
Fri Nov 06 00:35:06 2015 +0000
Parent:
0:5fa2b08cb3e0
Child:
2:a61b09bc91ea
Commit message:
first draft

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Oct 28 23:53:58 2015 +0000
+++ b/main.cpp	Fri Nov 06 00:35:06 2015 +0000
@@ -16,51 +16,53 @@
 #include <algorithm>
 #include "PinDetect.h"
 
+#define MIN_CHANGE_BATTERY_VOLTAGE 0
+
 mDot* dot;
-Ticker ledTick;
-Ticker batteryvoltageMonitorTick;
-Ticker periodicSendTick;
+Ticker ledTimer;
+Ticker batteryTimer;
      
-AnalogIn batt_voltage(PB_1);
-DigitalIn mt_pwron(PA_4);
-DigitalIn mt_caught(PA_5);
-DigitalOut transmitLED(LED1);
+AnalogIn batteryVoltage(PB_1);
+//DigitalIn mt_pwron(PA_4);
+//DigitalIn mt_caught(PA_5);
+DigitalOut transmitLED(PA_0);
 
 // Configuration variables
-static std::string config_network_name = "JFETENGINEERING";
-static std::string config_network_pass = "Deboraheng";
+static std::string config_network_name = "LORA_NET";
+static std::string config_network_pass = "BLUE_HEN1";
 static uint8_t config_frequency_sub_band = 7;
 
-
 //Global Variables
-//static uint16_t setPoint = 21;       //21 C is standard room temp
-static volatile bool timeToReadBatteryVoltage = true;
-static volatile bool dataChanged = true;
-//static volatile bool thermostatActivated = false;
-//static uint16_t sentSetPoint;
+bool readyToSend;
+uint16_t lastBatteryVoltage;
+float currentBatteryVoltage;
 
 //Function prototypes
-void ledTock();
+void ledWrite();
 void periodicSendTock();
-void batteryvoltageMonitorTock();
-//void up_button_callback();
-//void down_button_callback();
+void batteryRead();
 void printError(mDot* dot, int32_t returnCode);
 void printVersion();
 bool setFrequencySubBand(uint8_t subBand);
 bool setNetworkName(const std::string name);
 bool setNetworkPassphrase(const std::string passphrase);
+bool setTxDataRate(const int dataRate);
 bool setPower(uint8_t power);
 bool setAck(uint8_t retries);
 bool joinNetwork();
 bool send(const std::string text);
+char latestData[100];
 
 
 int main()
 {
-    int32_t ret;
+    bool configFail = false;
+    readyToSend = false;
+    lastBatteryVoltage = 0;
+    currentBatteryVoltage = 0.0;
+    
     //Start LED startup sequence
-    ledTick.attach(&ledTock, 0.1);
+    ledTimer.attach(&ledWrite, 0.5);
 
     printf("\r\n\r\n");
     printf("=====================================\r\n");
@@ -69,102 +71,93 @@
     printVersion();
 
     // get the mDot handle
-    dot = mDot::getInstance();
-    
+    dot = mDot::getInstance();  
     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
 
+    //*******************************************
+    // configuration
+    //*******************************************
     // reset to default config so we know what state we're in
     dot->resetNetworkSession();
     dot->resetConfig();
 
-    // set up the mDot with our network information
-    setNetworkName(config_network_name);
-    setNetworkPassphrase(config_network_pass);
-    setFrequencySubBand(config_frequency_sub_band);
-    setPower(20);    // Reduce latency for 868 units
-    setAck(0);      // Disable ack for less latency
-
-logInfo("Setting TX Spreading factor");
-if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
-    logError("Failed To Set Tx Datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
-}
-
-    while (!joinNetwork()) { wait(2); dot->resetNetworkSession(); }
+    // 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()    
+    configFail = !setFrequencySubBand(config_frequency_sub_band);
+    configFail |= !setNetworkName(config_network_name);
+    configFail |= !setNetworkPassphrase(config_network_pass);
+    configFail |= !setTxDataRate(mDot::SF_8);
+    configFail |= !setPower(20);                                    // Reduce latency for 868 units
+    configFail |= !setAck(0);                                       // Disable ack for less latency
+    
+    printf("Configration %s\r\n",configFail?"Failed":"Passed");
+     
+    // save this configuration to the mDot's NVM
+    printf("saving config\r\n");
+    if (! dot->saveConfig())
+    {
+        logError("failed to save configuration");
+    }
+    //*******************************************
+    // end of configuration
+    //*******************************************
+  
+    // keep trying to join the network
+    while (!joinNetwork()) 
+    { 
+        wait(200); 
+        dot->resetNetworkSession(); 
+    }
 
     // Stop LED startup sequence & configure them for operation
-    ledTick.detach();
+    ledTimer.detach();
     transmitLED = 1;
-//    buttonPressLED = 1;
-
-    // Configure timers
-    periodicSendTick.attach(&periodicSendTock, 5*60);
-    batteryvoltageMonitorTick.attach(&batteryvoltageMonitorTock, 0.15);
-
-    // Setup Interrupt callback function
-//    up_button.attach_deasserted(&up_button_callback);
-//    down_button.attach_deasserted(&down_button_callback);
-
-    // Start sampling buttons using interrupts
-//    up_button.setSampleFrequency();
-//    down_button.setSampleFrequency();
+    batteryTimer.attach(&batteryRead, 2.0);
 
-int old_mt_pwron = -1;
-int old_mt_caught = -1;
-
-while (1) {
-    if (timeToReadBatteryVoltage) 
-        
-    if (mt_pwron != old_mt_pwron) {
-        old_mt_pwron = mt_pwron;
-        dataChanged = true;
-    if (mt_caught != old_mt_caught) {
-        old_mt_caught = mt_caught;
-        dataChanged = true;
+    while (1) 
+    {
+        // is there anything to send
+        if(readyToSend)
+        {       
+            sprintf(latestData,"%2.2f 100 3.2",(double)currentBatteryVoltage);
+            printf("%s\r\n",latestData);
+            send(latestData);
+            readyToSend = false;
         }
-       // printf("\n\r mt_pwron: %d, mt_caught: %d, batt_voltage: %f", old_mt_pwron, old_mt_caught, (batt_voltage*3.3));
-       
-       timeToReadBatteryVoltage = false;
+     }
 }
 
-        if (dataChanged) {
-            char latestData[100];
-            transmitLED = 1;
-            //sprintf(latestData, "temp: %d,set: %d", temperature, sentSetPoint);
-            sprintf(latestData, "power on: %d, kill status: %d, battery voltage: %f", old_mt_pwron, old_mt_caught, (batt_voltage*3.3));
-            printf("%s\r\n", latestData);
-            
-            if (send(latestData)) {
-                dataChanged = false;
-            }
-            transmitLED = 0;
-        }
+void ledWrite()
+{
+    transmitLED = !transmitLED;
+}
+
+void batteryRead() 
+{
+    uint16_t battery,diff;
+    
+    battery = batteryVoltage.read_u16();
+    if(battery > lastBatteryVoltage)
+        diff = battery - lastBatteryVoltage;
+    else
+        diff = lastBatteryVoltage - battery;
+    if(diff > MIN_CHANGE_BATTERY_VOLTAGE)
+    {
+        lastBatteryVoltage = battery;
+        currentBatteryVoltage = batteryVoltage * (float)3.3;
+        readyToSend = true;
     }
 }
 
-
-void ledTock() {
-    transmitLED = !transmitLED;
-}
-
-void periodicSendTock() {
-    dataChanged = true;
-}
-
-void batteryvoltageMonitorTock() {
-    timeToReadBatteryVoltage = true;
-
-//    if (sentSetPoint != setPoint) {
-        dataChanged = true;
-    }
-
 void printVersion()
 {
-    printf("%s\r\n\r\n", dot->getId().c_str());
+    printf("%s Built on: %s %s\r\n\r\n", dot->getId().c_str(),__DATE__,__TIME__);
 }
 
 bool setFrequencySubBand(uint8_t subBand)
 {
     int32_t returnCode;
+    
     printf("Setting frequency sub band to '%d'...\r\n", subBand);
     if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) {
         printError(dot, returnCode);
@@ -176,6 +169,7 @@
 bool setNetworkName(const std::string name)
 {
     int32_t returnCode;
+    
     printf("Setting network name to '%s'...\r\n", name.c_str());
     if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK)
     {
@@ -188,6 +182,7 @@
 bool setNetworkPassphrase(const std::string passphrase)
 {
     int32_t returnCode;
+    
     printf("Setting passphrase to '%s'...\r\n", passphrase.c_str());
     if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK)
     {
@@ -197,11 +192,26 @@
     return true;
 }
 
+bool setTxDataRate(const int dataRate)
+{
+   int32_t returnCode;
+   
+    printf("Setting TX Spreading factor to %d...\r\n",dataRate);
+    if ((returnCode = dot->setTxDataRate(dataRate)) != mDot::MDOT_OK) 
+    {
+        logError("Failed To Set Tx Datarate %d:%s", returnCode, mDot::getReturnCodeString(returnCode).c_str());
+        printError(dot, returnCode);
+        return false;
+    }
+    return true;
+}
+
 bool setPower(uint8_t power)
 {
     int32_t returnCode;
     printf("Setting tx power to '%d'...\r\n", power);
-    if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) {
+    if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) 
+    {
         printError(dot, returnCode);
         return false;
     }
@@ -212,7 +222,8 @@
 {
     int32_t returnCode;
     printf("\r\nJoining network...\r\n");
-    if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
+    if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) 
+    {
         printError(dot, returnCode);
         return false;
     }