ludo teir / Mbed 2 deprecated mDot_Light_Sensor

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Workshop by The Things Network

Committer:
ludot
Date:
Thu Jun 16 09:44:05 2016 +0000
Revision:
14:02223911a495
Parent:
13:6ce90b84a3fe
Child:
15:3df9b2f9e815
optimized mDot workshop code. Quickstart for both ABP and OTAA.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:09250cd371d2 1 #include "mbed.h"
mfiore 0:09250cd371d2 2 #include "mDot.h"
mfiore 4:36e214ebfa56 3 #include "MTSLog.h"
mfiore 0:09250cd371d2 4 #include <string>
mfiore 0:09250cd371d2 5 #include <vector>
mfiore 4:36e214ebfa56 6 #include <algorithm>
mfiore 0:09250cd371d2 7
ludot 14:02223911a495 8 static bool join_mode = false; //true = OTAA / false = ABP
ludot 14:02223911a495 9
ludot 14:02223911a495 10 //enter here yout ABP address and keys
ludot 14:02223911a495 11 static uint8_t config_network_addr[] = { 0x6C, 0x63, 0x6F, 0x4B };
ludot 14:02223911a495 12 static uint8_t config_network_nskey[] = { 0x7A, 0xAC, 0x4E, 0xAD, 0xFF, 0x06, 0xFD, 0xBC, 0x2F, 0xF8, 0x2B, 0xE3, 0x9C, 0x5C, 0x78, 0x1A };
ludot 14:02223911a495 13 static uint8_t config_network_dskey[] = { 0xD0, 0x3F, 0x29, 0xCD, 0xFE, 0x43, 0x96, 0xC1, 0xCD, 0x3F, 0x2E, 0x5B, 0x81, 0x3E, 0xC6, 0x9E };
ludot 14:02223911a495 14
ludot 14:02223911a495 15 //enter here your OTAA Eui's Key
ludot 14:02223911a495 16 static uint8_t config_network_deveui [] = { 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x9F, 0xA6 };
ludot 14:02223911a495 17 static uint8_t config_network_appeui [] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x00, 0x03, 0xDD };
ludot 14:02223911a495 18 static uint8_t config_network_appkey [] = { 0xD0, 0xC1, 0xDC, 0xC9, 0x83, 0x43, 0x7E, 0x4C, 0x54, 0xE7, 0x61, 0xE0, 0x8D, 0xDA, 0xA8, 0x2E };
ludot 14:02223911a495 19
ludot 14:02223911a495 20 //configure the frequency band (only for Mdot 915 MHz)
ludot 14:02223911a495 21 static uint8_t config_frequency_sub_band = 1;
mfiore 0:09250cd371d2 22
johanstokking 9:d589fb5e68a4 23 mDot* dot;
johanstokking 9:d589fb5e68a4 24
johanstokking 10:5332d0939ebe 25 void setupNetwork();
johanstokking 10:5332d0939ebe 26
mfiore 0:09250cd371d2 27 int main() {
ludot 14:02223911a495 28
ludot 14:02223911a495 29 Serial pc(USBTX, USBRX);
johanstokking 12:343229a12653 30 // Set up the network
johanstokking 9:d589fb5e68a4 31 setupNetwork();
ludot 14:02223911a495 32
johanstokking 12:343229a12653 33 // Message you want to send
johanstokking 9:d589fb5e68a4 34 std::string data_str = "Hello!";
johanstokking 9:d589fb5e68a4 35
johanstokking 12:343229a12653 36 // Copy the message in an array of bytes
johanstokking 9:d589fb5e68a4 37 std::vector<uint8_t> data;
johanstokking 9:d589fb5e68a4 38 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
johanstokking 9:d589fb5e68a4 39 data.push_back((uint8_t) *it);
ludot 14:02223911a495 40
johanstokking 12:343229a12653 41 // Start the loop
johanstokking 10:5332d0939ebe 42 int32_t ret;
johanstokking 9:d589fb5e68a4 43 while (true) {
johanstokking 12:343229a12653 44 // Send the data
johanstokking 9:d589fb5e68a4 45 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
johanstokking 12:343229a12653 46 // Oops, there was an error, check the debug screen
johanstokking 10:5332d0939ebe 47 logError("Failed to send", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 48 //pc.printf("Failed to send");
johanstokking 9:d589fb5e68a4 49 } else {
johanstokking 12:343229a12653 50 // Sent the data
johanstokking 10:5332d0939ebe 51 logInfo("Successfully sent data");
ludot 14:02223911a495 52 //pc.printf("Successfully sent data");
johanstokking 9:d589fb5e68a4 53 }
ludot 14:02223911a495 54
johanstokking 12:343229a12653 55 // Wait 5 seconds
johanstokking 12:343229a12653 56 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
johanstokking 9:d589fb5e68a4 57 }
johanstokking 9:d589fb5e68a4 58
ludot 14:02223911a495 59 //return 0;
johanstokking 9:d589fb5e68a4 60 }
johanstokking 9:d589fb5e68a4 61
johanstokking 9:d589fb5e68a4 62 void setupNetwork() {
ludot 14:02223911a495 63
ludot 14:02223911a495 64 //Serial pc(USBTX, USBRX);
ludot 14:02223911a495 65
mfiore 0:09250cd371d2 66 int32_t ret;
johanstokking 9:d589fb5e68a4 67
mfiore 0:09250cd371d2 68 // get a mDot handle
mfiore 0:09250cd371d2 69 dot = mDot::getInstance();
ludot 14:02223911a495 70
mfiore 2:6e2c378339d9 71 // print library version information
johanstokking 10:5332d0939ebe 72 logInfo("Version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 73
mfiore 0:09250cd371d2 74 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 75 dot->resetConfig();
ludot 14:02223911a495 76
mfiore 4:36e214ebfa56 77 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 78
ludot 14:02223911a495 79 //joinmode: [MANUAL = ABP]/[OTA]/[AUTO_OTA]
ludot 14:02223911a495 80 if(join_mode)
ludot 14:02223911a495 81 dot->setJoinMode(mDot::OTA);
ludot 14:02223911a495 82 else
ludot 14:02223911a495 83 dot->setJoinMode(mDot::MANUAL);
johanstokking 13:6ce90b84a3fe 84
mfiore 2:6e2c378339d9 85 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 86 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
ludot 14:02223911a495 87
mfiore 4:36e214ebfa56 88 // frequency sub band is only applicable in the 915 (US) frequency band
johanstokking 10:5332d0939ebe 89 logInfo("Setting frequency sub band");
mfiore 0:09250cd371d2 90 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 91 logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 92 }
ludot 14:02223911a495 93
jreiss 5:6b988a804fcb 94 std::vector<uint8_t> temp;
ludot 14:02223911a495 95
ludot 14:02223911a495 96 if ((ret = dot->setPublicNetwork(join_mode)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 97 logError("Failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 8:308a67b71c86 98 }
johanstokking 13:6ce90b84a3fe 99
ludot 14:02223911a495 100 //Configuring OTAA/ABP
ludot 14:02223911a495 101 if (join_mode){
ludot 14:02223911a495 102 for (int i = 0; i < 4; i++) {
ludot 14:02223911a495 103 temp.push_back(config_network_addr[i]);
ludot 14:02223911a495 104 }
ludot 14:02223911a495 105
ludot 14:02223911a495 106 logInfo("setting network addr");
ludot 14:02223911a495 107 if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 108 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 109 }
ludot 14:02223911a495 110
ludot 14:02223911a495 111 temp.clear();
ludot 14:02223911a495 112 for (int i = 0; i < 16; i++) {
ludot 14:02223911a495 113 temp.push_back(config_network_nskey[i]);
ludot 14:02223911a495 114 }
ludot 14:02223911a495 115
ludot 14:02223911a495 116 logInfo("setting network password");
ludot 14:02223911a495 117 if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 118 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 119 }
ludot 14:02223911a495 120
ludot 14:02223911a495 121 temp.clear();
ludot 14:02223911a495 122 for (int i = 0; i < 16; i++) {
ludot 14:02223911a495 123 temp.push_back(config_network_dskey[i]);
ludot 14:02223911a495 124 }
ludot 14:02223911a495 125
ludot 14:02223911a495 126 logInfo("setting network password");
ludot 14:02223911a495 127 if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 128 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 129 }
ludot 14:02223911a495 130 }else{
ludot 14:02223911a495 131 temp.clear();
ludot 14:02223911a495 132 for (int i = 0; i < 8; i++) {
ludot 14:02223911a495 133 temp.push_back(config_network_deveui[i]);
ludot 14:02223911a495 134 }
ludot 14:02223911a495 135
ludot 14:02223911a495 136 logInfo("setting Device EUI");
ludot 14:02223911a495 137 if ((ret = dot->setDeviceId(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 138 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 139 }
ludot 14:02223911a495 140
ludot 14:02223911a495 141 temp.clear();
ludot 14:02223911a495 142 for (int i = 0; i < 8; i++) {
ludot 14:02223911a495 143 temp.push_back(config_network_appeui[i]);
ludot 14:02223911a495 144 }
ludot 14:02223911a495 145
ludot 14:02223911a495 146
ludot 14:02223911a495 147 logInfo("setting App Eui");
ludot 14:02223911a495 148 if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 149 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 150 }
ludot 14:02223911a495 151
ludot 14:02223911a495 152 temp.clear();
ludot 14:02223911a495 153 for (int i = 0; i < 16; i++) {
ludot 14:02223911a495 154 temp.push_back(config_network_appkey[i]);
ludot 14:02223911a495 155 }
ludot 14:02223911a495 156
ludot 14:02223911a495 157 logInfo("setting App Key");
ludot 14:02223911a495 158 if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) {
ludot 14:02223911a495 159 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 160 }
johanstokking 13:6ce90b84a3fe 161 }
ludot 14:02223911a495 162
mfiore 4:36e214ebfa56 163 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 164 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 165 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
johanstokking 10:5332d0939ebe 166 logInfo("Setting TX spreading factor");
johanstokking 9:d589fb5e68a4 167 if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 168 logError("Failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 169 }
ludot 14:02223911a495 170
mfiore 4:36e214ebfa56 171 // request receive confirmation of packets from the gateway
johanstokking 13:6ce90b84a3fe 172 logInfo("Disabling ACKs");
johanstokking 13:6ce90b84a3fe 173 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 174 logError("Failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 175 }
ludot 14:02223911a495 176
mfiore 4:36e214ebfa56 177 // save this configuration to the mDot's NVM
johanstokking 10:5332d0939ebe 178 logInfo("Saving config");
mfiore 2:6e2c378339d9 179 if (! dot->saveConfig()) {
johanstokking 10:5332d0939ebe 180 logError("Failed to save configuration");
mfiore 0:09250cd371d2 181 }
mfiore 0:09250cd371d2 182
mfiore 0:09250cd371d2 183 // attempt to join the network
ludot 14:02223911a495 184 logInfo("Joining network");
ludot 14:02223911a495 185 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
ludot 14:02223911a495 186 logError("Failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 14:02223911a495 187 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
ludot 14:02223911a495 188 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
ludot 14:02223911a495 189 }
ludot 14:02223911a495 190 logInfo("Successfully joined the network");
ludot 14:02223911a495 191 }