ludo teir / Mbed 2 deprecated mDot_Light_Sensor

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Workshop by The Things Network

Committer:
johanstokking
Date:
Mon Apr 04 22:26:40 2016 +0000
Revision:
9:d589fb5e68a4
Parent:
8:308a67b71c86
Child:
10:5332d0939ebe
Network setup moved to method for readibility

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
johanstokking 8:308a67b71c86 8 static uint8_t config_app_eui[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
johanstokking 8:308a67b71c86 9 static uint8_t config_app_key[] = { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x45 };
johanstokking 8:308a67b71c86 10 static uint8_t config_frequency_sub_band = 7;
mfiore 0:09250cd371d2 11
johanstokking 9:d589fb5e68a4 12 mDot* dot;
johanstokking 9:d589fb5e68a4 13
mfiore 0:09250cd371d2 14 int main() {
johanstokking 9:d589fb5e68a4 15 setupNetwork();
johanstokking 9:d589fb5e68a4 16
johanstokking 9:d589fb5e68a4 17 std::string data_str = "Hello!";
johanstokking 9:d589fb5e68a4 18
johanstokking 9:d589fb5e68a4 19 // format data for sending to the gateway
johanstokking 9:d589fb5e68a4 20 std::vector<uint8_t> data;
johanstokking 9:d589fb5e68a4 21 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
johanstokking 9:d589fb5e68a4 22 data.push_back((uint8_t) *it);
johanstokking 9:d589fb5e68a4 23
johanstokking 9:d589fb5e68a4 24 while (true) {
johanstokking 9:d589fb5e68a4 25 // send the data to the gateway
johanstokking 9:d589fb5e68a4 26 int32_t ret;
johanstokking 9:d589fb5e68a4 27 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
johanstokking 9:d589fb5e68a4 28 logError("failed to send", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 9:d589fb5e68a4 29 } else {
johanstokking 9:d589fb5e68a4 30 logInfo("successfully sent data to gateway");
johanstokking 9:d589fb5e68a4 31 }
johanstokking 9:d589fb5e68a4 32
johanstokking 9:d589fb5e68a4 33 osDelay(std::max((uint32_t)5000, (uint32_t)dot->getNextTxMs()));
johanstokking 9:d589fb5e68a4 34 }
johanstokking 9:d589fb5e68a4 35
johanstokking 9:d589fb5e68a4 36 return 0;
johanstokking 9:d589fb5e68a4 37 }
johanstokking 9:d589fb5e68a4 38
johanstokking 9:d589fb5e68a4 39 void setupNetwork() {
mfiore 0:09250cd371d2 40 int32_t ret;
johanstokking 9:d589fb5e68a4 41
mfiore 0:09250cd371d2 42 // get a mDot handle
mfiore 0:09250cd371d2 43 dot = mDot::getInstance();
mfiore 2:6e2c378339d9 44
mfiore 2:6e2c378339d9 45 // print library version information
mfiore 2:6e2c378339d9 46 logInfo("version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 47
mfiore 2:6e2c378339d9 48 //*******************************************
mfiore 2:6e2c378339d9 49 // configuration
mfiore 2:6e2c378339d9 50 //*******************************************
mfiore 0:09250cd371d2 51 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 52 dot->resetConfig();
mfiore 0:09250cd371d2 53
mfiore 4:36e214ebfa56 54 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 55
mfiore 2:6e2c378339d9 56 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 57 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
mfiore 4:36e214ebfa56 58
mfiore 4:36e214ebfa56 59 // frequency sub band is only applicable in the 915 (US) frequency band
mfiore 4:36e214ebfa56 60 // if using a MultiTech Conduit gateway, use the same sub band as your Conduit (1-8) - the mDot will use the 8 channels in that sub band
mfiore 4:36e214ebfa56 61 // if using a gateway that supports all 64 channels, use sub band 0 - the mDot will use all 64 channels
mfiore 2:6e2c378339d9 62 logInfo("setting frequency sub band");
mfiore 0:09250cd371d2 63 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 64 logError("failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 65 }
mfiore 4:36e214ebfa56 66
jreiss 5:6b988a804fcb 67 std::vector<uint8_t> temp;
jreiss 5:6b988a804fcb 68
johanstokking 8:308a67b71c86 69 for (int i = 0; i < 8; i++) {
jreiss 6:ac306e26e4cc 70 temp.push_back(config_app_eui[i]);
jreiss 5:6b988a804fcb 71 }
jreiss 5:6b988a804fcb 72
johanstokking 8:308a67b71c86 73 if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
johanstokking 9:d589fb5e68a4 74 logError("failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 8:308a67b71c86 75 }
johanstokking 8:308a67b71c86 76
jreiss 6:ac306e26e4cc 77 logInfo("setting app eui");
jreiss 6:ac306e26e4cc 78 if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) {
jreiss 6:ac306e26e4cc 79 logError("failed to set app eui %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 80 }
mfiore 4:36e214ebfa56 81
jreiss 5:6b988a804fcb 82 temp.clear();
jreiss 5:6b988a804fcb 83 for (int i = 0; i < 16; i++) {
jreiss 6:ac306e26e4cc 84 temp.push_back(config_app_key[i]);
jreiss 5:6b988a804fcb 85 }
jreiss 5:6b988a804fcb 86
jreiss 6:ac306e26e4cc 87 logInfo("setting app key");
jreiss 6:ac306e26e4cc 88 if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) {
jreiss 6:ac306e26e4cc 89 logError("failed to set app key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 90 }
mfiore 4:36e214ebfa56 91
mfiore 4:36e214ebfa56 92 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 93 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 94 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
mfiore 4:36e214ebfa56 95 logInfo("setting TX spreading factor");
johanstokking 9:d589fb5e68a4 96 if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 97 logError("failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 98 }
mfiore 4:36e214ebfa56 99
mfiore 4:36e214ebfa56 100 // request receive confirmation of packets from the gateway
mfiore 4:36e214ebfa56 101 logInfo("enabling ACKs");
mfiore 4:36e214ebfa56 102 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
mfiore 4:36e214ebfa56 103 logError("failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 104 }
mfiore 4:36e214ebfa56 105
mfiore 4:36e214ebfa56 106 // save this configuration to the mDot's NVM
mfiore 2:6e2c378339d9 107 logInfo("saving config");
mfiore 2:6e2c378339d9 108 if (! dot->saveConfig()) {
mfiore 2:6e2c378339d9 109 logError("failed to save configuration");
mfiore 0:09250cd371d2 110 }
mfiore 0:09250cd371d2 111
mfiore 0:09250cd371d2 112 // attempt to join the network
mfiore 2:6e2c378339d9 113 logInfo("joining network");
mfiore 0:09250cd371d2 114 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
mfiore 2:6e2c378339d9 115 logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 116 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 117 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 118 }
johanstokking 9:d589fb5e68a4 119 }