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