Commit 9a4c822f authored by Thierry Pelle's avatar Thierry Pelle
Browse files

Nettoyge

parent fa91590c
#include "11-Parametres_Compilation.h"
#include "12-Pins.h"
#include "13-Parametres_Capture.h"
/*
* ======= Directives de compilation ======
*/
// Sélection des méthodes de communication
//#define WIFI_ENABLED
#define LORAWAN_ENABLED
// Sélection des fonction de déboggage
#define DEBUG
//#define DEBUG_OLED
//#define NO_HX711
#define LMIC_ACKNOWLEDGE 1
// Gestion de la mise en veille
//#define SLEEP_ENABLED
#define TIME_TO_SLEEP 360 /* Time ESP32 will go to sleep (in seconds) */
// ======= Paramètres de connection à la carte TTGO ======
#define LOADCELL_DOUT_PIN 17
#define LOADCELL_SCK_PIN 16
#define DATA_DHT22_HAUSSE_PIN 13
#define DATA_DHT22_INT_PIN 4
#define I2C_SDL_PIN 22
#define I2C_SDA_PIN 21
// TTGO
#define SX1276_NSS 10
#define SX1276_RXTX LMIC_UNUSED_PIN
#define SX1276_RESET 7
#define SX1276_DIO0 4
#define SX1276_DIO1 5
#define SX1276_DIO2 LMIC_UNUSED_PIN
/*
//Wemos D1 (tests)
#define SX1276_NSS 10
#define SX1276_RXTX LMIC_UNUSED_PIN
#define SX1276_RESET 4
#define SX1276_DIO0 3
#define SX1276_DIO1 5
#define SX1276_DIO2 LMIC_UNUSED_PIN
*/
// ====== Interval entre deux captures et transmission ======
#define CAPTURE_INTERVAL 35000 // En millisecondes
// == Structure de données ==
typedef struct capturedData capturedData ;
struct capturedData{
float heatDHT22int ;
float moistureDHT22int ;
float heatDHT22hausse ;
float moistureDHT22hausse ;
float heatBME280int ;
float moistureBME280int ;
float pressureBME280int ;
float heatBME280ext ;
float moistureBME280ext ;
float pressureBME280ext ;
float HX7xx ;
} ;
/*
* Gestion de la journalisation
*/
#include "21-Journalisation.h"
/*
* Bibliothèques des capteurs
*/
#include "22-Bibliotheques_Capteurs.h"
/*
* Gestion des connexions réseau
*/
#ifdef WIFI_ENABLED
#include "wlan.h"
#endif
#ifdef LORAWAN_ENABLED
#include "lpwan.h"
#endif
/*
* Gestion de la captures des données
*/
#include "Donnees.h"
/*
* Gestion mise en veille
*/
#include "sleep_mode.h"
/*
* ============== JOURNALISATION OLED =============
*/
/*
* Journalisation OLED activée
*/
#ifdef DEBUG_OLED
#include <oled.h>
OLED display=OLED(I2C_SDA_PIN,I2C_SDL_PIN,-1,0x3C,128,64,true); // SDA, SDL, RST, ADDR, WIDTH, HIGHT, 1106
void initOled() {
display.begin();
display.clear();
display.setTTYMode(true);
display.printf(F("OLED STARTED!\n"));
}
void oledDebugMessage(char* message) {
display.printf(message);
display.printf("\n");
}
#endif
/*
* Journalisation OLED désactivée
*/
#ifndef DEBUG_OLED
void initOled() {};
void oledDebugMessage(char* message) {};
#endif
/*
* ============== JOURNALISATION SERIE =============
*/
/*
* Journalisation série activée
*/
#ifdef DEBUG
void debugMessageInt(String prefixe, int I, String suffixe) {
Serial.print(prefixe);
Serial.print(F(" "));
Serial.print(I);
Serial.print(F(" "));
Serial.println(suffixe);
}
void debugMessageFloat(String prefixe, float dataFloat, String suffixe) {
Serial.print(prefixe);
Serial.print(F(" "));
Serial.print(dataFloat);
Serial.print(F(" "));
Serial.println(suffixe);
}
void debugMessageUL(String prefixe, unsigned long UL, String suffixe) {
Serial.print(prefixe);
Serial.print(F(" "));
Serial.print(UL);
Serial.print(F(" "));
Serial.println(suffixe);
}
void debugMessage(String message) {
Serial.println(message) ;
}
#endif
// ====== Initialisation ======
void initSerial() {
Serial.begin(115200);
debugMessage(F("-- Serial -- OK"));
oledDebugMessage("Serial STARTED!");
}
/*
* Journalisation série désactivée
*/
#ifndef DEBUG
void initSerial() {};
void debugMessageInt(String prefixe, int I, String suffixe) {} ;
void debugMessageFloat(String prefixe, float F, String suffixe) {} ;
void debugMessageUL(String prefixe, unsigned long UL, String suffixe) {} ;
void debugMessage(String message) {} ;
#endif
#include "dht22.h"
#include "bme280.h"
#include "hx711.h"
/*
* Variables
*/
#ifndef VARIABLES
#define VARIABLES
#define DHT_TYPE DHT22
BME280 Capt_bme_int;
BME280 Capt_bme_ext;
DHT Capt_dht22_int(DATA_DHT22_INT_PIN, DHT_TYPE);
DHT Capt_dht22_hausse(DATA_DHT22_HAUSSE_PIN, DHT_TYPE );
#ifndef NO_HX711
HX711 scale; // Balance
#endif
// == Fonctions locales ==
void dataCapture(capturedData* results) ;
void dataDisplay(capturedData toDisplay) ;
void allSensorInit();
/*
* Initialisation senseurs
*/
void allSensorInit() {
initDHT22(&Capt_dht22_int, "-- DHT22 COEUR --");
initDHT22(&Capt_dht22_hausse, "-- DHT22 HAUSSE --");
initBME280(&Capt_bme_int, "-- BME280 COEUR --", 0x76);
initBME280(&Capt_bme_ext, "-- BME280 EXTERIEUR --", 0x77);
#ifndef NO_HX711
initHX711(&scale,"-- HX711 --");
#endif
float data[2];
readAllDHT22(Capt_dht22_int,data) ;
Serial.println(data[0]);
Serial.println(data[1]);
}
/*
* Capture des données
*/
capturedData sensorData ;
void dataCapture(capturedData* results) {
Serial.println(Capt_dht22_int.readTemperature());
(*results).heatDHT22int=readHeatDHT22(Capt_dht22_int);//(*results).heatDHT22int=;
(*results).moistureDHT22int=readMoistureDHT22(Capt_dht22_int);
(*results).heatDHT22hausse=readHeatDHT22(Capt_dht22_hausse);
(*results).moistureDHT22hausse=readMoistureDHT22(Capt_dht22_hausse);
(*results).heatBME280int=readHeatBME280(Capt_bme_int);
(*results).moistureBME280int=readMoistureBME280(Capt_bme_int);
(*results).pressureBME280int=readPressureBME280(Capt_bme_int);
(*results).heatBME280ext=readHeatBME280(Capt_bme_ext);
(*results).moistureBME280ext=readMoistureBME280(Capt_bme_ext);
(*results).pressureBME280ext=readPressureBME280(Capt_bme_ext);
#ifndef NO_HX711
(*results).HX7xx=readHX711(&scale);
#endif
}
void dataDisplay(capturedData toDisplay) {
Serial.println();
Serial.println("====== Données =====");
debugMessageFloat("DTH22 INT:", toDisplay.heatDHT22int, " °C");
debugMessageFloat("DTH22 HAUSSE:", toDisplay.heatDHT22hausse, " °C");
debugMessageFloat("BME280 INT:",toDisplay.heatBME280int , " °C");
debugMessageFloat("BME280 EXT:",toDisplay.heatBME280ext , " °C");
debugMessageFloat("DTH22 INT:", toDisplay.moistureDHT22int, " %");
debugMessageFloat("DTH22 HAUSSE:", toDisplay.moistureDHT22hausse, " %");
debugMessageFloat("BME280 INT:", toDisplay.heatBME280int, " %");
debugMessageFloat("BME280 EXT:", toDisplay.heatBME280ext, " %");
debugMessageFloat("BME280 INT:", toDisplay.pressureBME280int, " hPa");
debugMessageFloat("BME280 EXT:", toDisplay.pressureBME280ext, " hPa");
debugMessageFloat("Indice poids HX711:", toDisplay.HX7xx, " unités");
Serial.println();
}
#endif
/*
* Programme du TTGO (ESP32+LoRa)
*/
#include "1-Constantes.h" // ====== Données globales ======
#include "2-Preambule.h" // ====== Préambule ======
// ====== Initialisation du programme ======
void setup() {
#include "setup.h"
}
// ====== Programme ======
void loop() {
/*
* La fonction de bouclage est rendue inutile par l'usage de la mise
* en veille de l'ESP.
* Tout le code est donc placé dans la fonction setup
*/
#include "loop.h"
}
#include <SparkFunBME280.h> // Capteur BME280
#include <Wire.h> // Bus I2C
void initBME280(BME280 *capteur, String message, byte adresse) ;
float readHeatBME280(BME280* capteur) ;
float readMoistureBME280(BME280* capteur) ;
float readPressureBME280(BME280* capteur) ;
/*
* BME 280
*/
// ====== Initialisation ======
void initBME280(BME280 *capteur, String message, byte adresse) {
debugMessage(message);
debugMessage(" avec adresse ");
debugMessage(String(adresse));
capteur->settings.commInterface = I2C_MODE;
capteur->settings.I2CAddress = adresse;
capteur->settings.runMode = 3; // 0 : sleep | 1 : forced
capteur->settings.tStandby = 0;
capteur->settings.filter = 0;
capteur->settings.tempOverSample = 5 ; // 0 : pas de temp, de 1 à 5 précision echantillonage
capteur->settings.pressOverSample = 5 ; // 0 : pas de pression, de 1 à 5 précision echantillonage
capteur->settings.humidOverSample = 5; // 0 : pas de hum, de 1 à 5 précision echantillonage
delay(10); // Temps d'initialisationdu BME280
capteur->begin();// chargement de la configuration du capteur
debugMessage(" OK");
}
// ====== Lecture ======
float readHeatBME280(BME280 capteur) {
return (float)capteur.readTempC();
}
float readMoistureBME280(BME280 capteur) {
return (float)capteur.readFloatHumidity();
}
float readPressureBME280(BME280 capteur) {
return (float)capteur.readFloatPressure()/100.0;
}
/*
* DHT22
*/
#include <DHT.h> // Capture DHT22
void initDHT22(DHT* sensor, String message) ;
float readHeatDHT22(DHT sensor) ;
float readMoistureDHT22(DHT sensor) ;
void readAllDHT22(DHT sensor, float* data);
// ====== Initialisation ======
void initDHT22(DHT *sensor, String message) {
debugMessage(message);
sensor->begin();
delay(500); // Délai d'initialisation
Serial.println(sensor->readTemperature());
Serial.println(sensor->readHumidity());
debugMessage(" OK");
}
// ====== Lecture ======
float readHeatDHT22(DHT sensor) {
return (float)sensor.readTemperature();
}
float readMoistureDHT22(DHT sensor) {
return (float)sensor.readHumidity();
}
void readAllDHT22(DHT sensor, float* data) {
data[0]=sensor.readTemperature();
data[1]=sensor.readHumidity();
Serial.println(data[0]);
Serial.println(data[1]);
}
#ifndef NO_HX711
#include <HX711.h> // pour la balance version 2.0
// #include "soc/rtc.h" // pour ralentir le TTGO (nécessaire pour le HX711)
void initHX711(HX711* sensor) ;
/*
* HX711
*/
// ====== Initialisation ======
void initHX711(HX711* sensor, String message) {
debugMessage(message) ;
pinMode(LOADCELL_DOUT_PIN, INPUT);
pinMode(LOADCELL_SCK_PIN, OUTPUT);
sensor->begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
debugMessage(F("-- --------"));
}
// ====== Lecture ======
float readHX711(HX711* sensor) {
if (sensor->is_ready()) {
return float(int(sensor->read() / 1000)); // valeur brute
/*
* Exemples de transormation vers kg
* - Méthode 1: (V*1./1000.-195.)/20.*-1000.;
* - Méthode 2: (V/1000.-268+73)/21*1000;
*/
}
else {
debugMessage(F("HX711 not found."));
return -1 ;
}
}
#endif
#ifndef SLEEP_ENABLED
debugMessage("_____ SLEEPING MODE DISABLED _____");
debugMessage(String("-> Capture")) ;
dataCapture(&sensorData);
dataDisplay(sensorData);
#ifdef WIFI_ENABLED
debugMessage(String("-> Transmit Wifi"));
String WiFiMessage = formatForWifi(sensorData);
sendByWiFi(WiFiMessage);
#endif
#ifdef LORAWAN_ENABLED
debugMessage(String("-> Transmit LoRa"));
os_runloop_once();
#endif
debugMessage(String("-> Waiting next Capture"));
delay(CAPTURE_INTERVAL);
debugMessage(" ");
debugMessage(" ");
#endif
#include <lmic.h> // LoRaWAN (1/3)
#include <hal/hal.h> // LoRaWAN (2/3)
#include <SPI.h> // LoRaWAN (3/3)
#include <CayenneLPP.h> // Format Cayenne
#include "Donnees.h"
CayenneLPP lpp(51);
void os_getArtEui (u1_t* buf) ;
void os_getDevEui (u1_t* buf) ;
void os_getDevKey (u1_t* buf) ;
void do_send(osjob_t* j);
void onEvent (ev_t ev) ;
void setSessionParameters() ;
void initEU868Channels() ;
void disableUnusedChannels() ;
void initLora() ;
void sendByLoRaSFLT16(int port, float value) ;
void LoRaSendCaptures() ;
// ==== Paramètres TTN (OTA) ====
// LoRaWAN NwkSKey, network session key (MSB)
static const PROGMEM u1_t NWKSKEY[16] = { 0xA2, 0x05, 0xBD, 0x0C, 0x5C, 0xA0, 0xA7, 0xDA, 0x77, 0x57, 0x90, 0xE7, 0xB6, 0xE4, 0x30, 0xA2 } ;
// LoRaWAN AppSKey, application session key (MSB)
static const PROGMEM u1_t APPSKEY[16] = { 0xC3, 0xC3, 0x72, 0xB2, 0xEC, 0x75, 0xA2, 0x01, 0x46, 0x79, 0x8E, 0xEE, 0x5D, 0x03, 0x12, 0x66 } ;
// LoRaWAN end-device address (DevAddr) (MSB)
static const u4_t DEVADDR = 0x26011026 ;
// These callbacks are only used in over-the-air activation, so they are
// left empty here (we cannot leave them out completely unless
// DISABLE_JOIN is set in arduino-lmic/project_config/lmic_project_config.h,
// otherwise the linker will complain).
void os_getArtEui (u1_t* buf) { }
void os_getDevEui (u1_t* buf) { }
void os_getDevKey (u1_t* buf) { }
// ==== Gestionnaire d'évènements ====
static osjob_t sendjob;
void do_send(osjob_t* j);
// ==== Paramatrage de la carte SX1276 ====
// Schedule TX every this many seconds (might become longer due to duty cycle limitations).
#define TX_INTERVAL CAPTURE_INTERVAL
// Pin mapping
const lmic_pinmap lmic_pins = {
.nss = SX1276_NSS,
.rxtx = SX1276_RXTX,
.rst = SX1276_RESET,
.dio = {SX1276_DIO0, SX1276_DIO1, SX1276_DIO2,
}
};
void onEvent (ev_t ev) {
Serial.print("LoRa EVENT @");
Serial.print(os_getTime());
Serial.print(": ");
switch (ev) {
case EV_SCAN_TIMEOUT:
Serial.println(F("EV_SCAN_TIMEOUT"));
break;
case EV_BEACON_FOUND:
Serial.println(F("EV_BEACON_FOUND"));
break;
case EV_BEACON_MISSED:
Serial.println(F("EV_BEACON_MISSED"));
break;
case EV_BEACON_TRACKED:
Serial.println(F("EV_BEACON_TRACKED"));
break;
case EV_JOINING:
Serial.println(F("EV_JOINING"));
break;
case EV_JOINED:
Serial.println(F("EV_JOINED"));
break;
/*
|| This event is defined but not used in the code. No
|| point in wasting codespace on it.
||
|| case EV_RFU1:
|| Serial.println(F("EV_RFU1"));
|| break;
*/
case EV_JOIN_FAILED:
Serial.println(F("EV_JOIN_FAILED"));
break;
case EV_REJOIN_FAILED:
Serial.println(F("EV_REJOIN_FAILED"));
break;
case EV_TXCOMPLETE:
#ifdef DEBUG_OLED
display.printf("\nTX complete...");
#endif
Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)"));
if (LMIC.txrxFlags & TXRX_ACK)
Serial.println(F("Received ack"));
if (LMIC.dataLen) {
Serial.println(F("Received "));
Serial.println(LMIC.dataLen);
//display.printf("Received %d bytes\n",LMIC.dataLen);
//display.printf("RSSI=%d \n",LMIC.rssi);
Serial.println(F(" bytes of payload"));
}
// Schedule next transmission
Serial.println(F(" DEBUG:Scheduling next transmission"));
#ifdef DEBUG_OLED
display.printf("\nScheduling next transmission...");
#endif
os_setTimedCallback(&sendjob, os_getTime() + sec2osticks(TX_INTERVAL), do_send);
break;
case EV_LOST_TSYNC:
Serial.println(F("EV_LOST_TSYNC"));
break;
case EV_RESET:
#ifdef DEBUG_OLED
display.printf("\nLoRa RESET...");
#endif
Serial.println(F("EV_RESET"));
break;
case EV_RXCOMPLETE:
// data received in ping slot
Serial.println(F("EV_RXCOMPLETE"));
#ifdef DEBUG_OLED
display.printf("\nRX complete...");
#endif
break;
case EV_LINK_DEAD:
#ifdef DEBUG_OLED
display.printf("\nLink DEAD...");
#endif
Serial.println(F("EV_LINK_DEAD"));
break;
case EV_LINK_ALIVE:
Serial.println(F("EV_LINK_ALIVE"));
break;
/*
|| This event is defined but not used in the code. No