![](/media/cache/group/drone_pigeon.jpg.50x50_q85.jpg)
Test LORA NODE with Library SX1272, initially created by C.Pham, University of Pau, France for Arduino. Suitable for MBED / NUCLEO / STM32
This project is an adaptation of C.Pham project, university of Pau, France. Initially developed for Arduino, the library sx1272 and the test program was focused on NUCLEO STM32 by C.Dupaty, high school Fourcade 13120 Gardanne.
Tested on NUCLEO L073 with
- SX1272MB2xAS SHIELD
- DRAGINO SHIELD V95 WITH GPS http://wiki.dragino.com/index.php?title=Lora/GPS_Shield For DRAGINO move LORA_CLK LORA_DI LORA_DO straps to the right (arduino 11 12 13)
ALL CONFIGURATIONS FOR ARDUINO HAVE BEEN REMOVED WORK ONLY IN EUROPE
please visit http://cpham.perso.univ-pau.fr/LORA/LoRaDevices.html for original version for ARDUINO
main.cpp@2:4eef0eca4d26, 2018-02-06 (annotated)
- Committer:
- cdupaty
- Date:
- Tue Feb 06 10:04:55 2018 +0000
- Revision:
- 2:4eef0eca4d26
- Parent:
- 1:9f961d34dd8d
Test for NODE LORO with Library SX1272, initially created by C.Pham, University of Pau, France for Arduino.; Suitable for MBED / NUCLEO / STM32
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cdupaty | 0:9859cc8476f2 | 1 | /* |
cdupaty | 0:9859cc8476f2 | 2 | * temperature sensor on analog 8 to test the LoRa gateway |
cdupaty | 0:9859cc8476f2 | 3 | * |
cdupaty | 0:9859cc8476f2 | 4 | * Copyright (C) 2016 Congduc Pham, University of Pau, France |
cdupaty | 0:9859cc8476f2 | 5 | * |
cdupaty | 0:9859cc8476f2 | 6 | * This program is free software: you can redistribute it and/or modify |
cdupaty | 0:9859cc8476f2 | 7 | * it under the terms of the GNU General Public License as published by |
cdupaty | 0:9859cc8476f2 | 8 | * the Free Software Foundation, either version 3 of the License, or |
cdupaty | 0:9859cc8476f2 | 9 | * (at your option) any later version. |
cdupaty | 0:9859cc8476f2 | 10 | |
cdupaty | 0:9859cc8476f2 | 11 | * This program is distributed in the hope that it will be useful, |
cdupaty | 0:9859cc8476f2 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
cdupaty | 0:9859cc8476f2 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
cdupaty | 0:9859cc8476f2 | 14 | * GNU General Public License for more details. |
cdupaty | 0:9859cc8476f2 | 15 | * |
cdupaty | 0:9859cc8476f2 | 16 | * You should have received a copy of the GNU General Public License |
cdupaty | 0:9859cc8476f2 | 17 | * along with the program. If not, see <http://www.gnu.org/licenses/>. |
cdupaty | 0:9859cc8476f2 | 18 | * |
cdupaty | 0:9859cc8476f2 | 19 | ***************************************************************************** |
cdupaty | 0:9859cc8476f2 | 20 | * last update: Sep. 29th, 2017 by C. Pham |
cdupaty | 0:9859cc8476f2 | 21 | * last update: oct 30th , 2017 by C.Dupaty |
cdupaty | 2:4eef0eca4d26 | 22 | * ADAPTATION FOR NUCLEO STM32, TESTED ON NUCLEO-L073RZ WITH |
cdupaty | 2:4eef0eca4d26 | 23 | * SX1272MB2xAS SHIELD |
cdupaty | 2:4eef0eca4d26 | 24 | * DRAGINO SHIELD V95 WITH GPS http://wiki.dragino.com/index.php?title=Lora/GPS_Shield |
cdupaty | 2:4eef0eca4d26 | 25 | * For DRAGINO move LORA_CLK LORA_DI LORA_DO straps to the right (arduino 11 12 13) |
cdupaty | 1:9f961d34dd8d | 26 | * ALL CONFIGURATIONS FOR ARDUINO HAVE BEEN REMOVED |
cdupaty | 1:9f961d34dd8d | 27 | * WORK ONLY IN EUROPE |
cdupaty | 1:9f961d34dd8d | 28 | * please visit http://cpham.perso.univ-pau.fr/LORA/LoRaDevices.html |
cdupaty | 2:4eef0eca4d26 | 29 | * for original version for ARDUINO |
cdupaty | 2:4eef0eca4d26 | 30 | */ |
cdupaty | 2:4eef0eca4d26 | 31 | |
cdupaty | 0:9859cc8476f2 | 32 | #include "mbed.h" |
cdupaty | 0:9859cc8476f2 | 33 | #include "SX1272.h" |
cdupaty | 0:9859cc8476f2 | 34 | #define BAND868 |
cdupaty | 0:9859cc8476f2 | 35 | #define MAX_DBM 14 |
cdupaty | 0:9859cc8476f2 | 36 | |
cdupaty | 2:4eef0eca4d26 | 37 | const uint32_t DEFAULT_CHANNEL=CH_10_868; // for BAND868 |
cdupaty | 0:9859cc8476f2 | 38 | |
cdupaty | 2:4eef0eca4d26 | 39 | //#define WITH_EEPROM // TO DO ON STM32 |
cdupaty | 0:9859cc8476f2 | 40 | #define WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 41 | #define NEW_DATA_FIELD |
cdupaty | 2:4eef0eca4d26 | 42 | #define WITH_ACK |
cdupaty | 0:9859cc8476f2 | 43 | |
cdupaty | 0:9859cc8476f2 | 44 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 45 | // CHANGE HERE THE LORA MODE, NODE ADDRESS |
cdupaty | 0:9859cc8476f2 | 46 | #define LORAMODE 1 |
cdupaty | 0:9859cc8476f2 | 47 | #define node_addr 6 |
cdupaty | 0:9859cc8476f2 | 48 | ////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 49 | |
cdupaty | 0:9859cc8476f2 | 50 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 51 | // CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4 |
cdupaty | 0:9859cc8476f2 | 52 | #define field_index 1 |
cdupaty | 0:9859cc8476f2 | 53 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 54 | |
cdupaty | 0:9859cc8476f2 | 55 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 56 | // CHANGE HERE THE READ PIN AND THE POWER PIN FOR THE TEMP. SENSOR |
cdupaty | 1:9f961d34dd8d | 57 | #define TEMP_PIN_READ PA_4 |
cdupaty | 0:9859cc8476f2 | 58 | // use digital 9 to power the temperature sensor if needed |
cdupaty | 1:9f961d34dd8d | 59 | #define TEMP_PIN_POWER PB_0 |
cdupaty | 0:9859cc8476f2 | 60 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 61 | |
cdupaty | 0:9859cc8476f2 | 62 | /////////////////////////////////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 63 | // CHANGE HERE THE TIME IN SECONDS BETWEEN 2 READING & TRANSMISSION |
cdupaty | 2:4eef0eca4d26 | 64 | #define minTime 120 // 2 minutes |
cdupaty | 2:4eef0eca4d26 | 65 | #define maxTime 600 // 10 minutes |
cdupaty | 0:9859cc8476f2 | 66 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 67 | |
cdupaty | 0:9859cc8476f2 | 68 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 69 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 70 | // CHANGE HERE THE APPKEY, BUT IF GW CHECKS FOR APPKEY, MUST BE |
cdupaty | 0:9859cc8476f2 | 71 | // IN THE APPKEY LIST MAINTAINED BY GW. |
cdupaty | 0:9859cc8476f2 | 72 | uint8_t my_appKey[4]={5, 6, 7, 8}; |
cdupaty | 0:9859cc8476f2 | 73 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 74 | #endif |
cdupaty | 0:9859cc8476f2 | 75 | |
cdupaty | 0:9859cc8476f2 | 76 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 77 | #include <EEPROM.h> |
cdupaty | 0:9859cc8476f2 | 78 | #endif |
cdupaty | 0:9859cc8476f2 | 79 | |
cdupaty | 0:9859cc8476f2 | 80 | #define DEFAULT_DEST_ADDR 1 |
cdupaty | 0:9859cc8476f2 | 81 | |
cdupaty | 0:9859cc8476f2 | 82 | #ifdef WITH_ACK |
cdupaty | 0:9859cc8476f2 | 83 | #define NB_RETRIES 2 |
cdupaty | 0:9859cc8476f2 | 84 | #endif |
cdupaty | 0:9859cc8476f2 | 85 | |
cdupaty | 0:9859cc8476f2 | 86 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 87 | struct sx1272config { |
cdupaty | 0:9859cc8476f2 | 88 | |
cdupaty | 0:9859cc8476f2 | 89 | uint8_t flag1; |
cdupaty | 0:9859cc8476f2 | 90 | uint8_t flag2; |
cdupaty | 0:9859cc8476f2 | 91 | uint8_t seq; |
cdupaty | 0:9859cc8476f2 | 92 | // can add other fields such as LoRa mode,... |
cdupaty | 0:9859cc8476f2 | 93 | }; |
cdupaty | 0:9859cc8476f2 | 94 | |
cdupaty | 0:9859cc8476f2 | 95 | sx1272config my_sx1272config; |
cdupaty | 0:9859cc8476f2 | 96 | #endif |
cdupaty | 0:9859cc8476f2 | 97 | |
cdupaty | 2:4eef0eca4d26 | 98 | /////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 99 | // Globals variables |
cdupaty | 2:4eef0eca4d26 | 100 | /////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 101 | int loraMode=LORAMODE; |
cdupaty | 2:4eef0eca4d26 | 102 | // added by C.Dupaty |
cdupaty | 2:4eef0eca4d26 | 103 | // For test with LM35DZ temperature captor |
cdupaty | 0:9859cc8476f2 | 104 | DigitalOut temp_pin_power(TEMP_PIN_POWER); |
cdupaty | 0:9859cc8476f2 | 105 | AnalogIn temp_pin_read(TEMP_PIN_READ); |
cdupaty | 0:9859cc8476f2 | 106 | |
cdupaty | 2:4eef0eca4d26 | 107 | ///////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 108 | // SETUP SX1272 initialisation |
cdupaty | 2:4eef0eca4d26 | 109 | //////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 110 | void setup() |
cdupaty | 0:9859cc8476f2 | 111 | { |
cdupaty | 0:9859cc8476f2 | 112 | int e; |
cdupaty | 2:4eef0eca4d26 | 113 | // Print welcome message |
cdupaty | 2:4eef0eca4d26 | 114 | printf("-------------------------------------------------\n"); |
cdupaty | 2:4eef0eca4d26 | 115 | printf("------Simple LoRa temperature sensor-------------\n"); |
cdupaty | 2:4eef0eca4d26 | 116 | printf("--P.Pharm, adaptation NUCELO STM32 C.Dupaty------\n"); |
cdupaty | 2:4eef0eca4d26 | 117 | printf("-------------------------------------------------\n\n"); |
cdupaty | 2:4eef0eca4d26 | 118 | printf("!!!!!!!!! During configuration, all states numbers must read at 0\n"); |
cdupaty | 1:9f961d34dd8d | 119 | |
cdupaty | 0:9859cc8476f2 | 120 | #ifdef TARGET_NUCLEO_L073RZ |
cdupaty | 2:4eef0eca4d26 | 121 | printf("NUCLEO L073RZ detected\n"); |
cdupaty | 2:4eef0eca4d26 | 122 | #else |
cdupaty | 2:4eef0eca4d26 | 123 | printf("WARNING, this software has been tested on NUCLEO L073RZ only !!!\n"); |
cdupaty | 0:9859cc8476f2 | 124 | #endif |
cdupaty | 0:9859cc8476f2 | 125 | |
cdupaty | 0:9859cc8476f2 | 126 | // Power ON the module |
cdupaty | 0:9859cc8476f2 | 127 | sx1272.ON(); |
cdupaty | 0:9859cc8476f2 | 128 | |
cdupaty | 0:9859cc8476f2 | 129 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 130 | // get config from EEPROM |
cdupaty | 0:9859cc8476f2 | 131 | EEPROM.get(0, my_sx1272config); |
cdupaty | 0:9859cc8476f2 | 132 | |
cdupaty | 0:9859cc8476f2 | 133 | // found a valid config? |
cdupaty | 0:9859cc8476f2 | 134 | if (my_sx1272config.flag1==0x12 && my_sx1272config.flag2==0x34) { |
cdupaty | 2:4eef0eca4d26 | 135 | printf("Get back previous sx1272 config\n"); |
cdupaty | 0:9859cc8476f2 | 136 | |
cdupaty | 0:9859cc8476f2 | 137 | // set sequence number for SX1272 library |
cdupaty | 0:9859cc8476f2 | 138 | sx1272._packetNumber=my_sx1272config.seq; |
cdupaty | 2:4eef0eca4d26 | 139 | printf("Using packet sequence number of %d\n",sx1272._packetNumber); |
cdupaty | 0:9859cc8476f2 | 140 | } |
cdupaty | 0:9859cc8476f2 | 141 | else { |
cdupaty | 0:9859cc8476f2 | 142 | // otherwise, write config and start over |
cdupaty | 0:9859cc8476f2 | 143 | my_sx1272config.flag1=0x12; |
cdupaty | 0:9859cc8476f2 | 144 | my_sx1272config.flag2=0x34; |
cdupaty | 0:9859cc8476f2 | 145 | my_sx1272config.seq=sx1272._packetNumber; |
cdupaty | 0:9859cc8476f2 | 146 | } |
cdupaty | 0:9859cc8476f2 | 147 | #endif |
cdupaty | 0:9859cc8476f2 | 148 | |
cdupaty | 0:9859cc8476f2 | 149 | int error_config_sx1272=0; |
cdupaty | 0:9859cc8476f2 | 150 | |
cdupaty | 0:9859cc8476f2 | 151 | // Set transmission mode and print the result |
cdupaty | 0:9859cc8476f2 | 152 | e = sx1272.setMode(loraMode); |
cdupaty | 0:9859cc8476f2 | 153 | if (e) error_config_sx1272=1; |
cdupaty | 2:4eef0eca4d26 | 154 | printf("Setting Mode: state %d\n",e); |
cdupaty | 2:4eef0eca4d26 | 155 | |
cdupaty | 0:9859cc8476f2 | 156 | // enable carrier sense |
cdupaty | 0:9859cc8476f2 | 157 | sx1272._enableCarrierSense=true; |
cdupaty | 2:4eef0eca4d26 | 158 | |
cdupaty | 2:4eef0eca4d26 | 159 | // for LOW POWER |
cdupaty | 0:9859cc8476f2 | 160 | sx1272._RSSIonSend=false; |
cdupaty | 2:4eef0eca4d26 | 161 | |
cdupaty | 0:9859cc8476f2 | 162 | |
cdupaty | 0:9859cc8476f2 | 163 | // Select frequency channel |
cdupaty | 0:9859cc8476f2 | 164 | e = sx1272.setChannel(DEFAULT_CHANNEL); |
cdupaty | 0:9859cc8476f2 | 165 | if (e) error_config_sx1272=1; |
cdupaty | 2:4eef0eca4d26 | 166 | printf("Setting Channel: state %d\n",e); |
cdupaty | 0:9859cc8476f2 | 167 | |
cdupaty | 0:9859cc8476f2 | 168 | // Select amplifier line; PABOOST or RFO |
cdupaty | 0:9859cc8476f2 | 169 | #ifdef PABOOST |
cdupaty | 0:9859cc8476f2 | 170 | sx1272._needPABOOST=true; |
cdupaty | 0:9859cc8476f2 | 171 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 172 | // powerLevel='x'; |
cdupaty | 0:9859cc8476f2 | 173 | #else |
cdupaty | 0:9859cc8476f2 | 174 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 175 | // powerLevel='M'; |
cdupaty | 0:9859cc8476f2 | 176 | #endif |
cdupaty | 0:9859cc8476f2 | 177 | |
cdupaty | 0:9859cc8476f2 | 178 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 179 | // e = sx1272.setPower(powerLevel); |
cdupaty | 0:9859cc8476f2 | 180 | |
cdupaty | 0:9859cc8476f2 | 181 | e = sx1272.setPowerDBM((uint8_t)MAX_DBM); |
cdupaty | 0:9859cc8476f2 | 182 | if (e) error_config_sx1272=1; |
cdupaty | 2:4eef0eca4d26 | 183 | printf("Setting Power: state %d\n",e); |
cdupaty | 0:9859cc8476f2 | 184 | |
cdupaty | 0:9859cc8476f2 | 185 | // Set the node address and print the result |
cdupaty | 0:9859cc8476f2 | 186 | e = sx1272.setNodeAddress(node_addr); |
cdupaty | 0:9859cc8476f2 | 187 | if (e) error_config_sx1272=1; |
cdupaty | 2:4eef0eca4d26 | 188 | printf("Setting node addr: state %d\n",e); |
cdupaty | 0:9859cc8476f2 | 189 | |
cdupaty | 0:9859cc8476f2 | 190 | // Print a success message |
cdupaty | 2:4eef0eca4d26 | 191 | if (!error_config_sx1272) printf("SX1272 successfully configured\n"); |
cdupaty | 2:4eef0eca4d26 | 192 | else printf("ERREUR DE CONFIGURATION DU SX1272\n"); |
cdupaty | 0:9859cc8476f2 | 193 | |
cdupaty | 0:9859cc8476f2 | 194 | wait_ms(500); |
cdupaty | 0:9859cc8476f2 | 195 | } |
cdupaty | 0:9859cc8476f2 | 196 | |
cdupaty | 2:4eef0eca4d26 | 197 | ////////////////////////////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 198 | // main was named loop on Arduino |
cdupaty | 0:9859cc8476f2 | 199 | ////////////////////////////////////////////////////////////// |
cdupaty | 2:4eef0eca4d26 | 200 | int main(void) // return int for cpp compatibility but never ending the program |
cdupaty | 0:9859cc8476f2 | 201 | { |
cdupaty | 2:4eef0eca4d26 | 202 | long startSend; |
cdupaty | 2:4eef0eca4d26 | 203 | long endSend; |
cdupaty | 2:4eef0eca4d26 | 204 | uint8_t app_key_offset=0; |
cdupaty | 2:4eef0eca4d26 | 205 | int e; |
cdupaty | 2:4eef0eca4d26 | 206 | double temp; // variable temperature in this demo |
cdupaty | 2:4eef0eca4d26 | 207 | char float_str[20]; |
cdupaty | 2:4eef0eca4d26 | 208 | uint8_t message[100]; // the message that will be send to the gateway |
cdupaty | 0:9859cc8476f2 | 209 | |
cdupaty | 2:4eef0eca4d26 | 210 | setup(); |
cdupaty | 2:4eef0eca4d26 | 211 | |
cdupaty | 2:4eef0eca4d26 | 212 | // main loop start here |
cdupaty | 2:4eef0eca4d26 | 213 | while(1) { |
cdupaty | 0:9859cc8476f2 | 214 | |
cdupaty | 2:4eef0eca4d26 | 215 | // this demo use a LM35DZ temerature captor |
cdupaty | 2:4eef0eca4d26 | 216 | temp_pin_power=1; // LM35DZ is powering by a simple GPIO |
cdupaty | 0:9859cc8476f2 | 217 | wait_ms(200); |
cdupaty | 0:9859cc8476f2 | 218 | temp = 0; |
cdupaty | 0:9859cc8476f2 | 219 | int value; |
cdupaty | 0:9859cc8476f2 | 220 | for (int i=0; i<10; i++) { |
cdupaty | 2:4eef0eca4d26 | 221 | // compute temperature for LM35DZ captor |
cdupaty | 2:4eef0eca4d26 | 222 | value = temp_pin_read.read_u16(); |
cdupaty | 2:4eef0eca4d26 | 223 | temp += ((double)value*50.0/8192.0)/10; |
cdupaty | 2:4eef0eca4d26 | 224 | printf("Reading ADC temperature %d/10 -> %d\n",i,value); |
cdupaty | 0:9859cc8476f2 | 225 | wait_ms(100); |
cdupaty | 2:4eef0eca4d26 | 226 | } |
cdupaty | 2:4eef0eca4d26 | 227 | temp_pin_power=0; // LM35DZ power off |
cdupaty | 2:4eef0eca4d26 | 228 | printf("Mean temp is %f Cent\n",temp); |
cdupaty | 2:4eef0eca4d26 | 229 | // we have now temperature in the variable temp |
cdupaty | 0:9859cc8476f2 | 230 | |
cdupaty | 0:9859cc8476f2 | 231 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 232 | app_key_offset = sizeof(my_appKey); |
cdupaty | 0:9859cc8476f2 | 233 | // set the app key in the payload |
cdupaty | 0:9859cc8476f2 | 234 | memcpy(message,my_appKey,app_key_offset); |
cdupaty | 0:9859cc8476f2 | 235 | #endif |
cdupaty | 0:9859cc8476f2 | 236 | |
cdupaty | 0:9859cc8476f2 | 237 | uint8_t r_size; |
cdupaty | 0:9859cc8476f2 | 238 | |
cdupaty | 2:4eef0eca4d26 | 239 | // convert float temp to string ASCII |
cdupaty | 2:4eef0eca4d26 | 240 | sprintf(float_str,"%2.2f",temp); |
cdupaty | 2:4eef0eca4d26 | 241 | #ifdef NEW_DATA_FIELD |
cdupaty | 2:4eef0eca4d26 | 242 | // this is for testing, uncomment if you just want to test, without a real temp sensor plugged |
cdupaty | 2:4eef0eca4d26 | 243 | //strcpy(float_str, "21.55567"); |
cdupaty | 2:4eef0eca4d26 | 244 | r_size=sprintf((char*)message+app_key_offset,"\\!#%d#TC/%s",field_index,float_str); |
cdupaty | 2:4eef0eca4d26 | 245 | #else |
cdupaty | 2:4eef0eca4d26 | 246 | // this is for testing, uncomment if you just want to test, without a real temp sensor plugged |
cdupaty | 2:4eef0eca4d26 | 247 | //strcpy(float_str, "21.55567"); |
cdupaty | 2:4eef0eca4d26 | 248 | r_size=sprintf((char*)message+app_key_offset,"\\!#%d#%s",field_index,float_str); |
cdupaty | 2:4eef0eca4d26 | 249 | #endif |
cdupaty | 2:4eef0eca4d26 | 250 | |
cdupaty | 2:4eef0eca4d26 | 251 | printf("Sending %s\n",message+app_key_offset); |
cdupaty | 2:4eef0eca4d26 | 252 | printf("Real payload size is %d\n",r_size); |
cdupaty | 2:4eef0eca4d26 | 253 | |
cdupaty | 0:9859cc8476f2 | 254 | int pl=r_size+app_key_offset; |
cdupaty | 0:9859cc8476f2 | 255 | sx1272.CarrierSense(); |
cdupaty | 0:9859cc8476f2 | 256 | startSend=millis(); |
cdupaty | 0:9859cc8476f2 | 257 | |
cdupaty | 0:9859cc8476f2 | 258 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 259 | // indicate that we have an appkey |
cdupaty | 0:9859cc8476f2 | 260 | sx1272.setPacketType(PKT_TYPE_DATA | PKT_FLAG_DATA_WAPPKEY); |
cdupaty | 0:9859cc8476f2 | 261 | #else |
cdupaty | 0:9859cc8476f2 | 262 | // just a simple data packet |
cdupaty | 0:9859cc8476f2 | 263 | sx1272.setPacketType(PKT_TYPE_DATA); |
cdupaty | 0:9859cc8476f2 | 264 | #endif |
cdupaty | 0:9859cc8476f2 | 265 | |
cdupaty | 0:9859cc8476f2 | 266 | // Send message to the gateway and print the result |
cdupaty | 0:9859cc8476f2 | 267 | // with the app key if this feature is enabled |
cdupaty | 0:9859cc8476f2 | 268 | #ifdef WITH_ACK |
cdupaty | 0:9859cc8476f2 | 269 | int n_retry=NB_RETRIES; |
cdupaty | 0:9859cc8476f2 | 270 | do { |
cdupaty | 0:9859cc8476f2 | 271 | e = sx1272.sendPacketTimeoutACK(DEFAULT_DEST_ADDR, message, pl); |
cdupaty | 2:4eef0eca4d26 | 272 | if (e==3) printf("No ACK"); |
cdupaty | 0:9859cc8476f2 | 273 | n_retry--; |
cdupaty | 2:4eef0eca4d26 | 274 | if (n_retry) printf("Retry"); |
cdupaty | 2:4eef0eca4d26 | 275 | else printf("Abort"); |
cdupaty | 0:9859cc8476f2 | 276 | } while (e && n_retry); |
cdupaty | 0:9859cc8476f2 | 277 | #else |
cdupaty | 0:9859cc8476f2 | 278 | e = sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, pl); |
cdupaty | 0:9859cc8476f2 | 279 | #endif |
cdupaty | 0:9859cc8476f2 | 280 | endSend=millis(); |
cdupaty | 0:9859cc8476f2 | 281 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 282 | // save packet number for next packet in case of reboot |
cdupaty | 0:9859cc8476f2 | 283 | my_sx1272config.seq=sx1272._packetNumber; |
cdupaty | 0:9859cc8476f2 | 284 | EEPROM.put(0, my_sx1272config); |
cdupaty | 0:9859cc8476f2 | 285 | #endif |
cdupaty | 0:9859cc8476f2 | 286 | |
cdupaty | 2:4eef0eca4d26 | 287 | printf("LoRa pkt size %d\n",pl); |
cdupaty | 2:4eef0eca4d26 | 288 | printf("LoRa pkt seq %d\n",sx1272.packet_sent.packnum); |
cdupaty | 2:4eef0eca4d26 | 289 | printf("LoRa Sent in %ld\n",endSend-startSend); |
cdupaty | 2:4eef0eca4d26 | 290 | printf("LoRa Sent w/CAD in %ld\n", endSend-sx1272._startDoCad); |
cdupaty | 2:4eef0eca4d26 | 291 | printf("Packet sent, state %d\n",e); |
cdupaty | 2:4eef0eca4d26 | 292 | printf("Remaining ToA is %d\n",sx1272.getRemainingToA()); |
cdupaty | 0:9859cc8476f2 | 293 | |
cdupaty | 2:4eef0eca4d26 | 294 | // compute next time transmission in seconds |
cdupaty | 2:4eef0eca4d26 | 295 | int waitting = rand()%maxTime+minTime; // waitting in Sec |
cdupaty | 2:4eef0eca4d26 | 296 | printf("Switch to power saving mode\n"); |
cdupaty | 0:9859cc8476f2 | 297 | e = sx1272.setSleepMode(); |
cdupaty | 0:9859cc8476f2 | 298 | if (!e) |
cdupaty | 2:4eef0eca4d26 | 299 | printf("Successfully switch LoRa module in sleep mode\n"); |
cdupaty | 0:9859cc8476f2 | 300 | else |
cdupaty | 2:4eef0eca4d26 | 301 | printf("Could not switch LoRa module in sleep mode\n"); |
cdupaty | 2:4eef0eca4d26 | 302 | // Deep sleep |
cdupaty | 2:4eef0eca4d26 | 303 | printf("Deep sleep allowed: %i for %d seconds\n", sleep_manager_can_deep_sleep(), waitting); |
cdupaty | 2:4eef0eca4d26 | 304 | wait(waitting); |
cdupaty | 2:4eef0eca4d26 | 305 | }// end loop |
cdupaty | 0:9859cc8476f2 | 306 | |
cdupaty | 2:4eef0eca4d26 | 307 | //return (0); useless here, the program never ends |
cdupaty | 0:9859cc8476f2 | 308 | |
cdupaty | 0:9859cc8476f2 | 309 | } |