test demo

Dependencies:   PinDetect libmDot mbed-rtos mbed-src

Fork of mDot_LoRa_Connect_Woodstream_Demo by James Coleman

Committer:
tmulrooney
Date:
Sat Nov 07 01:15:29 2015 +0000
Revision:
2:a61b09bc91ea
Parent:
1:535366982662
Child:
3:34d92fdfbe06
three channels working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jlcolemanmbed 0:5fa2b08cb3e0 1 /**********************************************************************************
jlcolemanmbed 0:5fa2b08cb3e0 2 * This program monitors the power on state, kill state and battery voltage on
jlcolemanmbed 0:5fa2b08cb3e0 3 * Woodstream mouse trap using the Multitech mDot and UDK2 development system.
jlcolemanmbed 0:5fa2b08cb3e0 4 * The power on state is monitored on the PA_4(UDK2 Pin D10)and the kill state is
jlcolemanmbed 0:5fa2b08cb3e0 5 * monitored on the PA_5 (UDK2 Pin D13) Digital Input Pins. The battery voltage
jlcolemanmbed 0:5fa2b08cb3e0 6 * is monitored on the PB_1 (UDK2 Pin A0) Analog Input. The status of these pins
jlcolemanmbed 0:5fa2b08cb3e0 7 * are transferred from the mDot LoRa to the Conduit LoRa gateway. The status is
jlcolemanmbed 0:5fa2b08cb3e0 8 * also shown on the USB Debug Terminal Window.
jlcolemanmbed 0:5fa2b08cb3e0 9 *********************************************************************************/
jlcolemanmbed 0:5fa2b08cb3e0 10
jlcolemanmbed 0:5fa2b08cb3e0 11 #include "mbed.h"
jlcolemanmbed 0:5fa2b08cb3e0 12 #include "mDot.h"
jlcolemanmbed 0:5fa2b08cb3e0 13 #include "MTSLog.h"
jlcolemanmbed 0:5fa2b08cb3e0 14 #include <string>
jlcolemanmbed 0:5fa2b08cb3e0 15 #include <vector>
jlcolemanmbed 0:5fa2b08cb3e0 16 #include <algorithm>
jlcolemanmbed 0:5fa2b08cb3e0 17 #include "PinDetect.h"
jlcolemanmbed 0:5fa2b08cb3e0 18
tmulrooney 1:535366982662 19 #define MIN_CHANGE_BATTERY_VOLTAGE 0
tmulrooney 2:a61b09bc91ea 20 #define BATTERY_TIME 2.0
tmulrooney 2:a61b09bc91ea 21 #define KILL_STATUS_TIME 2.0
tmulrooney 2:a61b09bc91ea 22 #define POWER_ON_TIME 2.0
tmulrooney 1:535366982662 23
jlcolemanmbed 0:5fa2b08cb3e0 24 mDot* dot;
tmulrooney 1:535366982662 25 Ticker ledTimer;
tmulrooney 1:535366982662 26 Ticker batteryTimer;
tmulrooney 2:a61b09bc91ea 27 Ticker killStatusTimer;
tmulrooney 2:a61b09bc91ea 28 Ticker powerOnTimer;
jlcolemanmbed 0:5fa2b08cb3e0 29
tmulrooney 1:535366982662 30 AnalogIn batteryVoltage(PB_1);
tmulrooney 2:a61b09bc91ea 31 DigitalIn powerOn(PA_4);
tmulrooney 2:a61b09bc91ea 32 //DigitalIn KillStatus(PA_5);
tmulrooney 1:535366982662 33 DigitalOut transmitLED(PA_0);
jlcolemanmbed 0:5fa2b08cb3e0 34
jlcolemanmbed 0:5fa2b08cb3e0 35 // Configuration variables
tmulrooney 1:535366982662 36 static std::string config_network_name = "LORA_NET";
tmulrooney 1:535366982662 37 static std::string config_network_pass = "BLUE_HEN1";
jlcolemanmbed 0:5fa2b08cb3e0 38 static uint8_t config_frequency_sub_band = 7;
jlcolemanmbed 0:5fa2b08cb3e0 39
jlcolemanmbed 0:5fa2b08cb3e0 40 //Global Variables
tmulrooney 1:535366982662 41 bool readyToSend;
tmulrooney 1:535366982662 42 uint16_t lastBatteryVoltage;
tmulrooney 1:535366982662 43 float currentBatteryVoltage;
tmulrooney 2:a61b09bc91ea 44 uint8_t lastKillStatus;
tmulrooney 2:a61b09bc91ea 45 uint8_t lastPowerOn;
jlcolemanmbed 0:5fa2b08cb3e0 46
jlcolemanmbed 0:5fa2b08cb3e0 47 //Function prototypes
tmulrooney 1:535366982662 48 void ledWrite();
jlcolemanmbed 0:5fa2b08cb3e0 49 void periodicSendTock();
tmulrooney 1:535366982662 50 void batteryRead();
tmulrooney 2:a61b09bc91ea 51 void killStatusRead();
tmulrooney 2:a61b09bc91ea 52 void powerOnRead();
jlcolemanmbed 0:5fa2b08cb3e0 53 void printError(mDot* dot, int32_t returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 54 void printVersion();
jlcolemanmbed 0:5fa2b08cb3e0 55 bool setFrequencySubBand(uint8_t subBand);
jlcolemanmbed 0:5fa2b08cb3e0 56 bool setNetworkName(const std::string name);
jlcolemanmbed 0:5fa2b08cb3e0 57 bool setNetworkPassphrase(const std::string passphrase);
tmulrooney 1:535366982662 58 bool setTxDataRate(const int dataRate);
jlcolemanmbed 0:5fa2b08cb3e0 59 bool setPower(uint8_t power);
jlcolemanmbed 0:5fa2b08cb3e0 60 bool setAck(uint8_t retries);
jlcolemanmbed 0:5fa2b08cb3e0 61 bool joinNetwork();
jlcolemanmbed 0:5fa2b08cb3e0 62 bool send(const std::string text);
tmulrooney 1:535366982662 63 char latestData[100];
jlcolemanmbed 0:5fa2b08cb3e0 64
jlcolemanmbed 0:5fa2b08cb3e0 65
jlcolemanmbed 0:5fa2b08cb3e0 66 int main()
jlcolemanmbed 0:5fa2b08cb3e0 67 {
tmulrooney 1:535366982662 68 bool configFail = false;
tmulrooney 1:535366982662 69 readyToSend = false;
tmulrooney 1:535366982662 70 lastBatteryVoltage = 0;
tmulrooney 1:535366982662 71 currentBatteryVoltage = 0.0;
tmulrooney 2:a61b09bc91ea 72 lastKillStatus = transmitLED;
tmulrooney 2:a61b09bc91ea 73 lastPowerOn = powerOn;
tmulrooney 2:a61b09bc91ea 74
jlcolemanmbed 0:5fa2b08cb3e0 75 //Start LED startup sequence
tmulrooney 2:a61b09bc91ea 76 ledTimer.attach(&ledWrite, 1.5);
jlcolemanmbed 0:5fa2b08cb3e0 77
jlcolemanmbed 0:5fa2b08cb3e0 78 printf("\r\n\r\n");
jlcolemanmbed 0:5fa2b08cb3e0 79 printf("=====================================\r\n");
jlcolemanmbed 0:5fa2b08cb3e0 80 printf("WoodStream LoRa Mousetrap Demo \r\n");
jlcolemanmbed 0:5fa2b08cb3e0 81 printf("=====================================\r\n");
jlcolemanmbed 0:5fa2b08cb3e0 82 printVersion();
jlcolemanmbed 0:5fa2b08cb3e0 83
jlcolemanmbed 0:5fa2b08cb3e0 84 // get the mDot handle
tmulrooney 1:535366982662 85 dot = mDot::getInstance();
jlcolemanmbed 0:5fa2b08cb3e0 86 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
jlcolemanmbed 0:5fa2b08cb3e0 87
tmulrooney 1:535366982662 88 //*******************************************
tmulrooney 1:535366982662 89 // configuration
tmulrooney 1:535366982662 90 //*******************************************
jlcolemanmbed 0:5fa2b08cb3e0 91 // reset to default config so we know what state we're in
jlcolemanmbed 0:5fa2b08cb3e0 92 dot->resetNetworkSession();
jlcolemanmbed 0:5fa2b08cb3e0 93 dot->resetConfig();
jlcolemanmbed 0:5fa2b08cb3e0 94
tmulrooney 1:535366982662 95 // set up the mDot with our network information: frequency sub band, network name, and network password
tmulrooney 1:535366982662 96 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
tmulrooney 1:535366982662 97 configFail = !setFrequencySubBand(config_frequency_sub_band);
tmulrooney 1:535366982662 98 configFail |= !setNetworkName(config_network_name);
tmulrooney 1:535366982662 99 configFail |= !setNetworkPassphrase(config_network_pass);
tmulrooney 1:535366982662 100 configFail |= !setTxDataRate(mDot::SF_8);
tmulrooney 1:535366982662 101 configFail |= !setPower(20); // Reduce latency for 868 units
tmulrooney 1:535366982662 102 configFail |= !setAck(0); // Disable ack for less latency
tmulrooney 1:535366982662 103
tmulrooney 1:535366982662 104 printf("Configration %s\r\n",configFail?"Failed":"Passed");
tmulrooney 1:535366982662 105
tmulrooney 1:535366982662 106 // save this configuration to the mDot's NVM
tmulrooney 1:535366982662 107 printf("saving config\r\n");
tmulrooney 1:535366982662 108 if (! dot->saveConfig())
tmulrooney 1:535366982662 109 {
tmulrooney 1:535366982662 110 logError("failed to save configuration");
tmulrooney 1:535366982662 111 }
tmulrooney 1:535366982662 112 //*******************************************
tmulrooney 1:535366982662 113 // end of configuration
tmulrooney 1:535366982662 114 //*******************************************
tmulrooney 1:535366982662 115
tmulrooney 1:535366982662 116 // keep trying to join the network
tmulrooney 1:535366982662 117 while (!joinNetwork())
tmulrooney 1:535366982662 118 {
tmulrooney 1:535366982662 119 wait(200);
tmulrooney 1:535366982662 120 dot->resetNetworkSession();
tmulrooney 1:535366982662 121 }
jlcolemanmbed 0:5fa2b08cb3e0 122
jlcolemanmbed 0:5fa2b08cb3e0 123 // Stop LED startup sequence & configure them for operation
jlcolemanmbed 0:5fa2b08cb3e0 124 transmitLED = 1;
tmulrooney 2:a61b09bc91ea 125 batteryTimer.attach(batteryRead, BATTERY_TIME);
tmulrooney 2:a61b09bc91ea 126 killStatusTimer.attach(killStatusRead, KILL_STATUS_TIME);
tmulrooney 2:a61b09bc91ea 127 powerOnTimer.attach(powerOnRead, POWER_ON_TIME);
jlcolemanmbed 0:5fa2b08cb3e0 128
tmulrooney 1:535366982662 129 while (1)
tmulrooney 1:535366982662 130 {
tmulrooney 1:535366982662 131 // is there anything to send
tmulrooney 1:535366982662 132 if(readyToSend)
tmulrooney 1:535366982662 133 {
tmulrooney 2:a61b09bc91ea 134 sprintf(latestData,"%2.2f %d %d",(double)currentBatteryVoltage,lastKillStatus,lastPowerOn);
tmulrooney 2:a61b09bc91ea 135 printf("Battery Voltage: %2.2fV Kill Status: %d Power: %d\r\n",(double)currentBatteryVoltage,lastKillStatus,lastPowerOn);
tmulrooney 1:535366982662 136 send(latestData);
tmulrooney 1:535366982662 137 readyToSend = false;
jlcolemanmbed 0:5fa2b08cb3e0 138 }
tmulrooney 1:535366982662 139 }
jlcolemanmbed 0:5fa2b08cb3e0 140 }
jlcolemanmbed 0:5fa2b08cb3e0 141
tmulrooney 1:535366982662 142 void ledWrite()
tmulrooney 1:535366982662 143 {
tmulrooney 1:535366982662 144 transmitLED = !transmitLED;
tmulrooney 1:535366982662 145 }
tmulrooney 1:535366982662 146
tmulrooney 1:535366982662 147 void batteryRead()
tmulrooney 1:535366982662 148 {
tmulrooney 1:535366982662 149 uint16_t battery,diff;
tmulrooney 1:535366982662 150
tmulrooney 1:535366982662 151 battery = batteryVoltage.read_u16();
tmulrooney 1:535366982662 152 if(battery > lastBatteryVoltage)
tmulrooney 1:535366982662 153 diff = battery - lastBatteryVoltage;
tmulrooney 1:535366982662 154 else
tmulrooney 1:535366982662 155 diff = lastBatteryVoltage - battery;
tmulrooney 1:535366982662 156 if(diff > MIN_CHANGE_BATTERY_VOLTAGE)
tmulrooney 1:535366982662 157 {
tmulrooney 1:535366982662 158 lastBatteryVoltage = battery;
tmulrooney 1:535366982662 159 currentBatteryVoltage = batteryVoltage * (float)3.3;
tmulrooney 1:535366982662 160 readyToSend = true;
jlcolemanmbed 0:5fa2b08cb3e0 161 }
jlcolemanmbed 0:5fa2b08cb3e0 162 }
jlcolemanmbed 0:5fa2b08cb3e0 163
tmulrooney 2:a61b09bc91ea 164 void killStatusRead()
tmulrooney 2:a61b09bc91ea 165 {
tmulrooney 2:a61b09bc91ea 166 if(transmitLED != lastKillStatus)
tmulrooney 2:a61b09bc91ea 167 {
tmulrooney 2:a61b09bc91ea 168 lastKillStatus = transmitLED;
tmulrooney 2:a61b09bc91ea 169 readyToSend = true;
tmulrooney 2:a61b09bc91ea 170 }
tmulrooney 2:a61b09bc91ea 171 }
tmulrooney 2:a61b09bc91ea 172
tmulrooney 2:a61b09bc91ea 173 void powerOnRead()
tmulrooney 2:a61b09bc91ea 174 {
tmulrooney 2:a61b09bc91ea 175 if(powerOn != lastPowerOn)
tmulrooney 2:a61b09bc91ea 176 {
tmulrooney 2:a61b09bc91ea 177 lastPowerOn = powerOn;
tmulrooney 2:a61b09bc91ea 178 readyToSend = true;
tmulrooney 2:a61b09bc91ea 179 }
tmulrooney 2:a61b09bc91ea 180 }
tmulrooney 2:a61b09bc91ea 181
jlcolemanmbed 0:5fa2b08cb3e0 182 void printVersion()
jlcolemanmbed 0:5fa2b08cb3e0 183 {
tmulrooney 1:535366982662 184 printf("%s Built on: %s %s\r\n\r\n", dot->getId().c_str(),__DATE__,__TIME__);
jlcolemanmbed 0:5fa2b08cb3e0 185 }
jlcolemanmbed 0:5fa2b08cb3e0 186
jlcolemanmbed 0:5fa2b08cb3e0 187 bool setFrequencySubBand(uint8_t subBand)
jlcolemanmbed 0:5fa2b08cb3e0 188 {
jlcolemanmbed 0:5fa2b08cb3e0 189 int32_t returnCode;
tmulrooney 1:535366982662 190
jlcolemanmbed 0:5fa2b08cb3e0 191 printf("Setting frequency sub band to '%d'...\r\n", subBand);
jlcolemanmbed 0:5fa2b08cb3e0 192 if ((returnCode = dot->setFrequencySubBand(subBand)) != mDot::MDOT_OK) {
jlcolemanmbed 0:5fa2b08cb3e0 193 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 194 return false;
jlcolemanmbed 0:5fa2b08cb3e0 195 }
jlcolemanmbed 0:5fa2b08cb3e0 196 return true;
jlcolemanmbed 0:5fa2b08cb3e0 197 }
jlcolemanmbed 0:5fa2b08cb3e0 198
jlcolemanmbed 0:5fa2b08cb3e0 199 bool setNetworkName(const std::string name)
jlcolemanmbed 0:5fa2b08cb3e0 200 {
jlcolemanmbed 0:5fa2b08cb3e0 201 int32_t returnCode;
tmulrooney 1:535366982662 202
jlcolemanmbed 0:5fa2b08cb3e0 203 printf("Setting network name to '%s'...\r\n", name.c_str());
jlcolemanmbed 0:5fa2b08cb3e0 204 if ((returnCode = dot->setNetworkName(name)) != mDot::MDOT_OK)
jlcolemanmbed 0:5fa2b08cb3e0 205 {
jlcolemanmbed 0:5fa2b08cb3e0 206 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 207 return false;
jlcolemanmbed 0:5fa2b08cb3e0 208 }
jlcolemanmbed 0:5fa2b08cb3e0 209 return true;
jlcolemanmbed 0:5fa2b08cb3e0 210 }
jlcolemanmbed 0:5fa2b08cb3e0 211
jlcolemanmbed 0:5fa2b08cb3e0 212 bool setNetworkPassphrase(const std::string passphrase)
jlcolemanmbed 0:5fa2b08cb3e0 213 {
jlcolemanmbed 0:5fa2b08cb3e0 214 int32_t returnCode;
tmulrooney 1:535366982662 215
jlcolemanmbed 0:5fa2b08cb3e0 216 printf("Setting passphrase to '%s'...\r\n", passphrase.c_str());
jlcolemanmbed 0:5fa2b08cb3e0 217 if ((returnCode = dot->setNetworkPassphrase(passphrase)) != mDot::MDOT_OK)
jlcolemanmbed 0:5fa2b08cb3e0 218 {
jlcolemanmbed 0:5fa2b08cb3e0 219 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 220 return false;
jlcolemanmbed 0:5fa2b08cb3e0 221 }
jlcolemanmbed 0:5fa2b08cb3e0 222 return true;
jlcolemanmbed 0:5fa2b08cb3e0 223 }
jlcolemanmbed 0:5fa2b08cb3e0 224
tmulrooney 1:535366982662 225 bool setTxDataRate(const int dataRate)
tmulrooney 1:535366982662 226 {
tmulrooney 1:535366982662 227 int32_t returnCode;
tmulrooney 1:535366982662 228
tmulrooney 1:535366982662 229 printf("Setting TX Spreading factor to %d...\r\n",dataRate);
tmulrooney 1:535366982662 230 if ((returnCode = dot->setTxDataRate(dataRate)) != mDot::MDOT_OK)
tmulrooney 1:535366982662 231 {
tmulrooney 1:535366982662 232 logError("Failed To Set Tx Datarate %d:%s", returnCode, mDot::getReturnCodeString(returnCode).c_str());
tmulrooney 1:535366982662 233 printError(dot, returnCode);
tmulrooney 1:535366982662 234 return false;
tmulrooney 1:535366982662 235 }
tmulrooney 1:535366982662 236 return true;
tmulrooney 1:535366982662 237 }
tmulrooney 1:535366982662 238
jlcolemanmbed 0:5fa2b08cb3e0 239 bool setPower(uint8_t power)
jlcolemanmbed 0:5fa2b08cb3e0 240 {
jlcolemanmbed 0:5fa2b08cb3e0 241 int32_t returnCode;
jlcolemanmbed 0:5fa2b08cb3e0 242 printf("Setting tx power to '%d'...\r\n", power);
tmulrooney 1:535366982662 243 if ((returnCode = dot->setTxPower(power)) != mDot::MDOT_OK)
tmulrooney 1:535366982662 244 {
jlcolemanmbed 0:5fa2b08cb3e0 245 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 246 return false;
jlcolemanmbed 0:5fa2b08cb3e0 247 }
jlcolemanmbed 0:5fa2b08cb3e0 248 return true;
jlcolemanmbed 0:5fa2b08cb3e0 249 }
jlcolemanmbed 0:5fa2b08cb3e0 250
jlcolemanmbed 0:5fa2b08cb3e0 251 bool joinNetwork()
jlcolemanmbed 0:5fa2b08cb3e0 252 {
jlcolemanmbed 0:5fa2b08cb3e0 253 int32_t returnCode;
jlcolemanmbed 0:5fa2b08cb3e0 254 printf("\r\nJoining network...\r\n");
tmulrooney 1:535366982662 255 if ((returnCode = dot->joinNetworkOnce()) != mDot::MDOT_OK)
tmulrooney 1:535366982662 256 {
jlcolemanmbed 0:5fa2b08cb3e0 257 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 258 return false;
jlcolemanmbed 0:5fa2b08cb3e0 259 }
jlcolemanmbed 0:5fa2b08cb3e0 260 printf("Network Joined!\r\n");
jlcolemanmbed 0:5fa2b08cb3e0 261 return true;
jlcolemanmbed 0:5fa2b08cb3e0 262 }
jlcolemanmbed 0:5fa2b08cb3e0 263
jlcolemanmbed 0:5fa2b08cb3e0 264 bool setAck(uint8_t retries)
jlcolemanmbed 0:5fa2b08cb3e0 265 {
jlcolemanmbed 0:5fa2b08cb3e0 266 int32_t returnCode;
jlcolemanmbed 0:5fa2b08cb3e0 267 printf("Setting ack to '%d'...\r\n", retries);
jlcolemanmbed 0:5fa2b08cb3e0 268 if ((returnCode = dot->setAck(retries)) != mDot::MDOT_OK)
jlcolemanmbed 0:5fa2b08cb3e0 269 {
jlcolemanmbed 0:5fa2b08cb3e0 270 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 271 return false;
jlcolemanmbed 0:5fa2b08cb3e0 272 }
jlcolemanmbed 0:5fa2b08cb3e0 273 return true;
jlcolemanmbed 0:5fa2b08cb3e0 274 }
jlcolemanmbed 0:5fa2b08cb3e0 275
jlcolemanmbed 0:5fa2b08cb3e0 276 bool send(const std::string text)
jlcolemanmbed 0:5fa2b08cb3e0 277 {
jlcolemanmbed 0:5fa2b08cb3e0 278 int32_t returnCode;
jlcolemanmbed 0:5fa2b08cb3e0 279 uint32_t timeTillSend = dot->getNextTxMs();
jlcolemanmbed 0:5fa2b08cb3e0 280 if (timeTillSend != 0) {
jlcolemanmbed 0:5fa2b08cb3e0 281 printf("waiting %lu ms to send\r\n", timeTillSend);
jlcolemanmbed 0:5fa2b08cb3e0 282 return false;
jlcolemanmbed 0:5fa2b08cb3e0 283 }
jlcolemanmbed 0:5fa2b08cb3e0 284
jlcolemanmbed 0:5fa2b08cb3e0 285 printf("Sending data... ");
jlcolemanmbed 0:5fa2b08cb3e0 286 std::vector<uint8_t> data(text.begin(), text.end());
jlcolemanmbed 0:5fa2b08cb3e0 287 if ((returnCode = dot->send(data, 1)) != mDot::MDOT_OK)
jlcolemanmbed 0:5fa2b08cb3e0 288 {
jlcolemanmbed 0:5fa2b08cb3e0 289 printError(dot, returnCode);
jlcolemanmbed 0:5fa2b08cb3e0 290 return false;
jlcolemanmbed 0:5fa2b08cb3e0 291 }
jlcolemanmbed 0:5fa2b08cb3e0 292 printf("Data sent!\r\n");
jlcolemanmbed 0:5fa2b08cb3e0 293 return true;
jlcolemanmbed 0:5fa2b08cb3e0 294 }
jlcolemanmbed 0:5fa2b08cb3e0 295
jlcolemanmbed 0:5fa2b08cb3e0 296 void printError(mDot* dot, int32_t returnCode)
jlcolemanmbed 0:5fa2b08cb3e0 297 {
jlcolemanmbed 0:5fa2b08cb3e0 298 std::string error = mDot::getReturnCodeString(returnCode) + " - " + dot->getLastError();
jlcolemanmbed 0:5fa2b08cb3e0 299 printf("%s\r\n", error.c_str());
jlcolemanmbed 0:5fa2b08cb3e0 300 }