#include #include #include #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); #include #include #include #define MH_Z19_RX 16 #define MH_Z19_TX 17 SoftwareSerial co2Serial(MH_Z19_RX, MH_Z19_TX); // define MH-Z19 // Parametres WIFI - WiFi settings const char* ssid = "VOTRE_SID_WIFI"; // your network SSID (name of wifi network) const char* password = "VOTRE CLEF WIFI"; // your network password // Parametres Jeedom const char* host = "192.168.1.xxx"; // IP DE VOTRE JEEDOM const int port = 80; // PORT HTTP DU JEEDOM (nota : pas tester en https). const char* apiKey = "VOTRE API JEEDOM"; // Clef API du jeedom. const char* IDX_mhz19 = "2857"; // Index du virtuel qui va recevoir les valeurs ce co2 (en ppm). // Fréquance d'envoi les data a Jeedom const int watchdog = 120000; // 120000 = 120 secondes unsigned long previousMillis = millis(); HTTPClient http; void setup() { Serial.begin(115200); delay(100); if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); for(;;); } delay(2000); display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(0, 10); // Display static text display.println("Hello !"); display.println("Boot du"); display.println("capteur de CO2..."); display.display(); delay(2000); display.clearDisplay(); display.display(); Serial.setDebugOutput(true); Serial.println("Connecting Wifi..."); display.setCursor(0,0); display.println("Connecting Wifi..."); display.display(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("Connexion en cours..."); display.println("Connexion en cours..."); display.display(); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.print(WiFi.localIP()); Serial.println(""); display.println(WiFi.localIP()); display.display(); delay(2000); display.clearDisplay(); display.display(); co2Serial.begin(9600); //Init sensor MH-Z19(14) } void loop() { unsigned long currentMillis = millis(); if ( currentMillis - previousMillis > watchdog ) { previousMillis = currentMillis; if(WiFi.status() != WL_CONNECTED) { Serial.println("WiFi not connected !"); } else { Serial.println("Send data to Jeedom"); Serial.print("Requesting CO2 concentration..."); int ppm = readCO2(); Serial.println(" PPM = " + String(ppm)); // Formatage de l'envoi a Jeedom String baseurl = "/core/api/jeeApi.php?apikey="; baseurl += apiKey; baseurl += "&type=virtual&id="; String url = baseurl + IDX_mhz19; url += url + "&value="; url += String(ppm); sendToJeedom(url); delay(1000); display.setCursor(0,0); display.setTextSize(4); display.println(String(ppm)); display.setTextSize(2); display.println("ppm"); display.display(); delay(1000); display.clearDisplay(); } } } boolean sendToJeedom(String url){ Serial.print("connecting to "); Serial.println(host); Serial.print("Requesting URL: "); Serial.println(url); Serial.println(""); http.begin(host,port,url); int httpCode = http.GET(); Serial.println("closing connection"); http.end(); } int readCO2() { // D'après le code original de | From original code https://github.com/jehy/arduino-esp8266-mh-z19-serial byte cmd[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; // command to ask for data byte response[9]; // for answer co2Serial.write(cmd, 9); //request PPM CO2 // The serial stream can get out of sync. The response starts with 0xff, try to resync. while (co2Serial.available() > 0 && (unsigned char)co2Serial.peek() != 0xFF) { co2Serial.read(); } memset(response, 0, 9); co2Serial.readBytes(response, 9); if (response[1] != 0x86) { Serial.println("Invalid response from co2 sensor!"); return -1; } byte crc = 0; for (int i = 1; i < 8; i++) { crc += response[i]; } crc = 255 - crc + 1; if (response[8] == crc) { int responseHigh = (int) response[2]; int responseLow = (int) response[3]; int ppm = (256 * responseHigh) + responseLow; return ppm; } else { Serial.println("CRC error!"); return -1; } }