diff --git a/codec/simugaz.js b/codec/simugaz.js new file mode 100644 index 0000000..27db8f0 --- /dev/null +++ b/codec/simugaz.js @@ -0,0 +1,10 @@ +function decodeUplink(input) { + const b = input.bytes; + const pulses = (b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]; + return { + data: { + pulse_count: pulses, + index_m3: (pulses * 0.010).toFixed(3) + } + }; +} \ No newline at end of file diff --git a/lib/SimuGaz/simugaz.cpp b/lib/SimuGaz/simugaz.cpp new file mode 100644 index 0000000..25420dd --- /dev/null +++ b/lib/SimuGaz/simugaz.cpp @@ -0,0 +1,50 @@ +#include + +#define INITIAL_PULSE_CNT 185432 +#define INITIAL_SECONDS 0 +#define AVERAGE_DAILY_CONSUMPTION 2.66f + +// Source : opendata.grdf.fr/explore/dataset/correction_climatique_grdf/ +// Profil résidentiel chauffage (P011), moyenne 2019-2023, normalisée sur 12 mois +static const float GRDF_MONTHLY_COEFF[12] = { + 2.21f, // Janvier + 2.05f, // Février + 1.62f, // Mars + 0.97f, // Avril + 0.55f, // Mai + 0.22f, // Juin + 0.15f, // Juillet + 0.17f, // Août + 0.38f, // Septembre + 0.85f, // Octobre + 1.52f, // Novembre + 2.11f, // Décembre +}; + +// Base annuelle : 972 m³/an pour 100m² (CRE) +// - base mensuelle moyenne = 81 m³/mois +// - base journalière moyenne = 2.66 m³/j + +RTC_DATA_ATTR uint32_t pulseCount = INITIAL_PULSE_CNT; +RTC_DATA_ATTR uint32_t totalSeconds = INITIAL_SECONDS; + +uint32_t simulateGasPulses(uint32_t intervalSeconds) +{ + totalSeconds += intervalSeconds; + + uint32_t month = (totalSeconds / (86400UL * 30UL)) % 12; + + float dailyBase = AVERAGE_DAILY_CONSUMPTION * GRDF_MONTHLY_COEFF[month]; + + // Variabilité journalière ±15% + float dailyVariation = 1.0f + 0.30f * ((float)(esp_random() % 1000) / 1000.0f - 0.5f); + + // Résolution compteur Gazpar : 0.010 m³ par impulsion + float incrementM3 = dailyBase * intervalSeconds / 86400.0f * dailyVariation; + uint32_t incrementPulses = (uint32_t)(incrementM3 / 0.010f); + + pulseCount += incrementPulses; + + Serial.printf("[GAZ] Incrément de %lu impulsions - Total : %lu impulsions (%.3f m³)\n", incrementPulses, pulseCount, pulseCount * 0.010f); + return pulseCount; +} \ No newline at end of file diff --git a/lib/SimuGaz/simugaz.h b/lib/SimuGaz/simugaz.h new file mode 100644 index 0000000..d0df98d --- /dev/null +++ b/lib/SimuGaz/simugaz.h @@ -0,0 +1,3 @@ +#include + +uint32_t simulateGasPulses(uint32_t intervalSeconds = 86400); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 48125c3..0f3be4e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ #include #include "LoRaTransmitter.h" #include "config.h" +#include "simugaz.h" RTC_DATA_ATTR uint8_t restartCount = 0; @@ -28,6 +29,15 @@ void fatalError(const char* msg){ esp_deep_sleep_start(); } +void generateDailyGasPayload(uint8_t *payload){ + uint32_t pulses = simulateGasPulses(86400); + + payload[0] = (pulses >> 24) & 0xFF; + payload[1] = (pulses >> 16) & 0xFF; + payload[2] = (pulses >> 8) & 0xFF; + payload[3] = (pulses ) & 0xFF; +} + void setup() { Serial.begin(115200); delay(2000); @@ -42,7 +52,8 @@ void setup() { } void loop() { - uint8_t payload[] = { 0x01, 0x02, 0x03 }; + uint8_t payload[4]; + generateDailyGasPayload(payload); transmitter->send(payload, sizeof(payload)); Serial.flush();