Bon, je continue d'avancer
// Inclusion des librairies
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <DHT.h>
#include "ArduinoOTA.h"
#include "SdsDustSensor.h"
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Network credentials
const char* ssid = "***";
const char* password = ""***";
const char* serverName ="***";
// Constantes du programme
const byte brocheLed = 12;
const byte brocheRx = 19;
const byte brocheTx = 18;
const byte brocheDeBranchementDHT = 23;
const byte adresseI2CduBME280 = 0x76; // Adresse I2C du BME280 (0x76, dans mon cas, ce qui est souvent la valeur par défaut)
// Instanciation
SdsDustSensor sds(Serial1);
DHT dht(brocheDeBranchementDHT, DHT22);
Adafruit_BME280 bme;
//Const accés php
const String apiKeyValue = "123";
const String sensorName = "BME280DHTAIR";
const String sensorLocation = "Office";
// Gestion des tempos différentes suivant les capteurs
const unsigned long dureeTHP = 300000ul; // 300 secondes en ms (= 5 minutes)
const unsigned long dureeQA = 1800000ul; // 1800 secondes en ms ( = 30 minutes)
const unsigned long dureeBlink = 500ul; // 1Hz (c'est la demi période)
// Les variables
enum TypeMessage {REQUETE_QA, REQUETE_THP};
//je définis un type qui s'appelle TypeMessage et les variables de ce type peuvent prendre soit la valeur REQUETE_QA, soit la valeur REQUETE_THP.
String httpRequestData;
float TEMPMOY;
float HUMMOY;
float Pression;
float Moy_Pm10;
float Moy_Pm25;
float temperatureRessentieEnCelsius;
unsigned long chronoTHP; // pour la Temp / Hum / pression
unsigned long chronoQA; // pour la Qualité de l'air
unsigned long chronoBlink; // pour la la LED
int cpt;
void initWiFi() {
// Demaraage wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
void initWiFiB() {
//Connexion au wifi
IPAddress ip(192,168,0,20);
IPAddress gateway(192,168,0,254);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192,168,0,254);
WiFi.config(ip,gateway,subnet,dns);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { //Check la connexion
delay(1000);
Serial.println("Tentative de connexion au WiFi...");
cpt++;
if (cpt>30)
{
Serial.println("Pas de connexion au wifi! Reboot automatique...");
esp_restart();
}
}
Serial.println("Connexion au réseau local wifi: OK!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Nom de l'objet OTA
ArduinoOTA.setHostname("TestOTA");
ArduinoOTA.setPassword("test");
}
void messagehttp(TypeMessage typeEnvoi) {
//envoie un message selon THP QA ou ttes les mn
if (WiFi.status() == WL_CONNECTED) {
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure();
HTTPClient https;
https.begin(*client, serverName);
https.addHeader("Content-Type", "application/x-www-form-urlencoded");
if (typeEnvoi == REQUETE_THP) {
Serial.println("Requête THP");
httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + TEMPMOY
+ "&value2=" + HUMMOY + "&value3=" + String(bme.readPressure() / 100.0F) + "&value4=" + temperatureRessentieEnCelsius;
}
else if (typeEnvoi == REQUETE_QA) {
Serial.println("Requête QA");
httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&pm25=" + Moy_Pm25 + "&pm10=" + Moy_Pm10 ;
}
// Send HTTP POST request
Serial.print("URL POST: "); Serial.println(httpRequestData);
int httpResponseCode = https.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
https.end();
} else {
Serial.println("Pas de WiFi.");
}
}
void gestionTHP() {
// Relevé Temp / Hum et Pression
float TEMPBMP = bme.readTemperature();
float HUMBMP = bme.readHumidity();
float PREBMP = bme.readPressure();
Serial.print("**BMP280** ");
Serial.print("{\"Hum\": ");
Serial.print(HUMBMP);
Serial.print(", % \"Temp\": ");
Serial.print(TEMPBMP);
Serial.print(", °C \ Pression\": ");
Serial.print(PREBMP / 100);
Serial.print(" Mpa}\n");
// ****Mesure DHT****
float HUMDHT = dht.readHumidity();
float TEMPDHT = dht.readTemperature();
Serial.print("**DHT22** ");
//Serial.print("{\"humidity\": ");
Serial.print("{\"Hum\": ");
Serial.print(HUMDHT);
Serial.print(", % \"Temp\": ");
Serial.print(TEMPDHT);
Serial.print("°C}\n");
// Affichage de la TEMPÉRATURE MOY
TEMPMOY = (TEMPBMP + TEMPDHT) / 2;
Serial.print(F("Temp Moy = "));
Serial.print(TEMPMOY);
Serial.print(F(" °C "));
float ECARTTEMP = TEMPBMP - TEMPDHT;
Serial.print(F("Ecart "));
Serial.print(ECARTTEMP);
Serial.println(F(" °C"));
// Affichage du Tx Humidité moyen
HUMMOY = (HUMBMP + HUMDHT) / 2;
Serial.print(F("Hum Moy = "));
Serial.print(HUMMOY);
Serial.print(F(" % "));
float ECARTHUM = HUMBMP - HUMDHT;
Serial.print(F("Ecart "));
Serial.print(ECARTHUM);
Serial.println(F(" %"));
// Calcul de la température ressentie
temperatureRessentieEnCelsius = dht.computeHeatIndex(TEMPDHT, HUMDHT, false); // Le "false" est là pour dire qu'on travaille en °C, et non en °F
Serial.print("Température ressentie DHT= "); Serial.print(temperatureRessentieEnCelsius); Serial.println(" °C");
Pression = bme.readPressure() / 100.0F;
//Message http
messagehttp(REQUETE_THP);
}
void gestionQA() {
// Wake up SDS011
sds.wakeup();
delay(120000);
// Moyenne sur - mesures
float Som_Pm25 = 0;
float Som_Pm10 = 0;
float Mes_Pm25 = 0;
float Mes_Pm10 = 0;
int n = 0;
while (n < 10) {
// Boucle de mesure de la température
PmResult pm = sds.readPm();
if (pm.isOk()) {
if (n>2){ // pour eliminer les 2 premiéres mesures
Serial.print("PM2.5 = ");
Mes_Pm25 = pm.pm25;
Serial.print(Mes_Pm25);
Serial.print(", PM10 = ");
Mes_Pm10 = pm.pm10;
Serial.println(Mes_Pm10);
}
}
else {
Serial.print("Could not read values from sensor, reason: ");
Serial.println(pm.statusToString());
}
delay(10000);
Som_Pm25 = Som_Pm25 + Mes_Pm25;
Som_Pm10 = Som_Pm10 + Mes_Pm10;
n++;
}
float denominateur = n-3;
Moy_Pm25 = Som_Pm25 / denominateur;
Moy_Pm10 = Som_Pm10 / denominateur;
Serial.print("Moy PM25 = ");
Serial.print(Moy_Pm25);
Serial.print(", Moy PM10 = ");
Serial.println(Moy_Pm10);
sds.sleep();
//Message http
messagehttp(REQUETE_QA);
}
void setup() {
// Initialisation de la brocheLed
pinMode(brocheLed, OUTPUT);
delay(4000);
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, brocheRx, brocheTx);
// Intialisation des comteurs temps
chronoTHP = -dureeTHP; // pour qu'on déclenche une lecture dès le lancement de l'arduino
chronoQA = - dureeQA; // pour qu'on déclenche une lecture dès le lancement de l'arduino
// Initialisation wifi
initWiFiB();
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// Initialisation du controleur air;
Serial.println(sds.queryFirmwareVersion().toString());
Serial.println(sds.setActiveReportingMode().toString());
Serial.println(sds.setContinuousWorkingPeriod().toString());
sds.begin();
// initialisation DHT
dht.begin();
//Intialisation du BME280
if (bme.begin(adresseI2CduBME280) == false) {
Serial.println("BME280 absent. Stop.");
while (true);
}
}
void loop() {
//
ArduinoOTA.begin();
delay(5000);
cpt++;
ArduinoOTA.handle();
if (cpt>1000000) ESP.restart();
// si le WiFi ne fonctionne pas, pas la peine de faire les mesures
if (WiFi.status() != WL_CONNECTED) {
Serial.print(millis());
Serial.println("Reconnecting to WiFi...");
WiFi.disconnect();
delay(1000);
WiFi.reconnect();
} else {
if (millis() - chronoTHP >= dureeTHP) {
gestionTHP();
chronoTHP = millis();
}
if (millis() - chronoQA >= dureeQA ) {
gestionQA();
chronoQA = millis();
}
}
// on clignote toujours pour montrer que le code n'est pas planté
if (millis() - chronoBlink >= dureeBlink) {
digitalWrite(brocheLed, digitalRead(brocheLed) == HIGH ? LOW : HIGH);
chronoBlink = millis();
}
}
J'ai bien l'attribution de l'adresse IP
23:20:48.446 -> RSSI: -58
23:20:48.446 ->
23:20:48.446 -> Connected to WiFi network with IP Address: 192.168.0.20
23:20:48.957 -> Firmware version [year.month.day]: 21.5.16
23:20:49.439 -> Mode: active
23:20:49.950 -> Working period: continuous
Mais par contre je n'arrive plus à alimenter ma BD :
URL POST: api_key=123&sensor=BME280DHTAIR&location=Office&value1=18.93&value2=58.29&value3=1002.54&value4=18.24
23:20:57.162 -> HTTP Response code: 200
Je vais reprendre cela demain