Shoalhaven Water / Mbed OS Cloudtracker

Dependencies:   libmDot-mbed5 ISL29011

Files at this revision

API Documentation at this revision

Comitter:
aidanwynn
Date:
Mon Mar 01 02:47:26 2021 +0000
Parent:
45:0d541f98d058
Child:
47:2bc515eeaa18
Commit message:
OTA_examples modified to work for Cloudtracker Rain Gauge.

Changed in this revision

examples/example_config.h Show annotated file Show diff for this revision Revisions of this file
examples/src/dot_util.cpp Show annotated file Show diff for this revision Revisions of this file
examples/src/main.cpp Show annotated file Show diff for this revision Revisions of this file
examples/src/ota_example.cpp Show diff for this revision Revisions of this file
--- a/examples/example_config.h	Wed Nov 11 00:38:59 2020 +0000
+++ b/examples/example_config.h	Mon Mar 01 02:47:26 2021 +0000
@@ -24,7 +24,8 @@
 //      CP_AS923_JAPAN
 //      CP_IN865
 #if !defined(CHANNEL_PLAN)
-#define CHANNEL_PLAN CP_AS923
+//#define CHANNEL_PLAN CP_AS923
+#define CHANNEL_PLAN CP_AU915
 #endif
 
 #endif
--- a/examples/src/dot_util.cpp	Wed Nov 11 00:38:59 2020 +0000
+++ b/examples/src/dot_util.cpp	Mon Mar 01 02:47:26 2021 +0000
@@ -298,7 +298,7 @@
         if (ret != mDot::MDOT_OK) {
             logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
             // in some frequency bands we need to wait until another channel is available before transmitting again
-            uint32_t delay_s = 8;
+            uint32_t delay_s = 10;
             if (delay_s < 5) {
                 logInfo("waiting %lu s until next free channel", delay_s);
                 ThisThread::sleep_for(delay_s);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/src/main.cpp	Mon Mar 01 02:47:26 2021 +0000
@@ -0,0 +1,262 @@
+#include "dot_util.h"
+#include "RadioEvent.h"
+#include "InterruptIn.h"
+#include "Callback.h"
+
+#define CHANNEL_PLAN CP_AS923       // Uncomment for different Freq. plans.
+// #define CHANNEL_PLAN CP_AU915
+
+////// Change below //////
+static uint16_t locationID = 002;       // Change for different locations. (1 --> 65535)
+static int uplinkInterval = 1;         // Value of MINUTES between transmissions
+                                            // *This is the default interval
+                                            // **keep this is an integer value
+uint16_t rain_total = 0;
+static float batMax = 4.18;             // Change for different battery set up.
+static float rawLatitude = -34.406000;  // Change for different location 
+static float rawLongitude = 150.880429;     // left here as a potential feature
+
+//// Update if different TTN/TTI application is used. ////
+static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x3B, 0xB9 };
+static uint8_t network_key[] = { 0xBE, 0x20, 0x8D, 0x61, 0x38, 0xAE, 0xE0, 0x76, 0x47, 0xFC, 0x23, 0x9D, 0xE1, 0xD7, 0x8D, 0x40 };
+
+static std::string network_name = "MultiTech";
+static std::string network_passphrase = "MultiTech";
+static uint8_t frequency_sub_band = 0;
+static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
+static uint8_t join_delay = 5;
+static uint8_t ack = 0;
+static bool adr = true;
+
+static bool deep_sleep = false;
+I2C i2c(PC_9, PA_8 ); 
+
+mDot* dot = NULL;
+lora::ChannelPlan* plan = NULL;
+
+Serial pc(USBTX, USBRX);    
+
+// Initialize I2C
+const int addr7bit = 0x40;      // 7 bit I2C address
+const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x80
+
+volatile int _count = 0;
+
+void incrementCounter(){
+    _count++;
+    wait_us(200000);    // delay for debouncing
+}
+void voidCounter(){
+    wait_us(200000);    // delay for debouncing
+}
+int readCounter(){
+    return _count;
+}
+void clearCounter(){
+    logInfo("Setting counter to 0");
+    _count = 0;
+}
+
+int main(){
+    AnalogIn bat(A0);                   // Battery % analog input pin
+    DigitalIn tamp(PA_3);
+    
+    InterruptIn in(PB_0);           // interrupt in pin on A1, analog due to 
+                                    // nature of Rain Gauge pulse signal.
+    in.fall(&incrementCounter);     // rise OR fall
+    in.rise(&voidCounter);
+    in.mode(PullUp);
+    __enable_irq();
+    
+    RadioEvent events;
+    
+    pc.baud(115200);
+    i2c.frequency(400000);
+    
+    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
+    
+#if CHANNEL_PLAN == CP_AU915
+    plan = new lora::ChannelPlan_AU915();
+#elif CHANNEL_PLAN == CP_AS923
+    plan = new lora::ChannelPlan_AS923();
+#endif
+    assert(plan);
+
+    dot = mDot::getInstance(plan);
+    assert(dot);
+
+    // attach the custom events handler
+    dot->setEvents(&events);
+
+    if (!dot->getStandbyFlag() && !dot->getPreserveSession()) {
+        logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+
+        // start from a well-known state
+        logInfo("defaulting Dot configuration");
+        dot->resetConfig();
+        dot->resetNetworkSession();
+
+        // make sure library logging is turned on
+        dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
+
+        // update configuration if necessary
+        if (dot->getJoinMode() != mDot::OTA) {
+            logInfo("changing network join mode to OTA");
+            if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
+                logError("failed to set network join mode to OTA");
+            }
+        }
+
+        // To preserve session over power-off or reset enable this flag
+        // dot->setPreserveSession(true);
+
+        update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
+
+        // configure network link checks
+        // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
+        // check the link every count packets
+        // declare the Dot disconnected after threshold failed link checks
+        // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
+//        update_network_link_check_config(3, 5);
+        update_network_link_check_config(2, 3);
+        
+        // enable or disable Adaptive Data Rate
+        dot->setAdr(adr);
+        
+        // Configure the join delay
+        dot->setJoinDelay(join_delay);
+
+        // save changes to configuration
+        logInfo("saving configuration");
+        if (!dot->saveConfig()) {
+            logError("failed to save configuration");
+        }
+
+        // display configuration
+        display_config();
+    } else {
+        // restore the saved session if the dot woke from deepsleep mode
+        // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
+        logInfo("restoring network session from NVM");
+        dot->restoreNetworkSession();
+    }
+
+    while(true){
+        logInfo("Entering main loop!");
+        uint16_t rain;
+        uint16_t temp;
+        uint16_t humid;
+        uint32_t lat;
+        uint32_t lon;
+        uint8_t tamper;
+        
+        char cmd[2];        // I2C command address byte 8-bit
+        char my_data[2];    // I2C return address bytes 16-bit
+        
+        std::vector<uint8_t> tx_data;
+
+        //  join network if not joined
+        if (!dot->getNetworkJoinStatus()) {
+            join_network();
+        }
+        
+        // Wake sensor /////////////////////////////////////////////////////////
+        cmd[0] = 0x01;
+        cmd[1] = 0x00;
+        i2c.write(addr8bit, cmd, 2);
+        
+        // Read TEMERATURE
+        cmd[0] = 0xE3;     // For 
+        i2c.write(addr8bit, cmd, 1);
+        i2c.read(addr8bit, my_data, 2);
+        wait_us(20000);       
+        
+        uint16_t amb = (my_data[0] << 8) | my_data[1];
+        float temperature = amb;
+        temperature *= 175.72;
+        temperature /= 65536;
+        temperature -= 46.85;
+        logInfo("Temp = %4.2f*C", temperature);
+        
+        wait_us(6000);
+        
+        // Read HUMIDITY
+        cmd[0] = 0xE5;     // For
+        i2c.write(addr8bit, cmd, 1);
+        i2c.read( addr8bit, my_data, 2);
+        wait_us(20000);       
+
+        uint16_t hum = (my_data[0] << 8) | my_data[1];
+        float humidity = hum;
+        humidity *= 125;
+        humidity /= 65536;
+        humidity -= 6;
+        if(humidity > 100.00){
+            humidity = 100.00;
+        }
+        logInfo("Humidity = %4.2f%", humidity);
+        
+        // Shutdown temp sensor
+        cmd[0] = 0x00;
+        cmd[1] = 0x00;
+        i2c.write(addr8bit, cmd, 2);
+        ////////////////////////////////////////////////////////////////////////
+        
+        float battery = bat.read();
+        battery = ((3*battery)*2) / batMax * 100;   // Turn battery V to a %
+        logInfo("Battery = %f", battery);
+        if(battery > 100){
+            battery = 100;
+        }
+                    
+        // Send it to the Cloudtracker
+        rain = readCounter();   // Read the pulse count
+        rain_total += rain;
+        tx_data.push_back((rain >> 8) & 0xFF);
+        tx_data.push_back(rain & 0xFF);
+        logInfo("count = %d\n", readCounter());
+        logInfo("count_total = %d\n", rain_total);
+        clearCounter();         // Set pulse count back to 0
+        
+        uint16_t battery_send = battery * 10;
+        tx_data.push_back((battery_send >> 8) & 0xFF);
+        tx_data.push_back(battery_send & 0xFF);
+        
+        humid = humidity * 10;  
+        tx_data.push_back((humid >> 8) & 0xFF);
+        tx_data.push_back(humid & 0xFF);
+        
+        temp = temperature * 10;
+        tx_data.push_back((temp >> 8) & 0xFF);
+        tx_data.push_back(temp & 0xFF);
+        
+        tamper = 0;
+        if(tamp == 0){
+            tamper = 1;
+        }
+        tx_data.push_back(tamper & 0xFF);
+        
+        tx_data.push_back((locationID >> 8) & 0xFF);
+        tx_data.push_back(locationID & 0xFF);
+        
+        // lat = (rawLatitude * 10000) + 900000;
+        // tx_data.push_back((lat >> 16) & 0xFF);
+        // tx_data.push_back((lat >> 8) & 0xFF);
+        // tx_data.push_back(lat & 0xFF);
+        
+        // lon = (rawLongitude * 10000) + 1800000;
+        // tx_data.push_back((lon >> 16) & 0xFF);
+        // tx_data.push_back((lon >> 8) & 0xFF);
+        // tx_data.push_back(lon & 0xFF);
+        
+        tx_data.push_back((rain_total >> 8) & 0xFF);
+        tx_data.push_back(rain_total & 0xFF);
+        
+        send_data(tx_data);
+        
+        // Send to sleep for desired time. 
+        ThisThread::sleep_for((uplinkInterval * 60000) - 1750);  // (-1.75 sec for time to think)
+    }
+    
+    return 0;
+}
--- a/examples/src/ota_example.cpp	Wed Nov 11 00:38:59 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,271 +0,0 @@
-#include "dot_util.h"
-#include "RadioEvent.h"
-#include "InterruptIn.h"
-#include "Callback.h"
-
-#define CHANNEL_PLAN CP_AS923       // Uncomment for different Freq. plans.
-//#define CHANNEL_PLAN CP_AU915
-static float batMAx = 4.18;         // Change for different battery set up.
-static int uplinkInterval = 15;     // Value of MINUTES between transmissions
-static uint16_t locationID = 001;        // Change for different locations. (1 --> 65535)
-static float rawLatitude = -34.406000;      // Change for different location 
-static float rawLongitude = 150.880429;         // left here as a potential feature
-//// Update if different TTN application is used. ////
-static uint8_t network_id[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x3B, 0xB9 };
-static uint8_t network_key[] = { 0x12, 0x1A, 0x81, 0x8F, 0x10, 0x6B, 0x18, 0x67, 0x54, 0xE6, 0x9E, 0x70, 0x53, 0x7C, 0xAD, 0xCF };
-
-/////////////////////////////////////////////////////////////////////////////
-// -------------------- DOT LIBRARY REQUIRED ------------------------------//
-// * Because these example programs can be used for both mDot and xDot     //
-//     devices, the LoRa stack is not included. The libmDot library should //
-//     be imported if building for mDot devices. The libxDot library       //
-//     should be imported if building for xDot devices.                    //
-// * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/    //
-// * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/        //
-// * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/    //
-// * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/        //
-/////////////////////////////////////////////////////////////////////////////
-
-/////////////////////////////////////////////////////////////
-// * these options must match the settings on your gateway //
-// * edit their values to match your configuration         //
-// * frequency sub band is only relevant for the 915 bands //
-// * either the network name and passphrase can be used or //
-//     the network ID (8 bytes) and KEY (16 bytes)         //
-/////////////////////////////////////////////////////////////
-static std::string network_name = "MultiTech";
-static std::string network_passphrase = "MultiTech";
-static uint8_t frequency_sub_band = 0;
-static lora::NetworkType network_type = lora::PUBLIC_LORAWAN;
-static uint8_t join_delay = 5;
-static uint8_t ack = 0;
-static bool adr = true;
-
-// deepsleep consumes slightly less current than sleep
-// in sleep mode, IO state is maintained, RAM is retained, and application will resume after waking up
-// in deepsleep mode, IOs float, RAM is lost, and application will start from beginning after waking up
-// if deep_sleep == true, device will enter deepsleep mode
-static bool deep_sleep = false;
-I2C i2c(PC_9, PA_8 ); 
-
-mDot* dot = NULL;
-lora::ChannelPlan* plan = NULL;
-
-Serial pc(USBTX, USBRX);    
-
-// Initialize I2C
-const int addr7bit = 0x40;      // 7 bit I2C address
-const int addr8bit = addr7bit << 1; // 8bit I2C address, 0x80
-
-volatile int _count = 0;
-
-void incrementCounter(){
-    _count++;
-    wait_us(200000);    // delay for debouncing
-}
-void voidCounter(){
-    wait_us(200000);    // delay for debouncing
-}
-int readCounter(){
-    return _count;
-}
-void clearCounter(){
-    logInfo("Setting counter to 0");
-    _count = 0;
-}
-
-int main(){
-    AnalogIn bat(A0);       // Battery % analog input pin
-    DigitalIn tamp(PA_3);
-    
-    InterruptIn in(PA_11); 
-    in.fall(&incrementCounter);     // rise OR fall
-    in.rise(&voidCounter);
-    in.mode(PullUp);
-    __enable_irq();
-    
-    RadioEvent events;
-    
-    pc.baud(115200);
-    i2c.frequency(400000);
-    
-    mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
-    
-#if CHANNEL_PLAN == CP_AU915
-    plan = new lora::ChannelPlan_AU915();
-#elif CHANNEL_PLAN == CP_AS923
-    plan = new lora::ChannelPlan_AS923();
-#endif
-    assert(plan);
-
-    dot = mDot::getInstance(plan);
-    assert(dot);
-
-    // attach the custom events handler
-    dot->setEvents(&events);
-
-    if (!dot->getStandbyFlag() && !dot->getPreserveSession()) {
-        logInfo("mbed-os library version: %d.%d.%d", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
-
-        // start from a well-known state
-        logInfo("defaulting Dot configuration");
-        dot->resetConfig();
-        dot->resetNetworkSession();
-
-        // make sure library logging is turned on
-        dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
-
-        // update configuration if necessary
-        if (dot->getJoinMode() != mDot::OTA) {
-            logInfo("changing network join mode to OTA");
-            if (dot->setJoinMode(mDot::OTA) != mDot::MDOT_OK) {
-                logError("failed to set network join mode to OTA");
-            }
-        }
-
-        // To preserve session over power-off or reset enable this flag
-        // dot->setPreserveSession(true);
-
-        update_ota_config_id_key(network_id, network_key, frequency_sub_band, network_type, ack);
-
-        // configure network link checks
-        // network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
-        // check the link every count packets
-        // declare the Dot disconnected after threshold failed link checks
-        // for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
-        update_network_link_check_config(3, 5);
-
-        // enable or disable Adaptive Data Rate
-        dot->setAdr(adr);
-
-        // Configure the join delay
-        dot->setJoinDelay(join_delay);
-
-        // save changes to configuration
-        logInfo("saving configuration");
-        if (!dot->saveConfig()) {
-            logError("failed to save configuration");
-        }
-
-        // display configuration
-        display_config();
-    } else {
-        // restore the saved session if the dot woke from deepsleep mode
-        // useful to use with deepsleep because session info is otherwise lost when the dot enters deepsleep
-        logInfo("restoring network session from NVM");
-        dot->restoreNetworkSession();
-    }
-
-    while (true) {
-        uint16_t light;
-        uint16_t rain;
-        uint16_t temp;
-        uint16_t humid;
-        uint32_t lat;
-        uint32_t lon;
-        uint8_t tamper;
-        
-        char cmd[2];        // I2C command address byte 8-bit
-        char my_data[2];    // I2C return address bytes 16-bit
-        
-        std::vector<uint8_t> tx_data;
-
-        //  join network if not joined
-        if (!dot->getNetworkJoinStatus()) {
-            join_network();
-        }
-    
-        // Wake sensor /////////////////////////////////////////////////////////
-        cmd[0] = 0x01;
-        cmd[1] = 0x00;
-        i2c.write(addr8bit, cmd, 2);
-        
-        // Read TEMERATURE
-        cmd[0] = 0xE3;     // For 
-        i2c.write(addr8bit, cmd, 1);
-        i2c.read(addr8bit, my_data, 2);
-        wait_us(20000);       
-        
-        uint16_t amb = (my_data[0] << 8) | my_data[1];
-        float temperature = amb;
-        temperature *= 175.72;
-        temperature /= 65536;
-        temperature -= 46.85;
-        logInfo("Temp = %4.2f*C", temperature);
-        
-        wait_us(6000);
-        
-        // Read HUMIDITY
-        cmd[0] = 0xE5;     // For
-        i2c.write(addr8bit, cmd, 1);
-        i2c.read( addr8bit, my_data, 2);
-        wait_us(20000);       
-
-        uint16_t hum = (my_data[0] << 8) | my_data[1];
-        float humidity = hum;
-        humidity *= 125;
-        humidity /= 65536;
-        humidity -= 6;
-        if(humidity > 100.00){
-            humidity = 100.00;
-        }
-        logInfo("Humidity = %4.2f%", humidity);
-        
-        // Shutdown temp sensor
-        cmd[0] = 0x00;
-        cmd[1] = 0x00;
-        i2c.write(addr8bit, cmd, 2);
-        ////////////////////////////////////////////////////////////////////////
-        
-        float battery = bat.read();
-        battery = ((3*battery)*2) / batMAx * 100;   // Turn battery V to a %
-        logInfo("Battery = %f", battery);
-        if(battery > 100){
-            battery = 100;
-        }
-                    
-        // Send it to the Cloudtracker
-        rain = readCounter();   // Read the pulse count
-        tx_data.push_back((rain >> 8) & 0xFF);
-        tx_data.push_back(rain & 0xFF);
-        clearCounter();         // Set pulse count back to 0
-        
-        uint16_t battery_send = battery * 10;
-        tx_data.push_back((battery_send >> 8) & 0xFF);
-        tx_data.push_back(battery_send & 0xFF);
-        
-        humid = humidity * 10;  
-        tx_data.push_back((humid >> 8) & 0xFF);
-        tx_data.push_back(humid & 0xFF);
-        
-        temp = temperature * 10;
-        tx_data.push_back((temp >> 8) & 0xFF);
-        tx_data.push_back(temp & 0xFF);
-        
-        tamper = 0;
-        if(tamp == 0){
-            tamper = 1;
-        }
-        tx_data.push_back(tamper & 0xFF);
-        
-        tx_data.push_back((locationID >> 8) & 0xFF);
-        tx_data.push_back(locationID & 0xFF);
-        
-        // lat = (rawLatitude * 10000) + 900000;
-        // tx_data.push_back((lat >> 16) & 0xFF);
-        // tx_data.push_back((lat >> 8) & 0xFF);
-        // tx_data.push_back(lat & 0xFF);
-        
-        // lon = (rawLongitude * 10000) + 1800000;
-        // tx_data.push_back((lon >> 16) & 0xFF);
-        // tx_data.push_back((lon >> 8) & 0xFF);
-        // tx_data.push_back(lon & 0xFF);
-        
-        send_data(tx_data);
-        
-        // Send to sleep for desired time. 
-        ThisThread::sleep_for(uplinkInterval * 60000);  // seconds
-    }
-    
-    return 0;
-}