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:
Wed Jul 13 21:13:51 2016 +0000
Revision:
13:22e11a02c7a3
Parent:
11:3481e24747e2
Stable release -onnect to the The Things Network in OTAA mode.Make sure to program your specific keys into LoRa.h - from TTN Accnt Dashboard; The program also supports reading a BME280 sensor and logging.; Serial GPS is NOT working.

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 10:8071e1ae92ac 6 * Uses Standard Firmware from Multitech
AshuJoshi 10:8071e1ae92ac 7 *
AshuJoshi 0:3ec6a7645098 8 *****************************************************/
AshuJoshi 0:3ec6a7645098 9
AshuJoshi 0:3ec6a7645098 10 #include "mbed.h"
AshuJoshi 2:866a72c3c3bf 11 #include "mDot.h"
AshuJoshi 2:866a72c3c3bf 12 #include "MTSLog.h"
AshuJoshi 2:866a72c3c3bf 13 #include "MTSText.h"
AshuJoshi 6:35f934e83c74 14 #include <string>
AshuJoshi 10:8071e1ae92ac 15 #include "LoRa.h"
AshuJoshi 7:9e2454b0318a 16 #include "BME280.h"
AshuJoshi 10:8071e1ae92ac 17 //#include "SerialGPS.h"
AshuJoshi 0:3ec6a7645098 18
AshuJoshi 10:8071e1ae92ac 19 //using namespace mts;
AshuJoshi 2:866a72c3c3bf 20
AshuJoshi 2:866a72c3c3bf 21 #define MIN(a,b) (((a)<(b))?(a):(b))
AshuJoshi 2:866a72c3c3bf 22 #define MAX(a,b) (((a)>(b))?(a):(b))
AshuJoshi 5:4bc6ba66f28e 23
AshuJoshi 1:36e336869699 24 // mDot UDK Specific
AshuJoshi 1:36e336869699 25 // MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram
AshuJoshi 0:3ec6a7645098 26 // Uncomment this line if using a full sized UDK2.0 instead of a Micro UDK
AshuJoshi 1:36e336869699 27
AshuJoshi 0:3ec6a7645098 28 #define UDK2 1
AshuJoshi 0:3ec6a7645098 29 #ifdef UDK2
AshuJoshi 0:3ec6a7645098 30 DigitalOut led(LED1);
AshuJoshi 0:3ec6a7645098 31 #else
AshuJoshi 0:3ec6a7645098 32 DigitalOut led(XBEE_RSSI);
AshuJoshi 0:3ec6a7645098 33 #endif
AshuJoshi 0:3ec6a7645098 34
AshuJoshi 5:4bc6ba66f28e 35 //SerialGPS gps(PA_2, PA_3);
AshuJoshi 1:36e336869699 36 //BME280 sensor(I2C_SDA, I2C_SCL)
AshuJoshi 1:36e336869699 37 // MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_*
AshuJoshi 7:9e2454b0318a 38 BME280 b280(PC_9, PA_8);
AshuJoshi 8:c17b68b03791 39 AnalogIn light(PB_0); // This corresponds to A1 Connector on the Grove Shield
AshuJoshi 1:36e336869699 40
AshuJoshi 5:4bc6ba66f28e 41
AshuJoshi 0:3ec6a7645098 42 // Function Declarations
AshuJoshi 0:3ec6a7645098 43 void endLessTestLoop();
AshuJoshi 0:3ec6a7645098 44 void setUpLEDBlink();
AshuJoshi 0:3ec6a7645098 45 void blink();
AshuJoshi 7:9e2454b0318a 46 void readandprintBME280();
AshuJoshi 8:c17b68b03791 47 float readLightSensor();
AshuJoshi 2:866a72c3c3bf 48 void mDotConfig();
AshuJoshi 10:8071e1ae92ac 49 void mDotGotoDeepSleep(int seconds, bool sleepState);
AshuJoshi 3:5c2bcba214b5 50 void mDotConfigPrint();
AshuJoshi 4:97f9ad3f2566 51 void initSerialGPS();
AshuJoshi 5:4bc6ba66f28e 52 void setupNetwork();
AshuJoshi 5:4bc6ba66f28e 53 void joinNetwork();
AshuJoshi 8:c17b68b03791 54 void sendData();
AshuJoshi 2:866a72c3c3bf 55
AshuJoshi 6:35f934e83c74 56 // Globals
AshuJoshi 6:35f934e83c74 57 Ticker tick;
AshuJoshi 6:35f934e83c74 58 mDot* dot;
AshuJoshi 8:c17b68b03791 59 float llevel;
AshuJoshi 0:3ec6a7645098 60
AshuJoshi 0:3ec6a7645098 61 /*****************************************************
AshuJoshi 0:3ec6a7645098 62 * MAIN
AshuJoshi 0:3ec6a7645098 63 *****************************************************/
AshuJoshi 0:3ec6a7645098 64 int main(){
AshuJoshi 0:3ec6a7645098 65
AshuJoshi 0:3ec6a7645098 66 // Simple Test Functions, "Hello World on UDK
AshuJoshi 10:8071e1ae92ac 67 //setUpLEDBlink();
AshuJoshi 10:8071e1ae92ac 68
AshuJoshi 7:9e2454b0318a 69 mDotConfig();
AshuJoshi 10:8071e1ae92ac 70 // setupNetwork(); // Moved to mDotConfig
AshuJoshi 7:9e2454b0318a 71 joinNetwork();
AshuJoshi 8:c17b68b03791 72 sendData();
AshuJoshi 10:8071e1ae92ac 73 //endLessTestLoop();
AshuJoshi 0:3ec6a7645098 74
AshuJoshi 0:3ec6a7645098 75 return 0;
AshuJoshi 0:3ec6a7645098 76 }
AshuJoshi 0:3ec6a7645098 77
AshuJoshi 8:c17b68b03791 78 void sendData() {
AshuJoshi 8:c17b68b03791 79 std::vector<uint8_t> data;
AshuJoshi 8:c17b68b03791 80 std::string data_str = "hello!";
AshuJoshi 8:c17b68b03791 81 char string_buffer[64];
AshuJoshi 8:c17b68b03791 82 std::string separator_str = ",";
AshuJoshi 8:c17b68b03791 83 std::string temp_cls = "TC";
AshuJoshi 8:c17b68b03791 84 float temperature;
AshuJoshi 8:c17b68b03791 85 float pressure;
AshuJoshi 8:c17b68b03791 86 float humidity;
AshuJoshi 8:c17b68b03791 87 int32_t ret;
AshuJoshi 8:c17b68b03791 88
AshuJoshi 8:c17b68b03791 89 while (true) {
AshuJoshi 8:c17b68b03791 90 data.clear();
AshuJoshi 8:c17b68b03791 91
AshuJoshi 8:c17b68b03791 92 // Temperature
AshuJoshi 8:c17b68b03791 93 temperature = b280.getTemperature();
AshuJoshi 8:c17b68b03791 94 sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
AshuJoshi 8:c17b68b03791 95 logInfo("The temperature is %s", string_buffer);
AshuJoshi 8:c17b68b03791 96 for (int i = 0; i<strlen(string_buffer); i++)
AshuJoshi 8:c17b68b03791 97 {
AshuJoshi 8:c17b68b03791 98 data.push_back(((char*)string_buffer)[i]);
AshuJoshi 8:c17b68b03791 99 }
AshuJoshi 8:c17b68b03791 100
AshuJoshi 8:c17b68b03791 101 logDebug("Sending LoRa message, length: %d", data.size());
AshuJoshi 8:c17b68b03791 102 logDebug("sending data: ");
AshuJoshi 8:c17b68b03791 103 for(int i = 0; i < data.size(); i++)
AshuJoshi 8:c17b68b03791 104 {
AshuJoshi 8:c17b68b03791 105 printf("%c", data[i]);
AshuJoshi 8:c17b68b03791 106 }
AshuJoshi 8:c17b68b03791 107 printf("\n");
AshuJoshi 8:c17b68b03791 108
AshuJoshi 8:c17b68b03791 109 // send the data to the gateway
AshuJoshi 8:c17b68b03791 110 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
AshuJoshi 8:c17b68b03791 111 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 8:c17b68b03791 112 } else {
AshuJoshi 8:c17b68b03791 113 logInfo("successfully sent data to gateway");
AshuJoshi 8:c17b68b03791 114 }
AshuJoshi 8:c17b68b03791 115
AshuJoshi 8:c17b68b03791 116 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
AshuJoshi 8:c17b68b03791 117 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 8:c17b68b03791 118 data.clear();
AshuJoshi 8:c17b68b03791 119
AshuJoshi 8:c17b68b03791 120 // Pressure
AshuJoshi 8:c17b68b03791 121 pressure = b280.getPressure();
AshuJoshi 8:c17b68b03791 122 sprintf(string_buffer, "%s%04.2f", "hPa:", pressure);
AshuJoshi 8:c17b68b03791 123 logInfo("The pressure is %s", string_buffer);
AshuJoshi 8:c17b68b03791 124 for (int i = 0; i<strlen(string_buffer); i++)
AshuJoshi 8:c17b68b03791 125 {
AshuJoshi 8:c17b68b03791 126 data.push_back(((char*)string_buffer)[i]);
AshuJoshi 8:c17b68b03791 127 }
AshuJoshi 8:c17b68b03791 128
AshuJoshi 8:c17b68b03791 129 logDebug("Sending LoRa message, length: %d", data.size());
AshuJoshi 8:c17b68b03791 130 logDebug("sending data: ");
AshuJoshi 8:c17b68b03791 131 for(int i = 0; i < data.size(); i++)
AshuJoshi 8:c17b68b03791 132 {
AshuJoshi 8:c17b68b03791 133 printf("%c", data[i]);
AshuJoshi 8:c17b68b03791 134 }
AshuJoshi 8:c17b68b03791 135 printf("\n");
AshuJoshi 8:c17b68b03791 136
AshuJoshi 8:c17b68b03791 137 // send the data to the gateway
AshuJoshi 8:c17b68b03791 138 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
AshuJoshi 8:c17b68b03791 139 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 8:c17b68b03791 140 } else {
AshuJoshi 8:c17b68b03791 141 logInfo("successfully sent data to gateway");
AshuJoshi 8:c17b68b03791 142 }
AshuJoshi 8:c17b68b03791 143
AshuJoshi 8:c17b68b03791 144 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
AshuJoshi 8:c17b68b03791 145 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 8:c17b68b03791 146
AshuJoshi 8:c17b68b03791 147 data.clear();
AshuJoshi 8:c17b68b03791 148
AshuJoshi 8:c17b68b03791 149 // Humidity
AshuJoshi 8:c17b68b03791 150 humidity = b280.getHumidity();
AshuJoshi 8:c17b68b03791 151 sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
AshuJoshi 8:c17b68b03791 152 logInfo("The humidty is %s", string_buffer);
AshuJoshi 8:c17b68b03791 153
AshuJoshi 8:c17b68b03791 154 for (int i = 0; i<strlen(string_buffer); i++)
AshuJoshi 8:c17b68b03791 155 {
AshuJoshi 8:c17b68b03791 156 data.push_back(((char*)string_buffer)[i]);
AshuJoshi 8:c17b68b03791 157 }
AshuJoshi 8:c17b68b03791 158
AshuJoshi 8:c17b68b03791 159 logDebug("Sending LoRa message, length: %d", data.size());
AshuJoshi 8:c17b68b03791 160 logDebug("sending data: ");
AshuJoshi 8:c17b68b03791 161 for(int i = 0; i < data.size(); i++)
AshuJoshi 8:c17b68b03791 162 {
AshuJoshi 8:c17b68b03791 163 printf("%c", data[i]);
AshuJoshi 8:c17b68b03791 164 }
AshuJoshi 8:c17b68b03791 165 printf("\n");
AshuJoshi 8:c17b68b03791 166
AshuJoshi 8:c17b68b03791 167 // send the data to the gateway
AshuJoshi 8:c17b68b03791 168 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
AshuJoshi 8:c17b68b03791 169 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 8:c17b68b03791 170 } else {
AshuJoshi 8:c17b68b03791 171 logInfo("successfully sent data to gateway");
AshuJoshi 8:c17b68b03791 172 }
AshuJoshi 8:c17b68b03791 173
AshuJoshi 8:c17b68b03791 174 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
AshuJoshi 8:c17b68b03791 175 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 8:c17b68b03791 176
AshuJoshi 8:c17b68b03791 177 data.clear();
AshuJoshi 8:c17b68b03791 178
AshuJoshi 8:c17b68b03791 179 // Light Level
AshuJoshi 8:c17b68b03791 180 llevel = readLightSensor();
AshuJoshi 8:c17b68b03791 181 sprintf(string_buffer, "%s%5.1f", "LL:", llevel);
AshuJoshi 8:c17b68b03791 182 for (int i = 0; i<strlen(string_buffer); i++)
AshuJoshi 8:c17b68b03791 183 {
AshuJoshi 8:c17b68b03791 184 data.push_back(((char*)string_buffer)[i]);
AshuJoshi 8:c17b68b03791 185 }
AshuJoshi 8:c17b68b03791 186 logDebug("Sending LoRa message, length: %d", data.size());
AshuJoshi 8:c17b68b03791 187 logDebug("sending data: ");
AshuJoshi 8:c17b68b03791 188 for(int i = 0; i < data.size(); i++)
AshuJoshi 8:c17b68b03791 189 {
AshuJoshi 8:c17b68b03791 190 printf("%c", data[i]);
AshuJoshi 8:c17b68b03791 191 }
AshuJoshi 8:c17b68b03791 192 printf("\n");
AshuJoshi 8:c17b68b03791 193
AshuJoshi 8:c17b68b03791 194 // send the data to the gateway
AshuJoshi 8:c17b68b03791 195 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
AshuJoshi 8:c17b68b03791 196 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 8:c17b68b03791 197 } else {
AshuJoshi 8:c17b68b03791 198 logInfo("successfully sent data to gateway");
AshuJoshi 8:c17b68b03791 199 }
AshuJoshi 8:c17b68b03791 200
AshuJoshi 10:8071e1ae92ac 201 // Goto Sleep, commenting out the osDelay since next Tx would be after waking up
AshuJoshi 11:3481e24747e2 202 mDotGotoDeepSleep(60, false);
AshuJoshi 10:8071e1ae92ac 203
AshuJoshi 8:c17b68b03791 204 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
AshuJoshi 10:8071e1ae92ac 205 //osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 8:c17b68b03791 206
AshuJoshi 8:c17b68b03791 207 }
AshuJoshi 8:c17b68b03791 208
AshuJoshi 8:c17b68b03791 209 }
AshuJoshi 8:c17b68b03791 210
AshuJoshi 8:c17b68b03791 211
AshuJoshi 2:866a72c3c3bf 212 /*****************************************************
AshuJoshi 2:866a72c3c3bf 213 * mDot Functions
AshuJoshi 2:866a72c3c3bf 214 ****************************************************/
AshuJoshi 2:866a72c3c3bf 215
AshuJoshi 2:866a72c3c3bf 216
AshuJoshi 2:866a72c3c3bf 217 void mDotConfig() {
AshuJoshi 2:866a72c3c3bf 218 // get a mDot handle
AshuJoshi 2:866a72c3c3bf 219 dot = mDot::getInstance();
AshuJoshi 10:8071e1ae92ac 220 // Test if we've already saved the config
AshuJoshi 10:8071e1ae92ac 221 logInfo("Checking Config");
AshuJoshi 5:4bc6ba66f28e 222
AshuJoshi 10:8071e1ae92ac 223 std::string configNetworkName = dot->getNetworkName();
AshuJoshi 10:8071e1ae92ac 224 printf("Network Name is %s: \n", (char*)configNetworkName.c_str());
AshuJoshi 10:8071e1ae92ac 225 printf("Network Name is %s: \n", (char*)(config_network_name.c_str()));
AshuJoshi 10:8071e1ae92ac 226
AshuJoshi 10:8071e1ae92ac 227 if (configNetworkName.compare(config_network_name) != 0) {
AshuJoshi 10:8071e1ae92ac 228 logInfo("Setting Up Config");
AshuJoshi 10:8071e1ae92ac 229 setupNetwork();
AshuJoshi 10:8071e1ae92ac 230
AshuJoshi 10:8071e1ae92ac 231 } else {
AshuJoshi 10:8071e1ae92ac 232 logInfo("Config is good, skipping setting up... ");
AshuJoshi 10:8071e1ae92ac 233 }
AshuJoshi 2:866a72c3c3bf 234 }
AshuJoshi 2:866a72c3c3bf 235
AshuJoshi 10:8071e1ae92ac 236 void mDotGotoDeepSleep(int seconds, bool sleepState) {
AshuJoshi 10:8071e1ae92ac 237
AshuJoshi 2:866a72c3c3bf 238 // Should sleep here and wakeup after a set interval.
AshuJoshi 2:866a72c3c3bf 239 uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), seconds);
AshuJoshi 10:8071e1ae92ac 240 logInfo("Going to sleep for %d seconds", sleep_time);
AshuJoshi 10:8071e1ae92ac 241
AshuJoshi 2:866a72c3c3bf 242 // go to sleep and wake up automatically sleep_time seconds later
AshuJoshi 10:8071e1ae92ac 243 dot->sleep(sleep_time, mDot::RTC_ALARM, sleepState);
AshuJoshi 2:866a72c3c3bf 244
AshuJoshi 2:866a72c3c3bf 245 }
AshuJoshi 5:4bc6ba66f28e 246 void setupNetwork(){
AshuJoshi 5:4bc6ba66f28e 247 int32_t ret;
AshuJoshi 5:4bc6ba66f28e 248 std::vector<uint8_t> send_data;
AshuJoshi 5:4bc6ba66f28e 249 std::vector<uint8_t> recv_data;
AshuJoshi 5:4bc6ba66f28e 250 std::vector<uint8_t> nwkSKey;
AshuJoshi 5:4bc6ba66f28e 251 std::vector<uint8_t> appSKey;
AshuJoshi 5:4bc6ba66f28e 252 std::vector<uint8_t> nodeAddr;
AshuJoshi 5:4bc6ba66f28e 253 std::vector<uint8_t> networkAddr;
AshuJoshi 5:4bc6ba66f28e 254 // from OTAA
AshuJoshi 5:4bc6ba66f28e 255 std::vector<uint8_t> appEUI;
AshuJoshi 5:4bc6ba66f28e 256 std::vector<uint8_t> appKey;
AshuJoshi 5:4bc6ba66f28e 257
AshuJoshi 10:8071e1ae92ac 258
AshuJoshi 6:35f934e83c74 259 //*******************************************
AshuJoshi 6:35f934e83c74 260 // configuration
AshuJoshi 6:35f934e83c74 261 //*******************************************
AshuJoshi 6:35f934e83c74 262
AshuJoshi 5:4bc6ba66f28e 263 uint8_t *it = NwkSKey;
AshuJoshi 5:4bc6ba66f28e 264 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 265 nwkSKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 266 it = AppSKey;
AshuJoshi 5:4bc6ba66f28e 267 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 268 appSKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 269
AshuJoshi 5:4bc6ba66f28e 270
AshuJoshi 5:4bc6ba66f28e 271 it = AppEUI;
AshuJoshi 5:4bc6ba66f28e 272 for (uint8_t i = 0; i<8; i++)
AshuJoshi 5:4bc6ba66f28e 273 appEUI.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 274
AshuJoshi 5:4bc6ba66f28e 275 it = AppKey;
AshuJoshi 5:4bc6ba66f28e 276 for (uint8_t i = 0; i<16; i++)
AshuJoshi 5:4bc6ba66f28e 277 appKey.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 278
AshuJoshi 5:4bc6ba66f28e 279 it = NetworkAddr;
AshuJoshi 5:4bc6ba66f28e 280 for (uint8_t i = 0; i<4; i++)
AshuJoshi 5:4bc6ba66f28e 281 networkAddr.push_back((uint8_t) *it++);
AshuJoshi 5:4bc6ba66f28e 282
AshuJoshi 5:4bc6ba66f28e 283 logInfo("Resetting Config");
AshuJoshi 5:4bc6ba66f28e 284 // reset to default config so we know what state we're in
AshuJoshi 5:4bc6ba66f28e 285 dot->resetConfig();
AshuJoshi 10:8071e1ae92ac 286
AshuJoshi 10:8071e1ae92ac 287 //dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
AshuJoshi 10:8071e1ae92ac 288 dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
AshuJoshi 5:4bc6ba66f28e 289
AshuJoshi 10:8071e1ae92ac 290 logInfo("Setting Network name");
AshuJoshi 10:8071e1ae92ac 291 if ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
AshuJoshi 10:8071e1ae92ac 292 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 10:8071e1ae92ac 293 }
AshuJoshi 10:8071e1ae92ac 294
AshuJoshi 10:8071e1ae92ac 295 logInfo("Setting Network password");
AshuJoshi 10:8071e1ae92ac 296 if ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
AshuJoshi 10:8071e1ae92ac 297 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 10:8071e1ae92ac 298 }
AshuJoshi 10:8071e1ae92ac 299
AshuJoshi 10:8071e1ae92ac 300 // Set byte order
AshuJoshi 10:8071e1ae92ac 301 dot->setJoinByteOrder(mDot::LSB);
AshuJoshi 5:4bc6ba66f28e 302
AshuJoshi 5:4bc6ba66f28e 303 // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
AshuJoshi 5:4bc6ba66f28e 304 // Lower is higher data rate, larger packets and shorter range.
AshuJoshi 5:4bc6ba66f28e 305 logInfo("Set SF");
AshuJoshi 10:8071e1ae92ac 306 if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) {
AshuJoshi 10:8071e1ae92ac 307 //if((ret = dot->setTxDataRate( mDot::SF_8 )) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 308 logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 309 }
AshuJoshi 5:4bc6ba66f28e 310
AshuJoshi 5:4bc6ba66f28e 311 //logInfo("Set TxPower");
AshuJoshi 5:4bc6ba66f28e 312 //if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 313 // logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 314 //}
AshuJoshi 5:4bc6ba66f28e 315
AshuJoshi 5:4bc6ba66f28e 316 logInfo("Set Public mode");
AshuJoshi 5:4bc6ba66f28e 317 if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 318 logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 319 }
AshuJoshi 5:4bc6ba66f28e 320
AshuJoshi 5:4bc6ba66f28e 321 //logInfo("Set MANUAL Join mode");
AshuJoshi 5:4bc6ba66f28e 322 //if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 323 // logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 324 //}
AshuJoshi 5:4bc6ba66f28e 325
AshuJoshi 5:4bc6ba66f28e 326 logInfo("Set AUTO_OTA Join mode");
AshuJoshi 5:4bc6ba66f28e 327 if((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 328 logError("Failed to set AUTO_OTA Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 329 }
AshuJoshi 5:4bc6ba66f28e 330
AshuJoshi 5:4bc6ba66f28e 331 logInfo("Set Ack");
AshuJoshi 5:4bc6ba66f28e 332 // 1 retries on Ack, 0 to disable
AshuJoshi 5:4bc6ba66f28e 333 if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 334 logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 335 }
AshuJoshi 5:4bc6ba66f28e 336
AshuJoshi 5:4bc6ba66f28e 337 // Not applicable for 868MHz in EU
AshuJoshi 5:4bc6ba66f28e 338 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 339 logError("Failed to set frequency sub band %s", ret);
AshuJoshi 5:4bc6ba66f28e 340 }
AshuJoshi 5:4bc6ba66f28e 341
AshuJoshi 5:4bc6ba66f28e 342 logInfo("Set Network Address");
AshuJoshi 5:4bc6ba66f28e 343 if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 344 logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 345 }
AshuJoshi 5:4bc6ba66f28e 346
AshuJoshi 5:4bc6ba66f28e 347 logInfo("Set Data Session Key");
AshuJoshi 5:4bc6ba66f28e 348 if ((ret = dot->setDataSessionKey(appSKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 349 logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 350 }
AshuJoshi 5:4bc6ba66f28e 351
AshuJoshi 5:4bc6ba66f28e 352 logInfo("Set Network Session Key");
AshuJoshi 5:4bc6ba66f28e 353 if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 354 logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 355 }
AshuJoshi 5:4bc6ba66f28e 356
AshuJoshi 5:4bc6ba66f28e 357 logInfo("Set Network Id");
AshuJoshi 5:4bc6ba66f28e 358 if ((ret = dot->setNetworkId(appEUI)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 359 logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 360 }
AshuJoshi 5:4bc6ba66f28e 361 logInfo("Set Network Key");
AshuJoshi 5:4bc6ba66f28e 362 if ((ret = dot->setNetworkKey(appKey)) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 363 logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 364 }
AshuJoshi 5:4bc6ba66f28e 365
AshuJoshi 5:4bc6ba66f28e 366 logInfo("Saving Config");
AshuJoshi 5:4bc6ba66f28e 367 // Save config
AshuJoshi 5:4bc6ba66f28e 368 if (! dot->saveConfig()) {
AshuJoshi 5:4bc6ba66f28e 369 logError("failed to save configuration");
AshuJoshi 5:4bc6ba66f28e 370 }
AshuJoshi 5:4bc6ba66f28e 371
AshuJoshi 5:4bc6ba66f28e 372 //*******************************************
AshuJoshi 5:4bc6ba66f28e 373 // end of configuration
AshuJoshi 5:4bc6ba66f28e 374 //*******************************************
AshuJoshi 6:35f934e83c74 375
AshuJoshi 6:35f934e83c74 376 mDotConfigPrint();
AshuJoshi 7:9e2454b0318a 377
AshuJoshi 7:9e2454b0318a 378 }
AshuJoshi 7:9e2454b0318a 379
AshuJoshi 7:9e2454b0318a 380 void joinNetwork() {
AshuJoshi 7:9e2454b0318a 381 int32_t ret;
AshuJoshi 10:8071e1ae92ac 382
AshuJoshi 5:4bc6ba66f28e 383 logInfo("Joining Network");
AshuJoshi 5:4bc6ba66f28e 384 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
AshuJoshi 5:4bc6ba66f28e 385 logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
AshuJoshi 5:4bc6ba66f28e 386 //wait_ms(dot->getNextTxMs() + 1);
AshuJoshi 5:4bc6ba66f28e 387 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
AshuJoshi 5:4bc6ba66f28e 388 }
AshuJoshi 10:8071e1ae92ac 389
AshuJoshi 5:4bc6ba66f28e 390 logInfo("Joined Network");
AshuJoshi 6:35f934e83c74 391
AshuJoshi 6:35f934e83c74 392 }
AshuJoshi 6:35f934e83c74 393
AshuJoshi 3:5c2bcba214b5 394 void mDotConfigPrint() {
AshuJoshi 3:5c2bcba214b5 395
AshuJoshi 3:5c2bcba214b5 396 // Display what is set
AshuJoshi 3:5c2bcba214b5 397 printf("\r\n");
AshuJoshi 3:5c2bcba214b5 398 printf(" ********** mDot Configuration ************ \n");
AshuJoshi 5:4bc6ba66f28e 399 // print library version information
AshuJoshi 5:4bc6ba66f28e 400 logInfo("Firmware Version: %s", dot->getId().c_str());
AshuJoshi 5:4bc6ba66f28e 401
AshuJoshi 3:5c2bcba214b5 402 std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
AshuJoshi 3:5c2bcba214b5 403 printf("Network Session Key: ");
AshuJoshi 3:5c2bcba214b5 404 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 5:4bc6ba66f28e 405
AshuJoshi 3:5c2bcba214b5 406 tmp = dot->getDataSessionKey();
AshuJoshi 3:5c2bcba214b5 407 printf("Data Session Key: ");
AshuJoshi 3:5c2bcba214b5 408 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 409
AshuJoshi 3:5c2bcba214b5 410 tmp = dot->getNetworkId();
AshuJoshi 3:5c2bcba214b5 411 printf("App EUI: ");
AshuJoshi 3:5c2bcba214b5 412 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 413
AshuJoshi 3:5c2bcba214b5 414 tmp = dot->getNetworkKey();
AshuJoshi 3:5c2bcba214b5 415 printf("App Key: ");
AshuJoshi 3:5c2bcba214b5 416 printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
AshuJoshi 3:5c2bcba214b5 417
AshuJoshi 3:5c2bcba214b5 418 printf("Device ID ");
AshuJoshi 3:5c2bcba214b5 419 std::vector<uint8_t> deviceId;
AshuJoshi 3:5c2bcba214b5 420 deviceId = dot->getDeviceId();
AshuJoshi 3:5c2bcba214b5 421 for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
AshuJoshi 3:5c2bcba214b5 422 printf("%2.2x",*it );
AshuJoshi 3:5c2bcba214b5 423 printf("\n");
AshuJoshi 3:5c2bcba214b5 424 std::vector<uint8_t> netAddress;
AshuJoshi 3:5c2bcba214b5 425
AshuJoshi 3:5c2bcba214b5 426 printf("Network Address ");
AshuJoshi 3:5c2bcba214b5 427 netAddress = dot->getNetworkAddress();
AshuJoshi 3:5c2bcba214b5 428 for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
AshuJoshi 3:5c2bcba214b5 429 printf("%2.2x",*it );
AshuJoshi 3:5c2bcba214b5 430 printf("\n");
AshuJoshi 3:5c2bcba214b5 431
AshuJoshi 3:5c2bcba214b5 432 // Display LoRa parameters
AshuJoshi 3:5c2bcba214b5 433 // Display label and values in different colours, show pretty values not numeric values where applicable
AshuJoshi 10:8071e1ae92ac 434
AshuJoshi 10:8071e1ae92ac 435 printf("Network Name: %s\n", (char *)(dot->getNetworkName()).c_str());
AshuJoshi 10:8071e1ae92ac 436 printf("Network Name: %s\n", (char *)(dot->getNetworkPassphrase()).c_str());
AshuJoshi 3:5c2bcba214b5 437 printf("Public Network: %s\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
AshuJoshi 3:5c2bcba214b5 438 printf("Frequency: %s\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
AshuJoshi 3:5c2bcba214b5 439 printf("Sub Band: %s\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
AshuJoshi 3:5c2bcba214b5 440 printf("Join Mode: %s\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
AshuJoshi 3:5c2bcba214b5 441 printf("Join Retries: %d\n", dot->getJoinRetries() );
AshuJoshi 3:5c2bcba214b5 442 printf("Join Byte Order: %s\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
AshuJoshi 3:5c2bcba214b5 443 printf("Link Check Count: %d\n", dot->getLinkCheckCount() );
AshuJoshi 3:5c2bcba214b5 444 printf("Link Check Thold: %d\n", dot->getLinkCheckThreshold() );
AshuJoshi 3:5c2bcba214b5 445 printf("Tx Data Rate: %s\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
AshuJoshi 3:5c2bcba214b5 446 printf("Tx Power: %d\n", dot->getTxPower() );
AshuJoshi 3:5c2bcba214b5 447 printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
AshuJoshi 3:5c2bcba214b5 448 printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
AshuJoshi 3:5c2bcba214b5 449 printf("Ack: %s\n", (dot->getAck() ? "Y" : "N") );
AshuJoshi 3:5c2bcba214b5 450
AshuJoshi 3:5c2bcba214b5 451 }
AshuJoshi 3:5c2bcba214b5 452
AshuJoshi 1:36e336869699 453 /*****************************************************
AshuJoshi 1:36e336869699 454 * Sensor Functions
AshuJoshi 1:36e336869699 455 ****************************************************/
AshuJoshi 0:3ec6a7645098 456
AshuJoshi 7:9e2454b0318a 457
AshuJoshi 1:36e336869699 458 void readandprintBME280() {
AshuJoshi 2:866a72c3c3bf 459 float temperature;
AshuJoshi 2:866a72c3c3bf 460 float pressure;
AshuJoshi 2:866a72c3c3bf 461 float humidity;
AshuJoshi 2:866a72c3c3bf 462 char string_buffer[64];
AshuJoshi 5:4bc6ba66f28e 463 //time_t secs;
AshuJoshi 4:97f9ad3f2566 464
AshuJoshi 5:4bc6ba66f28e 465 //secs = time(NULL);
AshuJoshi 5:4bc6ba66f28e 466 //printf("Seconds since January 1, 1970: %d\n", secs);
AshuJoshi 5:4bc6ba66f28e 467 //printf("Time as a basic string = %s", ctime(&secs));
AshuJoshi 2:866a72c3c3bf 468
AshuJoshi 2:866a72c3c3bf 469 // Temperature
AshuJoshi 2:866a72c3c3bf 470 temperature = b280.getTemperature();
AshuJoshi 2:866a72c3c3bf 471 sprintf(string_buffer, "%s%3.2f", "TC:", temperature);
AshuJoshi 2:866a72c3c3bf 472 logInfo("The temperature is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 473 // Pressure
AshuJoshi 2:866a72c3c3bf 474 pressure = b280.getPressure();
AshuJoshi 2:866a72c3c3bf 475 sprintf(string_buffer, "%s%04.2f", "hPa:", pressure);
AshuJoshi 2:866a72c3c3bf 476 logInfo("The pressure is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 477 // Humidity
AshuJoshi 2:866a72c3c3bf 478 humidity = b280.getHumidity();
AshuJoshi 2:866a72c3c3bf 479 sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
AshuJoshi 2:866a72c3c3bf 480 logInfo("The humidty is %s", string_buffer);
AshuJoshi 2:866a72c3c3bf 481
AshuJoshi 5:4bc6ba66f28e 482 //printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
AshuJoshi 1:36e336869699 483 }
AshuJoshi 7:9e2454b0318a 484
AshuJoshi 8:c17b68b03791 485 float readLightSensor() {
AshuJoshi 8:c17b68b03791 486 float sensorValue;
AshuJoshi 8:c17b68b03791 487 float rsensor;
AshuJoshi 8:c17b68b03791 488 sensorValue = light.read();
AshuJoshi 8:c17b68b03791 489 rsensor = (float)(1023-sensorValue)*10/sensorValue;
AshuJoshi 8:c17b68b03791 490 printf("Sensor reading: %2.2f - %2.2f\r\n", sensorValue, rsensor);
AshuJoshi 8:c17b68b03791 491
AshuJoshi 8:c17b68b03791 492 return rsensor;
AshuJoshi 0:3ec6a7645098 493
AshuJoshi 8:c17b68b03791 494 }
AshuJoshi 0:3ec6a7645098 495
AshuJoshi 0:3ec6a7645098 496 /*****************************************************
AshuJoshi 1:36e336869699 497 * FUNCTIONS for Simple Testing
AshuJoshi 1:36e336869699 498 ****************************************************/
AshuJoshi 0:3ec6a7645098 499
AshuJoshi 0:3ec6a7645098 500 void setUpLEDBlink(){
AshuJoshi 0:3ec6a7645098 501 // configure the Ticker to blink the LED on 500ms interval
AshuJoshi 0:3ec6a7645098 502 tick.attach(&blink, 0.5);
AshuJoshi 0:3ec6a7645098 503 }
AshuJoshi 0:3ec6a7645098 504
AshuJoshi 0:3ec6a7645098 505 void endLessTestLoop() {
AshuJoshi 0:3ec6a7645098 506 while(true) {
AshuJoshi 1:36e336869699 507 // printf("Hello world!\r\n");
AshuJoshi 7:9e2454b0318a 508 //printf("BME280 Sensor: \n");
AshuJoshi 7:9e2454b0318a 509 readandprintBME280();
AshuJoshi 2:866a72c3c3bf 510
AshuJoshi 10:8071e1ae92ac 511 mDotGotoDeepSleep(60, true);
AshuJoshi 10:8071e1ae92ac 512 //wait(10);
AshuJoshi 2:866a72c3c3bf 513
AshuJoshi 0:3ec6a7645098 514 }
AshuJoshi 0:3ec6a7645098 515 }
AshuJoshi 0:3ec6a7645098 516
AshuJoshi 0:3ec6a7645098 517 // Callback function to change LED state
AshuJoshi 0:3ec6a7645098 518 void blink() {
AshuJoshi 0:3ec6a7645098 519 led = !led;
AshuJoshi 4:97f9ad3f2566 520 }
AshuJoshi 4:97f9ad3f2566 521
AshuJoshi 4:97f9ad3f2566 522