Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_Workshop by The Things Network

Committer:
ludot
Date:
Fri Jun 17 13:04:18 2016 +0000
Revision:
15:3df9b2f9e815
Parent:
14:02223911a495
analog sensor example v0

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"
ludot 15:3df9b2f9e815 4 #include <AnalogIn.h>
mfiore 0:09250cd371d2 5 #include <string>
mfiore 0:09250cd371d2 6 #include <vector>
mfiore 4:36e214ebfa56 7 #include <algorithm>
mfiore 0:09250cd371d2 8
ludot 15:3df9b2f9e815 9 static uint8_t config_network_addr[] = { 0x44, 0x08, 0xCC, 0x9A };
ludot 15:3df9b2f9e815 10 static uint8_t config_network_nskey[] = { 0x99, 0xC8, 0xA0, 0x4C, 0x50, 0x25, 0x94, 0xD2, 0xEB, 0xFD, 0xD9, 0x37, 0x56, 0x3C, 0x83, 0x32 };
ludot 15:3df9b2f9e815 11 static uint8_t config_network_dskey[] = { 0x07, 0xA5, 0x9B, 0xAD, 0x11, 0x0B, 0x94, 0x6E, 0xEE, 0xE5, 0xF0, 0x87, 0xD0, 0x10, 0x35, 0x07 };
ludot 14:02223911a495 12
ludot 15:3df9b2f9e815 13 static uint8_t config_frequency_sub_band = 7;
mfiore 0:09250cd371d2 14
johanstokking 9:d589fb5e68a4 15 mDot* dot;
johanstokking 9:d589fb5e68a4 16
johanstokking 10:5332d0939ebe 17 void setupNetwork();
ludot 15:3df9b2f9e815 18 //int readLight();
ludot 15:3df9b2f9e815 19
ludot 15:3df9b2f9e815 20 //AnalogIn light(A0);
johanstokking 10:5332d0939ebe 21
mfiore 0:09250cd371d2 22 int main() {
ludot 15:3df9b2f9e815 23
ludot 14:02223911a495 24 Serial pc(USBTX, USBRX);
johanstokking 12:343229a12653 25 // Set up the network
johanstokking 9:d589fb5e68a4 26 setupNetwork();
ludot 15:3df9b2f9e815 27
johanstokking 12:343229a12653 28 // Start the loop
johanstokking 10:5332d0939ebe 29 int32_t ret;
johanstokking 9:d589fb5e68a4 30 while (true) {
ludot 15:3df9b2f9e815 31
ludot 15:3df9b2f9e815 32 //PB_0 = Analog pin 1
ludot 15:3df9b2f9e815 33 AnalogIn pinn(PB_0);
ludot 15:3df9b2f9e815 34
ludot 15:3df9b2f9e815 35 //read voltage
ludot 15:3df9b2f9e815 36 uint16_t light = (int)(pinn.read()*1023);
ludot 15:3df9b2f9e815 37 logInfo("Light level :%d \n", light);
ludot 15:3df9b2f9e815 38
ludot 15:3df9b2f9e815 39 // Copy the message in an array of bytes
ludot 15:3df9b2f9e815 40 std::vector<uint8_t> data;
ludot 15:3df9b2f9e815 41 for (int i = 0; i <= sizeof(light); i++)
ludot 15:3df9b2f9e815 42 data.push_back((uint8_t)(light >> ((8*i-8) & 0xFF)));
ludot 15:3df9b2f9e815 43
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 15:3df9b2f9e815 54
johanstokking 12:343229a12653 55 // Wait 5 seconds
ludot 15:3df9b2f9e815 56 osDelay(std::max((uint32_t)1000, (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 15:3df9b2f9e815 63
ludot 14:02223911a495 64 //Serial pc(USBTX, USBRX);
ludot 15:3df9b2f9e815 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 15:3df9b2f9e815 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 15:3df9b2f9e815 76
mfiore 4:36e214ebfa56 77 dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
ludot 15:3df9b2f9e815 78
ludot 14:02223911a495 79 //joinmode: [MANUAL = ABP]/[OTA]/[AUTO_OTA]
ludot 15:3df9b2f9e815 80 dot->setJoinMode(mDot::MANUAL);
ludot 15:3df9b2f9e815 81
mfiore 2:6e2c378339d9 82 // set up the mDot with our network information: frequency sub band, network name, and network password
mfiore 2:6e2c378339d9 83 // these can all be saved in NVM so they don't need to be set every time - see mDot::saveConfig()
ludot 15:3df9b2f9e815 84
mfiore 4:36e214ebfa56 85 // frequency sub band is only applicable in the 915 (US) frequency band
johanstokking 10:5332d0939ebe 86 logInfo("Setting frequency sub band");
mfiore 0:09250cd371d2 87 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 88 logError("Failed to set frequency sub band %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 2:6e2c378339d9 89 }
ludot 15:3df9b2f9e815 90
jreiss 5:6b988a804fcb 91 std::vector<uint8_t> temp;
ludot 15:3df9b2f9e815 92
ludot 14:02223911a495 93
ludot 15:3df9b2f9e815 94 if ((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 95 logError("Failed to enable public network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
johanstokking 8:308a67b71c86 96 }
ludot 15:3df9b2f9e815 97
ludot 14:02223911a495 98
ludot 15:3df9b2f9e815 99 for (int i = 0; i < 4; i++) {
ludot 15:3df9b2f9e815 100 temp.push_back(config_network_addr[i]);
ludot 15:3df9b2f9e815 101 }
ludot 15:3df9b2f9e815 102
ludot 15:3df9b2f9e815 103 logInfo("setting network addr");
ludot 15:3df9b2f9e815 104 if ((ret = dot->setNetworkAddress(temp)) != mDot::MDOT_OK) {
ludot 15:3df9b2f9e815 105 logError("failed to set network name %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 15:3df9b2f9e815 106 }
ludot 15:3df9b2f9e815 107
ludot 15:3df9b2f9e815 108 temp.clear();
ludot 15:3df9b2f9e815 109 for (int i = 0; i < 16; i++) {
ludot 15:3df9b2f9e815 110 temp.push_back(config_network_nskey[i]);
johanstokking 13:6ce90b84a3fe 111 }
ludot 15:3df9b2f9e815 112
ludot 15:3df9b2f9e815 113 logInfo("setting network password");
ludot 15:3df9b2f9e815 114 if ((ret = dot->setNetworkSessionKey(temp)) != mDot::MDOT_OK) {
ludot 15:3df9b2f9e815 115 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 15:3df9b2f9e815 116 }
ludot 15:3df9b2f9e815 117
ludot 15:3df9b2f9e815 118 temp.clear();
ludot 15:3df9b2f9e815 119 for (int i = 0; i < 16; i++) {
ludot 15:3df9b2f9e815 120 temp.push_back(config_network_dskey[i]);
ludot 15:3df9b2f9e815 121 }
ludot 15:3df9b2f9e815 122
ludot 15:3df9b2f9e815 123 logInfo("setting network password");
ludot 15:3df9b2f9e815 124 if ((ret = dot->setDataSessionKey(temp)) != mDot::MDOT_OK) {
ludot 15:3df9b2f9e815 125 logError("failed to set network password %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
ludot 15:3df9b2f9e815 126 }
ludot 15:3df9b2f9e815 127
mfiore 4:36e214ebfa56 128 // a higher spreading factor allows for longer range but lower throughput
mfiore 4:36e214ebfa56 129 // in the 915 (US) frequency band, spreading factors 7 - 10 are available
mfiore 4:36e214ebfa56 130 // in the 868 (EU) frequency band, spreading factors 7 - 12 are available
johanstokking 10:5332d0939ebe 131 logInfo("Setting TX spreading factor");
johanstokking 9:d589fb5e68a4 132 if ((ret = dot->setTxDataRate(mDot::SF_8)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 133 logError("Failed to set TX datarate %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 134 }
ludot 15:3df9b2f9e815 135
mfiore 4:36e214ebfa56 136 // request receive confirmation of packets from the gateway
johanstokking 13:6ce90b84a3fe 137 logInfo("Disabling ACKs");
johanstokking 13:6ce90b84a3fe 138 if ((ret = dot->setAck(0)) != mDot::MDOT_OK) {
johanstokking 10:5332d0939ebe 139 logError("Failed to enable ACKs %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
mfiore 4:36e214ebfa56 140 }
ludot 15:3df9b2f9e815 141
mfiore 4:36e214ebfa56 142 // save this configuration to the mDot's NVM
johanstokking 10:5332d0939ebe 143 logInfo("Saving config");
mfiore 2:6e2c378339d9 144 if (! dot->saveConfig()) {
johanstokking 10:5332d0939ebe 145 logError("Failed to save configuration");
mfiore 0:09250cd371d2 146 }
ludot 15:3df9b2f9e815 147 }