Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
capteurs:wemos_d1_cjmcu_8128 [2021/05/01 12:30] – jeff | capteurs:wemos_d1_cjmcu_8128 [Date inconnue] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
- | ====== Capteur Température / humidité / pression / CO2 : Wemos D1 cjmcu 8128 ====== | ||
- | * [[https:// | ||
- | * CJMCU-811 CCS811 Indoor air quality monitoring digital gas sensor | ||
- | * BMP280 température/ | ||
- | * HDC1080 température et humidité | ||
- | * relai sur l' | ||
- | * batterie 18650 , et son régulateur | ||
- | |||
- | **Les mesures:** | ||
- | * TVCO (Total Volatile Organic Compound) | ||
- | * eCO2 (équivalent CO2) | ||
- | * Humidité (SI702x) | ||
- | * Température (BMP280 & SI702x) | ||
- | * Pression (BMP280) | ||
- | * tension batterie | ||
- | |||
- | |||
- | **Évolutions: | ||
- | * suppression de la mesure batterie qui consomme trop. Il suffit d' | ||
- | * ajout de la possibilité de télécharger un nouveau firmware pour mise à jour si version différente. Cela permet de laisser le shunt RESET/D0 pour le réveil auto en place. | ||
- | |||
- | **futures: | ||
- | * passage sur une autre platine esp8266 pour une alimentation soudée | ||
- | * test d'un autre capteur sans le eCO2 pour une serie température/ | ||
- | * ajout d' | ||
- | |||
- | pistes : | ||
- | * http:// | ||
- | * https:// | ||
- | |||
- | **Process** | ||
- | |||
- | Sortie du mode veille | ||
- | - se connecte au reseau Wifi | ||
- | - se met en mode OTA pour d' | ||
- | - actionne le relai de mise liaison Resistance 180k/A0 Wemos pour une mesure de la tension de la batterie | ||
- | - setup et initialisation des puces de mesure | ||
- | - si il y a des mesures envois les info au serveur MQTT | ||
- | Se remet en mode sommeil pour 15min | ||
- | |||
- | |||
- | ===== Codes ===== | ||
- | Mode Arduino / ESP8266 https:// | ||
- | ==== V5 ==== | ||
- | |||
- | <code c> | ||
- | |||
- | // ESP_Wemos | ||
- | // attend x seconde , poste en mqtt , deep sleep | ||
- | /* | ||
- | | ||
- | | ||
- | B - |) | ||
- | E / | ||
- | D8 -> 10k -> B(2N2222) | ||
- | GND -> C(2N2222) | ||
- | -> CJMCU | ||
- | | ||
- | | ||
- | -> relai entree contact | ||
- | relai sortie contact -> V CJMCU | ||
- | * */ | ||
- | |||
- | /* | ||
- | // ESP_Wemos | ||
- | CCS811 + BMP280 + HDC1080 ou SI7021 Carbon Monoxide Temperature Humidity Air Gas Sensor | ||
- | DH12 | ||
- | V5 | ||
- | Fonction: | ||
- | vérification et prise de mesures | ||
- | connection sur un des reseau wifi present | ||
- | se connecte au serveur MQTT | ||
- | s' | ||
- | poste en mqtt | ||
- | deep sleep | ||
- | |||
- | | ||
- | |||
- | GND -> 2N2222 | ||
- | D8 -> 10k -> B --- |)2N2222 | ||
- | relai bobine -> E -- / | ||
- | | | ||
- | \-> CJMCU | ||
- | |||
- | | ||
- | | ||
- | | ||
- | relai sortie contact -> V CJMCU | ||
- | * */ | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include " | ||
- | #include < | ||
- | Ticker coucou; | ||
- | ESP8266WiFiMulti wifiMulti; | ||
- | |||
- | //I2C address 0x40 HDC1080 | ||
- | //I2C address 0x5A CCS811 | ||
- | //I2C address 0x76 BMP280 | ||
- | |||
- | //#define PMS5003 | ||
- | //#define SI702x | ||
- | //#define HDC1080 | ||
- | //#define BMP280 | ||
- | //#define Capteur_CCS811 | ||
- | #define Capteur_DHT12 | ||
- | |||
- | ////////////////////// | ||
- | #ifdef SI702x | ||
- | #include " | ||
- | #endif | ||
- | #ifdef HDC1080 | ||
- | #include " | ||
- | ClosedCube_HDC1080 hdc1080; | ||
- | #endif | ||
- | |||
- | #include " | ||
- | #include " | ||
- | |||
- | #ifdef PMS5003 | ||
- | #include " | ||
- | #endif | ||
- | |||
- | #include < | ||
- | #ifdef Capteur_CCS811 | ||
- | #define CCS811_ADDR 0x5A //Alternate I2C Address | ||
- | CCS811 ccs811; | ||
- | #endif | ||
- | |||
- | #ifdef SI702x | ||
- | Adafruit_Si7021 SI702x = Adafruit_Si7021(); | ||
- | #endif | ||
- | Adafruit_BMP280 bmp; // I2C | ||
- | Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor(); | ||
- | Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor(); | ||
- | |||
- | #ifdef PMS5003 | ||
- | Pmsx003 pms(D4, D3); | ||
- | int rxPin = D3; // sds011 D1 D2 utilisé par DHT12 i2c | ||
- | int txPin = D4; | ||
- | #endif | ||
- | |||
- | #ifdef Capteur_DHT12 | ||
- | #include < | ||
- | DHT12 dht12; | ||
- | #endif | ||
- | |||
- | String prog_version = " | ||
- | String version_maj = " | ||
- | |||
- | const char* ssid1 = " | ||
- | const char* password1 = " | ||
- | const char* ssid2 = " | ||
- | const char* password2 = " | ||
- | const char* ssid3 = " | ||
- | const char* password3 = " | ||
- | const char* ssid4 = " | ||
- | const char* password4 = " | ||
- | |||
- | const char* mqtt_server = " | ||
- | char host_c[20] = " | ||
- | char host[20] = " | ||
- | const char* mqtt_user = " | ||
- | const char* mqtt_pass = " | ||
- | int mqtt_port = 1883; | ||
- | bool faire_maj = false; | ||
- | bool demande_maj = false; | ||
- | bool envoie_mqtt = false; | ||
- | bool connexion_prette = false; | ||
- | int donnees_prette = 0; | ||
- | int publie_ok = 0; | ||
- | int publie_max = 5; | ||
- | float temps_milli | ||
- | uint64_t minute_dodo | ||
- | |||
- | /////////////////////////////////// | ||
- | int retour_ok = 0; // retour du serveur | ||
- | float temp_bmp280 = 0; | ||
- | float press_bmp280 = 0; | ||
- | float humidite = 0; | ||
- | float temp_Si7021 = 0; | ||
- | float humid_Si7021 = 0; | ||
- | float temp_hdc1080 = 0; | ||
- | float humid_hdc1080 = 0; | ||
- | float temp_DHT12 = 0; | ||
- | float humid_DHT12 = 0; | ||
- | float pm_1 = 0; | ||
- | float pm_2_5 = 0; | ||
- | float pm_10 = 0; | ||
- | float pm_endessous_0_3 = 0; | ||
- | float pm_endessous_0_5 = 0; | ||
- | float pm_endessous_1 | ||
- | float pm_endessous_2_5 = 0; | ||
- | float pm_endessous_5 | ||
- | float pm_endessous_10 | ||
- | float eco2_ccs811 | ||
- | float etvoc_ccs811 | ||
- | |||
- | auto lastRead = millis(); | ||
- | |||
- | EspMQTTClient client(NULL, | ||
- | |||
- | void setup_wifi() { | ||
- | delay(10); | ||
- | // We start by connecting to a WiFi network | ||
- | Serial.println(); | ||
- | Serial.print(" | ||
- | Serial.print(ssid1); | ||
- | Serial.print(" | ||
- | Serial.print(ssid2); | ||
- | Serial.print(" | ||
- | Serial.print(ssid3); | ||
- | Serial.print(" | ||
- | Serial.println(ssid4); | ||
- | coucou.attach(1, | ||
- | |||
- | WiFi.mode(WIFI_STA); | ||
- | wifiMulti.addAP(ssid1, | ||
- | wifiMulti.addAP(ssid2, | ||
- | wifiMulti.addAP(ssid3, | ||
- | wifiMulti.addAP(ssid4, | ||
- | int boucle = 0; | ||
- | while (wifiMulti.run() != WL_CONNECTED) { | ||
- | delay(500); | ||
- | Serial.print(" | ||
- | if (boucle++ > 30) dodo(); | ||
- | } | ||
- | Serial.println("" | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | Serial.println(WiFi.localIP()); | ||
- | } | ||
- | void dodo() { | ||
- | digitalWrite(LED_BUILTIN, | ||
- | Serial.print(" | ||
- | Serial.println((float)minute_dodo); | ||
- | // Connect D0 to RST to wake up ! | ||
- | ESP.deepSleep(minute_dodo * 60 * 1000000); | ||
- | } | ||
- | int count = 0; | ||
- | |||
- | void flip() { | ||
- | int state = digitalRead(LED_BUILTIN); | ||
- | digitalWrite(LED_BUILTIN, | ||
- | |||
- | ++count; | ||
- | // when the counter reaches a certain value, start blinking like crazy | ||
- | if (count == 20) { | ||
- | coucou.attach(0.1, | ||
- | } | ||
- | // when the counter reaches yet another value, stop blinking | ||
- | else if (count == 120) { | ||
- | coucou.detach(); | ||
- | } | ||
- | } | ||
- | void setup() { | ||
- | temps_milli = millis(); | ||
- | pinMode(LED_BUILTIN, | ||
- | digitalWrite(LED_BUILTIN, | ||
- | |||
- | // Connect D0 to RST to wake up | ||
- | pinMode(D0, WAKEUP_PULLUP); | ||
- | Serial.begin(115200); | ||
- | coucou.attach(2, | ||
- | |||
- | pinMode(D8, OUTPUT); | ||
- | Serial.println("" | ||
- | Serial.print(" | ||
- | Serial.println(prog_version); | ||
- | Serial.print(" | ||
- | Serial.println(ESP.getChipId(), | ||
- | sprintf(host, | ||
- | setup_wifi(); | ||
- | Serial.println(host); | ||
- | strlcpy(host_c, | ||
- | /////////////// | ||
- | digitalWrite(D8, | ||
- | delay(200); | ||
- | Serial.println(); | ||
- | Wire.begin(); | ||
- | delay(1000); | ||
- | ////DHT12 | ||
- | #ifdef Capteur_DHT12 | ||
- | if (dht12.get() == 0) { | ||
- | Serial.print(" | ||
- | Serial.println(dht12.cTemp); | ||
- | Serial.print(" | ||
- | Serial.println(dht12.fTemp); | ||
- | Serial.print(" | ||
- | Serial.println(dht12.humidity); | ||
- | Serial.println(); | ||
- | } | ||
- | #endif | ||
- | //// CCS811 | ||
- | #ifdef Capteur_CCS811 | ||
- | Serial.println(" | ||
- | ccs811.set_i2cdelay(50); | ||
- | if (!ccs811.begin()) { | ||
- | Serial.println(" | ||
- | } | ||
- | bool ok = ccs811.start(CCS811_MODE_1SEC); | ||
- | if ( !ok ) Serial.println(" | ||
- | |||
- | else Serial.println(" | ||
- | #endif | ||
- | |||
- | // BMP280 | ||
- | #ifdef BMP280 | ||
- | delay(200); | ||
- | if (!bmp.begin(0x76)) { | ||
- | Serial.println(F(" | ||
- | } | ||
- | else Serial.println(" | ||
- | /* Default settings from datasheet. */ | ||
- | bmp.setSampling(Adafruit_BMP280:: | ||
- | Adafruit_BMP280:: | ||
- | Adafruit_BMP280:: | ||
- | Adafruit_BMP280:: | ||
- | Adafruit_BMP280:: | ||
- | |||
- | bmp_temp-> | ||
- | #endif | ||
- | ////Si7021 | ||
- | #ifdef SI702x | ||
- | Serial.println(" | ||
- | if (!SI702x.begin()) { | ||
- | Serial.println(" | ||
- | } | ||
- | else Serial.println(" | ||
- | #endif | ||
- | ////HDC1080 | ||
- | #ifdef HDC1080 | ||
- | hdc1080.begin(0x40); | ||
- | #endif | ||
- | //// PMS5003 //// | ||
- | #ifdef PMS5003 | ||
- | Serial.println(" | ||
- | pms.begin() ; | ||
- | pms.write(Pmsx003:: | ||
- | pms.write(Pmsx003:: | ||
- | pms.waitForData(Pmsx003:: | ||
- | // | ||
- | |||
- | Serial.println(" | ||
- | #endif | ||
- | } | ||
- | void onConnectionEstablished() { | ||
- | connexion_prette = true ; | ||
- | Serial.println(" | ||
- | Serial.print(mqtt_server); | ||
- | Serial.print(" | ||
- | Serial.print(mqtt_port); | ||
- | Serial.print(" | ||
- | Serial.print(host); | ||
- | Serial.print(" | ||
- | Serial.print(mqtt_pass); | ||
- | Serial.print(" | ||
- | Serial.println(mqtt_user); | ||
- | // regarde si maj firmware | ||
- | String topic = " | ||
- | client.subscribe(String(topic).c_str(), | ||
- | Serial.println(payload); | ||
- | version_maj = payload; | ||
- | }); | ||
- | coucou.attach(0.5, | ||
- | |||
- | } | ||
- | |||
- | void loop() { | ||
- | unsigned long now = millis(); | ||
- | Serial.print(" | ||
- | Serial.println((now - temps_milli) / 1000); | ||
- | |||
- | if (version_maj != prog_version) { | ||
- | Serial.println(" | ||
- | Serial.println(version_maj); | ||
- | faire_maj = true; | ||
- | if (demande_maj != true ) client.enableHTTPWebUpdater(); | ||
- | // User and password default to values of MQTTUsername and MQTTPassword. | ||
- | //These can be overrited with enableHTTPWebUpdater(" | ||
- | demande_maj = true; | ||
- | } | ||
- | envoie_mqtt = false; | ||
- | //// DHT12 | ||
- | #ifdef Capteur_DHT12 | ||
- | if (dht12.get() == 0) { | ||
- | Serial.print(" | ||
- | Serial.print(dht12.cTemp); | ||
- | Serial.println(" | ||
- | Serial.print(" | ||
- | Serial.println(dht12.humidity); | ||
- | temp_DHT12 = (float) dht12.cTemp; | ||
- | humid_DHT12 = (float) dht12.humidity; | ||
- | humidite = humid_DHT12; | ||
- | } | ||
- | #endif | ||
- | //// BMP280 | ||
- | #ifdef BMP280 | ||
- | sensors_event_t temp_event, pressure_event; | ||
- | bmp_temp-> | ||
- | bmp_pressure-> | ||
- | |||
- | Serial.print(F("/ | ||
- | Serial.print(temp_event.temperature); | ||
- | Serial.println(" | ||
- | Serial.print(F("/ | ||
- | Serial.print(pressure_event.pressure); | ||
- | Serial.println(" | ||
- | temp_bmp280 = (float)temp_event.temperature; | ||
- | press_bmp280 = (float)pressure_event.pressure; | ||
- | #endif | ||
- | //// HDC1080 | ||
- | #ifdef HDC1080 | ||
- | Serial.print(" | ||
- | Serial.print(hdc1080.readTemperature()); | ||
- | Serial.println(" | ||
- | Serial.print(" | ||
- | Serial.println(hdc1080.readHumidity()); | ||
- | temp_hdc1080 = (float) hdc1080.readTemperature(); | ||
- | humid_hdc1080 = (float) hdc1080.readHumidity(); | ||
- | humidite = humid_hdc1080; | ||
- | #endif | ||
- | //// Si7021 ///// | ||
- | #ifdef SI702x | ||
- | Serial.print(" | ||
- | Serial.print(SI702x.readTemperature(), | ||
- | Serial.println(" | ||
- | Serial.print(" | ||
- | Serial.println(SI702x.readHumidity(), | ||
- | temp_Si7021 = (float) SI702x.readTemperature(); | ||
- | humid_Si7021 = (float)SI702x.readHumidity(); | ||
- | humidite = humid_Si7021; | ||
- | #endif | ||
- | //// CCS811 ///// | ||
- | #ifdef CCS811 | ||
- | uint16_t eco2, etvoc, errstat, raw; // Read CCS811 | ||
- | ccs811.set_envdata(temp_event.temperature, | ||
- | ccs811.read(& | ||
- | int test = 0; | ||
- | while ( test++ < 20 ) { | ||
- | if ( errstat == CCS811_ERRSTAT_OK ) { | ||
- | Serial.print(" | ||
- | Serial.println(eco2); | ||
- | Serial.print(" | ||
- | Serial.println(etvoc); | ||
- | eco2_ccs811 = (float) eco2; | ||
- | etvoc_ccs811 = (float) etvoc; | ||
- | if (eco2_ccs811 > 100) break; | ||
- | delay(1000); | ||
- | } | ||
- | } | ||
- | #endif | ||
- | |||
- | #ifdef PMS5003 | ||
- | //// PMS5003 //// | ||
- | // | ||
- | // | ||
- | // | ||
- | pms.write(Pmsx003:: | ||
- | // | ||
- | delay(1000); | ||
- | const auto n = Pmsx003:: | ||
- | Pmsx003:: | ||
- | Pmsx003:: | ||
- | switch (status) { | ||
- | case Pmsx003:: | ||
- | { | ||
- | auto newRead = millis(); | ||
- | lastRead = newRead; | ||
- | // For loop starts from 3 | ||
- | // Skip the first three data (PM1dot0CF1, | ||
- | for (size_t i = Pmsx003:: | ||
- | Serial.print(data[i]); | ||
- | Serial.print(" | ||
- | Serial.print(Pmsx003:: | ||
- | Serial.print(" | ||
- | Serial.print(Pmsx003:: | ||
- | Serial.print(" | ||
- | Serial.println(); | ||
- | } | ||
- | pm_1 = (float)data[3]; | ||
- | pm_2_5 = (float)data[4]; | ||
- | pm_10 = (float)data[5]; | ||
- | pm_endessous_0_3 = (float)data[6]; | ||
- | pm_endessous_0_5 = (float)data[7]; | ||
- | pm_endessous_1 | ||
- | pm_endessous_2_5 = (float)data[9]; | ||
- | pm_endessous_5 | ||
- | pm_endessous_10 | ||
- | if (pm_1 > 0 || pm_2_5 > 0 || pm_10 > 0) envoie_mqtt = true; | ||
- | break; | ||
- | } | ||
- | case Pmsx003:: | ||
- | Serial.println(" | ||
- | break; | ||
- | default: | ||
- | Serial.println(" | ||
- | Serial.println(Pmsx003:: | ||
- | } | ||
- | /* | ||
- | // capteur sds011 | ||
- | PmResult pm = sds.readPm(); | ||
- | if (pm.isOk()) { | ||
- | pm_2_5 = (float)pm.pm25; | ||
- | pm_10 = (float)pm.pm10; | ||
- | Serial.print(" | ||
- | Serial.println(pm_2_5); | ||
- | Serial.print(" | ||
- | Serial.println(pm_10); | ||
- | // Serial.println(pm.toString()); | ||
- | } else { | ||
- | // notice that loop delay is set to 5s (sensor sends data every 3 minutes) and some reads are not available | ||
- | // | ||
- | // | ||
- | Serial.print(" | ||
- | Serial.println(pm_2_5); | ||
- | Serial.print(" | ||
- | Serial.println(pm_10); | ||
- | } | ||
- | */ | ||
- | #else | ||
- | envoie_mqtt = true; | ||
- | #endif | ||
- | ///////// MQTT config | ||
- | if (envoie_mqtt == true && press_bmp280 > 0 && humidite > 0 && eco2_ccs811 > 0) { | ||
- | // | ||
- | client.enableDebuggingMessages(); | ||
- | client.loop(); | ||
- | if (now - temps_milli > 60000) { | ||
- | Serial.println(" | ||
- | dodo(); | ||
- | } | ||
- | |||
- | if (now - temps_milli > 20000) { // attend 20s avant de faire | ||
- | Serial.print(" | ||
- | if ( faire_maj == true) Serial.println(WiFi.localIP()); | ||
- | if (connexion_prette == true && | ||
- | Serial.println(" | ||
- | coucou.attach(0.3, | ||
- | |||
- | String topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #ifdef HDC1080 | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #endif | ||
- | #ifdef Capteur_DHT12 | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | publie_max = 2; | ||
- | #endif | ||
- | #ifdef SI702x | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #endif | ||
- | #ifdef Capteur_CCS811 | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #endif | ||
- | #ifdef PMS5003 | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | |||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #endif | ||
- | Serial.println(publie_ok); | ||
- | if (publie_ok > publie_max) Serial.println(" | ||
- | delay(5000); | ||
- | #ifdef PMS5003 | ||
- | pms.write(Pmsx003:: | ||
- | #endif | ||
- | dodo(); | ||
- | } | ||
- | } | ||
- | delay(1000); | ||
- | } | ||
- | delay(1000); | ||
- | } | ||
- | </ |