James Coleman / mDot_LoRa_Connect_Woodstream_Demo

Dependencies:   PinDetect libmDot mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**********************************************************************************
00002  * This program monitors the power on state, kill state and battery voltage on
00003  * Woodstream mouse trap using the Multitech mDot and UDK2 development system.
00004  * The power on state is monitored on the PA_4(UDK2 Pin D10)and the kill state is
00005  * monitored on the PA_5 (UDK2 Pin D13) Digital Input Pins.  The battery voltage
00006  * is monitored on the PB_1 (UDK2 Pin A0) Analog Input.  The status of these pins
00007  * are transferred from the mDot LoRa to the Conduit LoRa gateway.  The status is
00008  * also shown on the USB Debug Terminal Window.
00009  *********************************************************************************/
00010 
00011 #include "mbed.h"
00012 #include "mDot.h"
00013 #include "MTSLog.h"
00014 #include <string>
00015 #include <vector>
00016 #include <algorithm>
00017 #include "PinDetect.h"
00018 
00019 mDot* dot;
00020 Ticker ledTick;
00021 Ticker batteryvoltageMonitorTick;
00022 Ticker periodicSendTick;
00023      
00024 AnalogIn batt_voltage(PB_1);
00025 DigitalIn mt_pwron(PA_4);
00026 DigitalIn mt_caught(PA_5);
00027 DigitalOut transmitLED(LED1);
00028 
00029 // Configuration variables
00030 static std::string config_network_name = "JFETENGINEERING";
00031 static std::string config_network_pass = "Deboraheng";
00032 static uint8_t config_frequency_sub_band = 7;
00033 
00034 
00035 //Global Variables
00036 //static uint16_t setPoint = 21;       //21 C is standard room temp
00037 static volatile bool timeToReadBatteryVoltage = true;
00038 static volatile bool dataChanged = true;
00039 //static volatile bool thermostatActivated = false;
00040 //static uint16_t sentSetPoint;
00041 
00042 //Function prototypes
00043 void ledTock();
00044 void periodicSendTock();
00045 void batteryvoltageMonitorTock();
00046 //void up_button_callback();
00047 //void down_button_callback();
00048 void printError(mDot* dot, int32_t returnCode);
00049 void printVersion();
00050 bool setFrequencySubBand(uint8_t subBand);
00051 bool setNetworkName(const std::string name);
00052 bool setNetworkPassphrase(const std::string passphrase);
00053 bool setPower(uint8_t power);
00054 bool setAck(uint8_t retries);
00055 bool joinNetwork();
00056 bool send(const std::string text);
00057 
00058 
00059 int main()
00060 {
00061     int32_t ret;
00062     //Start LED startup sequence
00063     ledTick.attach(&ledTock, 0.1);
00064 
00065     printf("\r\n\r\n");
00066     printf("=====================================\r\n");
00067     printf("WoodStream LoRa Mousetrap Demo \r\n");
00068     printf("=====================================\r\n");
00069     printVersion();
00070 
00071     // get the mDot handle
00072     dot = mDot::getInstance();
00073     
00074     dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
00075 
00076     // reset to default config so we know what state we're in
00077     dot->resetNetworkSession();
00078     dot->resetConfig();
00079 
00080     // set up the mDot with our network information
00081     setNetworkName(config_network_name);
00082     setNetworkPassphrase(config_network_pass);
00083     setFrequencySubBand(config_frequency_sub_band);
00084     setPower(20);    // Reduce latency for 868 units
00085     setAck(0);      // Disable ack for less latency
00086 
00087 logInfo("Setting TX Spreading factor");
00088 if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
00089     logError("Failed To Set Tx Datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
00090 }
00091 
00092     while (!joinNetwork()) { wait(2); dot->resetNetworkSession(); }
00093 
00094     // Stop LED startup sequence & configure them for operation
00095     ledTick.detach();
00096     transmitLED = 1;
00097 //    buttonPressLED = 1;
00098 
00099     // Configure timers
00100     periodicSendTick.attach(&periodicSendTock, 5*60);
00101     batteryvoltageMonitorTick.attach(&batteryvoltageMonitorTock, 0.15);
00102 
00103     // Setup Interrupt callback function
00104 //    up_button.attach_deasserted(&up_button_callback);
00105 //    down_button.attach_deasserted(&down_button_callback);
00106 
00107     // Start sampling buttons using interrupts
00108 //    up_button.setSampleFrequency();
00109 //    down_button.setSampleFrequency();
00110 
00111 int old_mt_pwron = -1;
00112 int old_mt_caught = -1;
00113 
00114 while (1) {
00115     if (timeToReadBatteryVoltage) 
00116         
00117     if (mt_pwron != old_mt_pwron) {
00118         old_mt_pwron = mt_pwron;
00119         dataChanged = true;
00120     if (mt_caught != old_mt_caught) {
00121         old_mt_caught = mt_caught;
00122         dataChanged = true;
00123         }
00124        // printf("\n\r mt_pwron: %d, mt_caught: %d, batt_voltage: %f", old_mt_pwron, old_mt_caught, (batt_voltage*3.3));
00125        
00126        timeToReadBatteryVoltage = false;
00127 }
00128 
00129         if (dataChanged) {
00130             char latestData[100];
00131             transmitLED = 1;
00132             //sprintf(latestData, "temp: %d,set: %d", temperature, sentSetPoint);
00133             sprintf(latestData, "power on: %d, kill status: %d, battery voltage: %f", old_mt_pwron, old_mt_caught, (batt_voltage*3.3));
00134             printf("%s\r\n", latestData);
00135             
00136             if (send(latestData)) {
00137                 dataChanged = false;
00138             }
00139             transmitLED = 0;
00140         }
00141     }
00142 }
00143 
00144 
00145 void ledTock() {
00146     transmitLED = !transmitLED;
00147 }
00148 
00149 void periodicSendTock() {
00150     dataChanged = true;
00151 }
00152 
00153 void batteryvoltageMonitorTock() {
00154     timeToReadBatteryVoltage = true;
00155 
00156 //    if (sentSetPoint != setPoint) {
00157         dataChanged = true;
00158     }
00159 
00160 void printVersion()
00161 {
00162     printf("%s\r\n\r\n", dot->getId().c_str());
00163 }
00164 
00165 bool setFrequencySubBand(uint8_t subBand)
00166 {
00167     int32_t returnCode;
00168     printf("Setting frequency sub band to '%d'...\r\n", subBand);
00169     if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) {
00170         printError(dot, returnCode);
00171         return false;
00172     }
00173     return true;
00174 }
00175 
00176 bool setNetworkName(const std::string name)
00177 {
00178     int32_t returnCode;
00179     printf("Setting network name to '%s'...\r\n", name.c_str());
00180     if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK)
00181     {
00182         printError(dot, returnCode);
00183         return false;
00184     }
00185     return true;
00186 }
00187 
00188 bool setNetworkPassphrase(const std::string passphrase)
00189 {
00190     int32_t returnCode;
00191     printf("Setting passphrase to '%s'...\r\n", passphrase.c_str());
00192     if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK)
00193     {
00194         printError(dot, returnCode);
00195         return false;
00196     }
00197     return true;
00198 }
00199 
00200 bool setPower(uint8_t power)
00201 {
00202     int32_t returnCode;
00203     printf("Setting tx power to '%d'...\r\n", power);
00204     if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK) {
00205         printError(dot, returnCode);
00206         return false;
00207     }
00208     return true;
00209 }
00210 
00211 bool joinNetwork()
00212 {
00213     int32_t returnCode;
00214     printf("\r\nJoining network...\r\n");
00215     if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK) {
00216         printError(dot, returnCode);
00217         return false;
00218     }
00219     printf("Network Joined!\r\n");
00220     return true;
00221 }
00222 
00223 bool setAck(uint8_t retries)
00224 {
00225     int32_t returnCode;
00226     printf("Setting ack to '%d'...\r\n", retries);
00227     if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK)
00228     {
00229         printError(dot, returnCode);
00230         return false;
00231     }
00232     return true;
00233 }
00234 
00235 bool send(const std::string text)
00236 {
00237     int32_t returnCode;
00238     uint32_t timeTillSend = dot->getNextTxMs();
00239     if (timeTillSend != 0) {
00240         printf("waiting %lu ms to send\r\n", timeTillSend);
00241         return false;
00242     }
00243 
00244     printf("Sending data...  ");
00245     std::vector<uint8_t> data(text.begin(), text.end());
00246     if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK)
00247     {
00248         printError(dot, returnCode);
00249         return false;
00250     }
00251     printf("Data sent!\r\n");
00252     return true;
00253 }
00254 
00255 void printError(mDot* dot, int32_t returnCode)
00256 {
00257     std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError();
00258     printf("%s\r\n", error.c_str());
00259  }