Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BME280 SerialGPS libmDot mbed-rtos mbed
Revision 5:4bc6ba66f28e, committed 2016-07-07
- Comitter:
- AshuJoshi
- Date:
- Thu Jul 07 01:24:10 2016 +0000
- Parent:
- 4:97f9ad3f2566
- Child:
- 6:35f934e83c74
- Commit message:
- Added code to configure the network for LoRa / TTN
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Jul 07 00:07:45 2016 +0000
+++ b/main.cpp Thu Jul 07 01:24:10 2016 +0000
@@ -11,13 +11,44 @@
#include "mDot.h"
#include "MTSLog.h"
#include "MTSText.h"
+ //#include "SerialGPS.h"
#include "BME280.h"
+
using namespace mts;
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
+
+// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+
+// Values as used by The Things Network
+// Application session key
+uint8_t AppSKey[16]= { 0x91, 0x5F, 0xCD, 0x2A, 0xED, 0x8E, 0x0C, 0x2B, 0x30, 0xEF, 0x35, 0x8D, 0xF7, 0xE7, 0x89, 0x0A };
+// Network session key
+uint8_t NwkSKey[16]= { 0x60, 0xBF, 0x44, 0xA9, 0x56, 0x0A, 0x4C, 0xB4, 0xF2, 0xEB, 0xB1, 0x6B, 0x9A, 0x2C, 0x57, 0x32 };
+
+// App Key 1DD7BB3D3E43ED13029996BEC25BF190
+uint8_t AppKey[16] = {0x1D, 0xD7, 0xBB, 0x3D, 0x3E, 0x43, 0xED, 0x13, 0x02, 0x99, 0x96, 0xBE, 0xC2, 0x5B, 0xF1, 0x90};
+// App EUI 70B3D57ED00005D5
+uint8_t AppEUI[8] = {0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x05, 0xD5};
+
+
+// Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
+//uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID
+uint8_t NetworkAddr[4] = { 0x08, 0xBE, 0xAB, 0x8A };
+
+// Some defines for the LoRa configuration
+#define LORA_ACK 0
+#define LORA_TXPOWER 20
+
+//Ignoring sub band for EU modules.
+static uint8_t config_frequency_sub_band = 7;
+
+// <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
+
+
// mDot UDK Specific
// MDot Pinout: https://developer.mbed.org/platforms/MTS-mDot-F411/#pinout-diagram
@@ -30,10 +61,14 @@
DigitalOut led(XBEE_RSSI);
#endif
+//SerialGPS gps(PA_2, PA_3);
//BME280 sensor(I2C_SDA, I2C_SCL)
// MDot UDK - I2C_SDA and I2C_SCL connected to PC_9/PA_*
BME280 b280(PC_9, PA_8);
+// Serial via USB for debugging only
+//Serial pc(USBTX,USBRX);
+
// Globals
Ticker tick;
@@ -49,6 +84,8 @@
void mDotGotoDeepSleep(int seconds);
void mDotConfigPrint();
void initSerialGPS();
+void setupNetwork();
+void joinNetwork();
@@ -60,6 +97,7 @@
// Simple Test Functions, "Hello World on UDK
setUpLEDBlink();
mDotConfig();
+ setupNetwork();
mDotConfigPrint();
endLessTestLoop();
@@ -77,8 +115,7 @@
dot = mDot::getInstance();
//dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);
- // print library version information
- logInfo("version: %s", dot->getId().c_str());
+
}
void mDotGotoDeepSleep(int seconds) {
@@ -91,45 +128,180 @@
dot->sleep(sleep_time, mDot::RTC_ALARM);
}
+void setupNetwork(){
+ int32_t ret;
+ std::vector<uint8_t> send_data;
+ std::vector<uint8_t> recv_data;
+ std::vector<uint8_t> nwkSKey;
+ std::vector<uint8_t> appSKey;
+ std::vector<uint8_t> nodeAddr;
+ std::vector<uint8_t> networkAddr;
+ // from OTAA
+ std::vector<uint8_t> appEUI;
+ std::vector<uint8_t> appKey;
+
+ // get a mDot handle
+ // already done in mDotConfig
+ //dot = mDot::getInstance();
+
+ logInfo("Checking Config");
+
+ // Test if we've already saved the config
+ std::string configNetworkName = dot->getNetworkName();
+
+ uint8_t *it = NwkSKey;
+ for (uint8_t i = 0; i<16; i++)
+ nwkSKey.push_back((uint8_t) *it++);
+ it = AppSKey;
+ for (uint8_t i = 0; i<16; i++)
+ appSKey.push_back((uint8_t) *it++);
+
+
+ it = AppEUI;
+ for (uint8_t i = 0; i<8; i++)
+ appEUI.push_back((uint8_t) *it++);
+
+ it = AppKey;
+ for (uint8_t i = 0; i<16; i++)
+ appKey.push_back((uint8_t) *it++);
+
+ it = NetworkAddr;
+ for (uint8_t i = 0; i<4; i++)
+ networkAddr.push_back((uint8_t) *it++);
+
+ logInfo("Resetting Config");
+ // reset to default config so we know what state we're in
+ dot->resetConfig();
+
+ // Set byte order - AEP less than 1.0.30
+ //dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit
+
+ // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
+ // Lower is higher data rate, larger packets and shorter range.
+ logInfo("Set SF");
+ //if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) {
+ if((ret = dot->setTxDataRate( mDot::SF_8 )) != mDot::MDOT_OK) {
+ logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ //logInfo("Set TxPower");
+ //if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
+ // logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ //}
+
+ logInfo("Set Public mode");
+ if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
+ logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ //logInfo("Set MANUAL Join mode");
+ //if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
+ // logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ //}
+
+ logInfo("Set AUTO_OTA Join mode");
+ if((ret = dot->setJoinMode(mDot::AUTO_OTA)) != mDot::MDOT_OK) {
+ logError("Failed to set AUTO_OTA Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ logInfo("Set Ack");
+ // 1 retries on Ack, 0 to disable
+ if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
+ logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+// Not applicable for 868MHz in EU
+ if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
+ logError("Failed to set frequency sub band %s", ret);
+ }
+
+ logInfo("Set Network Address");
+ if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
+ logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ logInfo("Set Data Session Key");
+ if ((ret = dot->setDataSessionKey(appSKey)) != mDot::MDOT_OK) {
+ logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ logInfo("Set Network Session Key");
+ if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
+ logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ logInfo("Set Network Id");
+ if ((ret = dot->setNetworkId(appEUI)) != mDot::MDOT_OK) {
+ logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+ logInfo("Set Network Key");
+ if ((ret = dot->setNetworkKey(appKey)) != mDot::MDOT_OK) {
+ logError("Failed to set Network Id %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
+ }
+
+ logInfo("Saving Config");
+ // Save config
+ if (! dot->saveConfig()) {
+ logError("failed to save configuration");
+ }
+
+ //*******************************************
+ // end of configuration
+ //*******************************************
+
+ //char dataBuf[50];
+
+
+
+}
+
+void joinNetwork() {
+ int32_t ret;
+ logInfo("Joining Network");
+ while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
+ logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
+ //wait_ms(dot->getNextTxMs() + 1);
+ osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
+ }
+ logInfo("Joined Network");
+ }
void mDotConfigPrint() {
// Display what is set
printf("\r\n");
printf(" ********** mDot Configuration ************ \n");
+ // print library version information
+ logInfo("Firmware Version: %s", dot->getId().c_str());
+
std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
printf("Network Session Key: ");
printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
-
+
tmp = dot->getDataSessionKey();
printf("Data Session Key: ");
printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
- // App EUI
tmp = dot->getNetworkId();
printf("App EUI: ");
printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
- // App Key
tmp = dot->getNetworkKey();
printf("App Key: ");
printf("%s\n", mts::Text::bin2hexString(tmp, " ").c_str());
-
printf("Device ID ");
std::vector<uint8_t> deviceId;
deviceId = dot->getDeviceId();
for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
printf("%2.2x",*it );
printf("\n");
-
std::vector<uint8_t> netAddress;
printf("Network Address ");
netAddress = dot->getNetworkAddress();
for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
printf("%2.2x",*it );
-
printf("\n");
// Display LoRa parameters
@@ -163,11 +335,11 @@
float pressure;
float humidity;
char string_buffer[64];
- time_t secs;
+ //time_t secs;
- secs = time(NULL);
- printf("Seconds since January 1, 1970: %d\n", secs);
- printf("Time as a basic string = %s", ctime(&secs));
+ //secs = time(NULL);
+ //printf("Seconds since January 1, 1970: %d\n", secs);
+ //printf("Time as a basic string = %s", ctime(&secs));
// Temperature
temperature = b280.getTemperature();
@@ -182,7 +354,7 @@
sprintf(string_buffer, "%s%03.2f", "H%:", humidity);
logInfo("The humidty is %s", string_buffer);
- // printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
+ //printf("%2.2f degC, %04.2f hPa, %2.2f %%\n", temperature, pressure, humidity);
}
@@ -202,7 +374,8 @@
printf("BME280 Sensor: \n");
readandprintBME280();
- mDotGotoDeepSleep(60);
+ wait(5);
+ //mDotGotoDeepSleep(60);
//wait(5);
}