Demo of DHT11->mDot->TTN

Dependencies:   DHT22 DS18B20_1wire SHTx TSL2561_I2C libmDot mbed-rtos mbed

Fork of mDot_TTN_DHT11 by Chris Merck

Committer:
merckeng
Date:
Wed Aug 17 19:23:32 2016 +0000
Revision:
16:01a1058d9c8e
Parent:
15:c61c5f1533e8
Child:
17:3dc30f4a8da2
DHT11 published

Who changed what in which revision?

UserRevisionLine numberNew contents of line
merckeng 14:e4574212176a 1 /** mDot_TTN_DHT11 -- The Things Network Temperature & Humidity Sensor
merckeng 16:01a1058d9c8e 2 *
merckeng 16:01a1058d9c8e 3 * This is a rough demo of mDot+DHT11 on The Things Network.
merckeng 16:01a1058d9c8e 4 * This code is not indended as a reference design.
merckeng 16:01a1058d9c8e 5 * In particular, it lacks:
merckeng 16:01a1058d9c8e 6 * (1) power management
merckeng 16:01a1058d9c8e 7 * (2) reasonable transmission period
merckeng 16:01a1058d9c8e 8 * (3) adaptive data rate
merckeng 16:01a1058d9c8e 9 *
SomeRandomBloke 0:5a0b43f3b143 10 * Uses MultiTech mDot developer board http://www.multitech.com/models/94558010LF
SomeRandomBloke 0:5a0b43f3b143 11 * Requires a MultiTech MultiConnect Conduit http://www.multitech.com/models/94557203LF
SomeRandomBloke 5:48eb9245a914 12 * http://www.multitech.net/developer/software/lora/conduit-mlinux-convert-to-basic-packet-forwarder/
SomeRandomBloke 5:48eb9245a914 13 * http://forum.thethingsnetwork.org/t/setting-up-multitech-conduit-gateway-for-ttn/216/35
SomeRandomBloke 0:5a0b43f3b143 14 *
merckeng 15:c61c5f1533e8 15 * To receive and visualize this data,
merckeng 15:c61c5f1533e8 16 * consider using InitialState and the bridge code here:
merckeng 15:c61c5f1533e8 17 * https://github.com/things-nyc/initial-state-example
SomeRandomBloke 0:5a0b43f3b143 18 */
SomeRandomBloke 0:5a0b43f3b143 19
SomeRandomBloke 0:5a0b43f3b143 20 #include "mbed.h"
merckeng 14:e4574212176a 21 #include "DHT11.h"
SomeRandomBloke 0:5a0b43f3b143 22 #include "mDot.h"
SomeRandomBloke 0:5a0b43f3b143 23 #include "MTSLog.h"
SomeRandomBloke 0:5a0b43f3b143 24 #include "MTSText.h"
SomeRandomBloke 0:5a0b43f3b143 25 #include <string>
SomeRandomBloke 0:5a0b43f3b143 26 #include <vector>
SomeRandomBloke 0:5a0b43f3b143 27
SomeRandomBloke 0:5a0b43f3b143 28 using namespace mts;
SomeRandomBloke 0:5a0b43f3b143 29
SomeRandomBloke 4:f649ab1b61d1 30 #define MIN(a,b) (((a)<(b))?(a):(b))
SomeRandomBloke 4:f649ab1b61d1 31 #define MAX(a,b) (((a)>(b))?(a):(b))
SomeRandomBloke 4:f649ab1b61d1 32
SomeRandomBloke 8:8070e9d660e4 33
merckeng 14:e4574212176a 34 /** ABP
merckeng 14:e4574212176a 35 * Register your device and update these values:
merckeng 14:e4574212176a 36 * https://account.thethingsnetwork.org/
merckeng 14:e4574212176a 37 */
merckeng 14:e4574212176a 38 uint8_t AppSKey[16]= { 0x50, 0x41, 0xE5, 0x57, 0xB6, 0x24, 0x7D, 0x4E, 0x6F, 0xF9, 0x9D, 0x0E, 0xDE, 0x13, 0xD6, 0xA2, };
merckeng 14:e4574212176a 39 uint8_t NwkSKey[16]= { 0x3E, 0x1C, 0xA9, 0x8A, 0x4C, 0x0A, 0xA9, 0x91, 0x3A, 0xFD, 0xAE, 0x70, 0x66, 0x37, 0x51, 0x9D, };
merckeng 14:e4574212176a 40 uint8_t NetworkAddr[4]= {0x26,0xFC,0x2A,0x9B};
SomeRandomBloke 8:8070e9d660e4 41
SomeRandomBloke 8:8070e9d660e4 42
SomeRandomBloke 8:8070e9d660e4 43 // Some defines for the LoRa configuration
merckeng 14:e4574212176a 44 #define LORA_SF mDot::SF_7
SomeRandomBloke 8:8070e9d660e4 45 #define LORA_ACK 0
merckeng 14:e4574212176a 46 #define LORA_TXPOWER 20
merckeng 14:e4574212176a 47 static uint8_t config_frequency_sub_band = 2;
SomeRandomBloke 5:48eb9245a914 48
merckeng 14:e4574212176a 49 // functions for ensuring network endianness (little-endian)
merckeng 14:e4574212176a 50 uint16_t hton16(const uint16_t x)
merckeng 14:e4574212176a 51 {
merckeng 14:e4574212176a 52 uint16_t t = x;
merckeng 14:e4574212176a 53 uint8_t * a = (uint8_t*)&t;
merckeng 14:e4574212176a 54 a[0] = x>>(8*1);
merckeng 14:e4574212176a 55 a[1] = x>>(8*0);
merckeng 14:e4574212176a 56 return t;
merckeng 14:e4574212176a 57 }
merckeng 14:e4574212176a 58 void hton16(uint16_t * x)
merckeng 14:e4574212176a 59 {
merckeng 14:e4574212176a 60 *x = hton16(*x);
merckeng 14:e4574212176a 61 }
merckeng 14:e4574212176a 62
SomeRandomBloke 0:5a0b43f3b143 63
merckeng 14:e4574212176a 64 // packet payload format
merckeng 14:e4574212176a 65 #pragma pack(push, 1) // exact fit - no padding
merckeng 14:e4574212176a 66 struct sigmap_packet_t {
merckeng 14:e4574212176a 67 uint16_t seq;
merckeng 14:e4574212176a 68 uint8_t pwr; /* tx power in dbm, +128 offset */
merckeng 14:e4574212176a 69 uint16_t temp; /* temperature, in hundreths of a degree C, +32768 offset */
merckeng 14:e4574212176a 70 uint16_t humid; /* relative humidity, in hundreths of a percent, +32768 offset */
merckeng 14:e4574212176a 71 void hton() {
merckeng 14:e4574212176a 72 hton16(&seq);
merckeng 14:e4574212176a 73 hton16(&temp);
merckeng 14:e4574212176a 74 hton16(&humid);
merckeng 14:e4574212176a 75 }
merckeng 14:e4574212176a 76 };
merckeng 14:e4574212176a 77 #pragma pack(pop) // back to whatever the previous packing mode was
SomeRandomBloke 0:5a0b43f3b143 78
SomeRandomBloke 3:367aa95f9771 79 // Temperature sensor object
merckeng 14:e4574212176a 80 #define DHT_PIN PB_1
merckeng 14:e4574212176a 81 DHT11 dht(DHT_PIN);
SomeRandomBloke 0:5a0b43f3b143 82
SomeRandomBloke 3:367aa95f9771 83 // Serial via USB for debugging only
SomeRandomBloke 0:5a0b43f3b143 84 Serial pc(USBTX,USBRX);
SomeRandomBloke 0:5a0b43f3b143 85
SomeRandomBloke 0:5a0b43f3b143 86 int main()
SomeRandomBloke 0:5a0b43f3b143 87 {
merckeng 14:e4574212176a 88 sigmap_packet_t pkt;
merckeng 14:e4574212176a 89
SomeRandomBloke 0:5a0b43f3b143 90 int32_t ret;
SomeRandomBloke 0:5a0b43f3b143 91 mDot* dot;
SomeRandomBloke 0:5a0b43f3b143 92 std::vector<uint8_t> send_data;
SomeRandomBloke 0:5a0b43f3b143 93 std::vector<uint8_t> recv_data;
SomeRandomBloke 5:48eb9245a914 94 std::vector<uint8_t> nwkSKey;
merckeng 14:e4574212176a 95 std::vector<uint8_t> appSKey;
SomeRandomBloke 5:48eb9245a914 96 std::vector<uint8_t> nodeAddr;
SomeRandomBloke 6:0a7760eeaba9 97 std::vector<uint8_t> networkAddr;
SomeRandomBloke 0:5a0b43f3b143 98
SomeRandomBloke 0:5a0b43f3b143 99 float temperature = 0.0;
SomeRandomBloke 1:45cec6aea002 100
SomeRandomBloke 0:5a0b43f3b143 101 pc.baud(115200);
merckeng 14:e4574212176a 102 pc.printf("TTN mDot LoRa Temperature & Humidity Sensor\n\r");
SomeRandomBloke 1:45cec6aea002 103
SomeRandomBloke 0:5a0b43f3b143 104 // get a mDot handle
SomeRandomBloke 0:5a0b43f3b143 105 dot = mDot::getInstance();
SomeRandomBloke 0:5a0b43f3b143 106
merckeng 14:e4574212176a 107 // dot->setLogLevel(MTSLog::WARNING_LEVEL);
merckeng 14:e4574212176a 108 dot->setLogLevel(MTSLog::TRACE_LEVEL);
SomeRandomBloke 0:5a0b43f3b143 109
SomeRandomBloke 1:45cec6aea002 110 logInfo("Checking Config");
SomeRandomBloke 1:45cec6aea002 111
SomeRandomBloke 1:45cec6aea002 112 // Test if we've already saved the config
SomeRandomBloke 1:45cec6aea002 113 std::string configNetworkName = dot->getNetworkName();
SomeRandomBloke 5:48eb9245a914 114
SomeRandomBloke 5:48eb9245a914 115 uint8_t *it = NwkSKey;
SomeRandomBloke 5:48eb9245a914 116 for (uint8_t i = 0; i<16; i++)
SomeRandomBloke 5:48eb9245a914 117 nwkSKey.push_back((uint8_t) *it++);
merckeng 14:e4574212176a 118
merckeng 14:e4574212176a 119 it = AppSKey;
merckeng 14:e4574212176a 120 for (uint8_t i = 0; i<16; i++)
merckeng 14:e4574212176a 121 appSKey.push_back((uint8_t) *it++);
SomeRandomBloke 5:48eb9245a914 122
SomeRandomBloke 6:0a7760eeaba9 123 it = NetworkAddr;
SomeRandomBloke 5:48eb9245a914 124 for (uint8_t i = 0; i<4; i++)
SomeRandomBloke 6:0a7760eeaba9 125 networkAddr.push_back((uint8_t) *it++);
SomeRandomBloke 1:45cec6aea002 126
SomeRandomBloke 9:086351e54b57 127 logInfo("Resetting Config");
SomeRandomBloke 9:086351e54b57 128 // reset to default config so we know what state we're in
SomeRandomBloke 9:086351e54b57 129 dot->resetConfig();
SomeRandomBloke 1:45cec6aea002 130
SomeRandomBloke 5:48eb9245a914 131 // Set byte order - AEP less than 1.0.30
SomeRandomBloke 8:8070e9d660e4 132 // dot->setJoinByteOrder(mDot::LSB);
SomeRandomBloke 8:8070e9d660e4 133 dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit
SomeRandomBloke 0:5a0b43f3b143 134
merckeng 14:e4574212176a 135
SomeRandomBloke 5:48eb9245a914 136
SomeRandomBloke 5:48eb9245a914 137 logInfo("Set TxPower");
SomeRandomBloke 8:8070e9d660e4 138 if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 139 logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 140 }
SomeRandomBloke 5:48eb9245a914 141
SomeRandomBloke 7:2a704d1a30e1 142 logInfo("Set Public mode");
SomeRandomBloke 7:2a704d1a30e1 143 if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 144 logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 145 }
SomeRandomBloke 7:2a704d1a30e1 146
SomeRandomBloke 7:2a704d1a30e1 147 logInfo("Set MANUAL Join mode");
SomeRandomBloke 7:2a704d1a30e1 148 if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 149 logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 150 }
SomeRandomBloke 7:2a704d1a30e1 151
SomeRandomBloke 5:48eb9245a914 152 logInfo("Set Ack");
SomeRandomBloke 5:48eb9245a914 153 // 1 retries on Ack, 0 to disable
SomeRandomBloke 8:8070e9d660e4 154 if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 155 logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 156 }
SomeRandomBloke 3:367aa95f9771 157
merckeng 13:761b9c929a3f 158 //Not applicable for 868MHz in EU
merckeng 13:761b9c929a3f 159 if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
merckeng 13:761b9c929a3f 160 logError("failed to set frequency sub band", ret);
merckeng 13:761b9c929a3f 161 }
SomeRandomBloke 1:45cec6aea002 162
SomeRandomBloke 6:0a7760eeaba9 163 logInfo("Set Network Address");
SomeRandomBloke 6:0a7760eeaba9 164 if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 165 logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 7:2a704d1a30e1 166 }
SomeRandomBloke 7:2a704d1a30e1 167
SomeRandomBloke 7:2a704d1a30e1 168 logInfo("Set Data Session Key");
merckeng 14:e4574212176a 169 if ((ret = dot->setDataSessionKey(appSKey)) != mDot::MDOT_OK) {
SomeRandomBloke 7:2a704d1a30e1 170 logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 171 }
SomeRandomBloke 0:5a0b43f3b143 172
SomeRandomBloke 5:48eb9245a914 173 logInfo("Set Network Session Key");
SomeRandomBloke 5:48eb9245a914 174 if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) {
SomeRandomBloke 5:48eb9245a914 175 logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 5:48eb9245a914 176 }
SomeRandomBloke 5:48eb9245a914 177
SomeRandomBloke 5:48eb9245a914 178 logInfo("Saving Config");
SomeRandomBloke 5:48eb9245a914 179 // Save config
SomeRandomBloke 5:48eb9245a914 180 if (! dot->saveConfig()) {
SomeRandomBloke 5:48eb9245a914 181 logError("failed to save configuration");
SomeRandomBloke 0:5a0b43f3b143 182 }
SomeRandomBloke 5:48eb9245a914 183
SomeRandomBloke 5:48eb9245a914 184 // Display what is set
SomeRandomBloke 5:48eb9245a914 185 std::vector<uint8_t> tmp = dot->getNetworkSessionKey();
SomeRandomBloke 5:48eb9245a914 186 pc.printf("Network Session Key: ");
SomeRandomBloke 5:48eb9245a914 187 pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
SomeRandomBloke 5:48eb9245a914 188
SomeRandomBloke 5:48eb9245a914 189 tmp = dot->getDataSessionKey();
SomeRandomBloke 5:48eb9245a914 190 pc.printf("Data Session Key: ");
SomeRandomBloke 5:48eb9245a914 191 pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str());
SomeRandomBloke 0:5a0b43f3b143 192
SomeRandomBloke 6:0a7760eeaba9 193 pc.printf("Device ID ");
SomeRandomBloke 6:0a7760eeaba9 194 std::vector<uint8_t> deviceId;
SomeRandomBloke 6:0a7760eeaba9 195 deviceId = dot->getDeviceId();
SomeRandomBloke 6:0a7760eeaba9 196 for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it)
SomeRandomBloke 5:48eb9245a914 197 pc.printf("%2.2x",*it );
SomeRandomBloke 6:0a7760eeaba9 198 pc.printf("\r\n");
SomeRandomBloke 5:48eb9245a914 199
SomeRandomBloke 6:0a7760eeaba9 200 std::vector<uint8_t> netAddress;
SomeRandomBloke 9:086351e54b57 201
SomeRandomBloke 6:0a7760eeaba9 202 pc.printf("Network Address ");
SomeRandomBloke 6:0a7760eeaba9 203 netAddress = dot->getNetworkAddress();
SomeRandomBloke 6:0a7760eeaba9 204 for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it)
SomeRandomBloke 5:48eb9245a914 205 pc.printf("%2.2x",*it );
SomeRandomBloke 5:48eb9245a914 206
SomeRandomBloke 5:48eb9245a914 207 pc.printf("\r\n");
SomeRandomBloke 5:48eb9245a914 208
SomeRandomBloke 5:48eb9245a914 209 // Display LoRa parameters
SomeRandomBloke 5:48eb9245a914 210 // Display label and values in different colours, show pretty values not numeric values where applicable
SomeRandomBloke 5:48eb9245a914 211 pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") );
SomeRandomBloke 5:48eb9245a914 212 pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() );
SomeRandomBloke 5:48eb9245a914 213 pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() );
SomeRandomBloke 5:48eb9245a914 214 pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() );
SomeRandomBloke 6:0a7760eeaba9 215 pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() );
SomeRandomBloke 6:0a7760eeaba9 216 pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") );
SomeRandomBloke 5:48eb9245a914 217 pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() );
SomeRandomBloke 7:2a704d1a30e1 218 pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() );
SomeRandomBloke 5:48eb9245a914 219 pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() );
SomeRandomBloke 5:48eb9245a914 220 pc.printf("Tx Power: %d\r\n", dot->getTxPower() );
SomeRandomBloke 6:0a7760eeaba9 221 pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" ));
SomeRandomBloke 5:48eb9245a914 222 pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") );
SomeRandomBloke 5:48eb9245a914 223 pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") );
SomeRandomBloke 5:48eb9245a914 224
SomeRandomBloke 9:086351e54b57 225 logInfo("Joining Network");
SomeRandomBloke 6:0a7760eeaba9 226
SomeRandomBloke 9:086351e54b57 227 while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
SomeRandomBloke 9:086351e54b57 228 logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 9:086351e54b57 229 wait_ms(dot->getNextTxMs() + 1);
SomeRandomBloke 9:086351e54b57 230 }
SomeRandomBloke 6:0a7760eeaba9 231
SomeRandomBloke 9:086351e54b57 232 logInfo("Joined Network");
SomeRandomBloke 6:0a7760eeaba9 233
SomeRandomBloke 0:5a0b43f3b143 234 char dataBuf[50];
merckeng 14:e4574212176a 235 uint16_t seq = 0;
merckeng 14:e4574212176a 236 char * sf_str;
SomeRandomBloke 0:5a0b43f3b143 237 while( 1 ) {
merckeng 14:e4574212176a 238
merckeng 14:e4574212176a 239 /* cycle through spreading factors */
merckeng 14:e4574212176a 240 uint8_t sf;
merckeng 14:e4574212176a 241 switch (seq % 4) {
merckeng 14:e4574212176a 242 case 0:
merckeng 14:e4574212176a 243 sf = mDot::SF_7;
merckeng 14:e4574212176a 244 sf_str = "SF7";
merckeng 14:e4574212176a 245 break;
merckeng 14:e4574212176a 246 case 1:
merckeng 14:e4574212176a 247 sf = mDot::SF_8;
merckeng 14:e4574212176a 248 sf_str = "SF8";
merckeng 14:e4574212176a 249 break;
merckeng 14:e4574212176a 250 case 2:
merckeng 14:e4574212176a 251 sf = mDot::SF_9;
merckeng 14:e4574212176a 252 sf_str = "SF9";
merckeng 14:e4574212176a 253 break;
merckeng 14:e4574212176a 254 case 3:
merckeng 14:e4574212176a 255 sf = mDot::SF_10;
merckeng 14:e4574212176a 256 sf_str = "SF10";
merckeng 14:e4574212176a 257 break;
merckeng 14:e4574212176a 258 }
merckeng 14:e4574212176a 259 // Set Spreading Factor, higher is lower data rate, smaller packets but longer range
merckeng 14:e4574212176a 260 // Lower is higher data rate, larger packets and shorter range.
merckeng 14:e4574212176a 261 logInfo("Set SF: %s",sf_str);
merckeng 14:e4574212176a 262 if((ret = dot->setTxDataRate( sf )) != mDot::MDOT_OK) {
merckeng 14:e4574212176a 263 logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
merckeng 14:e4574212176a 264 }
merckeng 14:e4574212176a 265
merckeng 14:e4574212176a 266 /* set default data values */
merckeng 14:e4574212176a 267 int temp = 0;
merckeng 14:e4574212176a 268 int humid = -1;
merckeng 14:e4574212176a 269
merckeng 14:e4574212176a 270 /* read from sensor */
merckeng 14:e4574212176a 271 int r = dht.readData();
merckeng 14:e4574212176a 272 switch (r) {
merckeng 14:e4574212176a 273 case DHT11::OK:
merckeng 14:e4574212176a 274 {
merckeng 14:e4574212176a 275 temp = dht.readTemperature();
merckeng 14:e4574212176a 276 humid = dht.readHumidity();
merckeng 14:e4574212176a 277 pc.printf("[DHT] T %d degC H %d %%\r\n",temp,humid);
merckeng 14:e4574212176a 278 break;
merckeng 14:e4574212176a 279 }
merckeng 14:e4574212176a 280 default:
merckeng 14:e4574212176a 281 {
merckeng 14:e4574212176a 282 pc.printf("[DHT] ERROR %d\r\n",r);
merckeng 14:e4574212176a 283 break;
merckeng 14:e4574212176a 284 }
merckeng 14:e4574212176a 285 };
merckeng 14:e4574212176a 286
merckeng 14:e4574212176a 287 /* build packet */
merckeng 14:e4574212176a 288 pkt.seq = seq;
merckeng 14:e4574212176a 289 pkt.pwr = LORA_TXPOWER + 128;
merckeng 14:e4574212176a 290 pkt.temp = temp*100 + 32768;
merckeng 14:e4574212176a 291 pkt.humid = humid*100 + 32768;
merckeng 14:e4574212176a 292
merckeng 14:e4574212176a 293 /* load vector */
merckeng 14:e4574212176a 294 pkt.hton();
merckeng 14:e4574212176a 295 send_data.clear();
merckeng 14:e4574212176a 296 for( int i=0; i< sizeof(pkt); i++ )
merckeng 14:e4574212176a 297 send_data.push_back( ((uint8_t*)&pkt)[i] );
SomeRandomBloke 0:5a0b43f3b143 298
merckeng 14:e4574212176a 299 /* send packet */
SomeRandomBloke 0:5a0b43f3b143 300 if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
SomeRandomBloke 0:5a0b43f3b143 301 logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
SomeRandomBloke 0:5a0b43f3b143 302 } else {
SomeRandomBloke 0:5a0b43f3b143 303 logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
SomeRandomBloke 0:5a0b43f3b143 304 }
SomeRandomBloke 0:5a0b43f3b143 305
merckeng 14:e4574212176a 306 /* sleep */
merckeng 14:e4574212176a 307 uint32_t sleep_time = MAX((dot->getNextTxMs() / 1000), 10 /* use 6000 for 10min */);
SomeRandomBloke 2:9db840d12557 308 logInfo("going to sleep for %d seconds", sleep_time);
merckeng 14:e4574212176a 309 wait_ms(10*1000);
merckeng 14:e4574212176a 310
merckeng 14:e4574212176a 311 seq++;
SomeRandomBloke 0:5a0b43f3b143 312 }
SomeRandomBloke 2:9db840d12557 313
SomeRandomBloke 0:5a0b43f3b143 314 return 0;
SomeRandomBloke 0:5a0b43f3b143 315 }