ludo teir / Mbed 2 deprecated mDot_Light_Sensor

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Workshop by The Things Network

Committer:
johanstokking
Date:
Tue Apr 05 02:14:38 2016 +0000
Revision:
10:5332d0939ebe
Parent:
9:d589fb5e68a4
Child:
12:343229a12653
Added temperature sample

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
johanstokking 10:5332d0939ebe 14 void setupNetwork();
johanstokking 10:5332d0939ebe 15
mfiore 0:09250cd371d2 16 int main() {
johanstokking 9:d589fb5e68a4 17 setupNetwork();
johanstokking 9:d589fb5e68a4 18
johanstokking 9:d589fb5e68a4 19 std::string data_str = "Hello!";
johanstokking 9:d589fb5e68a4 20
johanstokking 9:d589fb5e68a4 21 std::vector<uint8_t> data;
johanstokking 9:d589fb5e68a4 22 for (std::string::iterator it = data_str.begin(); it != data_str.end(); it++)
johanstokking 9:d589fb5e68a4 23 data.push_back((uint8_t) *it);
johanstokking 10:5332d0939ebe 24
johanstokking 10:5332d0939ebe 25 int32_t ret;
johanstokking 9:d589fb5e68a4 26 while (true) {
johanstokking 9:d589fb5e68a4 27 if ((ret = dot->send(data)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 28 logError("Failed to send", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 9:d589fb5e68a4 29 } else {
johanstokking 10:5332d0939ebe 30 logInfo("Successfully sent data");
johanstokking 9:d589fb5e68a4 31 }
johanstokking 10:5332d0939ebe 32
johanstokking 10:5332d0939ebe 33 osDelay(std::max((uint32_t)20000, (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
johanstokking 10:5332d0939ebe 46 logInfo("Version: %s", dot->getId().c_str());
mfiore 0:09250cd371d2 47
mfiore 0:09250cd371d2 48 // reset to default config so we know what state we're in
mfiore 0:09250cd371d2 49 dot->resetConfig();
mfiore 0:09250cd371d2 50
mfiore 4:36e214ebfa56 51 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
mfiore 0:09250cd371d2 52
mfiore 2:6e2c378339d9 53 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 54 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
mfiore 4:36e214ebfa56 55
mfiore 4:36e214ebfa56 56 // frequency sub band is only applicable in the 915 (US) frequency band
johanstokking 10:5332d0939ebe 57 logInfo("Setting frequency sub band");
mfiore 0:09250cd371d2 58 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 59 logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 60 }
mfiore 4:36e214ebfa56 61
jreiss 5:6b988a804fcb 62 std::vector<uint8_t> temp;
jreiss 5:6b988a804fcb 63
johanstokking 8:308a67b71c86 64 for (int i = 0; i < 8; i++) {
jreiss 6:ac306e26e4cc 65 temp.push_back(config_app_eui[i]);
jreiss 5:6b988a804fcb 66 }
jreiss 5:6b988a804fcb 67
johanstokking 8:308a67b71c86 68 if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 69 logError("Failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 8:308a67b71c86 70 }
johanstokking 8:308a67b71c86 71
johanstokking 10:5332d0939ebe 72 logInfo("Setting app eui");
jreiss 6:ac306e26e4cc 73 if ((ret = dot->setNetworkId(temp)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 74 logError("Failed to set app eui %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 75 }
mfiore 4:36e214ebfa56 76
jreiss 5:6b988a804fcb 77 temp.clear();
jreiss 5:6b988a804fcb 78 for (int i = 0; i < 16; i++) {
jreiss 6:ac306e26e4cc 79 temp.push_back(config_app_key[i]);
jreiss 5:6b988a804fcb 80 }
jreiss 5:6b988a804fcb 81
johanstokking 10:5332d0939ebe 82 logInfo("Setting app key");
jreiss 6:ac306e26e4cc 83 if ((ret = dot->setNetworkKey(temp)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 84 logError("Failed to set app key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 0:09250cd371d2 85 }
mfiore 4:36e214ebfa56 86
mfiore 4:36e214ebfa56 87 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 88 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 89 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
johanstokking 10:5332d0939ebe 90 logInfo("Setting TX spreading factor");
johanstokking 9:d589fb5e68a4 91 if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 92 logError("Failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 93 }
mfiore 4:36e214ebfa56 94
mfiore 4:36e214ebfa56 95 // request receive confirmation of packets from the gateway
johanstokking 10:5332d0939ebe 96 logInfo("Enabling ACKs");
mfiore 4:36e214ebfa56 97 if ((ret = dot->setAck(1)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 98 logError("Failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 99 }
mfiore 4:36e214ebfa56 100
mfiore 4:36e214ebfa56 101 // save this configuration to the mDot's NVM
johanstokking 10:5332d0939ebe 102 logInfo("Saving config");
mfiore 2:6e2c378339d9 103 if (! dot->saveConfig()) {
johanstokking 10:5332d0939ebe 104 logError("Failed to save configuration");
mfiore 0:09250cd371d2 105 }
mfiore 0:09250cd371d2 106
mfiore 0:09250cd371d2 107 // attempt to join the network
johanstokking 10:5332d0939ebe 108 logInfo("Joining network");
mfiore 0:09250cd371d2 109 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 110 logError("Failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 111 // in the 868 (EU) frequency band, we need to wait until another channel is available before transmitting again
mfiore 4:36e214ebfa56 112 osDelay(std::max((uint32_t)1000, (uint32_t)dot->getNextTxMs()));
mfiore 0:09250cd371d2 113 }
johanstokking 9:d589fb5e68a4 114 }