Commit 181d5e28 authored by Marcelo's avatar Marcelo
Browse files

Ajout protocole d'envoi

parent 9c73ba2a
......@@ -5,18 +5,29 @@
#include "sht15.hpp"
#include "standby.h"
const int T_mesure = 2;
const int T_sleep = 2;
// We're using 8-byte data that accepts values up to 255, so we won't be able to store a value greater than 25.5 considering one decimal.
// In this case, the best solution is to round the number in order to get numbers up to 255.0.
uint8_t round(float number)
// round the humidity
int16_t round(float number)
{
if(number > 255.0) return 0;
uint8_t result = (uint8_t) number;
int16_t result = (int16_t) number;
// we get the first decimal and test if it's greater than 5. If so, we round up, otherwise we round downd.
return result + (((number - result) * 10) > 5);
}
// round the temperature
int16_t roundt(float number)
{
if(number > 3276.5 || number < -3276.5) return 0;
number *= 10.0; // multiply by 10 in order to get one decimal of precision
int16_t result = (int16_t) number;
int16_t decimal = ( (number > 0) ? (((number - result) * 10) > 5) : -(((number - result) * 10) < -5) );
return result + decimal;
}
// Sensors
SHT31 sensor1 (I2C_SDA, I2C_SCL, false, true);
SHT31 sensor2 (I2C_SDA, I2C_SCL, false, false);
......@@ -53,7 +64,7 @@ int main ()
printf ("Humidité sol : %f\n", humi3);
// store the 6 mesures in rounded integer format
uint8_t mesures[6] = {round(temp1), round(temp2), round(temp3),
uint8_t mesures[6] = {roundt(temp1), roundt(temp2), roundt(temp3),
round(humi1), round(humi2), round(humi3)};
uint32_t temp = 0x00000000U;
uint32_t mesure_pointer = 0;
......@@ -64,73 +75,106 @@ int main ()
{
case 0:
refresh_RTC_bkp_reg(); // refresh all the BKPx registers so that they cant't lose their previous data
for(int i = 0; i < 4; i++) temp |= ((mesures[i] & 0xFFU) << (i * 8));
// backup the 4 first mesures in BKP0[0 - 31]
write_RTC_bkp_reg(BKP0, temp); // replace the BKP1 with the new data in temp
temp = 0;
temp |= ((mesures[4] & 0xFFU) | ((mesures[5] & 0xFFU) << 8));
// backup the 2 last mesures in BKP1[0 - 15]
write_RTC_bkp_reg(BKP1, temp);
mesure_pointer++;
// write the pointer in the 3 byte of BKP4[16 - 23]
write_RTC_bkp_reg(BKP4, ((mesure_pointer & 0xFFU) << 16));
// backup the 2 first WORDs in BKP0[0 - 31] (temp1, temp2)
for(int i = 0; i < 2; i++) temp |= ((mesures[i] & 0xFFFFU) << (i * 16));
write_RTC_bkp_reg(BKP0, temp);
// backup the 3 next data (1 WORD & 2 BYTEs) in BKP1[0 - 31] (temp3, humi1, humi2)
temp = 0;
temp |= ((mesures[2] & 0xFFFFU) | ((mesures[3] & 0xFFU) << 16) | ((mesures[4] & 0xFFU) << 24));
write_RTC_bkp_reg(BKP1, temp);
// backup the last BYTE in BKP4[0 - 7] (humi3), mesure pointer in BKP4[16 - 24]
mesure_pointer++;
temp = 0;
temp |= ((mesures[5] & 0xFFU) | ((mesure_pointer & 0xFF) << 16));
write_RTC_bkp_reg(BKP4, temp);
break;
case 1:
refresh_RTC_bkp_reg(); // refresh all the BKPx registers so that they cant't lose their previous data
temp = read_RTC_bkp_reg(BKP1); // retrieve the previous data
temp |= (((mesures[0] & 0xFFU) << 16) | ((mesures[1] & 0xFFU) << 24));
// backup the 2 first mesures in BKP1[16 - 31]
write_RTC_bkp_reg(BKP1, temp); // replace the BKP1 with the new data in temp
temp = 0;
for(int i = 2; i < 6; i++) temp |= ((mesures[i] & 0xFFU) << ((i - 2) * 8));
// backup the 4 last mesures in BKP2[0 - 31]
write_RTC_bkp_reg(BKP2, temp);
mesure_pointer++;
// rewrite the pointer in the 3 byte of BKP4[16 - 23]
write_RTC_bkp_reg(BKP4, ((mesure_pointer & 0xFFU) << 16));
// backup the 2 first WORDs in BKP2[0 - 31] (temp1, temp2)
for(int i = 0; i < 2; i++) temp |= ((mesures[i] & 0xFFFFU) << (i * 16));
write_RTC_bkp_reg(BKP2, temp);
// backup the 3 next data (1 WORD & 2 BYTEs) in BKP3[0 - 31] (temp3, humi1, humi2)
temp = 0;
temp |= ((mesures[2] & 0xFFFFU) | ((mesures[3] & 0xFFU) << 16) | ((mesures[4] & 0xFFU) << 24));
write_RTC_bkp_reg(BKP3, temp);
// backup the last BYTE in BKP4[8 - 15] (humi3), pointer in BKP4[16 - 24]
mesure_pointer++;
temp = read_RTC_bkp_reg(BKP4);
temp &= 0xFFU; // we keep the first BYTE (humi3 from mesure 1)
temp |= (((mesures[5] & 0xFFU) << 8) | ((mesure_pointer & 0xFFU) << 16));
write_RTC_bkp_reg(BKP4, temp);
break;
case 2:
refresh_RTC_bkp_reg(); // refresh all the BKPx registers so that they cant't lose their previous data
for(int i = 0; i < 4; i++) temp |= ((mesures[i] & 0xFFU) << (i * 8));
// backup the 4 first mesures in BKP3[0 - 31]
write_RTC_bkp_reg(BKP3, temp);
mesure_pointer++;
temp = 0;
temp |= ((mesures[4] & 0xFFU) | ((mesures[5] & 0xFFU) << 8) | ((mesure_pointer & 0xFFU) << 16));
// backup the 2 last mesures in BKP4[0 - 15]. rewrite the pointer in the 3rd byte of BKP4[16 - 23]
write_RTC_bkp_reg(BKP4, temp);
break;
case 3:
// get the data in the following order :
// - mesure 1 - 6 of the first, second, third and fourth times, respectively
float mesures_to_send[18 + 6]; // 3 previous mesures + the current one
// mesures_to_send[0 - 5] : temp1, temp2, temp3, humi1, humi2, humi3 of mesure 1
// mesures_to_send[6 - 11] : temp1, temp2, temp3, humi1, humi2, humi3 of mesure 2
// mesures_to_send[12 - 17] : temp1, temp2, temp3, humi1, humi2, humi3 of mesure 3
int16_t mesures_to_send[12 + 6]; // 2 previous mesures + the current one
temp = read_RTC_bkp_reg(BKP0);
// get the 4 first bytes of mesure 1 from BKP0
for(int i = 0; i < 4; i++) mesures_to_send[i] = (float)((temp >> (i * 8)) & 0xFFU);
// get the 2 first BYTEs of mesure 1 from BKP0 (temp1, temp2)
for(int i = 0; i < 2; i++) mesures_to_send[i] = (int16_t)((temp >> (i * 16)) & 0xFFFFU);
temp = read_RTC_bkp_reg(BKP1);
// get the 2 last bytes of mesure 1 and the 2 first of mesure 2
for(int i = 4; i < 8; i++) mesures_to_send[i] = (float)((temp >> ((i - 4) * 8)) & 0xFFU);
// get the 3 next BYTEs of mesure 1 from BKP1
mesures_to_send[2] = (int16_t)(temp & 0xFFFFU); // temp3
mesures_to_send[3] = (int16_t)((temp >> 16) & 0xFFU); // humi1
mesures_to_send[4] = (int16_t)((temp >> 24) & 0xFFU); // humi2
temp = read_RTC_bkp_reg(BKP2);
// get the 4 last bytes of mesure 2
for(int i = 8; i < 12; i++) mesures_to_send[i] = (float)((temp >> ((i - 8) * 8)) & 0xFFU);
// get the 2 first BYTEs of mesure 2 from BKP2 (temp1, temp2)
for(int i = 0; i < 2; i++) mesures_to_send[i + 6] = (int16_t)((temp >> (i * 16)) & 0xFFFFU);
temp = read_RTC_bkp_reg(BKP3);
// get the 4 first bytes of mesure 3
for(int i = 12; i < 16; i++) mesures_to_send[i] = (float)((temp >> ((i - 12) * 8)) & 0xFFU);
// get the 3 next BYTEs of mesure 2 from BKP3
mesures_to_send[8] = (int16_t)(temp & 0xFFFFU); // temp3
mesures_to_send[9] = (int16_t)((temp >> 16) & 0xFFU); // humi1
mesures_to_send[10] = (int16_t)((temp >> 24) & 0xFFU); // humi2
temp = read_RTC_bkp_reg(BKP4);
// get the 2 last bytes of mesure 3
for(int i = 16; i < 18; i++) mesures_to_send[i] = (float)((temp >> ((i - 16) * 8)) & 0xFFU);
// get the 6 current mesures (mesure 4)
for(int i = 18; i < 24; i++) mesures_to_send[i] = (float)mesures[i - 18];
// get the last BYTE of mesure 1 and mesure 2
mesures_to_send[5] = (int16_t)(temp & 0xFFU); // humi3 of mesure 1
mesures_to_send[11] = (int16_t)((temp >> 8) & 0xFFU); // humi3 of mesure 2
// get the 6 current BYTEs (mesure 3)
for(int i = 12; i < 18; i++) mesures_to_send[i] = mesures[i - 12];
// reset the registers
reset_RTC_bkp_reg();
// sending data (mesures_to_send)
for(int i = 0; i < 24; i++) printf("mesure %d = %2.2f \n", i, mesures_to_send[i]);
// =============== sending data (mesures_to_send) ===============
for(int i = 0; i < 18; i++) printf("mesure %d = %d \n", i, mesures_to_send[i]);
// send all the data from the 3 mesures * (9 bytes of data +
// 6 bytes of indentifiers) each = 45 bytes
uint8_t payload[45];
for(int i = 0; i < 45; i++) payload[i] = 0;
int j = 0; // payload index
for(int i = 0; i < 3; i++)
{
payload[j++] |= 0x01; // identifier - Température plafond
payload[j++] |= (mesures_to_send[0 + 6 * i] >> 8) & 0xFF; // Température plafond MSB
payload[j++] |= (mesures_to_send[0 + 6 * i]) & 0xFF; // Température plafond LSB
payload[j++] |= 0x0C; // identifier - Température sol
payload[j++] |= (mesures_to_send[1 + 6 * i] >> 8) & 0xFF; // Température sol MSB
payload[j++] |= (mesures_to_send[1 + 6 * i]) & 0xFF; // Température sol LSB
payload[j++] |= 0x19; // identifier - Température sous-terraine
payload[j++] |= (mesures_to_send[2 + 6 * i] >> 8) & 0xFF; // Température sous-terraine MSB
payload[j++] |= (mesures_to_send[2 + 6 * i]) & 0xFF; // Température sous-terraine LSB
payload[j++] |= 0x02; // identifier - Humidité plafond
payload[j++] |= (mesures_to_send[3 + 6 * i] & 0xFF); // Humidité plafond
payload[j++] |= 0x1A; // identifier - Humidité sol
payload[j++] |= (mesures_to_send[4 + 6 * i] & 0xFF); // Humidité sol
payload[j++] |= 0x1B; // identifier - Humidité sous-terraine
payload[j++] |= (mesures_to_send[5 + 6 * i] & 0xFF); // Humidité sous-terraine
}
// LoRa send payload
break;
}
//wait (2);
standby_mode(T_mesure);
standby_mode(T_sleep);
} // main
File added
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment