p2p-pingpong-clickbutton
Dependencies: PingPong-P2P-ClickButtonToWork-SX1272 Lora_SX1272_Coragem
main.cpp@0:9859cc8476f2, 2017-11-05 (annotated)
- Committer:
- cdupaty
- Date:
- Sun Nov 05 14:32:09 2017 +0000
- Revision:
- 0:9859cc8476f2
- Child:
- 1:9f961d34dd8d
Adaptation node Lora Arduino vers MBED NUCLEO STM32; D'apr?s C.Pham; http://cpham.perso.univ-pau.fr/LORA/LoRaDevices.html; - Connexion Raspberry Pi etablie
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 | 0:9859cc8476f2 | 22 | */ |
cdupaty | 0:9859cc8476f2 | 23 | #include "mbed.h" |
cdupaty | 0:9859cc8476f2 | 24 | |
cdupaty | 0:9859cc8476f2 | 25 | #include <SPI.h> |
cdupaty | 0:9859cc8476f2 | 26 | // Include the SX1272 |
cdupaty | 0:9859cc8476f2 | 27 | #include "SX1272.h" |
cdupaty | 0:9859cc8476f2 | 28 | |
cdupaty | 0:9859cc8476f2 | 29 | // IMPORTANT |
cdupaty | 0:9859cc8476f2 | 30 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 31 | // please uncomment only 1 choice |
cdupaty | 0:9859cc8476f2 | 32 | // |
cdupaty | 0:9859cc8476f2 | 33 | #define ETSI_EUROPE_REGULATION |
cdupaty | 0:9859cc8476f2 | 34 | //#define FCC_US_REGULATION |
cdupaty | 0:9859cc8476f2 | 35 | //#define SENEGAL_REGULATION |
cdupaty | 0:9859cc8476f2 | 36 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 37 | |
cdupaty | 0:9859cc8476f2 | 38 | // IMPORTANT |
cdupaty | 0:9859cc8476f2 | 39 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 40 | // please uncomment only 1 choice |
cdupaty | 0:9859cc8476f2 | 41 | #define BAND868 |
cdupaty | 0:9859cc8476f2 | 42 | //#define BAND900 |
cdupaty | 0:9859cc8476f2 | 43 | //#define BAND433 |
cdupaty | 0:9859cc8476f2 | 44 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 45 | |
cdupaty | 0:9859cc8476f2 | 46 | #ifdef ETSI_EUROPE_REGULATION |
cdupaty | 0:9859cc8476f2 | 47 | #define MAX_DBM 14 |
cdupaty | 0:9859cc8476f2 | 48 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 49 | // char powerLevel='M'; |
cdupaty | 0:9859cc8476f2 | 50 | #elif defined SENEGAL_REGULATION |
cdupaty | 0:9859cc8476f2 | 51 | #define MAX_DBM 10 |
cdupaty | 0:9859cc8476f2 | 52 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 53 | // 'H' is actually 6dBm, so better to use the new way to set output power |
cdupaty | 0:9859cc8476f2 | 54 | // char powerLevel='H'; |
cdupaty | 0:9859cc8476f2 | 55 | #elif defined FCC_US_REGULATION |
cdupaty | 0:9859cc8476f2 | 56 | #define MAX_DBM 14 |
cdupaty | 0:9859cc8476f2 | 57 | #endif |
cdupaty | 0:9859cc8476f2 | 58 | |
cdupaty | 0:9859cc8476f2 | 59 | #ifdef BAND868 |
cdupaty | 0:9859cc8476f2 | 60 | #ifdef SENEGAL_REGULATION |
cdupaty | 0:9859cc8476f2 | 61 | const uint32_t DEFAULT_CHANNEL=CH_04_868; |
cdupaty | 0:9859cc8476f2 | 62 | #else |
cdupaty | 0:9859cc8476f2 | 63 | const uint32_t DEFAULT_CHANNEL=CH_10_868; |
cdupaty | 0:9859cc8476f2 | 64 | #endif |
cdupaty | 0:9859cc8476f2 | 65 | #elif defined BAND900 |
cdupaty | 0:9859cc8476f2 | 66 | const uint32_t DEFAULT_CHANNEL=CH_05_900; |
cdupaty | 0:9859cc8476f2 | 67 | #elif defined BAND433 |
cdupaty | 0:9859cc8476f2 | 68 | const uint32_t DEFAULT_CHANNEL=CH_00_433; |
cdupaty | 0:9859cc8476f2 | 69 | #endif |
cdupaty | 0:9859cc8476f2 | 70 | |
cdupaty | 0:9859cc8476f2 | 71 | // IMPORTANT |
cdupaty | 0:9859cc8476f2 | 72 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 73 | // |
cdupaty | 0:9859cc8476f2 | 74 | // uncomment if your radio is an HopeRF RFM92W, HopeRF RFM95W, Modtronix inAir9B, NiceRF1276 |
cdupaty | 0:9859cc8476f2 | 75 | // or you known from the circuit diagram that output use the PABOOST line instead of the RFO line |
cdupaty | 0:9859cc8476f2 | 76 | //#define PABOOST |
cdupaty | 0:9859cc8476f2 | 77 | /////////////////////////////////////////////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 78 | |
cdupaty | 0:9859cc8476f2 | 79 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 80 | // COMMENT OR UNCOMMENT TO CHANGE FEATURES. |
cdupaty | 0:9859cc8476f2 | 81 | // ONLY IF YOU KNOW WHAT YOU ARE DOING!!! OTHERWISE LEAVE AS IT IS |
cdupaty | 0:9859cc8476f2 | 82 | //#define WITH_EEPROM // tous uncomment a l origine sauf WITH_ACK |
cdupaty | 0:9859cc8476f2 | 83 | #define WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 84 | #define FLOAT_TEMP |
cdupaty | 0:9859cc8476f2 | 85 | #define NEW_DATA_FIELD |
cdupaty | 0:9859cc8476f2 | 86 | //#define LOW_POWER |
cdupaty | 0:9859cc8476f2 | 87 | //#define LOW_POWER_HIBERNATE |
cdupaty | 0:9859cc8476f2 | 88 | //#define WITH_ACK |
cdupaty | 0:9859cc8476f2 | 89 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 90 | |
cdupaty | 0:9859cc8476f2 | 91 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 92 | // ADD HERE OTHER PLATFORMS THAT DO NOT SUPPORT EEPROM NOR LOW POWER |
cdupaty | 0:9859cc8476f2 | 93 | #if defined ARDUINO_SAM_DUE || defined __SAMD21G18A__ || defined TARGET_NUCLEO_L073RZ |
cdupaty | 0:9859cc8476f2 | 94 | #undef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 95 | #endif |
cdupaty | 0:9859cc8476f2 | 96 | |
cdupaty | 0:9859cc8476f2 | 97 | #if defined ARDUINO_SAM_DUE || defined TARGET_NUCLEO_L073RZ |
cdupaty | 0:9859cc8476f2 | 98 | #undef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 99 | #endif |
cdupaty | 0:9859cc8476f2 | 100 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 101 | |
cdupaty | 0:9859cc8476f2 | 102 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 103 | // CHANGE HERE THE LORA MODE, NODE ADDRESS |
cdupaty | 0:9859cc8476f2 | 104 | #define LORAMODE 1 |
cdupaty | 0:9859cc8476f2 | 105 | #define node_addr 6 |
cdupaty | 0:9859cc8476f2 | 106 | ////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 107 | |
cdupaty | 0:9859cc8476f2 | 108 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 109 | // CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4 |
cdupaty | 0:9859cc8476f2 | 110 | #define field_index 1 |
cdupaty | 0:9859cc8476f2 | 111 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 112 | |
cdupaty | 0:9859cc8476f2 | 113 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 114 | // CHANGE HERE THE READ PIN AND THE POWER PIN FOR THE TEMP. SENSOR |
cdupaty | 0:9859cc8476f2 | 115 | #define TEMP_PIN_READ PA_0 |
cdupaty | 0:9859cc8476f2 | 116 | // use digital 9 to power the temperature sensor if needed |
cdupaty | 0:9859cc8476f2 | 117 | #define TEMP_PIN_POWER PA_1 |
cdupaty | 0:9859cc8476f2 | 118 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 119 | |
cdupaty | 0:9859cc8476f2 | 120 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 121 | // CHANGE HERE THE TIME IN MINUTES BETWEEN 2 READING & TRANSMISSION |
cdupaty | 0:9859cc8476f2 | 122 | unsigned int idlePeriodInMin = 2; |
cdupaty | 0:9859cc8476f2 | 123 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 124 | |
cdupaty | 0:9859cc8476f2 | 125 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 126 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 127 | // CHANGE HERE THE APPKEY, BUT IF GW CHECKS FOR APPKEY, MUST BE |
cdupaty | 0:9859cc8476f2 | 128 | // IN THE APPKEY LIST MAINTAINED BY GW. |
cdupaty | 0:9859cc8476f2 | 129 | uint8_t my_appKey[4]={5, 6, 7, 8}; |
cdupaty | 0:9859cc8476f2 | 130 | /////////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 131 | #endif |
cdupaty | 0:9859cc8476f2 | 132 | |
cdupaty | 0:9859cc8476f2 | 133 | // we wrapped Serial.println to support the Arduino Zero or M0 |
cdupaty | 0:9859cc8476f2 | 134 | #if defined __SAMD21G18A__ && not defined ARDUINO_SAMD_FEATHER_M0 |
cdupaty | 0:9859cc8476f2 | 135 | #define PRINTLN SerialUSB.println("") |
cdupaty | 0:9859cc8476f2 | 136 | #define PRINT_CSTSTR(fmt,param) SerialUSB.print(F(param)) |
cdupaty | 0:9859cc8476f2 | 137 | #define PRINT_STR(fmt,param) SerialUSB.print(param) |
cdupaty | 0:9859cc8476f2 | 138 | #define PRINT_VALUE(fmt,param) SerialUSB.print(param) |
cdupaty | 0:9859cc8476f2 | 139 | #define FLUSHOUTPUT SerialUSB.flush(); |
cdupaty | 0:9859cc8476f2 | 140 | #else |
cdupaty | 0:9859cc8476f2 | 141 | #define PRINTLN Serial.println("") |
cdupaty | 0:9859cc8476f2 | 142 | #define PRINT_CSTSTR(fmt,param) Serial.print(F(param)) |
cdupaty | 0:9859cc8476f2 | 143 | #define PRINT_STR(fmt,param) Serial.print(param) |
cdupaty | 0:9859cc8476f2 | 144 | #define PRINT_VALUE(fmt,param) Serial.print(param) |
cdupaty | 0:9859cc8476f2 | 145 | #define FLUSHOUTPUT Serial.flush(); |
cdupaty | 0:9859cc8476f2 | 146 | #endif |
cdupaty | 0:9859cc8476f2 | 147 | |
cdupaty | 0:9859cc8476f2 | 148 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 149 | #include <EEPROM.h> |
cdupaty | 0:9859cc8476f2 | 150 | #endif |
cdupaty | 0:9859cc8476f2 | 151 | |
cdupaty | 0:9859cc8476f2 | 152 | #define DEFAULT_DEST_ADDR 1 |
cdupaty | 0:9859cc8476f2 | 153 | |
cdupaty | 0:9859cc8476f2 | 154 | #ifdef WITH_ACK |
cdupaty | 0:9859cc8476f2 | 155 | #define NB_RETRIES 2 |
cdupaty | 0:9859cc8476f2 | 156 | #endif |
cdupaty | 0:9859cc8476f2 | 157 | |
cdupaty | 0:9859cc8476f2 | 158 | #if defined ARDUINO_AVR_PRO || defined ARDUINO_AVR_MINI || defined ARDUINO_SAM_DUE || defined __MK20DX256__ || defined __MKL26Z64__ || defined __MK64FX512__ || defined __MK66FX1M0__ || defined __SAMD21G18A__ |
cdupaty | 0:9859cc8476f2 | 159 | // if you have a Pro Mini running at 5V, then change here |
cdupaty | 0:9859cc8476f2 | 160 | // these boards work in 3.3V |
cdupaty | 0:9859cc8476f2 | 161 | // Nexus board from Ideetron is a Mini |
cdupaty | 0:9859cc8476f2 | 162 | // __MK66FX1M0__ is for Teensy36 |
cdupaty | 0:9859cc8476f2 | 163 | // __MK64FX512__ is for Teensy35 |
cdupaty | 0:9859cc8476f2 | 164 | // __MK20DX256__ is for Teensy31/32 |
cdupaty | 0:9859cc8476f2 | 165 | // __MKL26Z64__ is for TeensyLC |
cdupaty | 0:9859cc8476f2 | 166 | // __SAMD21G18A__ is for Zero/M0 and FeatherM0 (Cortex-M0) |
cdupaty | 0:9859cc8476f2 | 167 | #define TEMP_SCALE 3300.0 |
cdupaty | 0:9859cc8476f2 | 168 | #else // ARDUINO_AVR_NANO || defined ARDUINO_AVR_UNO || defined ARDUINO_AVR_MEGA2560 |
cdupaty | 0:9859cc8476f2 | 169 | // also for all other boards, so change here if required. |
cdupaty | 0:9859cc8476f2 | 170 | #define TEMP_SCALE 5000.0 |
cdupaty | 0:9859cc8476f2 | 171 | #endif |
cdupaty | 0:9859cc8476f2 | 172 | |
cdupaty | 0:9859cc8476f2 | 173 | #ifdef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 174 | // this is for the Teensy36, Teensy35, Teensy31/32 & TeensyLC |
cdupaty | 0:9859cc8476f2 | 175 | // need v6 of Snooze library |
cdupaty | 0:9859cc8476f2 | 176 | #if defined __MK20DX256__ || defined __MKL26Z64__ || defined __MK64FX512__ || defined __MK66FX1M0__ |
cdupaty | 0:9859cc8476f2 | 177 | #define LOW_POWER_PERIOD 60 |
cdupaty | 0:9859cc8476f2 | 178 | #include <Snooze.h> |
cdupaty | 0:9859cc8476f2 | 179 | SnoozeTimer timer; |
cdupaty | 0:9859cc8476f2 | 180 | SnoozeBlock sleep_config(timer); |
cdupaty | 0:9859cc8476f2 | 181 | //#elif defined ARDUINO_AVR_FEATHER32U4 |
cdupaty | 0:9859cc8476f2 | 182 | //#define LOW_POWER_PERIOD 8 |
cdupaty | 0:9859cc8476f2 | 183 | //#include "Adafruit_SleepyDog.h" |
cdupaty | 0:9859cc8476f2 | 184 | #else // for all other boards based on ATMega168, ATMega328P, ATMega32U4, ATMega2560, ATMega256RFR2, ATSAMD21G18A |
cdupaty | 0:9859cc8476f2 | 185 | #define LOW_POWER_PERIOD 8 |
cdupaty | 0:9859cc8476f2 | 186 | // you need the LowPower library from RocketScream |
cdupaty | 0:9859cc8476f2 | 187 | // https://github.com/rocketscream/Low-Power |
cdupaty | 0:9859cc8476f2 | 188 | #include "LowPower.h" |
cdupaty | 0:9859cc8476f2 | 189 | |
cdupaty | 0:9859cc8476f2 | 190 | #ifdef __SAMD21G18A__ |
cdupaty | 0:9859cc8476f2 | 191 | // use the RTC library |
cdupaty | 0:9859cc8476f2 | 192 | #include "RTCZero.h" |
cdupaty | 0:9859cc8476f2 | 193 | /* Create an rtc object */ |
cdupaty | 0:9859cc8476f2 | 194 | RTCZero rtc; |
cdupaty | 0:9859cc8476f2 | 195 | #endif |
cdupaty | 0:9859cc8476f2 | 196 | #endif |
cdupaty | 0:9859cc8476f2 | 197 | unsigned int nCycle = idlePeriodInMin*60/LOW_POWER_PERIOD; |
cdupaty | 0:9859cc8476f2 | 198 | #endif |
cdupaty | 0:9859cc8476f2 | 199 | |
cdupaty | 0:9859cc8476f2 | 200 | double temp; |
cdupaty | 0:9859cc8476f2 | 201 | unsigned long nextTransmissionTime=0L; |
cdupaty | 0:9859cc8476f2 | 202 | char float_str[20]; |
cdupaty | 0:9859cc8476f2 | 203 | uint8_t message[100]; |
cdupaty | 0:9859cc8476f2 | 204 | int loraMode=LORAMODE; |
cdupaty | 0:9859cc8476f2 | 205 | |
cdupaty | 0:9859cc8476f2 | 206 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 207 | struct sx1272config { |
cdupaty | 0:9859cc8476f2 | 208 | |
cdupaty | 0:9859cc8476f2 | 209 | uint8_t flag1; |
cdupaty | 0:9859cc8476f2 | 210 | uint8_t flag2; |
cdupaty | 0:9859cc8476f2 | 211 | uint8_t seq; |
cdupaty | 0:9859cc8476f2 | 212 | // can add other fields such as LoRa mode,... |
cdupaty | 0:9859cc8476f2 | 213 | }; |
cdupaty | 0:9859cc8476f2 | 214 | |
cdupaty | 0:9859cc8476f2 | 215 | sx1272config my_sx1272config; |
cdupaty | 0:9859cc8476f2 | 216 | #endif |
cdupaty | 0:9859cc8476f2 | 217 | |
cdupaty | 0:9859cc8476f2 | 218 | // ajoute par C.Dupaty |
cdupaty | 0:9859cc8476f2 | 219 | //Serial pcmain(USBTX, USBRX); // tx, rx |
cdupaty | 0:9859cc8476f2 | 220 | DigitalOut temp_pin_power(TEMP_PIN_POWER); |
cdupaty | 0:9859cc8476f2 | 221 | AnalogIn temp_pin_read(TEMP_PIN_READ); |
cdupaty | 0:9859cc8476f2 | 222 | |
cdupaty | 0:9859cc8476f2 | 223 | void setup() |
cdupaty | 0:9859cc8476f2 | 224 | { |
cdupaty | 0:9859cc8476f2 | 225 | int e; |
cdupaty | 0:9859cc8476f2 | 226 | |
cdupaty | 0:9859cc8476f2 | 227 | // for the temperature sensor |
cdupaty | 0:9859cc8476f2 | 228 | //pinMode(TEMP_PIN_READ, INPUT); |
cdupaty | 0:9859cc8476f2 | 229 | // and to power the temperature sensor |
cdupaty | 0:9859cc8476f2 | 230 | //pinMode(TEMP_PIN_POWER,OUTPUT); |
cdupaty | 0:9859cc8476f2 | 231 | |
cdupaty | 0:9859cc8476f2 | 232 | #ifdef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 233 | #ifdef __SAMD21G18A__ |
cdupaty | 0:9859cc8476f2 | 234 | rtc.begin(); |
cdupaty | 0:9859cc8476f2 | 235 | #endif |
cdupaty | 0:9859cc8476f2 | 236 | #else |
cdupaty | 0:9859cc8476f2 | 237 | // digitalWrite(TEMP_PIN_POWER,HIGH); |
cdupaty | 0:9859cc8476f2 | 238 | temp_pin_power=1; |
cdupaty | 0:9859cc8476f2 | 239 | #endif |
cdupaty | 0:9859cc8476f2 | 240 | |
cdupaty | 0:9859cc8476f2 | 241 | wait_ms(3000); |
cdupaty | 0:9859cc8476f2 | 242 | /* |
cdupaty | 0:9859cc8476f2 | 243 | // Open serial communications and wait for port to open: |
cdupaty | 0:9859cc8476f2 | 244 | #if defined __SAMD21G18A__ && not defined ARDUINO_SAMD_FEATHER_M0 |
cdupaty | 0:9859cc8476f2 | 245 | SerialUSB.begin(38400); |
cdupaty | 0:9859cc8476f2 | 246 | #else |
cdupaty | 0:9859cc8476f2 | 247 | Serial.begin(38400); |
cdupaty | 0:9859cc8476f2 | 248 | #endif |
cdupaty | 0:9859cc8476f2 | 249 | */ |
cdupaty | 0:9859cc8476f2 | 250 | |
cdupaty | 0:9859cc8476f2 | 251 | // Print a start message |
cdupaty | 0:9859cc8476f2 | 252 | printf("%s","Simple LoRa temperature sensor\n"); |
cdupaty | 0:9859cc8476f2 | 253 | printf("%s","--------------------------------\n"); |
cdupaty | 0:9859cc8476f2 | 254 | printf("%s","---- VERSION NUCLEO STM32-------\n"); |
cdupaty | 0:9859cc8476f2 | 255 | printf("%s","--------------------------------\n"); |
cdupaty | 0:9859cc8476f2 | 256 | #ifdef ARDUINO_AVR_PRO |
cdupaty | 0:9859cc8476f2 | 257 | printf("%s","Arduino Pro Mini detected\n"); |
cdupaty | 0:9859cc8476f2 | 258 | #endif |
cdupaty | 0:9859cc8476f2 | 259 | #ifdef ARDUINO_AVR_NANO |
cdupaty | 0:9859cc8476f2 | 260 | printf("%s","Arduino Nano detected\n"); |
cdupaty | 0:9859cc8476f2 | 261 | #endif |
cdupaty | 0:9859cc8476f2 | 262 | #ifdef ARDUINO_AVR_MINI |
cdupaty | 0:9859cc8476f2 | 263 | printf("%s","Arduino Mini/Nexus detected\n"); |
cdupaty | 0:9859cc8476f2 | 264 | #endif |
cdupaty | 0:9859cc8476f2 | 265 | #ifdef ARDUINO_AVR_MEGA2560 |
cdupaty | 0:9859cc8476f2 | 266 | printf("%s","Arduino Mega2560 detected\n"); |
cdupaty | 0:9859cc8476f2 | 267 | #endif |
cdupaty | 0:9859cc8476f2 | 268 | #ifdef ARDUINO_SAM_DUE |
cdupaty | 0:9859cc8476f2 | 269 | printf("%s","Arduino Due detected\n"); |
cdupaty | 0:9859cc8476f2 | 270 | #endif |
cdupaty | 0:9859cc8476f2 | 271 | #ifdef __MK66FX1M0__ |
cdupaty | 0:9859cc8476f2 | 272 | printf("%s","Teensy36 MK66FX1M0 detected\n"); |
cdupaty | 0:9859cc8476f2 | 273 | #endif |
cdupaty | 0:9859cc8476f2 | 274 | #ifdef __MK64FX512__ |
cdupaty | 0:9859cc8476f2 | 275 | printf("%s","Teensy35 MK64FX512 detected\n"); |
cdupaty | 0:9859cc8476f2 | 276 | #endif |
cdupaty | 0:9859cc8476f2 | 277 | #ifdef __MK20DX256__ |
cdupaty | 0:9859cc8476f2 | 278 | printf("%s","Teensy31/32 MK20DX256 detected\n"); |
cdupaty | 0:9859cc8476f2 | 279 | #endif |
cdupaty | 0:9859cc8476f2 | 280 | #ifdef __MKL26Z64__ |
cdupaty | 0:9859cc8476f2 | 281 | printf("%s","TeensyLC MKL26Z64 detected\n"); |
cdupaty | 0:9859cc8476f2 | 282 | #endif |
cdupaty | 0:9859cc8476f2 | 283 | #ifdef ARDUINO_SAMD_ZERO |
cdupaty | 0:9859cc8476f2 | 284 | printf("%s","Arduino M0/Zero detected\n"); |
cdupaty | 0:9859cc8476f2 | 285 | #endif |
cdupaty | 0:9859cc8476f2 | 286 | #ifdef ARDUINO_AVR_FEATHER32U4 |
cdupaty | 0:9859cc8476f2 | 287 | printf("%s","Adafruit Feather32U4 detected\n"); |
cdupaty | 0:9859cc8476f2 | 288 | #endif |
cdupaty | 0:9859cc8476f2 | 289 | #ifdef ARDUINO_SAMD_FEATHER_M0 |
cdupaty | 0:9859cc8476f2 | 290 | printf("%s","Adafruit FeatherM0 detected\n"); |
cdupaty | 0:9859cc8476f2 | 291 | #endif |
cdupaty | 0:9859cc8476f2 | 292 | |
cdupaty | 0:9859cc8476f2 | 293 | // See http://www.nongnu.org/avr-libc/user-manual/using_tools.html |
cdupaty | 0:9859cc8476f2 | 294 | // for the list of define from the AVR compiler |
cdupaty | 0:9859cc8476f2 | 295 | |
cdupaty | 0:9859cc8476f2 | 296 | #ifdef __AVR_ATmega328P__ |
cdupaty | 0:9859cc8476f2 | 297 | printf("%s","ATmega328P detected\n"); |
cdupaty | 0:9859cc8476f2 | 298 | #endif |
cdupaty | 0:9859cc8476f2 | 299 | #ifdef __AVR_ATmega32U4__ |
cdupaty | 0:9859cc8476f2 | 300 | printf("%s","ATmega32U4 detected\n"); |
cdupaty | 0:9859cc8476f2 | 301 | #endif |
cdupaty | 0:9859cc8476f2 | 302 | #ifdef __AVR_ATmega2560__ |
cdupaty | 0:9859cc8476f2 | 303 | printf("%s","ATmega2560 detected\n"); |
cdupaty | 0:9859cc8476f2 | 304 | #endif |
cdupaty | 0:9859cc8476f2 | 305 | #ifdef __SAMD21G18A__ |
cdupaty | 0:9859cc8476f2 | 306 | printf("%s","SAMD21G18A ARM Cortex-M0 detected\n"); |
cdupaty | 0:9859cc8476f2 | 307 | #endif |
cdupaty | 0:9859cc8476f2 | 308 | #ifdef __SAM3X8E__ |
cdupaty | 0:9859cc8476f2 | 309 | printf("%s","SAM3X8E ARM Cortex-M3 detected\n"); |
cdupaty | 0:9859cc8476f2 | 310 | #endif |
cdupaty | 0:9859cc8476f2 | 311 | |
cdupaty | 0:9859cc8476f2 | 312 | #ifdef TARGET_NUCLEO_L073RZ |
cdupaty | 0:9859cc8476f2 | 313 | printf("%s","NUCLEO L073RZ detected\n"); |
cdupaty | 0:9859cc8476f2 | 314 | #endif |
cdupaty | 0:9859cc8476f2 | 315 | |
cdupaty | 0:9859cc8476f2 | 316 | // Power ON the module |
cdupaty | 0:9859cc8476f2 | 317 | sx1272.ON(); |
cdupaty | 0:9859cc8476f2 | 318 | |
cdupaty | 0:9859cc8476f2 | 319 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 320 | // get config from EEPROM |
cdupaty | 0:9859cc8476f2 | 321 | EEPROM.get(0, my_sx1272config); |
cdupaty | 0:9859cc8476f2 | 322 | |
cdupaty | 0:9859cc8476f2 | 323 | // found a valid config? |
cdupaty | 0:9859cc8476f2 | 324 | if (my_sx1272config.flag1==0x12 && my_sx1272config.flag2==0x34) { |
cdupaty | 0:9859cc8476f2 | 325 | printf("%s","Get back previous sx1272 config\n"); |
cdupaty | 0:9859cc8476f2 | 326 | |
cdupaty | 0:9859cc8476f2 | 327 | // set sequence number for SX1272 library |
cdupaty | 0:9859cc8476f2 | 328 | sx1272._packetNumber=my_sx1272config.seq; |
cdupaty | 0:9859cc8476f2 | 329 | printf("%s","Using packet sequence number of "); |
cdupaty | 0:9859cc8476f2 | 330 | printf("%d", sx1272._packetNumber); |
cdupaty | 0:9859cc8476f2 | 331 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 332 | } |
cdupaty | 0:9859cc8476f2 | 333 | else { |
cdupaty | 0:9859cc8476f2 | 334 | // otherwise, write config and start over |
cdupaty | 0:9859cc8476f2 | 335 | my_sx1272config.flag1=0x12; |
cdupaty | 0:9859cc8476f2 | 336 | my_sx1272config.flag2=0x34; |
cdupaty | 0:9859cc8476f2 | 337 | my_sx1272config.seq=sx1272._packetNumber; |
cdupaty | 0:9859cc8476f2 | 338 | } |
cdupaty | 0:9859cc8476f2 | 339 | #endif |
cdupaty | 0:9859cc8476f2 | 340 | |
cdupaty | 0:9859cc8476f2 | 341 | int error_config_sx1272=0; |
cdupaty | 0:9859cc8476f2 | 342 | |
cdupaty | 0:9859cc8476f2 | 343 | // Set transmission mode and print the result |
cdupaty | 0:9859cc8476f2 | 344 | printf("%s","\n-------------------------DEBUT Setting mode -----------> \n"); |
cdupaty | 0:9859cc8476f2 | 345 | e = sx1272.setMode(loraMode); |
cdupaty | 0:9859cc8476f2 | 346 | printf("%s","\n-------------------------FIN Setting mode -----------> \n"); |
cdupaty | 0:9859cc8476f2 | 347 | if (e) error_config_sx1272=1; |
cdupaty | 0:9859cc8476f2 | 348 | printf("%s","Setting Mode: state "); |
cdupaty | 0:9859cc8476f2 | 349 | printf("%d", e); |
cdupaty | 0:9859cc8476f2 | 350 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 351 | |
cdupaty | 0:9859cc8476f2 | 352 | // enable carrier sense |
cdupaty | 0:9859cc8476f2 | 353 | sx1272._enableCarrierSense=true; |
cdupaty | 0:9859cc8476f2 | 354 | #ifdef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 355 | // TODO: with low power, when setting the radio module in sleep mode |
cdupaty | 0:9859cc8476f2 | 356 | // there seem to be some issue with RSSI reading |
cdupaty | 0:9859cc8476f2 | 357 | sx1272._RSSIonSend=false; |
cdupaty | 0:9859cc8476f2 | 358 | #endif |
cdupaty | 0:9859cc8476f2 | 359 | |
cdupaty | 0:9859cc8476f2 | 360 | // Select frequency channel |
cdupaty | 0:9859cc8476f2 | 361 | e = sx1272.setChannel(DEFAULT_CHANNEL); |
cdupaty | 0:9859cc8476f2 | 362 | if (e) error_config_sx1272=1; |
cdupaty | 0:9859cc8476f2 | 363 | printf("%s","Setting Channel: state "); |
cdupaty | 0:9859cc8476f2 | 364 | printf("%d", e); |
cdupaty | 0:9859cc8476f2 | 365 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 366 | |
cdupaty | 0:9859cc8476f2 | 367 | // Select amplifier line; PABOOST or RFO |
cdupaty | 0:9859cc8476f2 | 368 | #ifdef PABOOST |
cdupaty | 0:9859cc8476f2 | 369 | sx1272._needPABOOST=true; |
cdupaty | 0:9859cc8476f2 | 370 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 371 | // powerLevel='x'; |
cdupaty | 0:9859cc8476f2 | 372 | #else |
cdupaty | 0:9859cc8476f2 | 373 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 374 | // powerLevel='M'; |
cdupaty | 0:9859cc8476f2 | 375 | #endif |
cdupaty | 0:9859cc8476f2 | 376 | |
cdupaty | 0:9859cc8476f2 | 377 | // previous way for setting output power |
cdupaty | 0:9859cc8476f2 | 378 | // e = sx1272.setPower(powerLevel); |
cdupaty | 0:9859cc8476f2 | 379 | |
cdupaty | 0:9859cc8476f2 | 380 | e = sx1272.setPowerDBM((uint8_t)MAX_DBM); |
cdupaty | 0:9859cc8476f2 | 381 | if (e) error_config_sx1272=1; |
cdupaty | 0:9859cc8476f2 | 382 | printf("%s","Setting Power: state "); |
cdupaty | 0:9859cc8476f2 | 383 | printf("%d", e); |
cdupaty | 0:9859cc8476f2 | 384 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 385 | |
cdupaty | 0:9859cc8476f2 | 386 | // Set the node address and print the result |
cdupaty | 0:9859cc8476f2 | 387 | e = sx1272.setNodeAddress(node_addr); |
cdupaty | 0:9859cc8476f2 | 388 | if (e) error_config_sx1272=1; |
cdupaty | 0:9859cc8476f2 | 389 | printf("%s","Setting node addr: state "); |
cdupaty | 0:9859cc8476f2 | 390 | printf("%d", e); |
cdupaty | 0:9859cc8476f2 | 391 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 392 | |
cdupaty | 0:9859cc8476f2 | 393 | // Print a success message |
cdupaty | 0:9859cc8476f2 | 394 | if (!error_config_sx1272) printf("%s","SX1272 successfully configured\n"); |
cdupaty | 0:9859cc8476f2 | 395 | else printf("%s","ERREUR DE CONFIGURATION DU SX1272\n"); |
cdupaty | 0:9859cc8476f2 | 396 | |
cdupaty | 0:9859cc8476f2 | 397 | wait_ms(500); |
cdupaty | 0:9859cc8476f2 | 398 | } |
cdupaty | 0:9859cc8476f2 | 399 | |
cdupaty | 0:9859cc8476f2 | 400 | char *ftoa(char *a, double f, int precision) |
cdupaty | 0:9859cc8476f2 | 401 | { |
cdupaty | 0:9859cc8476f2 | 402 | long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000}; |
cdupaty | 0:9859cc8476f2 | 403 | |
cdupaty | 0:9859cc8476f2 | 404 | char *ret = a; |
cdupaty | 0:9859cc8476f2 | 405 | long heiltal = (long)f; |
cdupaty | 0:9859cc8476f2 | 406 | //modifie pa C.Dupaty |
cdupaty | 0:9859cc8476f2 | 407 | // itoa(heiltal, a, 10); |
cdupaty | 0:9859cc8476f2 | 408 | sprintf(a,"%d",heiltal); |
cdupaty | 0:9859cc8476f2 | 409 | while (*a != '\0') a++; |
cdupaty | 0:9859cc8476f2 | 410 | *a++ = '.'; |
cdupaty | 0:9859cc8476f2 | 411 | long desimal = abs((long)((f - heiltal) * p[precision])); |
cdupaty | 0:9859cc8476f2 | 412 | if (desimal < p[precision-1]) { |
cdupaty | 0:9859cc8476f2 | 413 | *a++ = '0'; |
cdupaty | 0:9859cc8476f2 | 414 | } |
cdupaty | 0:9859cc8476f2 | 415 | |
cdupaty | 0:9859cc8476f2 | 416 | //modifie pa C.Dupaty |
cdupaty | 0:9859cc8476f2 | 417 | // itoa(desimal, a, 10); |
cdupaty | 0:9859cc8476f2 | 418 | sprintf(a,"%d",desimal); |
cdupaty | 0:9859cc8476f2 | 419 | |
cdupaty | 0:9859cc8476f2 | 420 | |
cdupaty | 0:9859cc8476f2 | 421 | return ret; |
cdupaty | 0:9859cc8476f2 | 422 | } |
cdupaty | 0:9859cc8476f2 | 423 | |
cdupaty | 0:9859cc8476f2 | 424 | |
cdupaty | 0:9859cc8476f2 | 425 | ////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 426 | // programme principal , loop sur Arduino |
cdupaty | 0:9859cc8476f2 | 427 | ////////////////////////////////////////////////////////////// |
cdupaty | 0:9859cc8476f2 | 428 | int main(void) |
cdupaty | 0:9859cc8476f2 | 429 | { |
cdupaty | 0:9859cc8476f2 | 430 | long startSend; |
cdupaty | 0:9859cc8476f2 | 431 | long endSend; |
cdupaty | 0:9859cc8476f2 | 432 | uint8_t app_key_offset=0; |
cdupaty | 0:9859cc8476f2 | 433 | int e; |
cdupaty | 0:9859cc8476f2 | 434 | |
cdupaty | 0:9859cc8476f2 | 435 | setup(); |
cdupaty | 0:9859cc8476f2 | 436 | |
cdupaty | 0:9859cc8476f2 | 437 | while(1) { /////////////// debut boucle |
cdupaty | 0:9859cc8476f2 | 438 | //printf("%s","\n-------------------------BOUCLE SANS FIN -----------> \n"); |
cdupaty | 0:9859cc8476f2 | 439 | //while(1); |
cdupaty | 0:9859cc8476f2 | 440 | |
cdupaty | 0:9859cc8476f2 | 441 | #ifndef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 442 | // 600000+random(15,60)*1000 |
cdupaty | 0:9859cc8476f2 | 443 | if (millis() > nextTransmissionTime) { |
cdupaty | 0:9859cc8476f2 | 444 | #endif |
cdupaty | 0:9859cc8476f2 | 445 | |
cdupaty | 0:9859cc8476f2 | 446 | #ifdef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 447 | // digitalWrite(TEMP_PIN_POWER,HIGH); |
cdupaty | 0:9859cc8476f2 | 448 | temp_pin_power=1; |
cdupaty | 0:9859cc8476f2 | 449 | // security? |
cdupaty | 0:9859cc8476f2 | 450 | wait_ms(200); |
cdupaty | 0:9859cc8476f2 | 451 | #endif |
cdupaty | 0:9859cc8476f2 | 452 | |
cdupaty | 0:9859cc8476f2 | 453 | temp = 0; |
cdupaty | 0:9859cc8476f2 | 454 | int value; |
cdupaty | 0:9859cc8476f2 | 455 | |
cdupaty | 0:9859cc8476f2 | 456 | for (int i=0; i<10; i++) { |
cdupaty | 0:9859cc8476f2 | 457 | // change here how the temperature should be computed depending on your sensor type |
cdupaty | 0:9859cc8476f2 | 458 | // |
cdupaty | 0:9859cc8476f2 | 459 | value = temp_pin_read; |
cdupaty | 0:9859cc8476f2 | 460 | temp += (value*TEMP_SCALE/1024.0)/10; |
cdupaty | 0:9859cc8476f2 | 461 | |
cdupaty | 0:9859cc8476f2 | 462 | printf("%s","Reading "); |
cdupaty | 0:9859cc8476f2 | 463 | printf("%d", value); |
cdupaty | 0:9859cc8476f2 | 464 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 465 | wait_ms(100); |
cdupaty | 0:9859cc8476f2 | 466 | } |
cdupaty | 0:9859cc8476f2 | 467 | |
cdupaty | 0:9859cc8476f2 | 468 | #ifdef LOW_POWER |
cdupaty | 0:9859cc8476f2 | 469 | //digitalWrite(TEMP_PIN_POWER,LOW); |
cdupaty | 0:9859cc8476f2 | 470 | temp_pin_ower=0; |
cdupaty | 0:9859cc8476f2 | 471 | #endif |
cdupaty | 0:9859cc8476f2 | 472 | |
cdupaty | 0:9859cc8476f2 | 473 | printf("%s","Mean temp is "); |
cdupaty | 0:9859cc8476f2 | 474 | temp = temp/10; |
cdupaty | 0:9859cc8476f2 | 475 | printf("%f", temp); |
cdupaty | 0:9859cc8476f2 | 476 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 477 | |
cdupaty | 0:9859cc8476f2 | 478 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 479 | app_key_offset = sizeof(my_appKey); |
cdupaty | 0:9859cc8476f2 | 480 | // set the app key in the payload |
cdupaty | 0:9859cc8476f2 | 481 | memcpy(message,my_appKey,app_key_offset); |
cdupaty | 0:9859cc8476f2 | 482 | #endif |
cdupaty | 0:9859cc8476f2 | 483 | |
cdupaty | 0:9859cc8476f2 | 484 | uint8_t r_size; |
cdupaty | 0:9859cc8476f2 | 485 | |
cdupaty | 0:9859cc8476f2 | 486 | #ifdef FLOAT_TEMP |
cdupaty | 0:9859cc8476f2 | 487 | ftoa(float_str,temp,2); |
cdupaty | 0:9859cc8476f2 | 488 | |
cdupaty | 0:9859cc8476f2 | 489 | #ifdef NEW_DATA_FIELD |
cdupaty | 0:9859cc8476f2 | 490 | // this is for testing, uncomment if you just want to test, without a real temp sensor plugged |
cdupaty | 0:9859cc8476f2 | 491 | //strcpy(float_str, "21.55567"); |
cdupaty | 0:9859cc8476f2 | 492 | r_size=sprintf((char*)message+app_key_offset,"\\!#%d#TC/%s",field_index,float_str); |
cdupaty | 0:9859cc8476f2 | 493 | #else |
cdupaty | 0:9859cc8476f2 | 494 | // this is for testing, uncomment if you just want to test, without a real temp sensor plugged |
cdupaty | 0:9859cc8476f2 | 495 | //strcpy(float_str, "21.55567"); |
cdupaty | 0:9859cc8476f2 | 496 | r_size=sprintf((char*)message+app_key_offset,"\\!#%d#%s",field_index,float_str); |
cdupaty | 0:9859cc8476f2 | 497 | #endif |
cdupaty | 0:9859cc8476f2 | 498 | |
cdupaty | 0:9859cc8476f2 | 499 | #else |
cdupaty | 0:9859cc8476f2 | 500 | |
cdupaty | 0:9859cc8476f2 | 501 | #ifdef NEW_DATA_FIELD |
cdupaty | 0:9859cc8476f2 | 502 | r_size=sprintf((char*)message+app_key_offset, "\\!#%d#TC/%d", field_index, (int)temp); |
cdupaty | 0:9859cc8476f2 | 503 | #else |
cdupaty | 0:9859cc8476f2 | 504 | r_size=sprintf((char*)message+app_key_offset, "\\!#%d#%d", field_index, (int)temp); |
cdupaty | 0:9859cc8476f2 | 505 | #endif |
cdupaty | 0:9859cc8476f2 | 506 | #endif |
cdupaty | 0:9859cc8476f2 | 507 | |
cdupaty | 0:9859cc8476f2 | 508 | printf("%s","Sending "); |
cdupaty | 0:9859cc8476f2 | 509 | printf("%s",(char*)(message+app_key_offset)); |
cdupaty | 0:9859cc8476f2 | 510 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 511 | |
cdupaty | 0:9859cc8476f2 | 512 | printf("%s","Real payload size is "); |
cdupaty | 0:9859cc8476f2 | 513 | printf("%d", r_size); |
cdupaty | 0:9859cc8476f2 | 514 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 515 | |
cdupaty | 0:9859cc8476f2 | 516 | int pl=r_size+app_key_offset; |
cdupaty | 0:9859cc8476f2 | 517 | |
cdupaty | 0:9859cc8476f2 | 518 | sx1272.CarrierSense(); |
cdupaty | 0:9859cc8476f2 | 519 | |
cdupaty | 0:9859cc8476f2 | 520 | startSend=millis(); |
cdupaty | 0:9859cc8476f2 | 521 | |
cdupaty | 0:9859cc8476f2 | 522 | #ifdef WITH_APPKEY |
cdupaty | 0:9859cc8476f2 | 523 | // indicate that we have an appkey |
cdupaty | 0:9859cc8476f2 | 524 | sx1272.setPacketType(PKT_TYPE_DATA | PKT_FLAG_DATA_WAPPKEY); |
cdupaty | 0:9859cc8476f2 | 525 | #else |
cdupaty | 0:9859cc8476f2 | 526 | // just a simple data packet |
cdupaty | 0:9859cc8476f2 | 527 | sx1272.setPacketType(PKT_TYPE_DATA); |
cdupaty | 0:9859cc8476f2 | 528 | #endif |
cdupaty | 0:9859cc8476f2 | 529 | |
cdupaty | 0:9859cc8476f2 | 530 | // Send message to the gateway and print the result |
cdupaty | 0:9859cc8476f2 | 531 | // with the app key if this feature is enabled |
cdupaty | 0:9859cc8476f2 | 532 | #ifdef WITH_ACK |
cdupaty | 0:9859cc8476f2 | 533 | int n_retry=NB_RETRIES; |
cdupaty | 0:9859cc8476f2 | 534 | |
cdupaty | 0:9859cc8476f2 | 535 | do { |
cdupaty | 0:9859cc8476f2 | 536 | e = sx1272.sendPacketTimeoutACK(DEFAULT_DEST_ADDR, message, pl); |
cdupaty | 0:9859cc8476f2 | 537 | |
cdupaty | 0:9859cc8476f2 | 538 | if (e==3) |
cdupaty | 0:9859cc8476f2 | 539 | printf("%s","No ACK"); |
cdupaty | 0:9859cc8476f2 | 540 | |
cdupaty | 0:9859cc8476f2 | 541 | n_retry--; |
cdupaty | 0:9859cc8476f2 | 542 | |
cdupaty | 0:9859cc8476f2 | 543 | if (n_retry) |
cdupaty | 0:9859cc8476f2 | 544 | printf("%s","Retry"); |
cdupaty | 0:9859cc8476f2 | 545 | else |
cdupaty | 0:9859cc8476f2 | 546 | printf("%s","Abort"); |
cdupaty | 0:9859cc8476f2 | 547 | |
cdupaty | 0:9859cc8476f2 | 548 | } while (e && n_retry); |
cdupaty | 0:9859cc8476f2 | 549 | #else |
cdupaty | 0:9859cc8476f2 | 550 | e = sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, pl); |
cdupaty | 0:9859cc8476f2 | 551 | #endif |
cdupaty | 0:9859cc8476f2 | 552 | endSend=millis(); |
cdupaty | 0:9859cc8476f2 | 553 | |
cdupaty | 0:9859cc8476f2 | 554 | #ifdef WITH_EEPROM |
cdupaty | 0:9859cc8476f2 | 555 | // save packet number for next packet in case of reboot |
cdupaty | 0:9859cc8476f2 | 556 | my_sx1272config.seq=sx1272._packetNumber; |
cdupaty | 0:9859cc8476f2 | 557 | EEPROM.put(0, my_sx1272config); |
cdupaty | 0:9859cc8476f2 | 558 | #endif |
cdupaty | 0:9859cc8476f2 | 559 | |
cdupaty | 0:9859cc8476f2 | 560 | printf("%s","LoRa pkt size "); |
cdupaty | 0:9859cc8476f2 | 561 | printf("%d", pl); |
cdupaty | 0:9859cc8476f2 | 562 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 563 | |
cdupaty | 0:9859cc8476f2 | 564 | printf("%s","LoRa pkt seq "); |
cdupaty | 0:9859cc8476f2 | 565 | printf("%d", sx1272.packet_sent.packnum); |
cdupaty | 0:9859cc8476f2 | 566 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 567 | |
cdupaty | 0:9859cc8476f2 | 568 | printf("%s","LoRa Sent in "); |
cdupaty | 0:9859cc8476f2 | 569 | printf("%ld", endSend-startSend); |
cdupaty | 0:9859cc8476f2 | 570 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 571 | |
cdupaty | 0:9859cc8476f2 | 572 | printf("%s","LoRa Sent w/CAD in "); |
cdupaty | 0:9859cc8476f2 | 573 | printf("%ld", endSend-sx1272._startDoCad); |
cdupaty | 0:9859cc8476f2 | 574 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 575 | |
cdupaty | 0:9859cc8476f2 | 576 | printf("%s","Packet sent, state "); |
cdupaty | 0:9859cc8476f2 | 577 | printf("%d", e); |
cdupaty | 0:9859cc8476f2 | 578 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 579 | |
cdupaty | 0:9859cc8476f2 | 580 | printf("%s","Remaining ToA is "); |
cdupaty | 0:9859cc8476f2 | 581 | printf("%d", sx1272.getRemainingToA()); |
cdupaty | 0:9859cc8476f2 | 582 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 583 | |
cdupaty | 0:9859cc8476f2 | 584 | #if defined LOW_POWER && not defined ARDUINO_SAM_DUE |
cdupaty | 0:9859cc8476f2 | 585 | printf("%s","Switch to power saving mode\n"); |
cdupaty | 0:9859cc8476f2 | 586 | |
cdupaty | 0:9859cc8476f2 | 587 | e = sx1272.setSleepMode(); |
cdupaty | 0:9859cc8476f2 | 588 | |
cdupaty | 0:9859cc8476f2 | 589 | if (!e) |
cdupaty | 0:9859cc8476f2 | 590 | printf("%s","Successfully switch LoRa module in sleep mode\n"); |
cdupaty | 0:9859cc8476f2 | 591 | else |
cdupaty | 0:9859cc8476f2 | 592 | printf("%s","Could not switch LoRa module in sleep mode\n"); |
cdupaty | 0:9859cc8476f2 | 593 | |
cdupaty | 0:9859cc8476f2 | 594 | FLUSHOUTPUT |
cdupaty | 0:9859cc8476f2 | 595 | wait_ms(50); |
cdupaty | 0:9859cc8476f2 | 596 | |
cdupaty | 0:9859cc8476f2 | 597 | #ifdef __SAMD21G18A__ |
cdupaty | 0:9859cc8476f2 | 598 | // For Arduino M0 or Zero we use the built-in RTC |
cdupaty | 0:9859cc8476f2 | 599 | //LowPower.standby(); |
cdupaty | 0:9859cc8476f2 | 600 | rtc.setTime(17, 0, 0); |
cdupaty | 0:9859cc8476f2 | 601 | rtc.setDate(1, 1, 2000); |
cdupaty | 0:9859cc8476f2 | 602 | rtc.setAlarmTime(17, idlePeriodInMin, 0); |
cdupaty | 0:9859cc8476f2 | 603 | // for testing with 20s |
cdupaty | 0:9859cc8476f2 | 604 | //rtc.setAlarmTime(17, 0, 20); |
cdupaty | 0:9859cc8476f2 | 605 | rtc.enableAlarm(rtc.MATCH_HHMMSS); |
cdupaty | 0:9859cc8476f2 | 606 | //rtc.attachInterrupt(alarmMatch); |
cdupaty | 0:9859cc8476f2 | 607 | rtc.standbyMode(); |
cdupaty | 0:9859cc8476f2 | 608 | |
cdupaty | 0:9859cc8476f2 | 609 | printf("%s","SAMD21G18A wakes up from standby\n"); |
cdupaty | 0:9859cc8476f2 | 610 | FLUSHOUTPUT |
cdupaty | 0:9859cc8476f2 | 611 | #else |
cdupaty | 0:9859cc8476f2 | 612 | nCycle = idlePeriodInMin*60/LOW_POWER_PERIOD + random(2,4); |
cdupaty | 0:9859cc8476f2 | 613 | |
cdupaty | 0:9859cc8476f2 | 614 | #if defined __MK20DX256__ || defined __MKL26Z64__ || defined __MK64FX512__ || defined __MK66FX1M0__ |
cdupaty | 0:9859cc8476f2 | 615 | // warning, setTimer accepts value from 1ms to 65535ms max |
cdupaty | 0:9859cc8476f2 | 616 | timer.setTimer(LOW_POWER_PERIOD*1000 + random(1,5)*1000);// milliseconds |
cdupaty | 0:9859cc8476f2 | 617 | |
cdupaty | 0:9859cc8476f2 | 618 | nCycle = idlePeriodInMin*60/LOW_POWER_PERIOD; |
cdupaty | 0:9859cc8476f2 | 619 | #endif |
cdupaty | 0:9859cc8476f2 | 620 | for (int i=0; i<nCycle; i++) { |
cdupaty | 0:9859cc8476f2 | 621 | |
cdupaty | 0:9859cc8476f2 | 622 | #if defined ARDUINO_AVR_PRO || defined ARDUINO_AVR_NANO || defined ARDUINO_AVR_UNO || defined ARDUINO_AVR_MINI || defined __AVR_ATmega32U4__ |
cdupaty | 0:9859cc8476f2 | 623 | // ATmega328P, ATmega168, ATmega32U4 |
cdupaty | 0:9859cc8476f2 | 624 | LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); |
cdupaty | 0:9859cc8476f2 | 625 | |
cdupaty | 0:9859cc8476f2 | 626 | //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, |
cdupaty | 0:9859cc8476f2 | 627 | // SPI_OFF, USART0_OFF, TWI_OFF); |
cdupaty | 0:9859cc8476f2 | 628 | #elif defined ARDUINO_AVR_MEGA2560 |
cdupaty | 0:9859cc8476f2 | 629 | // ATmega2560 |
cdupaty | 0:9859cc8476f2 | 630 | LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); |
cdupaty | 0:9859cc8476f2 | 631 | |
cdupaty | 0:9859cc8476f2 | 632 | //LowPower.idle(SLEEP_8S, ADC_OFF, TIMER5_OFF, TIMER4_OFF, TIMER3_OFF, |
cdupaty | 0:9859cc8476f2 | 633 | // TIMER2_OFF, TIMER1_OFF, TIMER0_OFF, SPI_OFF, USART3_OFF, |
cdupaty | 0:9859cc8476f2 | 634 | // USART2_OFF, USART1_OFF, USART0_OFF, TWI_OFF); |
cdupaty | 0:9859cc8476f2 | 635 | #elif defined __MK20DX256__ || defined __MKL26Z64__ || defined __MK64FX512__ || defined __MK66FX1M0__ |
cdupaty | 0:9859cc8476f2 | 636 | // Teensy31/32 & TeensyLC |
cdupaty | 0:9859cc8476f2 | 637 | #ifdef LOW_POWER_HIBERNATE |
cdupaty | 0:9859cc8476f2 | 638 | Snooze.hibernate(sleep_config); |
cdupaty | 0:9859cc8476f2 | 639 | #else |
cdupaty | 0:9859cc8476f2 | 640 | Snooze.deepSleep(sleep_config); |
cdupaty | 0:9859cc8476f2 | 641 | #endif |
cdupaty | 0:9859cc8476f2 | 642 | #else |
cdupaty | 0:9859cc8476f2 | 643 | // use the wait_ms function |
cdupaty | 0:9859cc8476f2 | 644 | wait_ms(LOW_POWER_PERIOD*1000); |
cdupaty | 0:9859cc8476f2 | 645 | #endif |
cdupaty | 0:9859cc8476f2 | 646 | printf("%s","."); |
cdupaty | 0:9859cc8476f2 | 647 | FLUSHOUTPUT |
cdupaty | 0:9859cc8476f2 | 648 | wait_ms(10); |
cdupaty | 0:9859cc8476f2 | 649 | } |
cdupaty | 0:9859cc8476f2 | 650 | |
cdupaty | 0:9859cc8476f2 | 651 | wait_ms(50); |
cdupaty | 0:9859cc8476f2 | 652 | #endif |
cdupaty | 0:9859cc8476f2 | 653 | |
cdupaty | 0:9859cc8476f2 | 654 | #else |
cdupaty | 0:9859cc8476f2 | 655 | printf("%ld ", nextTransmissionTime); |
cdupaty | 0:9859cc8476f2 | 656 | printf("%s","Will send next value at "); |
cdupaty | 0:9859cc8476f2 | 657 | // use a random part also to avoid collision |
cdupaty | 0:9859cc8476f2 | 658 | nextTransmissionTime=millis()+(unsigned long)idlePeriodInMin*60*1000+(unsigned long)(rand()%60+15)*1000; |
cdupaty | 0:9859cc8476f2 | 659 | printf("%ld", nextTransmissionTime); |
cdupaty | 0:9859cc8476f2 | 660 | printf("\n"); |
cdupaty | 0:9859cc8476f2 | 661 | } |
cdupaty | 0:9859cc8476f2 | 662 | #endif |
cdupaty | 0:9859cc8476f2 | 663 | |
cdupaty | 0:9859cc8476f2 | 664 | }// fin boucle |
cdupaty | 0:9859cc8476f2 | 665 | |
cdupaty | 0:9859cc8476f2 | 666 | //return (0); |
cdupaty | 0:9859cc8476f2 | 667 | |
cdupaty | 0:9859cc8476f2 | 668 | } |