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