1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// Inclusion des librairies
#include <DHT.h>
#include "SdsDustSensor.h"
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <HTTPClient.h>
//#include <Wire.h> /: Vérifier si nécessaire car librairie pour commander des com I2C
// Constantes du programme
#define brocheDeBranchementDHT 23
#define typeDeDHT DHT22
#define adresseI2CduBME280 0x76 // Adresse I2C du BME280 (0x76, dans mon cas, ce qui est souvent la valeur par défaut)
#define pressionAuNiveauDeLaMerEnHpa 1024.90 // https://fr.wikipedia.org/wiki/Pression_atmospherique (1013.25 hPa en moyenne, valeur "par défaut")
#define delaiRafraichissementAffichage 10000 // Délai de rafraîchissement de l'affichage (en millisecondes)
int led = 12;
const int rxPin = 19;
const int txPin = 18;
String sensorName = "BME280";
String sensorLocation = "Ext";
// Network credentials
const char* ssid = "LilounetworkBUREAU";
const char* password = "motdepasse*";
// Variable to store the HTTP request
String header;
//Votre nom de domaine avec chemin URL ou adresse IP avec chemin
const char* serverName = "http://frevale.free.fr/esp-post-data.php";
// Conservez cette valeur de clé API pour être compatible avec le code PHP fourni dans la page du projet.
// Si vous modifiez la valeur apiKeyValue, le fichier PHP /esp-post-data.php doit également avoir la même clé
String apiKeyValue = "motdepasse";
// les variables suivantes sont des longueurs non signées car le temps, mesuré en
// millisecondes, deviendra rapidement un nombre plus grand que celui qui peut être stocké dans un int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
unsigned long httptimerDelay = 60000; // modif le 9/10 à 60 000 au lieu de 600 000 1mn ald 10mn
// Set timer to 30 seconds (30000)
//unsigned long timerDelay30s = 30000;
// Instanciation des librairies
SdsDustSensor sds(Serial1);
DHT dht(brocheDeBranchementDHT, typeDeDHT);
// Instanciation de la librairie BME280
Adafruit_BME280 bme;
// ========================
// Initialisation programme
// ========================
void setup() {
// Initialisation diode;
pinMode(led, OUTPUT);
// Initialisation du DHT22;
dht.begin();
// Initialisation du moniteur;
Serial.begin(9600);
// Initialisation du controleur air;
Serial1.begin(9600, SERIAL_8N1, rxPin, txPin);
sds.begin();
Serial.println(sds.queryFirmwareVersion().toString());
Serial.println(sds.setActiveReportingMode().toString());
Serial.println(sds.setContinuousWorkingPeriod().toString());
// Initialisation du BME280
Serial.print("Initialisation du BME280, à l'adresse [0x");
Serial.print(adresseI2CduBME280, HEX);
Serial.println("]");
if(!bme.begin(adresseI2CduBME280)) {
Serial.println("--> ÉCHEC…");
while(1); // Arrêt du programme, en cas d'échec de l'initialisation
} else {
Serial.println("--> REUSSIE !");
}
Serial.println();
// Connection au reseau wifi et attribution IP
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connecté au réseau WiFi avec l'adresse IP : ");
Serial.println(WiFi.localIP());
// détails de la connexion: https://www.upesy.fr/blogs/tutorials/how-to-connect-wifi-acces-point-with-esp32
Serial.println("\nConnected to the WiFi network");
Serial.print("[+] Gateway IP : ");
Serial.println(WiFi.gatewayIP());
Serial.println((String)"[+] RSSI : " + WiFi.RSSI() + " dB");;
}
// ======================================
// Boucle principale (boucle perpétuelle)
// ======================================
void loop() {
digitalWrite(led, HIGH);
// **********BMP180********************
// Affichage de la TEMPÉRATURE
// ****Mesure BMP****
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
float 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
float 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
float 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");
//***Mesure de particules
PmResult pm = sds.readPm();
if (pm.isOk()) {
Serial.print("PM2.5 = ");
Serial.print(pm.pm25);
Serial.print(", PM10 = ");
Serial.println(pm.pm10);
}
else {
Serial.print("Could not read values from sensor, reason: ");
Serial.println(pm.statusToString());
}
//***************************************************************
// *****Partie WEB***********************************************
//***************************************************************
//Envoie une requête HTTP POST toutes les xx minutes
if ((millis() - lastTime) > httptimerDelay) {
// Vérifier l'état de la connexion Wi-Fi
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Votre nom de domaine avec chemin URL ou adresse IP avec chemin
//http.begin(client, serverName);// version d'origine
http.begin(serverName);//version test
Serial.println("données reseau");
Serial.println(client);
Serial.println(serverName);
// Spécifiez l'en-tête du type de contenu
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Pour test
// Data to send with HTTP POST
String httpRequestData = "http://frevale.free.fr/esp-post-data.php&api_key=motdepasse&sensor=BME280&value1=24.25&value2=49&value3=1005.14";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
//******
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// Send HTTP POST request
//int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
delay(delaiRafraichissementAffichage/2);
digitalWrite(led, LOW);
Serial.println("******************************************");
delay(delaiRafraichissementAffichage/2);
} |
Partager