EasyButtonCode

Dependencies:   libmDot mbed-rtos mbed

Fork of mDot_TTN_DS18B20 by Thing Innovations

Committer:
SomeRandomBloke
Date:
Sat Nov 14 20:17:03 2015 +0000
Revision:
10:717a3d909c6b
Parent:
9:086351e54b57
Child:
11:38ce10209eff
Release version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 5:48eb9245a914 1 /** mDot_TTN_DS18B20 - Simple mDot temperature sensor using Dallas Semiconductors DS18B20 OneWire temperature sensor.
SomeRandomBloke 9:086351e54b57 2 * It used the MANUAL join mode with parameters for The Things Network.
SomeRandomBloke 9:086351e54b57 3 *
SomeRandomBloke 2:9db840d12557 4 *
SomeRandomBloke 0:5a0b43f3b143 5 * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF
SomeRandomBloke 0:5a0b43f3b143 6 * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF
SomeRandomBloke 5:48eb9245a914 7 * http://www.multitech.net/developer/software/lora/conduit-mlinux-convert-to-basic-packet-forwarder/
SomeRandomBloke 5:48eb9245a914 8 * http://forum.thethingsnetwork.org/t/setting-up-multitech-conduit-gateway-for-ttn/216/35
SomeRandomBloke 0:5a0b43f3b143 9 *
SomeRandomBloke 0:5a0b43f3b143 10 */
SomeRandomBloke 0:5a0b43f3b143 11
SomeRandomBloke 0:5a0b43f3b143 12 #include "mbed.h"
SomeRandomBloke 0:5a0b43f3b143 13 #include "DS1820.h"
SomeRandomBloke 0:5a0b43f3b143 14 #include "mDot.h"
SomeRandomBloke 0:5a0b43f3b143 15 #include "MTSLog.h"
SomeRandomBloke 0:5a0b43f3b143 16 #include "MTSText.h"
SomeRandomBloke 0:5a0b43f3b143 17 #include <string>
SomeRandomBloke 0:5a0b43f3b143 18 #include <vector>
SomeRandomBloke 0:5a0b43f3b143 19
SomeRandomBloke 0:5a0b43f3b143 20 using namespace mts;
SomeRandomBloke 0:5a0b43f3b143 21
SomeRandomBloke 4:f649ab1b61d1 22 #define MIN(a,b) (((a)<(b))?(a):(b))
SomeRandomBloke 4:f649ab1b61d1 23 #define MAX(a,b) (((a)>(b))?(a):(b))
SomeRandomBloke 4:f649ab1b61d1 24
SomeRandomBloke 8:8070e9d660e4 25 // Values as used by The Things Network
SomeRandomBloke 5:48eb9245a914 26 //const uint8_t AppKey[16]={0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
SomeRandomBloke 5:48eb9245a914 27 // Application session key
SomeRandomBloke 5:48eb9245a914 28 uint8_t AppSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
SomeRandomBloke 5:48eb9245a914 29 // Network session key
SomeRandomBloke 5:48eb9245a914 30 uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C};
SomeRandomBloke 8:8070e9d660e4 31
SomeRandomBloke 9:086351e54b57 32 // Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace
SomeRandomBloke 10:717a3d909c6b 33 uint8_t NetworkAddr[4]= {0x02,0x01,0xXX,0xXX}; // Our Network address or Node ID
SomeRandomBloke 8:8070e9d660e4 34
SomeRandomBloke 8:8070e9d660e4 35
SomeRandomBloke 8:8070e9d660e4 36 // Some defines for the LoRa configuration
SomeRandomBloke 8:8070e9d660e4 37 #define LORA_SF mDot::SF_12
SomeRandomBloke 8:8070e9d660e4 38 #define LORA_ACK 0
SomeRandomBloke 8:8070e9d660e4 39 #define LORA_TXPOWER 14
SomeRandomBloke 5:48eb9245a914 40
SomeRandomBloke 3:367aa95f9771 41 // Ignoring sub band for EU modules.
SomeRandomBloke 1:45cec6aea002 42 //static uint8_t config_frequency_sub_band = 1;
SomeRandomBloke 0:5a0b43f3b143 43
SomeRandomBloke 2:9db840d12557 44 // DS18B20 OneWire pin
SomeRandomBloke 8:8070e9d660e4 45 // D13 on Dev Board, pin 18 on mDot, Compatible with Oxford Flood Network PCB temperature sensor.
SomeRandomBloke 1:45cec6aea002 46 #define DATA_PIN PA_5
SomeRandomBloke 3:367aa95f9771 47 // A0 on Dev Board, pin 20 on mDot
SomeRandomBloke 1:45cec6aea002 48 //#define DATA_PIN PB_1
SomeRandomBloke 0:5a0b43f3b143 49
SomeRandomBloke 3:367aa95f9771 50 // A2 - input to reset LoRaWAN config. Pin 15 om mDot.
SomeRandomBloke 3:367aa95f9771 51 #define CONFIG_RESET PC_1
SomeRandomBloke 3:367aa95f9771 52
SomeRandomBloke 3:367aa95f9771 53 // Temperature sensor object
SomeRandomBloke 0:5a0b43f3b143 54 DS1820 probe(DATA_PIN);
SomeRandomBloke 0:5a0b43f3b143 55
SomeRandomBloke 3:367aa95f9771 56 // Serial via USB for debugging only
SomeRandomBloke 0:5a0b43f3b143 57 Serial pc(USBTX,USBRX);
SomeRandomBloke 0:5a0b43f3b143 58
SomeRandomBloke 0:5a0b43f3b143 59 int main()
SomeRandomBloke 0:5a0b43f3b143 60 {
SomeRandomBloke 0:5a0b43f3b143 61 int32_t ret;
SomeRandomBloke 0:5a0b43f3b143 62 mDot* dot;
SomeRandomBloke 0:5a0b43f3b143 63 std::vector<uint8_t> send_data;
SomeRandomBloke 0:5a0b43f3b143 64 std::vector<uint8_t> recv_data;
SomeRandomBloke 5:48eb9245a914 65 std::vector<uint8_t> nwkSKey;
SomeRandomBloke 5:48eb9245a914 66 std::vector<uint8_t> nodeAddr;
SomeRandomBloke 6:0a7760eeaba9 67 std::vector<uint8_t> networkAddr;
SomeRandomBloke 0:5a0b43f3b143 68
SomeRandomBloke 0:5a0b43f3b143 69 float temperature = 0.0;
SomeRandomBloke 1:45cec6aea002 70
SomeRandomBloke 3:367aa95f9771 71 // Enable internal pullup on input pin
SomeRandomBloke 3:367aa95f9771 72 configReset.mode(PullUp);
SomeRandomBloke 3:367aa95f9771 73
SomeRandomBloke 0:5a0b43f3b143 74 pc.baud(115200);
SomeRandomBloke 5:48eb9245a914 75 pc.printf("TTN mDot LoRa Temperature sensor\n\r");
SomeRandomBloke 1:45cec6aea002 76
SomeRandomBloke 0:5a0b43f3b143 77 // get a mDot handle
SomeRandomBloke 0:5a0b43f3b143 78 dot = mDot::getInstance();
SomeRandomBloke 0:5a0b43f3b143 79
SomeRandomBloke 9:086351e54b57 80 dot->setLogLevel(MTSLog::WARNING_LEVEL);
SomeRandomBloke 9:086351e54b57 81 // dot->setLogLevel(MTSLog::TRACE_LEVEL);
SomeRandomBloke 0:5a0b43f3b143 82
SomeRandomBloke 1:45cec6aea002 83 logInfo("Checking Config");
SomeRandomBloke 1:45cec6aea002 84
SomeRandomBloke 1:45cec6aea002 85 // Test if we've already saved the config
SomeRandomBloke 1:45cec6aea002 86 std::string configNetworkName = dot->getNetworkName();
SomeRandomBloke 5:48eb9245a914 87
SomeRandomBloke 5:48eb9245a914 88 uint8_t *it = NwkSKey;
SomeRandomBloke 5:48eb9245a914 89 for (uint8_t i = 0; i<16; i++)
SomeRandomBloke 5:48eb9245a914 90 nwkSKey.push_back((uint8_t) *it++);
SomeRandomBloke 5:48eb9245a914 91
SomeRandomBloke 6:0a7760eeaba9 92 it = NetworkAddr;
SomeRandomBloke 5:48eb9245a914 93 for (uint8_t i = 0; i<4; i++)
SomeRandomBloke 6:0a7760eeaba9 94 networkAddr.push_back((uint8_t) *it++);
SomeRandomBloke 1:45cec6aea002 95
SomeRandomBloke 9:086351e54b57 96 logInfo("Resetting Config");
SomeRandomBloke 9:086351e54b57 97 // reset to default config so we know what state we're in
SomeRandomBloke 9:086351e54b57 98 dot->resetConfig();
SomeRandomBloke 1:45cec6aea002 99
SomeRandomBloke 5:48eb9245a914 100 // Set byte order - AEP less than 1.0.30
SomeRandomBloke 8:8070e9d660e4 101 // dot->setJoinByteOrder(mDot::LSB);
SomeRandomBloke 8:8070e9d660e4 102 dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit
SomeRandomBloke 0:5a0b43f3b143 103
SomeRandomBloke 5:48eb9245a914 104 // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
SomeRandomBloke 5:48eb9245a914 105 // Lower is higher data rate, larger packets and shorter range.
SomeRandomBloke 5:48eb9245a914 106 logInfo("Set SF");
SomeRandomBloke 8:8070e9d660e4 107 if((ret = dot->setTxDataRate( LORA_SF )) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 108 logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 109 }
SomeRandomBloke 5:48eb9245a914 110
SomeRandomBloke 5:48eb9245a914 111 logInfo("Set TxPower");
SomeRandomBloke 8:8070e9d660e4 112 if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 113 logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 114 }
SomeRandomBloke 5:48eb9245a914 115
SomeRandomBloke 7:2a704d1a30e1 116 logInfo("Set Public mode");
SomeRandomBloke 7:2a704d1a30e1 117 if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 118 logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 119 }
SomeRandomBloke 7:2a704d1a30e1 120
SomeRandomBloke 7:2a704d1a30e1 121 logInfo("Set MANUAL Join mode");
SomeRandomBloke 7:2a704d1a30e1 122 if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 123 logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 124 }
SomeRandomBloke 7:2a704d1a30e1 125
SomeRandomBloke 5:48eb9245a914 126 logInfo("Set Ack");
SomeRandomBloke 5:48eb9245a914 127 // 1 retries on Ack, 0 to disable
SomeRandomBloke 8:8070e9d660e4 128 if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 129 logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 130 }
SomeRandomBloke 3:367aa95f9771 131
SomeRandomBloke 1:45cec6aea002 132 // Not applicable for 868MHz in EU
SomeRandomBloke 1:45cec6aea002 133 // if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
SomeRandomBloke 1:45cec6aea002 134 // initStatus = false;
SomeRandomBloke 0:5a0b43f3b143 135 // logError(dot, "failed to set frequency sub band", ret);
SomeRandomBloke 1:45cec6aea002 136 // }
SomeRandomBloke 1:45cec6aea002 137
SomeRandomBloke 6:0a7760eeaba9 138 logInfo("Set Network Address");
SomeRandomBloke 6:0a7760eeaba9 139 if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 140 logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 141 }
SomeRandomBloke 7:2a704d1a30e1 142
SomeRandomBloke 7:2a704d1a30e1 143 logInfo("Set Data Session Key");
SomeRandomBloke 7:2a704d1a30e1 144 if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 145 logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 146 }
SomeRandomBloke 0:5a0b43f3b143 147
SomeRandomBloke 5:48eb9245a914 148 logInfo("Set Network Session Key");
SomeRandomBloke 5:48eb9245a914 149 if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 150 logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 151 }
SomeRandomBloke 5:48eb9245a914 152
SomeRandomBloke 5:48eb9245a914 153 logInfo("Saving Config");
SomeRandomBloke 5:48eb9245a914 154 // Save config
SomeRandomBloke 5:48eb9245a914 155 if (! dot->saveConfig()) {
SomeRandomBloke 5:48eb9245a914 156 logError("failed to save configuration");
SomeRandomBloke 0:5a0b43f3b143 157 }
SomeRandomBloke 5:48eb9245a914 158
SomeRandomBloke 5:48eb9245a914 159 // Display what is set
SomeRandomBloke 5:48eb9245a914 160 std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
SomeRandomBloke 5:48eb9245a914 161 pc.printf("Network Session Key: ");
SomeRandomBloke 5:48eb9245a914 162 pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
SomeRandomBloke 5:48eb9245a914 163
SomeRandomBloke 5:48eb9245a914 164 tmp = dot->getDataSessionKey();
SomeRandomBloke 5:48eb9245a914 165 pc.printf("Data Session Key: ");
SomeRandomBloke 5:48eb9245a914 166 pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
SomeRandomBloke 0:5a0b43f3b143 167
SomeRandomBloke 6:0a7760eeaba9 168 pc.printf("Device ID ");
SomeRandomBloke 6:0a7760eeaba9 169 std::vector<uint8_t> deviceId;
SomeRandomBloke 6:0a7760eeaba9 170 deviceId = dot->getDeviceId();
SomeRandomBloke 6:0a7760eeaba9 171 for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
SomeRandomBloke 5:48eb9245a914 172 pc.printf("%2.2x",*it );
SomeRandomBloke 6:0a7760eeaba9 173 pc.printf("\r\n");
SomeRandomBloke 5:48eb9245a914 174
SomeRandomBloke 6:0a7760eeaba9 175 std::vector<uint8_t> netAddress;
SomeRandomBloke 9:086351e54b57 176
SomeRandomBloke 6:0a7760eeaba9 177 pc.printf("Network Address ");
SomeRandomBloke 6:0a7760eeaba9 178 netAddress = dot->getNetworkAddress();
SomeRandomBloke 6:0a7760eeaba9 179 for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
SomeRandomBloke 5:48eb9245a914 180 pc.printf("%2.2x",*it );
SomeRandomBloke 5:48eb9245a914 181
SomeRandomBloke 5:48eb9245a914 182 pc.printf("\r\n");
SomeRandomBloke 5:48eb9245a914 183
SomeRandomBloke 5:48eb9245a914 184 // Display LoRa parameters
SomeRandomBloke 5:48eb9245a914 185 // Display label and values in different colours, show pretty values not numeric values where applicable
SomeRandomBloke 5:48eb9245a914 186 pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
SomeRandomBloke 5:48eb9245a914 187 pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
SomeRandomBloke 5:48eb9245a914 188 pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
SomeRandomBloke 5:48eb9245a914 189 pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
SomeRandomBloke 6:0a7760eeaba9 190 pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() );
SomeRandomBloke 6:0a7760eeaba9 191 pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
SomeRandomBloke 5:48eb9245a914 192 pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() );
SomeRandomBloke 7:2a704d1a30e1 193 pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() );
SomeRandomBloke 5:48eb9245a914 194 pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
SomeRandomBloke 5:48eb9245a914 195 pc.printf("Tx Power: %d\r\n", dot->getTxPower() );
SomeRandomBloke 6:0a7760eeaba9 196 pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
SomeRandomBloke 5:48eb9245a914 197 pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
SomeRandomBloke 5:48eb9245a914 198 pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") );
SomeRandomBloke 5:48eb9245a914 199
SomeRandomBloke 9:086351e54b57 200 logInfo("Joining Network");
SomeRandomBloke 6:0a7760eeaba9 201
SomeRandomBloke 9:086351e54b57 202 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
SomeRandomBloke 9:086351e54b57 203 logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 9:086351e54b57 204 wait_ms(dot->getNextTxMs() + 1);
SomeRandomBloke 9:086351e54b57 205 }
SomeRandomBloke 6:0a7760eeaba9 206
SomeRandomBloke 9:086351e54b57 207 logInfo("Joined Network");
SomeRandomBloke 6:0a7760eeaba9 208
SomeRandomBloke 9:086351e54b57 209 // Set the Temperature sesnor resolution, 9 bits is enough and makes it faster to provide a reading.
SomeRandomBloke 0:5a0b43f3b143 210 probe.setResolution(9);
SomeRandomBloke 0:5a0b43f3b143 211
SomeRandomBloke 0:5a0b43f3b143 212 char dataBuf[50];
SomeRandomBloke 0:5a0b43f3b143 213 while( 1 ) {
SomeRandomBloke 9:086351e54b57 214 //Start temperature conversion, wait until ready
SomeRandomBloke 9:086351e54b57 215 probe.convertTemperature(true, DS1820::all_devices);
SomeRandomBloke 7:2a704d1a30e1 216 // Output data as JSON e.g. {"t":21.3}
SomeRandomBloke 0:5a0b43f3b143 217 temperature = probe.temperature();
SomeRandomBloke 7:2a704d1a30e1 218 sprintf(dataBuf, "{\"t\":%3.1f}", temperature );
SomeRandomBloke 0:5a0b43f3b143 219 send_data.clear();
SomeRandomBloke 0:5a0b43f3b143 220 // probably not the most efficent way to do this
SomeRandomBloke 0:5a0b43f3b143 221 for( int i=0; i< strlen(dataBuf); i++ )
SomeRandomBloke 0:5a0b43f3b143 222 send_data.push_back( dataBuf[i] );
SomeRandomBloke 0:5a0b43f3b143 223
SomeRandomBloke 0:5a0b43f3b143 224 if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
SomeRandomBloke 0:5a0b43f3b143 225 logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 0:5a0b43f3b143 226 } else {
SomeRandomBloke 0:5a0b43f3b143 227 logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
SomeRandomBloke 0:5a0b43f3b143 228 }
SomeRandomBloke 0:5a0b43f3b143 229
SomeRandomBloke 8:8070e9d660e4 230 // Should sleep here and wakeup after a set 10 minute interval.
SomeRandomBloke 8:8070e9d660e4 231 uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), 600);
SomeRandomBloke 2:9db840d12557 232 logInfo("going to sleep for %d seconds", sleep_time);
SomeRandomBloke 0:5a0b43f3b143 233
SomeRandomBloke 1:45cec6aea002 234 // go to sleep and wake up automatically sleep_time seconds later
SomeRandomBloke 8:8070e9d660e4 235 dot->sleep(sleep_time, mDot::RTC_ALARM);
SomeRandomBloke 0:5a0b43f3b143 236 }
SomeRandomBloke 2:9db840d12557 237
SomeRandomBloke 0:5a0b43f3b143 238 return 0;
SomeRandomBloke 0:5a0b43f3b143 239 }