![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
EasyButtonCode
Dependencies: libmDot mbed-rtos mbed
Fork of easyButton_GP_IOT by
main.cpp@14:9358bae86990, 2016-04-26 (annotated)
- Committer:
- jlathem
- Date:
- Tue Apr 26 19:04:57 2016 +0000
- Revision:
- 14:9358bae86990
- Parent:
- 13:aebe2bf2aa8b
Name Change
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 | |
jlathem | 14:9358bae86990 | 47 | easyButton.mode(PullDown); |
jlathem | 14:9358bae86990 | 48 | |
SomeRandomBloke | 0:5a0b43f3b143 | 49 | // get a mDot handle |
SomeRandomBloke | 0:5a0b43f3b143 | 50 | dot = mDot::getInstance(); |
jlathem | 14:9358bae86990 | 51 | |
jlathem | 13:aebe2bf2aa8b | 52 | dot->setLogLevel(MTSLog::INFO_LEVEL); |
SomeRandomBloke | 0:5a0b43f3b143 | 53 | |
SomeRandomBloke | 1:45cec6aea002 | 54 | logInfo("Checking Config"); |
SomeRandomBloke | 1:45cec6aea002 | 55 | |
SomeRandomBloke | 1:45cec6aea002 | 56 | // Test if we've already saved the config |
SomeRandomBloke | 1:45cec6aea002 | 57 | std::string configNetworkName = dot->getNetworkName(); |
SomeRandomBloke | 5:48eb9245a914 | 58 | |
SomeRandomBloke | 5:48eb9245a914 | 59 | uint8_t *it = NwkSKey; |
SomeRandomBloke | 5:48eb9245a914 | 60 | for (uint8_t i = 0; i<16; i++) |
SomeRandomBloke | 5:48eb9245a914 | 61 | nwkSKey.push_back((uint8_t) *it++); |
SomeRandomBloke | 5:48eb9245a914 | 62 | |
SomeRandomBloke | 6:0a7760eeaba9 | 63 | it = NetworkAddr; |
SomeRandomBloke | 5:48eb9245a914 | 64 | for (uint8_t i = 0; i<4; i++) |
SomeRandomBloke | 6:0a7760eeaba9 | 65 | networkAddr.push_back((uint8_t) *it++); |
SomeRandomBloke | 1:45cec6aea002 | 66 | |
SomeRandomBloke | 9:086351e54b57 | 67 | logInfo("Resetting Config"); |
SomeRandomBloke | 9:086351e54b57 | 68 | // reset to default config so we know what state we're in |
SomeRandomBloke | 9:086351e54b57 | 69 | dot->resetConfig(); |
SomeRandomBloke | 1:45cec6aea002 | 70 | |
SomeRandomBloke | 5:48eb9245a914 | 71 | // Set byte order - AEP less than 1.0.30 |
SomeRandomBloke | 8:8070e9d660e4 | 72 | dot->setJoinByteOrder(mDot::MSB); // This is default for > 1.0.30 Conduit |
SomeRandomBloke | 0:5a0b43f3b143 | 73 | |
SomeRandomBloke | 5:48eb9245a914 | 74 | // Set Spreading Factor, higher is lower data rate, smaller packets but longer range |
SomeRandomBloke | 5:48eb9245a914 | 75 | // Lower is higher data rate, larger packets and shorter range. |
SomeRandomBloke | 5:48eb9245a914 | 76 | logInfo("Set SF"); |
jlathem | 13:aebe2bf2aa8b | 77 | if((ret = dot->setTxDataRate( mDot::SF_10 )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 78 | logError("Failed to set SF %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 79 | } |
SomeRandomBloke | 5:48eb9245a914 | 80 | |
SomeRandomBloke | 5:48eb9245a914 | 81 | logInfo("Set TxPower"); |
SomeRandomBloke | 8:8070e9d660e4 | 82 | if((ret = dot->setTxPower( LORA_TXPOWER )) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 83 | logError("Failed to set Tx Power %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 84 | } |
SomeRandomBloke | 5:48eb9245a914 | 85 | |
SomeRandomBloke | 7:2a704d1a30e1 | 86 | logInfo("Set Public mode"); |
SomeRandomBloke | 7:2a704d1a30e1 | 87 | if((ret = dot->setPublicNetwork(true)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 88 | logError("failed to set Public Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 89 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 90 | |
SomeRandomBloke | 7:2a704d1a30e1 | 91 | logInfo("Set MANUAL Join mode"); |
SomeRandomBloke | 7:2a704d1a30e1 | 92 | if((ret = dot->setJoinMode(mDot::MANUAL)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 93 | logError("Failed to set MANUAL Join Mode %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 94 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 95 | |
SomeRandomBloke | 5:48eb9245a914 | 96 | logInfo("Set Ack"); |
SomeRandomBloke | 5:48eb9245a914 | 97 | // 1 retries on Ack, 0 to disable |
SomeRandomBloke | 8:8070e9d660e4 | 98 | if((ret = dot->setAck( LORA_ACK)) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 99 | logError("Failed to set Ack %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 100 | } |
SomeRandomBloke | 3:367aa95f9771 | 101 | |
SomeRandomBloke | 1:45cec6aea002 | 102 | // Not applicable for 868MHz in EU |
jlathem | 13:aebe2bf2aa8b | 103 | if ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
jlathem | 13:aebe2bf2aa8b | 104 | logError("Failed to set frequency sub band %s", ret); |
jlathem | 13:aebe2bf2aa8b | 105 | } |
SomeRandomBloke | 1:45cec6aea002 | 106 | |
SomeRandomBloke | 6:0a7760eeaba9 | 107 | logInfo("Set Network Address"); |
SomeRandomBloke | 6:0a7760eeaba9 | 108 | if ((ret = dot->setNetworkAddress(networkAddr)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 109 | logError("Failed to set Network Address %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 7:2a704d1a30e1 | 110 | } |
SomeRandomBloke | 7:2a704d1a30e1 | 111 | |
SomeRandomBloke | 7:2a704d1a30e1 | 112 | logInfo("Set Data Session Key"); |
SomeRandomBloke | 7:2a704d1a30e1 | 113 | if ((ret = dot->setDataSessionKey(nwkSKey)) != mDot::MDOT_OK) { |
SomeRandomBloke | 7:2a704d1a30e1 | 114 | logError("Failed to set Data Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 115 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 116 | |
SomeRandomBloke | 5:48eb9245a914 | 117 | logInfo("Set Network Session Key"); |
SomeRandomBloke | 5:48eb9245a914 | 118 | if ((ret = dot->setNetworkSessionKey(nwkSKey)) != mDot::MDOT_OK) { |
SomeRandomBloke | 5:48eb9245a914 | 119 | logError("Failed to set Network Session Key %d:%s", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 120 | } |
SomeRandomBloke | 5:48eb9245a914 | 121 | |
SomeRandomBloke | 5:48eb9245a914 | 122 | logInfo("Saving Config"); |
SomeRandomBloke | 5:48eb9245a914 | 123 | // Save config |
SomeRandomBloke | 5:48eb9245a914 | 124 | if (! dot->saveConfig()) { |
SomeRandomBloke | 5:48eb9245a914 | 125 | logError("failed to save configuration"); |
SomeRandomBloke | 0:5a0b43f3b143 | 126 | } |
SomeRandomBloke | 5:48eb9245a914 | 127 | |
SomeRandomBloke | 5:48eb9245a914 | 128 | // Display what is set |
SomeRandomBloke | 5:48eb9245a914 | 129 | std::vector<uint8_t> tmp = dot->getNetworkSessionKey(); |
SomeRandomBloke | 5:48eb9245a914 | 130 | pc.printf("Network Session Key: "); |
SomeRandomBloke | 5:48eb9245a914 | 131 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 5:48eb9245a914 | 132 | |
SomeRandomBloke | 5:48eb9245a914 | 133 | tmp = dot->getDataSessionKey(); |
SomeRandomBloke | 5:48eb9245a914 | 134 | pc.printf("Data Session Key: "); |
SomeRandomBloke | 5:48eb9245a914 | 135 | pc.printf("%s\r\n", mts::Text::bin2hexString(tmp, " ").c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 136 | |
SomeRandomBloke | 6:0a7760eeaba9 | 137 | pc.printf("Device ID "); |
SomeRandomBloke | 6:0a7760eeaba9 | 138 | std::vector<uint8_t> deviceId; |
SomeRandomBloke | 6:0a7760eeaba9 | 139 | deviceId = dot->getDeviceId(); |
SomeRandomBloke | 6:0a7760eeaba9 | 140 | for (std::vector<uint8_t>::iterator it = deviceId.begin() ; it != deviceId.end(); ++it) |
SomeRandomBloke | 5:48eb9245a914 | 141 | pc.printf("%2.2x",*it ); |
SomeRandomBloke | 6:0a7760eeaba9 | 142 | pc.printf("\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 143 | |
SomeRandomBloke | 6:0a7760eeaba9 | 144 | std::vector<uint8_t> netAddress; |
SomeRandomBloke | 9:086351e54b57 | 145 | |
SomeRandomBloke | 6:0a7760eeaba9 | 146 | pc.printf("Network Address "); |
SomeRandomBloke | 6:0a7760eeaba9 | 147 | netAddress = dot->getNetworkAddress(); |
SomeRandomBloke | 6:0a7760eeaba9 | 148 | for (std::vector<uint8_t>::iterator it = netAddress.begin() ; it != netAddress.end(); ++it) |
SomeRandomBloke | 5:48eb9245a914 | 149 | pc.printf("%2.2x",*it ); |
SomeRandomBloke | 5:48eb9245a914 | 150 | |
SomeRandomBloke | 5:48eb9245a914 | 151 | pc.printf("\r\n"); |
SomeRandomBloke | 5:48eb9245a914 | 152 | |
SomeRandomBloke | 5:48eb9245a914 | 153 | // Display LoRa parameters |
SomeRandomBloke | 5:48eb9245a914 | 154 | // Display label and values in different colours, show pretty values not numeric values where applicable |
SomeRandomBloke | 5:48eb9245a914 | 155 | pc.printf("Public Network: %s\r\n", (char*)(dot->getPublicNetwork() ? "Yes" : "No") ); |
SomeRandomBloke | 5:48eb9245a914 | 156 | pc.printf("Frequency: %s\r\n", (char*)mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 157 | pc.printf("Sub Band: %s\r\n", (char*)mDot::FrequencySubBandStr(dot->getFrequencySubBand()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 158 | pc.printf("Join Mode: %s\r\n", (char*)mDot::JoinModeStr(dot->getJoinMode()).c_str() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 159 | pc.printf("Join Retries: %d\r\n", dot->getJoinRetries() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 160 | pc.printf("Join Byte Order: %s\r\n", (char*)(dot->getJoinByteOrder() == 0 ? "LSB" : "MSB") ); |
SomeRandomBloke | 5:48eb9245a914 | 161 | pc.printf("Link Check Count: %d\r\n", dot->getLinkCheckCount() ); |
SomeRandomBloke | 7:2a704d1a30e1 | 162 | pc.printf("Link Check Thold: %d\r\n", dot->getLinkCheckThreshold() ); |
SomeRandomBloke | 5:48eb9245a914 | 163 | pc.printf("Tx Data Rate: %s\r\n", (char*)mDot::DataRateStr(dot->getTxDataRate()).c_str() ); |
SomeRandomBloke | 5:48eb9245a914 | 164 | pc.printf("Tx Power: %d\r\n", dot->getTxPower() ); |
SomeRandomBloke | 6:0a7760eeaba9 | 165 | pc.printf("TxWait: %s, ", (dot->getTxWait() ? "Y" : "N" )); |
SomeRandomBloke | 5:48eb9245a914 | 166 | pc.printf("CRC: %s, ", (dot->getCrc() ? "Y" : "N") ); |
SomeRandomBloke | 5:48eb9245a914 | 167 | pc.printf("Ack: %s\r\n", (dot->getAck() ? "Y" : "N") ); |
SomeRandomBloke | 5:48eb9245a914 | 168 | |
jlathem | 13:aebe2bf2aa8b | 169 | char dataBuf[50]; |
jlathem | 13:aebe2bf2aa8b | 170 | |
SomeRandomBloke | 9:086351e54b57 | 171 | logInfo("Joining Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 172 | |
SomeRandomBloke | 9:086351e54b57 | 173 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
SomeRandomBloke | 9:086351e54b57 | 174 | logError("failed to join network [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 9:086351e54b57 | 175 | wait_ms(dot->getNextTxMs() + 1); |
SomeRandomBloke | 9:086351e54b57 | 176 | } |
jlathem | 13:aebe2bf2aa8b | 177 | |
SomeRandomBloke | 9:086351e54b57 | 178 | logInfo("Joined Network"); |
SomeRandomBloke | 6:0a7760eeaba9 | 179 | |
jlathem | 13:aebe2bf2aa8b | 180 | |
SomeRandomBloke | 0:5a0b43f3b143 | 181 | while( 1 ) { |
jlathem | 13:aebe2bf2aa8b | 182 | pc.printf("Waiting for Button Push...."); |
jlathem | 13:aebe2bf2aa8b | 183 | while (easyButton == 0){wait(0.1);} |
jlathem | 13:aebe2bf2aa8b | 184 | pc.printf("Button Pushed!\n"); |
jlathem | 13:aebe2bf2aa8b | 185 | while (easyButton == 1) {wait(0.1);} |
jlathem | 13:aebe2bf2aa8b | 186 | |
jlathem | 13:aebe2bf2aa8b | 187 | sprintf(dataBuf, "easy"); |
jlathem | 13:aebe2bf2aa8b | 188 | |
SomeRandomBloke | 0:5a0b43f3b143 | 189 | send_data.clear(); |
SomeRandomBloke | 0:5a0b43f3b143 | 190 | // probably not the most efficent way to do this |
SomeRandomBloke | 0:5a0b43f3b143 | 191 | for( int i=0; i< strlen(dataBuf); i++ ) |
SomeRandomBloke | 0:5a0b43f3b143 | 192 | send_data.push_back( dataBuf[i] ); |
SomeRandomBloke | 0:5a0b43f3b143 | 193 | |
SomeRandomBloke | 0:5a0b43f3b143 | 194 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
SomeRandomBloke | 0:5a0b43f3b143 | 195 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 196 | } else { |
SomeRandomBloke | 0:5a0b43f3b143 | 197 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
SomeRandomBloke | 0:5a0b43f3b143 | 198 | } |
jlathem | 13:aebe2bf2aa8b | 199 | |
jlathem | 13:aebe2bf2aa8b | 200 | dot->resetCpu(); |
SomeRandomBloke | 0:5a0b43f3b143 | 201 | } |
SomeRandomBloke | 0:5a0b43f3b143 | 202 | return 0; |
SomeRandomBloke | 0:5a0b43f3b143 | 203 | } |