p2p-pingpong-clickbutton

Dependencies:   PingPong-P2P-ClickButtonToWork-SX1272 Lora_SX1272_Coragem

Committer:
marcoantonioara
Date:
Tue Sep 03 21:25:05 2019 +0000
Revision:
3:cbe3f441353e
Parent:
2:4eef0eca4d26
Child:
4:05d5aa4d3f2d
Child:
5:ef21a65a3450
Coragem Adaptation

Who changed what in which revision?

UserRevisionLine numberNew 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"
marcoantonioara 3:cbe3f441353e 34
marcoantonioara 3:cbe3f441353e 35
cdupaty 0:9859cc8476f2 36 #define BAND868
cdupaty 0:9859cc8476f2 37 #define MAX_DBM 14
marcoantonioara 3:cbe3f441353e 38 #define DATA 0
marcoantonioara 3:cbe3f441353e 39 #define PING 1
marcoantonioara 3:cbe3f441353e 40 #define PONG 2
cdupaty 0:9859cc8476f2 41
marcoantonioara 3:cbe3f441353e 42 #define ADDRESS 10
marcoantonioara 3:cbe3f441353e 43
marcoantonioara 3:cbe3f441353e 44 const uint32_t DEFAULT_CHANNEL=CH_12_900;//915Mhz CH_12_900 =>0xE4C000
cdupaty 0:9859cc8476f2 45
cdupaty 2:4eef0eca4d26 46 //#define WITH_EEPROM // TO DO ON STM32
cdupaty 0:9859cc8476f2 47 #define WITH_APPKEY
cdupaty 0:9859cc8476f2 48 #define NEW_DATA_FIELD
marcoantonioara 3:cbe3f441353e 49 //#define WITH_ACK
cdupaty 0:9859cc8476f2 50
cdupaty 0:9859cc8476f2 51 ///////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 52 // CHANGE HERE THE LORA MODE, NODE ADDRESS
cdupaty 0:9859cc8476f2 53 #define LORAMODE 1
cdupaty 0:9859cc8476f2 54 #define node_addr 6
cdupaty 0:9859cc8476f2 55 //////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 56
cdupaty 0:9859cc8476f2 57 ///////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 58 // CHANGE HERE THE THINGSPEAK FIELD BETWEEN 1 AND 4
cdupaty 0:9859cc8476f2 59 #define field_index 1
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
marcoantonioara 3:cbe3f441353e 64 //#define minTime 120 // 2 minutes
marcoantonioara 3:cbe3f441353e 65 //#define maxTime 600 // 10 minutes
marcoantonioara 3:cbe3f441353e 66 #define minTime 5 // seconds
marcoantonioara 3:cbe3f441353e 67 #define maxTime 25 // seconds
cdupaty 0:9859cc8476f2 68 ///////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 69
cdupaty 0:9859cc8476f2 70 #ifdef WITH_APPKEY
cdupaty 0:9859cc8476f2 71 ///////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 72 // CHANGE HERE THE APPKEY, BUT IF GW CHECKS FOR APPKEY, MUST BE
cdupaty 0:9859cc8476f2 73 // IN THE APPKEY LIST MAINTAINED BY GW.
cdupaty 0:9859cc8476f2 74 uint8_t my_appKey[4]={5, 6, 7, 8};
cdupaty 0:9859cc8476f2 75 ///////////////////////////////////////////////////////////////////
cdupaty 0:9859cc8476f2 76 #endif
cdupaty 0:9859cc8476f2 77
cdupaty 0:9859cc8476f2 78 #ifdef WITH_EEPROM
cdupaty 0:9859cc8476f2 79 #include <EEPROM.h>
cdupaty 0:9859cc8476f2 80 #endif
cdupaty 0:9859cc8476f2 81
marcoantonioara 3:cbe3f441353e 82 #define DEFAULT_DEST_ADDR 6
cdupaty 0:9859cc8476f2 83
cdupaty 0:9859cc8476f2 84 #ifdef WITH_ACK
cdupaty 0:9859cc8476f2 85 #define NB_RETRIES 2
cdupaty 0:9859cc8476f2 86 #endif
cdupaty 0:9859cc8476f2 87
cdupaty 0:9859cc8476f2 88 #ifdef WITH_EEPROM
cdupaty 0:9859cc8476f2 89 struct sx1272config {
cdupaty 0:9859cc8476f2 90
marcoantonioara 3:cbe3f441353e 91 uint8_t flag1;
marcoantonioara 3:cbe3f441353e 92 uint8_t flag2;
marcoantonioara 3:cbe3f441353e 93 uint8_t seq;
marcoantonioara 3:cbe3f441353e 94 // can add other fields such as LoRa mode,...
cdupaty 0:9859cc8476f2 95 };
cdupaty 0:9859cc8476f2 96
cdupaty 0:9859cc8476f2 97 sx1272config my_sx1272config;
cdupaty 0:9859cc8476f2 98 #endif
cdupaty 0:9859cc8476f2 99
cdupaty 2:4eef0eca4d26 100 ///////////////////////////////////////
cdupaty 2:4eef0eca4d26 101 // Globals variables
cdupaty 2:4eef0eca4d26 102 ///////////////////////////////////////
marcoantonioara 3:cbe3f441353e 103
marcoantonioara 3:cbe3f441353e 104 //______________BME_280________________
marcoantonioara 3:cbe3f441353e 105 char cmd_bme[18];
marcoantonioara 3:cbe3f441353e 106 uint32_t hum_raw;
marcoantonioara 3:cbe3f441353e 107 float humf;
marcoantonioara 3:cbe3f441353e 108 uint32_t temp_raw;
marcoantonioara 3:cbe3f441353e 109 float tempf;
marcoantonioara 3:cbe3f441353e 110 uint32_t press_raw;
marcoantonioara 3:cbe3f441353e 111 float pressf;
marcoantonioara 3:cbe3f441353e 112 //char cmd[4];
marcoantonioara 3:cbe3f441353e 113
marcoantonioara 3:cbe3f441353e 114 //___________SX1272____________________
marcoantonioara 3:cbe3f441353e 115 int e;
marcoantonioara 3:cbe3f441353e 116
marcoantonioara 3:cbe3f441353e 117 //___________mensagem_____________________
marcoantonioara 3:cbe3f441353e 118 uint8_t message[100];
marcoantonioara 3:cbe3f441353e 119 uint8_t buffer[100];
marcoantonioara 3:cbe3f441353e 120 uint8_t message_pong[]="Pong";
marcoantonioara 3:cbe3f441353e 121 uint8_t message_ping[]="Ping";
marcoantonioara 3:cbe3f441353e 122 uint8_t r_size;
marcoantonioara 3:cbe3f441353e 123 char float_temp[10];
marcoantonioara 3:cbe3f441353e 124 char float_press[10];
marcoantonioara 3:cbe3f441353e 125 char float_hum[10];
marcoantonioara 3:cbe3f441353e 126 int cont =0;
marcoantonioara 3:cbe3f441353e 127
marcoantonioara 3:cbe3f441353e 128
marcoantonioara 3:cbe3f441353e 129 DigitalOut led1(P1_13);
marcoantonioara 3:cbe3f441353e 130 DigitalOut led2(P1_14);
marcoantonioara 3:cbe3f441353e 131 InterruptIn dio0(P0_12);
marcoantonioara 3:cbe3f441353e 132 DigitalIn button1(P1_11);
marcoantonioara 3:cbe3f441353e 133 DigitalIn button2(P1_12);
marcoantonioara 3:cbe3f441353e 134 DigitalIn button3(P0_30);
marcoantonioara 3:cbe3f441353e 135
marcoantonioara 3:cbe3f441353e 136 EventQueue queue;
marcoantonioara 3:cbe3f441353e 137
cdupaty 2:4eef0eca4d26 138 int loraMode=LORAMODE;
marcoantonioara 3:cbe3f441353e 139
marcoantonioara 3:cbe3f441353e 140 //_________BME 280___
marcoantonioara 3:cbe3f441353e 141
marcoantonioara 3:cbe3f441353e 142 I2C sensor(p13, p15);
marcoantonioara 3:cbe3f441353e 143 const int BME280_ADDR = (0x77 << 1);
marcoantonioara 3:cbe3f441353e 144
marcoantonioara 3:cbe3f441353e 145 uint16_t dig_T1,dig_P1,dig_H1, dig_H3;
marcoantonioara 3:cbe3f441353e 146 int16_t dig_T2, dig_T3,dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9,dig_H2, dig_H4, dig_H5, dig_H6;
marcoantonioara 3:cbe3f441353e 147 int32_t t_fine;
marcoantonioara 3:cbe3f441353e 148
marcoantonioara 3:cbe3f441353e 149 //============= internet of turtles =============
marcoantonioara 3:cbe3f441353e 150 Timer time_breathing;
marcoantonioara 3:cbe3f441353e 151 Timer time_diving;
marcoantonioara 3:cbe3f441353e 152 bool beathing = false;
marcoantonioara 3:cbe3f441353e 153 float last_breathing_time = 0.0;
marcoantonioara 3:cbe3f441353e 154 float last_diving_time = 0.0;
marcoantonioara 3:cbe3f441353e 155
marcoantonioara 3:cbe3f441353e 156 char float_breathing_time[10];
marcoantonioara 3:cbe3f441353e 157 char float_diving_time[10];
marcoantonioara 3:cbe3f441353e 158
marcoantonioara 3:cbe3f441353e 159 Timer seconds;
marcoantonioara 3:cbe3f441353e 160
marcoantonioara 3:cbe3f441353e 161 //================================================
marcoantonioara 3:cbe3f441353e 162 void bme_init() { //bme initialization
marcoantonioara 3:cbe3f441353e 163
marcoantonioara 3:cbe3f441353e 164
marcoantonioara 3:cbe3f441353e 165 sensor.frequency(1000000);
marcoantonioara 3:cbe3f441353e 166
marcoantonioara 3:cbe3f441353e 167 cmd_bme[0] = 0xf2; // ctrl_hum
marcoantonioara 3:cbe3f441353e 168 cmd_bme[1] = 0x01;
marcoantonioara 3:cbe3f441353e 169 sensor.write(BME280_ADDR, cmd_bme, 2);
marcoantonioara 3:cbe3f441353e 170
marcoantonioara 3:cbe3f441353e 171 cmd_bme[0] = 0xf4; // ctrl_meas
marcoantonioara 3:cbe3f441353e 172 cmd_bme[1] = 0x27;
marcoantonioara 3:cbe3f441353e 173 sensor.write(BME280_ADDR, cmd_bme, 2);
marcoantonioara 3:cbe3f441353e 174
marcoantonioara 3:cbe3f441353e 175 cmd_bme[0] = 0xf5; // config
marcoantonioara 3:cbe3f441353e 176 cmd_bme[1] = 0xa0;
marcoantonioara 3:cbe3f441353e 177 sensor.write(BME280_ADDR, cmd_bme, 2);
marcoantonioara 3:cbe3f441353e 178
marcoantonioara 3:cbe3f441353e 179 cmd_bme[0] = 0x88; // read dig_T regs
marcoantonioara 3:cbe3f441353e 180 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 181 sensor.read(BME280_ADDR, cmd_bme, 6);
marcoantonioara 3:cbe3f441353e 182
marcoantonioara 3:cbe3f441353e 183 dig_T1 = (cmd_bme[1] << 8) | cmd_bme[0];
marcoantonioara 3:cbe3f441353e 184 dig_T2 = (cmd_bme[3] << 8) | cmd_bme[2];
marcoantonioara 3:cbe3f441353e 185 dig_T3 = (cmd_bme[5] << 8) | cmd_bme[4];
marcoantonioara 3:cbe3f441353e 186
marcoantonioara 3:cbe3f441353e 187 // DEBUG_PRINT("dig_T = 0x%x, 0x%x, 0x%x\n", dig_T1, dig_T2, dig_T3);
marcoantonioara 3:cbe3f441353e 188
marcoantonioara 3:cbe3f441353e 189 cmd_bme[0] = 0x8E; // read dig_P regs
marcoantonioara 3:cbe3f441353e 190 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 191 sensor.read(BME280_ADDR, cmd_bme, 18);
marcoantonioara 3:cbe3f441353e 192
marcoantonioara 3:cbe3f441353e 193 dig_P1 = (cmd_bme[ 1] << 8) | cmd_bme[ 0];
marcoantonioara 3:cbe3f441353e 194 dig_P2 = (cmd_bme[ 3] << 8) | cmd_bme[ 2];
marcoantonioara 3:cbe3f441353e 195 dig_P3 = (cmd_bme[ 5] << 8) | cmd_bme[ 4];
marcoantonioara 3:cbe3f441353e 196 dig_P4 = (cmd_bme[ 7] << 8) | cmd_bme[ 6];
marcoantonioara 3:cbe3f441353e 197 dig_P5 = (cmd_bme[ 9] << 8) | cmd_bme[ 8];
marcoantonioara 3:cbe3f441353e 198 dig_P6 = (cmd_bme[11] << 8) | cmd_bme[10];
marcoantonioara 3:cbe3f441353e 199 dig_P7 = (cmd_bme[13] << 8) | cmd_bme[12];
marcoantonioara 3:cbe3f441353e 200 dig_P8 = (cmd_bme[15] << 8) | cmd_bme[14];
marcoantonioara 3:cbe3f441353e 201 dig_P9 = (cmd_bme[17] << 8) | cmd_bme[16];
marcoantonioara 3:cbe3f441353e 202
marcoantonioara 3:cbe3f441353e 203 // DEBUG_PRINT("dig_P = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_P1, dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9);
marcoantonioara 3:cbe3f441353e 204
marcoantonioara 3:cbe3f441353e 205 cmd_bme[0] = 0xA1; // read dig_H regs
marcoantonioara 3:cbe3f441353e 206 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 207 sensor.read(BME280_ADDR, cmd_bme, 9);
marcoantonioara 3:cbe3f441353e 208
marcoantonioara 3:cbe3f441353e 209 dig_H1 = cmd_bme[0];
marcoantonioara 3:cbe3f441353e 210 dig_H2 = (cmd_bme[2] << 8) | cmd_bme[1];
marcoantonioara 3:cbe3f441353e 211 dig_H3 = cmd_bme[3];
marcoantonioara 3:cbe3f441353e 212 dig_H4 = (cmd_bme[4] << 4) | (cmd_bme[5] & 0x0f);
marcoantonioara 3:cbe3f441353e 213 dig_H5 = (cmd_bme[7] << 4) | ((cmd_bme[6]>>4) & 0x0f);
marcoantonioara 3:cbe3f441353e 214 dig_H6 = cmd_bme[8];
marcoantonioara 3:cbe3f441353e 215
marcoantonioara 3:cbe3f441353e 216 // DEBUG_PRINT("dig_H = 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", dig_H1, dig_H2, dig_H3, dig_H4, dig_H5, dig_H6);
marcoantonioara 3:cbe3f441353e 217 }
marcoantonioara 3:cbe3f441353e 218
marcoantonioara 3:cbe3f441353e 219 float getTemperature(){
marcoantonioara 3:cbe3f441353e 220
marcoantonioara 3:cbe3f441353e 221
marcoantonioara 3:cbe3f441353e 222 cmd_bme[0] = 0xfa; // temp_msb
marcoantonioara 3:cbe3f441353e 223 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 224 sensor.read(BME280_ADDR, &cmd_bme[1], 1);
marcoantonioara 3:cbe3f441353e 225
marcoantonioara 3:cbe3f441353e 226 cmd_bme[0] = 0xfb; // temp_lsb
marcoantonioara 3:cbe3f441353e 227 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 228 sensor.read(BME280_ADDR, &cmd_bme[2], 1);
marcoantonioara 3:cbe3f441353e 229
marcoantonioara 3:cbe3f441353e 230 cmd_bme[0] = 0xfc; // temp_xlsb
marcoantonioara 3:cbe3f441353e 231 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 232 sensor.read(BME280_ADDR, &cmd_bme[3], 1);
marcoantonioara 3:cbe3f441353e 233
marcoantonioara 3:cbe3f441353e 234 temp_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
marcoantonioara 3:cbe3f441353e 235
marcoantonioara 3:cbe3f441353e 236 int32_t temp;
marcoantonioara 3:cbe3f441353e 237
marcoantonioara 3:cbe3f441353e 238 temp =
marcoantonioara 3:cbe3f441353e 239 (((((temp_raw >> 3) - (dig_T1 << 1))) * dig_T2) >> 11) +
marcoantonioara 3:cbe3f441353e 240 ((((((temp_raw >> 4) - dig_T1) * ((temp_raw >> 4) - dig_T1)) >> 12) * dig_T3) >> 14);
marcoantonioara 3:cbe3f441353e 241
marcoantonioara 3:cbe3f441353e 242 t_fine = temp;
marcoantonioara 3:cbe3f441353e 243 temp = (temp * 5 + 128) >> 8;
marcoantonioara 3:cbe3f441353e 244 tempf = (float)temp;
marcoantonioara 3:cbe3f441353e 245
marcoantonioara 3:cbe3f441353e 246 return (tempf/100.0f);
marcoantonioara 3:cbe3f441353e 247 }
marcoantonioara 3:cbe3f441353e 248
marcoantonioara 3:cbe3f441353e 249 float getPressure(){
marcoantonioara 3:cbe3f441353e 250
marcoantonioara 3:cbe3f441353e 251 cmd_bme[0] = 0xf7; // press_msb
marcoantonioara 3:cbe3f441353e 252 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 253 sensor.read(BME280_ADDR, &cmd_bme[1], 1);
marcoantonioara 3:cbe3f441353e 254
marcoantonioara 3:cbe3f441353e 255 cmd_bme[0] = 0xf8; // press_lsb
marcoantonioara 3:cbe3f441353e 256 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 257 sensor.read(BME280_ADDR, &cmd_bme[2], 1);
marcoantonioara 3:cbe3f441353e 258
marcoantonioara 3:cbe3f441353e 259 cmd_bme[0] = 0xf9; // press_xlsb
marcoantonioara 3:cbe3f441353e 260 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 261 sensor.read(BME280_ADDR, &cmd_bme[3], 1);
marcoantonioara 3:cbe3f441353e 262
marcoantonioara 3:cbe3f441353e 263 press_raw = (cmd_bme[1] << 12) | (cmd_bme[2] << 4) | (cmd_bme[3] >> 4);
marcoantonioara 3:cbe3f441353e 264
marcoantonioara 3:cbe3f441353e 265 int32_t var1, var2;
marcoantonioara 3:cbe3f441353e 266 uint32_t press;
marcoantonioara 3:cbe3f441353e 267
marcoantonioara 3:cbe3f441353e 268 var1 = (t_fine >> 1) - 64000;
marcoantonioara 3:cbe3f441353e 269 var2 = (((var1 >> 2) * (var1 >> 2)) >> 11) * dig_P6;
marcoantonioara 3:cbe3f441353e 270 var2 = var2 + ((var1 * dig_P5) << 1);
marcoantonioara 3:cbe3f441353e 271 var2 = (var2 >> 2) + (dig_P4 << 16);
marcoantonioara 3:cbe3f441353e 272 var1 = (((dig_P3 * (((var1 >> 2)*(var1 >> 2)) >> 13)) >> 3) + ((dig_P2 * var1) >> 1)) >> 18;
marcoantonioara 3:cbe3f441353e 273 var1 = ((32768 + var1) * dig_P1) >> 15;
marcoantonioara 3:cbe3f441353e 274 if (var1 == 0) {
marcoantonioara 3:cbe3f441353e 275 return 0;
marcoantonioara 3:cbe3f441353e 276 }
marcoantonioara 3:cbe3f441353e 277 press = (((1048576 - press_raw) - (var2 >> 12))) * 3125;
marcoantonioara 3:cbe3f441353e 278 if(press < 0x80000000) {
marcoantonioara 3:cbe3f441353e 279 press = (press << 1) / var1;
marcoantonioara 3:cbe3f441353e 280 }
marcoantonioara 3:cbe3f441353e 281 else {
marcoantonioara 3:cbe3f441353e 282 press = (press / var1) * 2;
marcoantonioara 3:cbe3f441353e 283 }
marcoantonioara 3:cbe3f441353e 284 var1 = ((int32_t)dig_P9 * ((int32_t)(((press >> 3) * (press >> 3)) >> 13))) >> 12;
marcoantonioara 3:cbe3f441353e 285 var2 = (((int32_t)(press >> 2)) * (int32_t)dig_P8) >> 13;
marcoantonioara 3:cbe3f441353e 286 press = (press + ((var1 + var2 + dig_P7) >> 4));
marcoantonioara 3:cbe3f441353e 287
marcoantonioara 3:cbe3f441353e 288 pressf = (float)press;
marcoantonioara 3:cbe3f441353e 289 return (pressf/100.0f);
marcoantonioara 3:cbe3f441353e 290 }
marcoantonioara 3:cbe3f441353e 291
marcoantonioara 3:cbe3f441353e 292 float getHumidity(){
marcoantonioara 3:cbe3f441353e 293
marcoantonioara 3:cbe3f441353e 294
marcoantonioara 3:cbe3f441353e 295 cmd_bme[0] = 0xfd; // hum_msb
marcoantonioara 3:cbe3f441353e 296 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 297 sensor.read(BME280_ADDR, &cmd_bme[1], 1);
marcoantonioara 3:cbe3f441353e 298
marcoantonioara 3:cbe3f441353e 299 cmd_bme[0] = 0xfd; // hum_lsb
marcoantonioara 3:cbe3f441353e 300 sensor.write(BME280_ADDR, cmd_bme, 1);
marcoantonioara 3:cbe3f441353e 301 sensor.read(BME280_ADDR, &cmd_bme[2], 1);
marcoantonioara 3:cbe3f441353e 302
marcoantonioara 3:cbe3f441353e 303 hum_raw = (cmd_bme[1] << 8) | cmd_bme[2];
marcoantonioara 3:cbe3f441353e 304
marcoantonioara 3:cbe3f441353e 305 int32_t v_x1;
marcoantonioara 3:cbe3f441353e 306
marcoantonioara 3:cbe3f441353e 307 v_x1 = t_fine - 76800;
marcoantonioara 3:cbe3f441353e 308 v_x1 = (((((hum_raw << 14) -(((int32_t)dig_H4) << 20) - (((int32_t)dig_H5) * v_x1)) +
marcoantonioara 3:cbe3f441353e 309 ((int32_t)16384)) >> 15) * (((((((v_x1 * (int32_t)dig_H6) >> 10) *
marcoantonioara 3:cbe3f441353e 310 (((v_x1 * ((int32_t)dig_H3)) >> 11) + 32768)) >> 10) + 2097152) *
marcoantonioara 3:cbe3f441353e 311 (int32_t)dig_H2 + 8192) >> 14));
marcoantonioara 3:cbe3f441353e 312 v_x1 = (v_x1 - (((((v_x1 >> 15) * (v_x1 >> 15)) >> 7) * (int32_t)dig_H1) >> 4));
marcoantonioara 3:cbe3f441353e 313 v_x1 = (v_x1 < 0 ? 0 : v_x1);
marcoantonioara 3:cbe3f441353e 314 v_x1 = (v_x1 > 419430400 ? 419430400 : v_x1);
marcoantonioara 3:cbe3f441353e 315
marcoantonioara 3:cbe3f441353e 316 humf = (float)(v_x1 >> 12);
marcoantonioara 3:cbe3f441353e 317
marcoantonioara 3:cbe3f441353e 318 return (humf/1024.0f);
marcoantonioara 3:cbe3f441353e 319 }
cdupaty 0:9859cc8476f2 320
cdupaty 2:4eef0eca4d26 321 /////////////////////////////////////////
cdupaty 2:4eef0eca4d26 322 // SETUP SX1272 initialisation
cdupaty 2:4eef0eca4d26 323 ////////////////////////////////////////
marcoantonioara 3:cbe3f441353e 324 void setup(){
marcoantonioara 3:cbe3f441353e 325
marcoantonioara 3:cbe3f441353e 326
marcoantonioara 3:cbe3f441353e 327 printf("------Coragem LoRa temperature sensor-------------\n");
marcoantonioara 3:cbe3f441353e 328 //sx1272.ON(); // Power ON the module
cdupaty 0:9859cc8476f2 329
marcoantonioara 3:cbe3f441353e 330 int error_config_sx1272=0;
marcoantonioara 3:cbe3f441353e 331
marcoantonioara 3:cbe3f441353e 332 // Set transmission mode and print the result
marcoantonioara 3:cbe3f441353e 333 e = sx1272.setMode(loraMode);
marcoantonioara 3:cbe3f441353e 334 printf("Mode: %d\n",loraMode);
marcoantonioara 3:cbe3f441353e 335 if (e) error_config_sx1272=1;
marcoantonioara 3:cbe3f441353e 336 printf("Setting Mode: state %d\n",e);
marcoantonioara 3:cbe3f441353e 337
marcoantonioara 3:cbe3f441353e 338 // enable carrier sense
marcoantonioara 3:cbe3f441353e 339 sx1272._enableCarrierSense=true;
marcoantonioara 3:cbe3f441353e 340
marcoantonioara 3:cbe3f441353e 341 // for LOW POWER
marcoantonioara 3:cbe3f441353e 342 sx1272._RSSIonSend=false;
marcoantonioara 3:cbe3f441353e 343
marcoantonioara 3:cbe3f441353e 344
marcoantonioara 3:cbe3f441353e 345 // Select frequency channel
marcoantonioara 3:cbe3f441353e 346 e = sx1272.setChannel(DEFAULT_CHANNEL);
marcoantonioara 3:cbe3f441353e 347 if (e) error_config_sx1272=1;
marcoantonioara 3:cbe3f441353e 348 printf("Setting Channel: state %d\n",e);
cdupaty 0:9859cc8476f2 349
marcoantonioara 3:cbe3f441353e 350 // Select amplifier line; PABOOST or RFO
marcoantonioara 3:cbe3f441353e 351 // #ifdef PABOOST
marcoantonioara 3:cbe3f441353e 352 // printf("pabboost\n");
marcoantonioara 3:cbe3f441353e 353 // sx1272._needPABOOST=true;
marcoantonioara 3:cbe3f441353e 354 // // previous way for setting output power
marcoantonioara 3:cbe3f441353e 355 // // powerLevel='x';
marcoantonioara 3:cbe3f441353e 356 // #else
marcoantonioara 3:cbe3f441353e 357 // // previous way for setting output power
marcoantonioara 3:cbe3f441353e 358 // // powerLevel='M';
marcoantonioara 3:cbe3f441353e 359 // #endif
marcoantonioara 3:cbe3f441353e 360
marcoantonioara 3:cbe3f441353e 361 // previous way for setting output power
marcoantonioara 3:cbe3f441353e 362 // e = sx1272.setPower(powerLevel);
marcoantonioara 3:cbe3f441353e 363
marcoantonioara 3:cbe3f441353e 364 e = sx1272.setPowerDBM((uint8_t)MAX_DBM);
marcoantonioara 3:cbe3f441353e 365 if (e) error_config_sx1272=1;
marcoantonioara 3:cbe3f441353e 366 printf("Setting Power: state %d\n",e);
marcoantonioara 3:cbe3f441353e 367
marcoantonioara 3:cbe3f441353e 368 // Set the node address and print the result
marcoantonioara 3:cbe3f441353e 369 e = sx1272.setNodeAddress(node_addr);
marcoantonioara 3:cbe3f441353e 370 if (e) error_config_sx1272=1;
marcoantonioara 3:cbe3f441353e 371 printf("Setting node addr: state %d\n",e);
marcoantonioara 3:cbe3f441353e 372
marcoantonioara 3:cbe3f441353e 373 // Print a success message
marcoantonioara 3:cbe3f441353e 374 if (!error_config_sx1272) printf("SX1272 successfully configured\n");
marcoantonioara 3:cbe3f441353e 375 else printf("ERROR CONFIGURATION SX1272\n");
marcoantonioara 3:cbe3f441353e 376
marcoantonioara 3:cbe3f441353e 377 wait_ms(400);
cdupaty 0:9859cc8476f2 378 }
cdupaty 0:9859cc8476f2 379
cdupaty 0:9859cc8476f2 380
marcoantonioara 3:cbe3f441353e 381 void send_packet(int mode){
marcoantonioara 3:cbe3f441353e 382 led1=1;
marcoantonioara 3:cbe3f441353e 383 sx1272.writeRegister(0x01,129);//standby
marcoantonioara 3:cbe3f441353e 384 sx1272.setPacketType(PKT_TYPE_DATA);
marcoantonioara 3:cbe3f441353e 385
marcoantonioara 3:cbe3f441353e 386 printf ("sendpacket1\n");
marcoantonioara 3:cbe3f441353e 387 if (mode == DATA ) {
marcoantonioara 3:cbe3f441353e 388 // uint8_t message[100];
marcoantonioara 3:cbe3f441353e 389 sprintf(float_temp,"%2.2f",getTemperature());
marcoantonioara 3:cbe3f441353e 390 sprintf(float_press,"%04.2f",getPressure());
marcoantonioara 3:cbe3f441353e 391 sprintf(float_hum,"%2.2f",getHumidity());
marcoantonioara 3:cbe3f441353e 392
marcoantonioara 3:cbe3f441353e 393 printf ("sendpacket2\n");
marcoantonioara 3:cbe3f441353e 394 //============= internet of turtles =============
marcoantonioara 3:cbe3f441353e 395 sprintf(float_breathing_time,"%2.2f", last_breathing_time);
marcoantonioara 3:cbe3f441353e 396 sprintf(float_diving_time,"%2.2f", last_diving_time);
marcoantonioara 3:cbe3f441353e 397 r_size=sprintf((char*)message,"\\!#%s°C_%shPa_%s_Dt%s_Bt%s%%",float_temp,float_press,float_hum,float_diving_time,float_breathing_time);
marcoantonioara 3:cbe3f441353e 398 // ==============================================
marcoantonioara 3:cbe3f441353e 399
marcoantonioara 3:cbe3f441353e 400 printf ("sendpacket3\n");
marcoantonioara 3:cbe3f441353e 401 //size=sprintf((char*)message,"\\!#%s°C_%shPa_%s%%",float_temp,float_press,float_hum);
marcoantonioara 3:cbe3f441353e 402 sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, r_size);
marcoantonioara 3:cbe3f441353e 403 printf ("sendpacket4\n");
marcoantonioara 3:cbe3f441353e 404 printf("packet send :\n%s\n",message);
marcoantonioara 3:cbe3f441353e 405
marcoantonioara 3:cbe3f441353e 406 }
marcoantonioara 3:cbe3f441353e 407 else if (mode == PING ){
marcoantonioara 3:cbe3f441353e 408
marcoantonioara 3:cbe3f441353e 409 // uint8_t message[]="Ping";
marcoantonioara 3:cbe3f441353e 410 //strcpy( message, "Ping");
cdupaty 0:9859cc8476f2 411
marcoantonioara 3:cbe3f441353e 412 sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_ping, sizeof(message_ping));
marcoantonioara 3:cbe3f441353e 413 printf("packet send :\n%s\n",message_ping);
marcoantonioara 3:cbe3f441353e 414
marcoantonioara 3:cbe3f441353e 415 //sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message, sizeof(message));
marcoantonioara 3:cbe3f441353e 416 // sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO
marcoantonioara 3:cbe3f441353e 417
marcoantonioara 3:cbe3f441353e 418 // sx1272.writeRegister(0x0E,0x00);
marcoantonioara 3:cbe3f441353e 419 // sx1272.writeRegister(0x0D,0x00);
marcoantonioara 3:cbe3f441353e 420
marcoantonioara 3:cbe3f441353e 421 // sx1272.writeRegister(REG_PAYLOAD_LENGTH_LORA, sizeof(message));
marcoantonioara 3:cbe3f441353e 422 // for(unsigned int i = 0; i < sizeof(message); i++)
marcoantonioara 3:cbe3f441353e 423 // {
marcoantonioara 3:cbe3f441353e 424 // sx1272.writeRegister(REG_FIFO, message[i]); // Writing the payload in FIFO
marcoantonioara 3:cbe3f441353e 425 // }
marcoantonioara 3:cbe3f441353e 426 // sx1272.writeRegister(REG_OP_MODE, LORA_TX_MODE); // LORA mode - Tx
marcoantonioara 3:cbe3f441353e 427
marcoantonioara 3:cbe3f441353e 428 // int exitTime = millis()+2000;//2 segundos para sair do for
marcoantonioara 3:cbe3f441353e 429
marcoantonioara 3:cbe3f441353e 430 // char value = sx1272.readRegister(REG_IRQ_FLAGS);
marcoantonioara 3:cbe3f441353e 431 // while (( value && 8 == 0) && (millis() < exitTime))
marcoantonioara 3:cbe3f441353e 432 // {
marcoantonioara 3:cbe3f441353e 433 // value = sx1272.readRegister(REG_IRQ_FLAGS);
marcoantonioara 3:cbe3f441353e 434 // }
marcoantonioara 3:cbe3f441353e 435
cdupaty 0:9859cc8476f2 436
marcoantonioara 3:cbe3f441353e 437 // printf("packet send :\n%s\n",message);
marcoantonioara 3:cbe3f441353e 438 // wait_ms(500);
marcoantonioara 3:cbe3f441353e 439 // sx1272.writeRegister(0x12,255);//clean flags
marcoantonioara 3:cbe3f441353e 440 // sx1272.writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby LoRa mode to write in FIFO
marcoantonioara 3:cbe3f441353e 441
marcoantonioara 3:cbe3f441353e 442 }
marcoantonioara 3:cbe3f441353e 443 else if (mode == PONG ) {
marcoantonioara 3:cbe3f441353e 444 // uint8_t message_pong[]="Pong";
marcoantonioara 3:cbe3f441353e 445 //strcpy( message, "Pong");
marcoantonioara 3:cbe3f441353e 446
marcoantonioara 3:cbe3f441353e 447 sx1272.sendPacketTimeout(DEFAULT_DEST_ADDR, message_pong, sizeof(message_pong));
marcoantonioara 3:cbe3f441353e 448 printf("packet send :\n%s\n",message_pong);
marcoantonioara 3:cbe3f441353e 449
marcoantonioara 3:cbe3f441353e 450 }
marcoantonioara 3:cbe3f441353e 451
marcoantonioara 3:cbe3f441353e 452 cont++;
marcoantonioara 3:cbe3f441353e 453 led1=0;
marcoantonioara 3:cbe3f441353e 454 sx1272.writeRegister(0x01,133); //leitura continua
marcoantonioara 3:cbe3f441353e 455 printf("number=%d\n",cont);
marcoantonioara 3:cbe3f441353e 456 }
marcoantonioara 3:cbe3f441353e 457
marcoantonioara 3:cbe3f441353e 458
marcoantonioara 3:cbe3f441353e 459 void print_packet(){
marcoantonioara 3:cbe3f441353e 460
marcoantonioara 3:cbe3f441353e 461 led2=1;
marcoantonioara 3:cbe3f441353e 462 sx1272.writeRegister(0x01,129);//standby
cdupaty 0:9859cc8476f2 463
marcoantonioara 3:cbe3f441353e 464 uint8_t pac_size;
marcoantonioara 3:cbe3f441353e 465
marcoantonioara 3:cbe3f441353e 466 sx1272.writeRegister(0x0D,sx1272.readRegister(0x10));//set fifo pointer to read packet
marcoantonioara 3:cbe3f441353e 467 pac_size = sx1272.readRegister(0x13);//read size of packet
marcoantonioara 3:cbe3f441353e 468 for(int i =0 ; i<pac_size ; i++){ //print packet
marcoantonioara 3:cbe3f441353e 469 buffer[i]=sx1272.readRegister(REG_FIFO);
marcoantonioara 3:cbe3f441353e 470 printf("%c",buffer[i]);//print packet
marcoantonioara 3:cbe3f441353e 471 }
marcoantonioara 3:cbe3f441353e 472 printf("\n");
cdupaty 0:9859cc8476f2 473
marcoantonioara 3:cbe3f441353e 474 for(int i =0 ; i<pac_size ; i++){ //look for Ping in packet
marcoantonioara 3:cbe3f441353e 475 if ((buffer[i] =='P') && (buffer[i+1] =='i') ){
marcoantonioara 3:cbe3f441353e 476 wait(1);
marcoantonioara 3:cbe3f441353e 477 sx1272.writeRegister(0x12,255);//clean flags
marcoantonioara 3:cbe3f441353e 478 send_packet(PONG);
marcoantonioara 3:cbe3f441353e 479 i=pac_size;
marcoantonioara 3:cbe3f441353e 480 }
marcoantonioara 3:cbe3f441353e 481 }
marcoantonioara 3:cbe3f441353e 482
marcoantonioara 3:cbe3f441353e 483 for(int i =0 ; i<pac_size ; i++){ //look for Ping in packet
marcoantonioara 3:cbe3f441353e 484 if ((buffer[i] =='R') && (buffer[i+1] =='e') && (buffer[i+2] =='s') && (buffer[i+3] =='e') ){
marcoantonioara 3:cbe3f441353e 485 NVIC_SystemReset();
marcoantonioara 3:cbe3f441353e 486 }
marcoantonioara 3:cbe3f441353e 487 }
marcoantonioara 3:cbe3f441353e 488
marcoantonioara 3:cbe3f441353e 489 wait_ms(300);
marcoantonioara 3:cbe3f441353e 490 sx1272.writeRegister(0x12,255);//clean flags
marcoantonioara 3:cbe3f441353e 491
marcoantonioara 3:cbe3f441353e 492 led2=0;
cdupaty 0:9859cc8476f2 493
cdupaty 0:9859cc8476f2 494 }
marcoantonioara 3:cbe3f441353e 495
marcoantonioara 3:cbe3f441353e 496
marcoantonioara 3:cbe3f441353e 497 int main(void) {
marcoantonioara 3:cbe3f441353e 498 led1=1;//on leds
marcoantonioara 3:cbe3f441353e 499 led2=1;
marcoantonioara 3:cbe3f441353e 500
marcoantonioara 3:cbe3f441353e 501 Thread eventThread;
marcoantonioara 3:cbe3f441353e 502 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
marcoantonioara 3:cbe3f441353e 503
marcoantonioara 3:cbe3f441353e 504 dio0.rise(queue.event(&print_packet)); //configura rotina de interrupçao para quando receber pacote
marcoantonioara 3:cbe3f441353e 505 seconds.start();
marcoantonioara 3:cbe3f441353e 506
marcoantonioara 3:cbe3f441353e 507 bme_init();//configura sensor bme280
marcoantonioara 3:cbe3f441353e 508 setup(); //configura sx1272
marcoantonioara 3:cbe3f441353e 509
marcoantonioara 3:cbe3f441353e 510 //TESTE_COMUNICAÇAO LABRADOR
marcoantonioara 3:cbe3f441353e 511 //sx1272.writeRegister(0x0B,0x3B);
marcoantonioara 3:cbe3f441353e 512 //sx1272.writeRegister(0x18,0x10);
marcoantonioara 3:cbe3f441353e 513 //sx1272.writeRegister(0x33,0x26);
marcoantonioara 3:cbe3f441353e 514 //sx1272.writeRegister(0x1D,0x08);
marcoantonioara 3:cbe3f441353e 515 //sx1272.writeRegister(0x1E,0xC4);
marcoantonioara 3:cbe3f441353e 516
marcoantonioara 3:cbe3f441353e 517
marcoantonioara 3:cbe3f441353e 518 sx1272.writeRegister(REG_OP_MODE,133); //leitura continua
marcoantonioara 3:cbe3f441353e 519 sx1272.writeRegister(0x11,187);//configure interrupt mask to interrupt only when a packet receive and packet envied
marcoantonioara 3:cbe3f441353e 520
marcoantonioara 3:cbe3f441353e 521 led1=0;//off leds
marcoantonioara 3:cbe3f441353e 522 led2=0;
marcoantonioara 3:cbe3f441353e 523
marcoantonioara 3:cbe3f441353e 524 // for (int i=2 ; i<255;i++){
marcoantonioara 3:cbe3f441353e 525 // int value = sx1272.readRegister(i);
marcoantonioara 3:cbe3f441353e 526 // if (value !=0 )
marcoantonioara 3:cbe3f441353e 527 // printf("reg 0x%02x= 0x%02x\n",i,value);
marcoantonioara 3:cbe3f441353e 528 // }
marcoantonioara 3:cbe3f441353e 529
marcoantonioara 3:cbe3f441353e 530 //long int time_start = seconds.read();
marcoantonioara 3:cbe3f441353e 531 //printf ("timestart=%d leitura = %d\n",time_start,seconds.read());
marcoantonioara 3:cbe3f441353e 532 while(1){
marcoantonioara 3:cbe3f441353e 533
marcoantonioara 3:cbe3f441353e 534 if (button3 != beathing) {
marcoantonioara 3:cbe3f441353e 535 beathing = !beathing;
marcoantonioara 3:cbe3f441353e 536 if(button3){
marcoantonioara 3:cbe3f441353e 537 send_packet(DATA);
marcoantonioara 3:cbe3f441353e 538
marcoantonioara 3:cbe3f441353e 539 time_breathing.reset();
marcoantonioara 3:cbe3f441353e 540 time_breathing.start();
marcoantonioara 3:cbe3f441353e 541
marcoantonioara 3:cbe3f441353e 542 time_diving.stop();
marcoantonioara 3:cbe3f441353e 543 last_diving_time = time_diving.read();
marcoantonioara 3:cbe3f441353e 544 }else{
marcoantonioara 3:cbe3f441353e 545 time_diving.reset();
marcoantonioara 3:cbe3f441353e 546 time_diving.start();
marcoantonioara 3:cbe3f441353e 547
marcoantonioara 3:cbe3f441353e 548 time_breathing.stop();
marcoantonioara 3:cbe3f441353e 549 last_breathing_time = time_breathing.read();
marcoantonioara 3:cbe3f441353e 550
marcoantonioara 3:cbe3f441353e 551 }
marcoantonioara 3:cbe3f441353e 552
marcoantonioara 3:cbe3f441353e 553 }
marcoantonioara 3:cbe3f441353e 554
marcoantonioara 3:cbe3f441353e 555 wait_ms(50);
marcoantonioara 3:cbe3f441353e 556 }
marcoantonioara 3:cbe3f441353e 557
marcoantonioara 3:cbe3f441353e 558 }