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:26:39 2016 +0000
Revision:
8:c17b68b03791
Parent:
7:9e2454b0318a
Child:
10:8071e1ae92ac
Sending data to GW/TTN working, sending sensor data from BME280 and the Light Sensor

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