EasyButtonCode
Dependencies: libmDot mbed-rtos mbed
Fork of mDot_TTN_DS18B20 by
main.cpp@13:aebe2bf2aa8b, 2016-04-19 (annotated)
- Committer:
- jlathem
- Date:
- Tue Apr 19 13:01:23 2016 +0000
- Revision:
- 13:aebe2bf2aa8b
- Parent:
- 12:e82913ee9c61
Final Version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:5a0b43f3b143 | 1 | #include "mbed.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 2 | #include "mDot.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 3 | #include "MTSLog.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 4 | #include "MTSText.h" |
SomeRandomBloke | 0:5a0b43f3b143 | 5 | #include <string> |
SomeRandomBloke | 0:5a0b43f3b143 | 6 | #include <vector> |
SomeRandomBloke | 0:5a0b43f3b143 | 7 | |
SomeRandomBloke | 0:5a0b43f3b143 | 8 | using namespace mts; |
SomeRandomBloke | 0:5a0b43f3b143 | 9 | |
SomeRandomBloke | 4:f649ab1b61d1 | 10 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
SomeRandomBloke | 4:f649ab1b61d1 | 11 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
SomeRandomBloke | 4:f649ab1b61d1 | 12 | |
SomeRandomBloke | 8:8070e9d660e4 | 13 | // Values as used by The Things Network |
SomeRandomBloke | 5:48eb9245a914 | 14 | // Application session key |
SomeRandomBloke | 5:48eb9245a914 | 15 | uint8_t AppSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
SomeRandomBloke | 5:48eb9245a914 | 16 | // Network session key |
SomeRandomBloke | 5:48eb9245a914 | 17 | uint8_t NwkSKey[16]= {0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C}; |
SomeRandomBloke | 8:8070e9d660e4 | 18 | |
SomeRandomBloke | 9:086351e54b57 | 19 | // Network Address - Get your own address range at http://thethingsnetwork.org/wiki/AddressSpace |
jlathem | 13:aebe2bf2aa8b | 20 | uint8_t NetworkAddr[4]= {0x02,0x01,0x6C,0x02}; // Our Network address or Node ID |
SomeRandomBloke | 8:8070e9d660e4 | 21 | |
SomeRandomBloke | 8:8070e9d660e4 | 22 | |
SomeRandomBloke | 8:8070e9d660e4 | 23 | // Some defines for the LoRa configuration |
SomeRandomBloke | 8:8070e9d660e4 | 24 | #define LORA_ACK 0 |
jlathem | 13:aebe2bf2aa8b | 25 | #define LORA_TXPOWER 20 |
SomeRandomBloke | 0:5a0b43f3b143 | 26 | |
jlathem | 13:aebe2bf2aa8b | 27 | //Ignoring sub band for EU modules. |
jlathem | 13:aebe2bf2aa8b | 28 | static uint8_t config_frequency_sub_band = 6; |
SomeRandomBloke | 0:5a0b43f3b143 | 29 | |
SomeRandomBloke | 3:367aa95f9771 | 30 | // Serial via USB for debugging only |
SomeRandomBloke | 0:5a0b43f3b143 | 31 | Serial pc(USBTX,USBRX); |
SomeRandomBloke | 0:5a0b43f3b143 | 32 | |
jlathem | 13:aebe2bf2aa8b | 33 | DigitalIn easyButton(PA_5); |
jlathem | 13:aebe2bf2aa8b | 34 | |
jlathem | 13:aebe2bf2aa8b | 35 | int main(){ |
SomeRandomBloke | 0:5a0b43f3b143 | 36 | int32_t ret; |
SomeRandomBloke | 0:5a0b43f3b143 | 37 | mDot* dot; |
SomeRandomBloke | 0:5a0b43f3b143 | 38 | std::vector<uint8_t> send_data; |
SomeRandomBloke | 0:5a0b43f3b143 | 39 | std::vector<uint8_t> recv_data; |
SomeRandomBloke | 5:48eb9245a914 | 40 | std::vector<uint8_t> nwkSKey; |
SomeRandomBloke | 5:48eb9245a914 | 41 | std::vector<uint8_t> nodeAddr; |
SomeRandomBloke | 6:0a7760eeaba9 | 42 | std::vector<uint8_t> networkAddr; |
SomeRandomBloke | 0:5a0b43f3b143 | 43 | |
SomeRandomBloke | 0:5a0b43f3b143 | 44 | pc.baud(115200); |
jlathem | 13:aebe2bf2aa8b | 45 | pc.printf("TTN Easy Button\n\r"); |
SomeRandomBloke | 1:45cec6aea002 | 46 | |
SomeRandomBloke | 0:5a0b43f3b143 | 47 | // get a mDot handle |
SomeRandomBloke | 0:5a0b43f3b143 | 48 | dot = mDot::getInstance(); |
SomeRandomBloke | 0:5a0b43f3b143 | 49 | |
jlathem | 13:aebe2bf2aa8b | 50 | // dot->setLogLevel(MTSLog::WARNING_LEVEL); |
jlathem | 13:aebe2bf2aa8b | 51 | dot->setLogLevel(MTSLog::INFO_LEVEL); |
SomeRandomBloke | 0:5a0b43f3b143 | 52 | |
SomeRandomBloke | 1:45cec6aea002 | 53 | logInfo("Checking Config"); |
SomeRandomBloke | 1:45cec6aea002 | 54 | |
SomeRandomBloke | 1:45cec6aea002 | 55 | // Test if we've already saved the config |
SomeRandomBloke | 1:45cec6aea002 | 56 | std::string configNetworkName = dot->getNetworkName(); |
SomeRandomBloke | 5:48eb9245a914 | 57 | |
SomeRandomBloke | 5:48eb9245a914 | 58 | uint8_t *it = NwkSKey; |
SomeRandomBloke | 5:48eb9245a914 | 59 | for (uint8_t i = 0; i<16; i++) |
SomeRandomBloke | 5:48eb9245a914 | 60 | nwkSKey.push_back((uint8_t) *it++); |
SomeRandomBloke | 5:48eb9245a914 | 61 | |
SomeRandomBloke | 6:0a7760eeaba9 | 62 | it = NetworkAddr; |
SomeRandomBloke | 5:48eb9245a914 | 63 | for (uint8_t i = 0; i<4; i++) |
SomeRandomBloke | 6:0a7760eeaba9 | 64 | networkAddr.push_back((uint8_t) *it++); |
SomeRandomBloke | 1:45cec6aea002 | 65 | |
SomeRandomBloke | 9:086351e54b57 | 66 | logInfo("Resetting Config"); |
SomeRandomBloke | 9:086351e54b57 | 67 | // reset to default config so we know what state we're in |
SomeRandomBloke | 9:086351e54b57 | 68 | dot->resetConfig(); |
SomeRandomBloke | 1:45cec6aea002 | 69 | |
SomeRandomBloke | 5:48eb9245a914 | 70 | // Set byte order - AEP less than 1.0.30 |
SomeRandomBloke | 8:8070e9d660e4 | 71 | dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit |
SomeRandomBloke | 0:5a0b43f3b143 | 72 | |
SomeRandomBloke | 5:48eb9245a914 | 73 | // Set Spreading Factor, higher is lower data rate, smaller packets but longer range |
SomeRandomBloke | 5:48eb9245a914 | 74 | // Lower is higher data rate, larger packets and shorter range. |
SomeRandomBloke | 5:48eb9245a914 | 75 | logInfo("Set SF"); |
jlathem | 13:aebe2bf2aa8b | 76 | if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 77 | logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 78 | } |
SomeRandomBloke | 5:48eb9245a914 | 79 | |
SomeRandomBloke | 5:48eb9245a914 | 80 | logInfo("Set TxPower"); |
SomeRandomBloke | 8:8070e9d660e4 | 81 | if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 82 | logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 83 | } |
SomeRandomBloke | 5:48eb9245a914 | 84 | |
SomeRandomBloke | 7:2a704d1a30e1 | 85 | logInfo("Set Public mode"); |
SomeRandomBloke | 7:2a704d1a30e1 | 86 | if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 87 | logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 88 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 89 | |
SomeRandomBloke | 7:2a704d1a30e1 | 90 | logInfo("Set MANUAL Join mode"); |
SomeRandomBloke | 7:2a704d1a30e1 | 91 | if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 92 | logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 93 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 94 | |
SomeRandomBloke | 5:48eb9245a914 | 95 | logInfo("Set Ack"); |
SomeRandomBloke | 5:48eb9245a914 | 96 | // 1 retries on Ack, 0 to disable |
SomeRandomBloke | 8:8070e9d660e4 | 97 | if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 98 | logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 99 | } |
SomeRandomBloke | 3:367aa95f9771 | 100 | |
SomeRandomBloke | 1:45cec6aea002 | 101 | // Not applicable for 868MHz in EU |
jlathem | 13:aebe2bf2aa8b | 102 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
jlathem | 13:aebe2bf2aa8b | 103 | logError("Failed to set frequency sub band %s", ret); |
jlathem | 13:aebe2bf2aa8b | 104 | } |
SomeRandomBloke | 1:45cec6aea002 | 105 | |
SomeRandomBloke | 6:0a7760eeaba9 | 106 | logInfo("Set Network Address"); |
SomeRandomBloke | 6:0a7760eeaba9 | 107 | if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 108 | logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 109 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 110 | |
SomeRandomBloke | 7:2a704d1a30e1 | 111 | logInfo("Set Data Session Key"); |
SomeRandomBloke | 7:2a704d1a30e1 | 112 | if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 113 | logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 114 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 115 | |
SomeRandomBloke | 5:48eb9245a914 | 116 | logInfo("Set Network Session Key"); |
SomeRandomBloke | 5:48eb9245a914 | 117 | if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 118 | logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 119 | } |
SomeRandomBloke | 5:48eb9245a914 | 120 | |
SomeRandomBloke | 5:48eb9245a914 | 121 | logInfo("Saving Config"); |
SomeRandomBloke | 5:48eb9245a914 | 122 | // Save config |
SomeRandomBloke | 5:48eb9245a914 | 123 | if (! dot->saveConfig()) { |
SomeRandomBloke | 5:48eb9245a914 | 124 | logError("failed to save configuration"); |
SomeRandomBloke | 0:5a0b43f3b143 | 125 | } |
SomeRandomBloke | 5:48eb9245a914 | 126 | |
SomeRandomBloke | 5:48eb9245a914 | 127 | // Display what is set |
SomeRandomBloke | 5:48eb9245a914 | 128 | std::vector<uint8_t> tmp = dot->getNetworkSessionKey(); |
SomeRandomBloke | 5:48eb9245a914 | 129 | pc.printf("Network Session Key: "); |
SomeRandomBloke | 5:48eb9245a914 | 130 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 131 | |
SomeRandomBloke | 5:48eb9245a914 | 132 | tmp = dot->getDataSessionKey(); |
SomeRandomBloke | 5:48eb9245a914 | 133 | pc.printf("Data Session Key: "); |
SomeRandomBloke | 5:48eb9245a914 | 134 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 135 | |
SomeRandomBloke | 6:0a7760eeaba9 | 136 | pc.printf("Device ID "); |
SomeRandomBloke | 6:0a7760eeaba9 | 137 | std::vector<uint8_t> deviceId; |
SomeRandomBloke | 6:0a7760eeaba9 | 138 | deviceId = dot->getDeviceId(); |
SomeRandomBloke | 6:0a7760eeaba9 | 139 | for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it) |
SomeRandomBloke | 5:48eb9245a914 | 140 | pc.printf("%2.2x",*it ); |
SomeRandomBloke | 6:0a7760eeaba9 | 141 | pc.printf("\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 142 | |
SomeRandomBloke | 6:0a7760eeaba9 | 143 | std::vector<uint8_t> netAddress; |
SomeRandomBloke | 9:086351e54b57 | 144 | |
SomeRandomBloke | 6:0a7760eeaba9 | 145 | pc.printf("Network Address "); |
SomeRandomBloke | 6:0a7760eeaba9 | 146 | netAddress = dot->getNetworkAddress(); |
SomeRandomBloke | 6:0a7760eeaba9 | 147 | for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it) |
SomeRandomBloke | 5:48eb9245a914 | 148 | pc.printf("%2.2x",*it ); |
SomeRandomBloke | 5:48eb9245a914 | 149 | |
SomeRandomBloke | 5:48eb9245a914 | 150 | pc.printf("\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 151 | |
SomeRandomBloke | 5:48eb9245a914 | 152 | // Display LoRa parameters |
SomeRandomBloke | 5:48eb9245a914 | 153 | // Display label and values in different colours, show pretty values not numeric values where applicable |
SomeRandomBloke | 5:48eb9245a914 | 154 | pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); |
SomeRandomBloke | 5:48eb9245a914 | 155 | pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 156 | pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 157 | pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 158 | pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 159 | pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); |
SomeRandomBloke | 5:48eb9245a914 | 160 | pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); |
SomeRandomBloke | 7:2a704d1a30e1 | 161 | pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); |
SomeRandomBloke | 5:48eb9245a914 | 162 | pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 163 | pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 164 | pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); |
SomeRandomBloke | 5:48eb9245a914 | 165 | pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); |
SomeRandomBloke | 5:48eb9245a914 | 166 | pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); |
SomeRandomBloke | 5:48eb9245a914 | 167 | |
jlathem | 13:aebe2bf2aa8b | 168 | char dataBuf[50]; |
jlathem | 13:aebe2bf2aa8b | 169 | |
SomeRandomBloke | 9:086351e54b57 | 170 | logInfo("Joining Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 171 | |
SomeRandomBloke | 9:086351e54b57 | 172 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
SomeRandomBloke | 9:086351e54b57 | 173 | logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 9:086351e54b57 | 174 | wait_ms(dot->getNextTxMs() + 1); |
SomeRandomBloke | 9:086351e54b57 | 175 | } |
jlathem | 13:aebe2bf2aa8b | 176 | |
SomeRandomBloke | 9:086351e54b57 | 177 | logInfo("Joined Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 178 | |
jlathem | 13:aebe2bf2aa8b | 179 | |
SomeRandomBloke | 0:5a0b43f3b143 | 180 | while( 1 ) { |
jlathem | 13:aebe2bf2aa8b | 181 | pc.printf("Waiting for Button Push...."); |
jlathem | 13:aebe2bf2aa8b | 182 | while (easyButton == 0){wait(0.1);} |
jlathem | 13:aebe2bf2aa8b | 183 | pc.printf("Button Pushed!\n"); |
jlathem | 13:aebe2bf2aa8b | 184 | while (easyButton == 1) {wait(0.1);} |
jlathem | 13:aebe2bf2aa8b | 185 | |
jlathem | 13:aebe2bf2aa8b | 186 | sprintf(dataBuf, "easy"); |
jlathem | 13:aebe2bf2aa8b | 187 | |
SomeRandomBloke | 0:5a0b43f3b143 | 188 | send_data.clear(); |
SomeRandomBloke | 0:5a0b43f3b143 | 189 | // probably not the most efficent way to do this |
SomeRandomBloke | 0:5a0b43f3b143 | 190 | for( int i=0; i< strlen(dataBuf); i++ ) |
SomeRandomBloke | 0:5a0b43f3b143 | 191 | send_data.push_back( dataBuf[i] ); |
SomeRandomBloke | 0:5a0b43f3b143 | 192 | |
SomeRandomBloke | 0:5a0b43f3b143 | 193 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
SomeRandomBloke | 0:5a0b43f3b143 | 194 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 195 | } else { |
SomeRandomBloke | 0:5a0b43f3b143 | 196 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 197 | } |
jlathem | 13:aebe2bf2aa8b | 198 | |
jlathem | 13:aebe2bf2aa8b | 199 | dot->resetCpu(); |
SomeRandomBloke | 0:5a0b43f3b143 | 200 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 201 | return 0; |
SomeRandomBloke | 0:5a0b43f3b143 | 202 | } |