Commit d8733548 authored by Eric Duminil's avatar Eric Duminil
Browse files

One fewer dependency

parent d5eeba11
......@@ -142,7 +142,7 @@ void loop() {
#endif
#if defined(LORAWAN) && defined(ESP32)
lorawan::preparePayloadIfTimehasCome();
lorawan::preparePayloadIfTimeHasCome(sensor::co2, sensor::temperature, sensor::humidity);
#endif
}
......
......@@ -143,18 +143,18 @@ namespace lorawan {
}
}
void preparePayload() {
void preparePayload(int16_t co2, float temperature, float humidity) {
// Check if there is not a current TX/RX job running
if (LMIC.opmode & OP_TXRXPEND) {
Serial.println(F("OP_TXRXPEND, not sending"));
} else {
uint8_t buff[3];
// Mapping CO2 from 0ppm to 5100ppm to [0, 255], with 20ppm increments.
buff[0] = (util::min(util::max(sensor::co2, 0), 5100) + 10) / 20;
buff[0] = (util::min(util::max(co2, 0), 5100) + 10) / 20;
// Mapping temperatures from [-10°C, 41°C] to [0, 255], with 0.2°C increment
buff[1] = static_cast<uint8_t>((util::min(util::max(sensor::temperature, -10), 41) + 10.1f) * 5);
buff[1] = static_cast<uint8_t>((util::min(util::max(temperature, -10), 41) + 10.1f) * 5);
// Mapping humidity from [0%, 100%] to [0, 200], with 0.5°C increment (0.4°C would also be possible)
buff[2] = static_cast<uint8_t>(util::min(util::max(sensor::humidity, 0) + 0.25f, 100) * 2);
buff[2] = static_cast<uint8_t>(util::min(util::max(humidity, 0) + 0.25f, 100) * 2);
Serial.print(F("LoRa - Payload : '"));
printHex2(buff[0]);
......@@ -183,12 +183,12 @@ namespace lorawan {
}
}
void preparePayloadIfTimehasCome() {
void preparePayloadIfTimeHasCome(int16_t co2, float temperature, float humidity) {
static unsigned long last_sent_at = 0;
unsigned long now = seconds();
if (now - last_sent_at > config::lorawan_sending_interval) {
last_sent_at = now;
preparePayload();
preparePayload(co2, temperature, humidity);
}
}
}
......
......@@ -9,7 +9,6 @@
#include <arduino_lmic_hal_boards.h>
#include <SPI.h>
#include "co2_sensor.h"
#include "led_effects.h"
#include "config.h"
......@@ -41,7 +40,7 @@ namespace lorawan {
extern String last_transmission;
void initialize();
void process();
void preparePayloadIfTimehasCome();
void preparePayloadIfTimeHasCome(int16_t co2, float temp, float hum);
}
#endif
......
Markdown is supported
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