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/04/12 19:51] – 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:// | ||
- | ==== V3 ==== | ||
- | |||
- | <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 | ||
- | * */ | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include < | ||
- | #include " | ||
- | |||
- | //I2C address 0x40 SIS7021 | ||
- | //I2C address 0x5A CCS811 | ||
- | //I2C address 0x76 BMP280 | ||
- | |||
- | //#define PMS5003 | ||
- | //#define SI702x | ||
- | #define HDC1080 | ||
- | |||
- | #ifdef SI702x | ||
- | #include " | ||
- | #endif | ||
- | #ifdef HDC1080 | ||
- | #include " | ||
- | ClosedCube_HDC1080 hdc1080; | ||
- | #endif | ||
- | |||
- | #include " | ||
- | #include " | ||
- | |||
- | #ifdef PMS5003 | ||
- | #include " | ||
- | #endif | ||
- | |||
- | #include < | ||
- | |||
- | #define CCS811_ADDR 0x5A //Alternate I2C Address | ||
- | CCS811 ccs811; | ||
- | #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 | ||
- | |||
- | String prog_version = " | ||
- | String version_maj = " | ||
- | |||
- | const char* ssid = " | ||
- | const char* password = " | ||
- | const char* mqtt_server = " | ||
- | const char* host_c = " | ||
- | 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; | ||
- | 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 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.println(ssid); | ||
- | |||
- | WiFi.mode(WIFI_STA); | ||
- | WiFi.begin(ssid, | ||
- | while (WiFi.status() != WL_CONNECTED) { | ||
- | delay(500); | ||
- | Serial.print(" | ||
- | } | ||
- | Serial.println("" | ||
- | Serial.println(" | ||
- | Serial.println(" | ||
- | Serial.println(WiFi.localIP()); | ||
- | } | ||
- | void dodo() { | ||
- | Serial.print(" | ||
- | Serial.println((float)minute_dodo); | ||
- | // Connect D0 to RST to wake up ! | ||
- | ESP.deepSleep(minute_dodo * 60 * 1000000); | ||
- | } | ||
- | |||
- | void setup() { | ||
- | temps_milli = millis(); | ||
- | |||
- | // Connect D0 to RST to wake up | ||
- | pinMode(D0, WAKEUP_PULLUP); | ||
- | Serial.begin(115200); | ||
- | pinMode(D8, OUTPUT); | ||
- | Serial.println("" | ||
- | Serial.print(" | ||
- | Serial.println(prog_version); | ||
- | Serial.print(" | ||
- | Serial.println(ESP.getChipId(), | ||
- | sprintf(host, | ||
- | setup_wifi(); | ||
- | |||
- | /////////////// | ||
- | digitalWrite(D8, | ||
- | delay(200); | ||
- | Serial.println(); | ||
- | Wire.begin(); | ||
- | //// 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(" | ||
- | // 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-> | ||
- | |||
- | ////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; | ||
- | }); | ||
- | |||
- | } | ||
- | |||
- | 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; | ||
- | //// 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; | ||
- | //// 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 ///// | ||
- | 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); | ||
- | } | ||
- | } | ||
- | |||
- | |||
- | #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 > 30000) { | ||
- | if ( faire_maj != true) dodo(); | ||
- | } | ||
- | if (now - temps_milli > 10000) { // attend 10s avant de faire | ||
- | Serial.print(" | ||
- | if ( faire_maj == true) Serial.println(WiFi.localIP()); | ||
- | if (connexion_prette == true && | ||
- | Serial.println(" | ||
- | 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 SI702x | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #endif | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | topic = " | ||
- | if ( client.publish(String(topic).c_str(), | ||
- | #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 > 5) Serial.println(" | ||
- | delay(5000); | ||
- | #ifdef PMS5003 | ||
- | pms.write(Pmsx003:: | ||
- | #endif | ||
- | } | ||
- | } | ||
- | delay(1000); | ||
- | } | ||
- | delay(1000); | ||
- | } | ||
- | |||
- | </ |