This program connects to the The Things Network backend in OTAA Mode. It logs sensor values from a BME 280 to the backend. Tried adding support for Grove GPS using SerialGPS library but it is not working - conflicting with mbed-rtos, so it commented. Deep Sleep for mDot implemented BUT avoiding reprogramming of the mDot config is NOT working.

Dependencies:   BME280 SerialGPS libmDot mbed-rtos mbed

Committer:
AshuJoshi
Date:
Fri Jul 08 03:09:14 2016 +0000
Revision:
7:9e2454b0318a
Parent:
6:35f934e83c74
Child:
8:c17b68b03791
Now stable - can join the LoRa network in OTAA mode.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AshuJoshi 0:3ec6a7645098 1 /******************************************************
AshuJoshi 0:3ec6a7645098 2 * A Program to interface the Grove Base Shielf V2
AshuJoshi 0:3ec6a7645098 3 * to the mDot UDK.
AshuJoshi 0:3ec6a7645098 4 * Additionally sample code to compress the data
AshuJoshi 0:3ec6a7645098 5 * for use with LPWANs such as LoRa
AshuJoshi 0:3ec6a7645098 6 *****************************************************/
AshuJoshi 0:3ec6a7645098 7
AshuJoshi 0:3ec6a7645098 8 #include "mbed.h"
AshuJoshi 2:866a72c3c3bf 9 #include "mDot.h"
AshuJoshi 2:866a72c3c3bf 10 #include "MTSLog.h"
AshuJoshi 2:866a72c3c3bf 11 #include "MTSText.h"
AshuJoshi 6:35f934e83c74 12 #include <string>
AshuJoshi 6:35f934e83c74 13
AshuJoshi 7:9e2454b0318a 14 #include "BME280.h"
AshuJoshi 5:4bc6ba66f28e 15 //#include "SerialGPS.h"
AshuJoshi 0:3ec6a7645098 16
AshuJoshi 2:866a72c3c3bf 17 using namespace mts;
AshuJoshi 2:866a72c3c3bf 18
AshuJoshi 2:866a72c3c3bf 19 #define MIN(a,b) (((a)<(b))?(a):(b))
AshuJoshi 2:866a72c3c3bf 20 #define MAX(a,b) (((a)>(b))?(a):(b))
AshuJoshi 5:4bc6ba66f28e 21
AshuJoshi 5:4bc6ba66f28e 22 // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
AshuJoshi 5:4bc6ba66f28e 23
AshuJoshi 5:4bc6ba66f28e 24 // Values as used by The Things Network
AshuJoshi 5:4bc6ba66f28e 25 // Application session key
AshuJoshi 5:4bc6ba66f28e 26 uint8_t AppSKey[16]= { 0x91, 0x5F, 0xCD, 0x2A, 0xED, 0x8E, 0x0C, 0x2B, 0x30, 0xEF, 0x35, 0x8D, 0xF7, 0xE7, 0x89, 0x0A };
AshuJoshi 5:4bc6ba66f28e 27 // Network session key
AshuJoshi 5:4bc6ba66f28e 28 uint8_t NwkSKey[16]= { 0x60, 0xBF, 0x44, 0xA9, 0x56, 0x0A, 0x4C, 0xB4, 0xF2, 0xEB, 0xB1, 0x6B, 0x9A, 0x2C, 0x57, 0x32 };
AshuJoshi 5:4bc6ba66f28e 29
AshuJoshi 5:4bc6ba66f28e 30 // App Key 1DD7BB3D3E43ED13029996BEC25BF190
AshuJoshi 5:4bc6ba66f28e 31 uint8_t AppKey[16] = {0x1D, 0xD7, 0xBB, 0x3D, 0x3E, 0x43, 0xED, 0x13, 0x02, 0x99, 0x96, 0xBE, 0xC2, 0x5B, 0xF1, 0x90};
AshuJoshi 5:4bc6ba66f28e 32 // App EUI 70B3D57ED00005D5
AshuJoshi 5:4bc6ba66f28e 33 uint8_t AppEUI[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x05, 0xD5};
AshuJoshi 5:4bc6ba66f28e 34
AshuJoshi 5:4bc6ba66f28e 35
AshuJoshi 5:4bc6ba66f28e 36 // Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
AshuJoshi 5:4bc6ba66f28e 37 //uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID
AshuJoshi 5:4bc6ba66f28e 38 uint8_t NetworkAddr[4] = { 0x08, 0xBE, 0xAB, 0x8A };
AshuJoshi 5:4bc6ba66f28e 39
AshuJoshi 5:4bc6ba66f28e 40 // Some defines for the LoRa configuration
AshuJoshi 5:4bc6ba66f28e 41 #define LORA_ACK 0
AshuJoshi 5:4bc6ba66f28e 42 #define LORA_TXPOWER 20
AshuJoshi 5:4bc6ba66f28e 43
AshuJoshi 5:4bc6ba66f28e 44 //Ignoring sub band for EU modules.
AshuJoshi 5:4bc6ba66f28e 45 static uint8_t config_frequency_sub_band = 7;
AshuJoshi 5:4bc6ba66f28e 46
AshuJoshi 5:4bc6ba66f28e 47 // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
AshuJoshi 5:4bc6ba66f28e 48
AshuJoshi 5:4bc6ba66f28e 49
AshuJoshi 2:866a72c3c3bf 50
AshuJoshi 1:36e336869699 51 // mDot UDK Specific
AshuJoshi 1:36e336869699 52 // MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram
AshuJoshi 0:3ec6a7645098 53 // Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK
AshuJoshi 1:36e336869699 54
AshuJoshi 0:3ec6a7645098 55 #define UDK2 1
AshuJoshi 0:3ec6a7645098 56 #ifdef UDK2
AshuJoshi 0:3ec6a7645098 57 DigitalOut led(LED1);
AshuJoshi 0:3ec6a7645098 58 #else
AshuJoshi 0:3ec6a7645098 59 DigitalOut led(XBEE_RSSI);
AshuJoshi 0:3ec6a7645098 60 #endif
AshuJoshi 0:3ec6a7645098 61
AshuJoshi 5:4bc6ba66f28e 62 //SerialGPS gps(PA_2, PA_3);
AshuJoshi 1:36e336869699 63 //BME280 sensor(I2C_SDA, I2C_SCL)
AshuJoshi 1:36e336869699 64 // MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_*
AshuJoshi 7:9e2454b0318a 65 BME280 b280(PC_9, PA_8);
AshuJoshi 1:36e336869699 66
AshuJoshi 5:4bc6ba66f28e 67 // Serial via USB for debugging only
AshuJoshi 5:4bc6ba66f28e 68 //Serial pc(USBTX,USBRX);
AshuJoshi 5:4bc6ba66f28e 69
AshuJoshi 0:3ec6a7645098 70 // Function Declarations
AshuJoshi 0:3ec6a7645098 71 void endLessTestLoop();
AshuJoshi 0:3ec6a7645098 72 void setUpLEDBlink();
AshuJoshi 0:3ec6a7645098 73 void blink();
AshuJoshi 7:9e2454b0318a 74 void readandprintBME280();
AshuJoshi 2:866a72c3c3bf 75 void mDotConfig();
AshuJoshi 2:866a72c3c3bf 76 void mDotGotoDeepSleep(int seconds);
AshuJoshi 3:5c2bcba214b5 77 void mDotConfigPrint();
AshuJoshi 4:97f9ad3f2566 78 void initSerialGPS();
AshuJoshi 5:4bc6ba66f28e 79 void setupNetwork();
AshuJoshi 5:4bc6ba66f28e 80 void joinNetwork();
AshuJoshi 2:866a72c3c3bf 81
AshuJoshi 6:35f934e83c74 82 // Globals
AshuJoshi 6:35f934e83c74 83 Ticker tick;
AshuJoshi 6:35f934e83c74 84 mDot* dot;
AshuJoshi 0:3ec6a7645098 85
AshuJoshi 0:3ec6a7645098 86 /*****************************************************
AshuJoshi 0:3ec6a7645098 87 * MAIN
AshuJoshi 0:3ec6a7645098 88 *****************************************************/
AshuJoshi 0:3ec6a7645098 89 int main(){
AshuJoshi 0:3ec6a7645098 90
AshuJoshi 0:3ec6a7645098 91 // Simple Test Functions, "Hello World on UDK
AshuJoshi 0:3ec6a7645098 92 setUpLEDBlink();
AshuJoshi 7:9e2454b0318a 93 mDotConfig();
AshuJoshi 5:4bc6ba66f28e 94 setupNetwork();
AshuJoshi 6:35f934e83c74 95 //wait(15);
AshuJoshi 7:9e2454b0318a 96 joinNetwork();
AshuJoshi 0:3ec6a7645098 97 endLessTestLoop();
AshuJoshi 0:3ec6a7645098 98
AshuJoshi 0:3ec6a7645098 99 return 0;
AshuJoshi 0:3ec6a7645098 100 }
AshuJoshi 0:3ec6a7645098 101
AshuJoshi 2:866a72c3c3bf 102 /*****************************************************
AshuJoshi 2:866a72c3c3bf 103 * mDot Functions
AshuJoshi 2:866a72c3c3bf 104 ****************************************************/
AshuJoshi 2:866a72c3c3bf 105
AshuJoshi 2:866a72c3c3bf 106
AshuJoshi 2:866a72c3c3bf 107 void mDotConfig() {
AshuJoshi 2:866a72c3c3bf 108 // get a mDot handle
AshuJoshi 2:866a72c3c3bf 109 dot = mDot::getInstance();
AshuJoshi 2:866a72c3c3bf 110 //dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
AshuJoshi 2:866a72c3c3bf 111 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
AshuJoshi 5:4bc6ba66f28e 112
AshuJoshi 2:866a72c3c3bf 113 }
AshuJoshi 2:866a72c3c3bf 114
AshuJoshi 2:866a72c3c3bf 115 void mDotGotoDeepSleep(int seconds) {
AshuJoshi 2:866a72c3c3bf 116 // logInfo("input to sleep routine %d", seconds);
AshuJoshi 2:866a72c3c3bf 117 // Should sleep here and wakeup after a set interval.
AshuJoshi 2:866a72c3c3bf 118 uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), seconds);
AshuJoshi 2:866a72c3c3bf 119 logInfo("going to sleep for %d seconds", sleep_time);
AshuJoshi 2:866a72c3c3bf 120 // go to sleep and wake up automatically sleep_time seconds later
AshuJoshi 4:97f9ad3f2566 121 //dot->sleep(sleep_time, mDot::RTC_ALARM, false);
AshuJoshi 4:97f9ad3f2566 122 dot->sleep(sleep_time, mDot::RTC_ALARM);
AshuJoshi 2:866a72c3c3bf 123
AshuJoshi 2:866a72c3c3bf 124 }
AshuJoshi 5:4bc6ba66f28e 125 void setupNetwork(){
AshuJoshi 5:4bc6ba66f28e 126 int32_t ret;
AshuJoshi 5:4bc6ba66f28e 127 std::vector<uint8_t> send_data;
AshuJoshi 5:4bc6ba66f28e 128 std::vector<uint8_t> recv_data;
AshuJoshi 5:4bc6ba66f28e 129 std::vector<uint8_t> nwkSKey;
AshuJoshi 5:4bc6ba66f28e 130 std::vector<uint8_t> appSKey;
AshuJoshi 5:4bc6ba66f28e 131 std::vector<uint8_t> nodeAddr;
AshuJoshi 5:4bc6ba66f28e 132 std::vector<uint8_t> networkAddr;
AshuJoshi 5:4bc6ba66f28e 133 // from OTAA
AshuJoshi 5:4bc6ba66f28e 134 std::vector<uint8_t> appEUI;
AshuJoshi 5:4bc6ba66f28e 135 std::vector<uint8_t> appKey;
AshuJoshi 5:4bc6ba66f28e 136
AshuJoshi 5:4bc6ba66f28e 137 // get a mDot handle
AshuJoshi 7:9e2454b0318a 138 // dot = mDot::getInstance();
AshuJoshi 5:4bc6ba66f28e 139
AshuJoshi 6:35f934e83c74 140 //*******************************************
AshuJoshi 6:35f934e83c74 141 // configuration
AshuJoshi 6:35f934e83c74 142 //*******************************************
AshuJoshi 6:35f934e83c74 143
AshuJoshi 6:35f934e83c74 144 //dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
AshuJoshi 7:9e2454b0318a 145 //dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
AshuJoshi 7:9e2454b0318a 146 //logInfo("Checking Config");
AshuJoshi 5:4bc6ba66f28e 147
AshuJoshi 5:4bc6ba66f28e 148 // Test if we've already saved the config
AshuJoshi 5:4bc6ba66f28e 149 std::string configNetworkName = dot->getNetworkName();
AshuJoshi 5:4bc6ba66f28e 150
AshuJoshi 5:4bc6ba66f28e 151 uint8_t *it = NwkSKey;
AshuJoshi 5:4bc6ba66f28e 152 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 153 nwkSKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 154 it = AppSKey;
AshuJoshi 5:4bc6ba66f28e 155 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 156 appSKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 157
AshuJoshi 5:4bc6ba66f28e 158
AshuJoshi 5:4bc6ba66f28e 159 it = AppEUI;
AshuJoshi 5:4bc6ba66f28e 160 for (uint8_t i = 0; i<8; i++)
AshuJoshi 5:4bc6ba66f28e 161 appEUI.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 162
AshuJoshi 5:4bc6ba66f28e 163 it = AppKey;
AshuJoshi 5:4bc6ba66f28e 164 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 165 appKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 166
AshuJoshi 5:4bc6ba66f28e 167 it = NetworkAddr;
AshuJoshi 5:4bc6ba66f28e 168 for (uint8_t i = 0; i<4; i++)
AshuJoshi 5:4bc6ba66f28e 169 networkAddr.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 170
AshuJoshi 5:4bc6ba66f28e 171 logInfo("Resetting Config");
AshuJoshi 5:4bc6ba66f28e 172 // reset to default config so we know what state we're in
AshuJoshi 5:4bc6ba66f28e 173 dot->resetConfig();
AshuJoshi 5:4bc6ba66f28e 174
AshuJoshi 5:4bc6ba66f28e 175 // Set byte order - AEP less than 1.0.30
AshuJoshi 5:4bc6ba66f28e 176 //dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit
AshuJoshi 5:4bc6ba66f28e 177
AshuJoshi 5:4bc6ba66f28e 178 // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
AshuJoshi 5:4bc6ba66f28e 179 // Lower is higher data rate, larger packets and shorter range.
AshuJoshi 5:4bc6ba66f28e 180 logInfo("Set SF");
AshuJoshi 5:4bc6ba66f28e 181 //if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 182 if((ret = dot->setTxDataRate( mDot::SF_8 )) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 183 logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 184 }
AshuJoshi 5:4bc6ba66f28e 185
AshuJoshi 5:4bc6ba66f28e 186 //logInfo("Set TxPower");
AshuJoshi 5:4bc6ba66f28e 187 //if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 188 // logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 189 //}
AshuJoshi 5:4bc6ba66f28e 190
AshuJoshi 5:4bc6ba66f28e 191 logInfo("Set Public mode");
AshuJoshi 5:4bc6ba66f28e 192 if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 193 logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 194 }
AshuJoshi 5:4bc6ba66f28e 195
AshuJoshi 5:4bc6ba66f28e 196 //logInfo("Set MANUAL Join mode");
AshuJoshi 5:4bc6ba66f28e 197 //if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 198 // logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 199 //}
AshuJoshi 5:4bc6ba66f28e 200
AshuJoshi 5:4bc6ba66f28e 201 logInfo("Set AUTO_OTA Join mode");
AshuJoshi 5:4bc6ba66f28e 202 if((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 203 logError("Failed to set AUTO_OTA Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 204 }
AshuJoshi 5:4bc6ba66f28e 205
AshuJoshi 5:4bc6ba66f28e 206 logInfo("Set Ack");
AshuJoshi 5:4bc6ba66f28e 207 // 1 retries on Ack, 0 to disable
AshuJoshi 5:4bc6ba66f28e 208 if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 209 logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 210 }
AshuJoshi 5:4bc6ba66f28e 211
AshuJoshi 5:4bc6ba66f28e 212 // Not applicable for 868MHz in EU
AshuJoshi 5:4bc6ba66f28e 213 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 214 logError("Failed to set frequency sub band %s", ret);
AshuJoshi 5:4bc6ba66f28e 215 }
AshuJoshi 5:4bc6ba66f28e 216
AshuJoshi 5:4bc6ba66f28e 217 logInfo("Set Network Address");
AshuJoshi 5:4bc6ba66f28e 218 if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 219 logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 220 }
AshuJoshi 5:4bc6ba66f28e 221
AshuJoshi 5:4bc6ba66f28e 222 logInfo("Set Data Session Key");
AshuJoshi 5:4bc6ba66f28e 223 if ((ret = dot->setDataSessionKey(appSKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 224 logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 225 }
AshuJoshi 5:4bc6ba66f28e 226
AshuJoshi 5:4bc6ba66f28e 227 logInfo("Set Network Session Key");
AshuJoshi 5:4bc6ba66f28e 228 if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 229 logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 230 }
AshuJoshi 5:4bc6ba66f28e 231
AshuJoshi 5:4bc6ba66f28e 232 logInfo("Set Network Id");
AshuJoshi 5:4bc6ba66f28e 233 if ((ret = dot->setNetworkId(appEUI)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 234 logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 235 }
AshuJoshi 5:4bc6ba66f28e 236 logInfo("Set Network Key");
AshuJoshi 5:4bc6ba66f28e 237 if ((ret = dot->setNetworkKey(appKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 238 logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 239 }
AshuJoshi 5:4bc6ba66f28e 240
AshuJoshi 5:4bc6ba66f28e 241 logInfo("Saving Config");
AshuJoshi 5:4bc6ba66f28e 242 // Save config
AshuJoshi 5:4bc6ba66f28e 243 if (! dot->saveConfig()) {
AshuJoshi 5:4bc6ba66f28e 244 logError("failed to save configuration");
AshuJoshi 5:4bc6ba66f28e 245 }
AshuJoshi 5:4bc6ba66f28e 246
AshuJoshi 5:4bc6ba66f28e 247 //*******************************************
AshuJoshi 5:4bc6ba66f28e 248 // end of configuration
AshuJoshi 5:4bc6ba66f28e 249 //*******************************************
AshuJoshi 6:35f934e83c74 250
AshuJoshi 6:35f934e83c74 251 mDotConfigPrint();
AshuJoshi 5:4bc6ba66f28e 252
AshuJoshi 5:4bc6ba66f28e 253 //char dataBuf[50];
AshuJoshi 5:4bc6ba66f28e 254
AshuJoshi 7:9e2454b0318a 255
AshuJoshi 7:9e2454b0318a 256
AshuJoshi 7:9e2454b0318a 257
AshuJoshi 7:9e2454b0318a 258 }
AshuJoshi 7:9e2454b0318a 259
AshuJoshi 7:9e2454b0318a 260 void joinNetwork() {
AshuJoshi 7:9e2454b0318a 261 int32_t ret;
AshuJoshi 5:4bc6ba66f28e 262 logInfo("Joining Network");
AshuJoshi 6:35f934e83c74 263
AshuJoshi 5:4bc6ba66f28e 264 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 265 logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 266 //wait_ms(dot->getNextTxMs() + 1);
AshuJoshi 5:4bc6ba66f28e 267 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 5:4bc6ba66f28e 268 }
AshuJoshi 5:4bc6ba66f28e 269 logInfo("Joined Network");
AshuJoshi 6:35f934e83c74 270 wait(10);
AshuJoshi 6:35f934e83c74 271
AshuJoshi 6:35f934e83c74 272 }
AshuJoshi 6:35f934e83c74 273
AshuJoshi 6:35f934e83c74 274
AshuJoshi 0:3ec6a7645098 275
AshuJoshi 3:5c2bcba214b5 276 void mDotConfigPrint() {
AshuJoshi 3:5c2bcba214b5 277
AshuJoshi 3:5c2bcba214b5 278 // Display what is set
AshuJoshi 3:5c2bcba214b5 279 printf("\r\n");
AshuJoshi 3:5c2bcba214b5 280 printf(" ********** mDot Configuration ************ \n");
AshuJoshi 5:4bc6ba66f28e 281 // print library version information
AshuJoshi 5:4bc6ba66f28e 282 logInfo("Firmware Version: %s", dot->getId().c_str());
AshuJoshi 5:4bc6ba66f28e 283
AshuJoshi 3:5c2bcba214b5 284 std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
AshuJoshi 3:5c2bcba214b5 285 printf("Network Session Key: ");
AshuJoshi 3:5c2bcba214b5 286 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 5:4bc6ba66f28e 287
AshuJoshi 3:5c2bcba214b5 288 tmp = dot->getDataSessionKey();
AshuJoshi 3:5c2bcba214b5 289 printf("Data Session Key: ");
AshuJoshi 3:5c2bcba214b5 290 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 291
AshuJoshi 3:5c2bcba214b5 292 tmp = dot->getNetworkId();
AshuJoshi 3:5c2bcba214b5 293 printf("App EUI: ");
AshuJoshi 3:5c2bcba214b5 294 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 295
AshuJoshi 3:5c2bcba214b5 296 tmp = dot->getNetworkKey();
AshuJoshi 3:5c2bcba214b5 297 printf("App Key: ");
AshuJoshi 3:5c2bcba214b5 298 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 299
AshuJoshi 3:5c2bcba214b5 300 printf("Device ID ");
AshuJoshi 3:5c2bcba214b5 301 std::vector<uint8_t> deviceId;
AshuJoshi 3:5c2bcba214b5 302 deviceId = dot->getDeviceId();
AshuJoshi 3:5c2bcba214b5 303 for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
AshuJoshi 3:5c2bcba214b5 304 printf("%2.2x",*it );
AshuJoshi 3:5c2bcba214b5 305 printf("\n");
AshuJoshi 3:5c2bcba214b5 306 std::vector<uint8_t> netAddress;
AshuJoshi 3:5c2bcba214b5 307
AshuJoshi 3:5c2bcba214b5 308 printf("Network Address ");
AshuJoshi 3:5c2bcba214b5 309 netAddress = dot->getNetworkAddress();
AshuJoshi 3:5c2bcba214b5 310 for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
AshuJoshi 3:5c2bcba214b5 311 printf("%2.2x",*it );
AshuJoshi 3:5c2bcba214b5 312 printf("\n");
AshuJoshi 3:5c2bcba214b5 313
AshuJoshi 3:5c2bcba214b5 314 // Display LoRa parameters
AshuJoshi 3:5c2bcba214b5 315 // Display label and values in different colours, show pretty values not numeric values where applicable
AshuJoshi 3:5c2bcba214b5 316 printf("Public Network: %s\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
AshuJoshi 3:5c2bcba214b5 317 printf("Frequency: %s\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
AshuJoshi 3:5c2bcba214b5 318 printf("Sub Band: %s\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
AshuJoshi 3:5c2bcba214b5 319 printf("Join Mode: %s\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
AshuJoshi 3:5c2bcba214b5 320 printf("Join Retries: %d\n", dot->getJoinRetries() );
AshuJoshi 3:5c2bcba214b5 321 printf("Join Byte Order: %s\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
AshuJoshi 3:5c2bcba214b5 322 printf("Link Check Count: %d\n", dot->getLinkCheckCount() );
AshuJoshi 3:5c2bcba214b5 323 printf("Link Check Thold: %d\n", dot->getLinkCheckThreshold() );
AshuJoshi 3:5c2bcba214b5 324 printf("Tx Data Rate: %s\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
AshuJoshi 3:5c2bcba214b5 325 printf("Tx Power: %d\n", dot->getTxPower() );
AshuJoshi 3:5c2bcba214b5 326 printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
AshuJoshi 3:5c2bcba214b5 327 printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
AshuJoshi 3:5c2bcba214b5 328 printf("Ack: %s\n", (dot->getAck() ? "Y" : "N") );
AshuJoshi 3:5c2bcba214b5 329
AshuJoshi 3:5c2bcba214b5 330
AshuJoshi 3:5c2bcba214b5 331
AshuJoshi 3:5c2bcba214b5 332 }
AshuJoshi 3:5c2bcba214b5 333
AshuJoshi 0:3ec6a7645098 334
AshuJoshi 0:3ec6a7645098 335
AshuJoshi 1:36e336869699 336 /*****************************************************
AshuJoshi 1:36e336869699 337 * Sensor Functions
AshuJoshi 1:36e336869699 338 ****************************************************/
AshuJoshi 0:3ec6a7645098 339
AshuJoshi 7:9e2454b0318a 340
AshuJoshi 1:36e336869699 341 void readandprintBME280() {
AshuJoshi 2:866a72c3c3bf 342 float temperature;
AshuJoshi 2:866a72c3c3bf 343 float pressure;
AshuJoshi 2:866a72c3c3bf 344 float humidity;
AshuJoshi 2:866a72c3c3bf 345 char string_buffer[64];
AshuJoshi 5:4bc6ba66f28e 346 //time_t secs;
AshuJoshi 4:97f9ad3f2566 347
AshuJoshi 5:4bc6ba66f28e 348 //secs = time(NULL);
AshuJoshi 5:4bc6ba66f28e 349 //printf("Seconds since January 1, 1970: %d\n", secs);
AshuJoshi 5:4bc6ba66f28e 350 //printf("Time as a basic string = %s", ctime(&secs));
AshuJoshi 2:866a72c3c3bf 351
AshuJoshi 2:866a72c3c3bf 352 // Temperature
AshuJoshi 2:866a72c3c3bf 353 temperature = b280.getTemperature();
AshuJoshi 2:866a72c3c3bf 354 sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
AshuJoshi 2:866a72c3c3bf 355 logInfo("The temperature is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 356 // Pressure
AshuJoshi 2:866a72c3c3bf 357 pressure = b280.getPressure();
AshuJoshi 2:866a72c3c3bf 358 sprintf(string_buffer, "%s%04.2f", "hPa:", pressure);
AshuJoshi 2:866a72c3c3bf 359 logInfo("The pressure is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 360 // Humidity
AshuJoshi 2:866a72c3c3bf 361 humidity = b280.getHumidity();
AshuJoshi 2:866a72c3c3bf 362 sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
AshuJoshi 2:866a72c3c3bf 363 logInfo("The humidty is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 364
AshuJoshi 5:4bc6ba66f28e 365 //printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
AshuJoshi 1:36e336869699 366 }
AshuJoshi 7:9e2454b0318a 367
AshuJoshi 0:3ec6a7645098 368
AshuJoshi 0:3ec6a7645098 369
AshuJoshi 0:3ec6a7645098 370 /*****************************************************
AshuJoshi 1:36e336869699 371 * FUNCTIONS for Simple Testing
AshuJoshi 1:36e336869699 372 ****************************************************/
AshuJoshi 0:3ec6a7645098 373
AshuJoshi 0:3ec6a7645098 374 void setUpLEDBlink(){
AshuJoshi 0:3ec6a7645098 375 // configure the Ticker to blink the LED on 500ms interval
AshuJoshi 0:3ec6a7645098 376 tick.attach(&blink, 0.5);
AshuJoshi 0:3ec6a7645098 377 }
AshuJoshi 0:3ec6a7645098 378
AshuJoshi 0:3ec6a7645098 379 void endLessTestLoop() {
AshuJoshi 0:3ec6a7645098 380 while(true) {
AshuJoshi 1:36e336869699 381 // printf("Hello world!\r\n");
AshuJoshi 7:9e2454b0318a 382 //printf("BME280 Sensor: \n");
AshuJoshi 7:9e2454b0318a 383 readandprintBME280();
AshuJoshi 2:866a72c3c3bf 384
AshuJoshi 5:4bc6ba66f28e 385 wait(5);
AshuJoshi 5:4bc6ba66f28e 386 //mDotGotoDeepSleep(60);
AshuJoshi 4:97f9ad3f2566 387 //wait(5);
AshuJoshi 2:866a72c3c3bf 388
AshuJoshi 0:3ec6a7645098 389 }
AshuJoshi 0:3ec6a7645098 390 }
AshuJoshi 0:3ec6a7645098 391
AshuJoshi 0:3ec6a7645098 392 // Callback function to change LED state
AshuJoshi 0:3ec6a7645098 393 void blink() {
AshuJoshi 0:3ec6a7645098 394 led = !led;
AshuJoshi 4:97f9ad3f2566 395 }
AshuJoshi 4:97f9ad3f2566 396
AshuJoshi 4:97f9ad3f2566 397