Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /**MIT License 00002 00003 Copyright (c) 2016 Daniel Knox 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in all 00013 copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00021 SOFTWARE.**/ 00022 00023 #include "mbed.h" 00024 #include "RN2483.h" 00025 00026 Serial pc(USBTX, USBRX); 00027 00028 // Lora Declarations - using unpopulated header on K64F 00029 RN2483 lorabee(D1, D0); // tx, rx 00030 DigitalInOut loraResetPin(PTB9); 00031 00032 // Provisioned on network server. 00033 const uint8_t devEUI[8] = 00034 {0x09, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; 00035 00036 const uint8_t appEUI[8] = 00037 {0x09, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01}; 00038 00039 const uint8_t appKey[16] = 00040 {0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x91, 0x23}; 00041 00042 const uint8_t devAddr[4] = 00043 {0x094, 0x19, 0x27, 0xf4}; 00044 00045 const uint8_t appSKey[16] = 00046 {0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x91, 0x23}; 00047 00048 const uint8_t nwkSKey[16] = 00049 {0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x91, 0x23}; 00050 00051 uint8_t testPayload[] = 00052 { 00053 0x30, 0x31, 0xFF, 0xDE, 0xAD 00054 }; 00055 00056 // Foward Declarations 00057 void printMenu(); 00058 void hardwareResetLoRa(); 00059 int timedRead(Serial &, unsigned long); 00060 size_t readBytesUntil(Serial &, char, char *, size_t); 00061 void configJoinNetworkOTTA(); 00062 void configJoinNetworkABP(); 00063 void joinOTTA(); 00064 void joinABP(); 00065 void sleepWakeupTest(); 00066 void readVDDTest(); 00067 void setBatteryTest(); 00068 void setLinkCheckIntervalTest(); 00069 void forceEnableTest(); 00070 void sendACK(); 00071 void send(); 00072 void printHWEUI(); 00073 00074 int main() { 00075 pc.baud(38400); 00076 hardwareResetLoRa(); 00077 printMenu(); 00078 while(true){ 00079 char buffer[5]; 00080 int bytesRead = readBytesUntil(pc, '\n', buffer, 5); 00081 if(bytesRead>0){ 00082 switch (strtol(buffer,NULL,10)){ 00083 case 1: 00084 configJoinNetworkOTTA(); 00085 break; 00086 case 2: 00087 configJoinNetworkABP(); 00088 break; 00089 case 3: 00090 joinOTTA(); 00091 break; 00092 case 4: 00093 joinABP(); 00094 break; 00095 case 5: 00096 sleepWakeupTest(); 00097 break; 00098 case 6: 00099 readVDDTest(); 00100 break; 00101 case 7: 00102 setBatteryTest(); 00103 break; 00104 case 8: 00105 setLinkCheckIntervalTest(); 00106 break; 00107 case 9: 00108 forceEnableTest(); 00109 break; 00110 case 10: 00111 sendACK(); 00112 break; 00113 case 11: 00114 send(); 00115 break; 00116 case 12: 00117 printHWEUI(); 00118 break; 00119 default: 00120 printf("Invalid Choice"); 00121 break; 00122 } 00123 Thread::wait(3000); 00124 printMenu(); 00125 } 00126 } 00127 } 00128 00129 void printMenu(){ 00130 printf("------------------------\r\n"); 00131 printf("Config Join OTTA : 1\r\n"); 00132 printf("Config Join ABP : 2\r\n"); 00133 printf("Join OTTA : 3\r\n"); 00134 printf("Join ABP : 4\r\n"); 00135 printf("Sleep Wakeup : 5\r\n"); 00136 printf("Read VDD : 6\r\n"); 00137 printf("Set Battery : 7\r\n"); 00138 printf("Link Check Test : 8\r\n"); 00139 printf("Force Enable : 9\r\n"); 00140 printf("Send Ack MSG : 10\r\n"); 00141 printf("Send NoAck MSG : 11\r\n"); 00142 printf("Print HWEUI : 12\r\n"); 00143 printf("------------------------\r\n"); 00144 } 00145 00146 void hardwareResetLoRa() { 00147 loraResetPin.input(); 00148 Thread::wait(500); 00149 loraResetPin.output() ; 00150 loraResetPin = 1; 00151 Thread::wait(500); 00152 } 00153 00154 void configJoinNetworkOTTA(){ 00155 printf("Network (OTA) Join %s\r\n", lorabee.initOTA(devEUI,appEUI,appKey,true) ? "Success": "Failed"); 00156 } 00157 00158 void configJoinNetworkABP(){ 00159 printf("Network (ABP) Join %s\r\n", lorabee.initABP(devAddr, appSKey, nwkSKey, true) ? "Success": "Failed"); 00160 } 00161 00162 void joinOTTA(){ 00163 printf("Network (OTTA) Join %s\r\n", lorabee.joinOTTA() ? "Success": "Failed"); 00164 } 00165 00166 void joinABP(){ 00167 printf("Network (ABP) Join %s\r\n", lorabee.joinABP() ? "Success": "Failed"); 00168 } 00169 00170 void sleepWakeupTest(){ 00171 printf("RN2483 Sleeping for 10 seconds.\r\n"); 00172 lorabee.sleep(10000); 00173 Thread::wait(20000); 00174 printf("RN2483 Sleeping for 60 seconds.\r\n"); 00175 lorabee.sleep(60000); 00176 Thread::wait(20000); 00177 lorabee.wakeUp(); 00178 printf("RN2483 was told to wake up.\r\n"); 00179 } 00180 00181 void readVDDTest(){ 00182 long vdd; 00183 if (lorabee.getVDD(&vdd)){ 00184 printf("RN2483 voltage in mV %ld.\r\n", vdd); 00185 } else { 00186 printf("Error reading VDD"); 00187 } 00188 00189 } 00190 00191 void setBatteryTest(){ 00192 printf("Battery Setting to 50percent %s\r\n", lorabee.setBattery(125) ? "Success": "Failed"); 00193 Thread::wait(2000); 00194 printf("Battery Setting to external power %s\r\n", lorabee.setBattery(0) ? "Success": "Failed"); 00195 Thread::wait(2000); 00196 printf("Battery Setting to unable to read power %s\r\n", lorabee.setBattery(255) ? "Success": "Failed"); 00197 } 00198 00199 void setLinkCheckIntervalTest(){ 00200 printf("Set Link Check 120 seconds %s\r\n", lorabee.setLinkCheckInterval(120) ? "Success": "Failed"); 00201 Thread::wait(2000); 00202 printf("Set Link Check Disable seconds %s\r\n", lorabee.setLinkCheckInterval(0) ? "Success": "Failed"); 00203 } 00204 00205 void forceEnableTest(){ 00206 printf("Remove silence %s\r\n", lorabee.forceEnable() ? "Success": "Failed"); 00207 } 00208 00209 void printHWEUI(){ 00210 uint8_t buffer[10]; 00211 uint8_t bufferSize = lorabee.getHWEUI(buffer,10); 00212 printf("Hardware EUI: "); 00213 if(bufferSize > 0){ 00214 for(int i = 0; i < bufferSize; i++){ 00215 printf("%x",buffer[i]); 00216 } 00217 } 00218 printf("\r\n"); 00219 } 00220 00221 void sendACK(){ 00222 switch (lorabee.sendReqAck(1, testPayload, 5, 3)) 00223 { 00224 case NoError: 00225 printf("Successful transmission.\r\n"); 00226 break; 00227 case NoResponse: 00228 printf("There was no response from the device.\r\n"); 00229 break; 00230 case Silent: 00231 printf("The device was silenced\r\n"); 00232 break; 00233 case Timedout: 00234 printf("Connection timed-out. Check your serial connection to the device!\r\n"); 00235 break; 00236 case PayloadSizeError: 00237 printf("The size of the payload is greater than allowed. Transmission failed!\r\n"); 00238 break; 00239 case InternalError: 00240 printf("Serious Error\r\n."); 00241 break; 00242 case Busy: 00243 printf("The device is busy.\r\n"); 00244 break; 00245 case NetworkFatalError: 00246 printf("There is a non-recoverable error with the network connection. You should re-connect.\r\n"); 00247 break; 00248 case NotConnected: 00249 printf("The device is not connected to the network. Please connect to the network before attempting to send data.\r\n"); 00250 break; 00251 case NoAcknowledgment: 00252 printf("There was no acknowledgment sent back!\r\n"); 00253 break; 00254 default: 00255 break; 00256 } 00257 } 00258 00259 void send(){ 00260 switch (lorabee.send(1, testPayload, 5)) 00261 { 00262 case NoError: 00263 printf("Successful transmission.\r\n"); 00264 break; 00265 case NoResponse: 00266 printf("There was no response from the device.\r\n"); 00267 break; 00268 case Silent: 00269 printf("The device was silenced\r\n"); 00270 break; 00271 case Timedout: 00272 printf("Connection timed-out. Check your serial connection to the device!\r\n"); 00273 break; 00274 case PayloadSizeError: 00275 printf("The size of the payload is greater than allowed. Transmission failed!\r\n"); 00276 break; 00277 case InternalError: 00278 printf("Serious Error\r\n."); 00279 break; 00280 case Busy: 00281 printf("The device is busy.\r\n"); 00282 break; 00283 case NetworkFatalError: 00284 printf("There is a non-recoverable error with the network connection. You should re-connect.\r\n"); 00285 break; 00286 case NotConnected: 00287 printf("The device is not connected to the network. Please connect to the network before attempting to send data.\r\n"); 00288 break; 00289 default: 00290 break; 00291 } 00292 } 00293 00294 int timedRead(Serial &serialInterface, unsigned long _timeout){ 00295 int c; 00296 Timer t; 00297 t.start(); 00298 unsigned long _startMillis = t.read_ms(); // get milliseconds 00299 while (! serialInterface.readable() && t.read_ms() - _startMillis <_timeout) { 00300 Thread::yield(); 00301 } 00302 t.stop(); 00303 if(serialInterface.readable()){ 00304 c = serialInterface.getc(); 00305 if (c >= 0){ 00306 return c; 00307 } 00308 } 00309 return -1; // -1 indicates timeout 00310 } 00311 00312 size_t readBytesUntil(Serial &serialInterface, char terminator, char *buffer, size_t length) 00313 { 00314 if (length < 1) return 0; 00315 size_t index = 0; 00316 while (index < length) { 00317 int c = timedRead(serialInterface, 1000); 00318 if (c < 0 || c == terminator) break; 00319 *buffer++ = (char)c; 00320 index++; 00321 } 00322 return index; // return number of characters, not including null terminator 00323 }
Generated on Fri Jul 15 2022 03:44:55 by
1.7.2