#include <ESP8266WiFi.h>#include <ESP8266HTTPClient.h>
#include <MD_Parola.h>#include <MD_MAX72xx.h>#include <SPI.h>
// Define the number of devices we have in the chain and the hardware interface// NOTE: These pin numbers will probably not work with your hardware and may// need to be adapted#define HARDWARE_TYPE MD_MAX72XX::FC16_HW#define MAX_DEVICES 4
#define CLK_PIN D5#define DATA_PIN D7#define CS_PIN D8
// Hardware SPI connectionMD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);// Arbitrary output pins// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
#include <Wire.h> // I2C library#include "ccs811.h" // CCS811 library
// Wiring for ESP8266 NodeMCU boards: VDD to 3V3, GND to GND, SDA to D2, SCL to D1, nWAKE to D3 (or GND)CCS811 ccs811(D3); // nWAKE on D3
// WiFi settings#define wifi_ssid "xxxxx"#define wifi_password "xxxxx"
// HTTP Domoticz settingsconst char* host = "192.168.1.19";const int port = 8080;
HTTPClient http;
void setup() { Serial.begin(115200); setup_wifi();
Serial.println(""); Serial.println("setup: Starting CCS811 "); Serial.print("setup: ccs811 lib version: "); Serial.println(CCS811_VERSION);
// Enable I2C Wire.begin(); // Enable CCS811 ccs811.set_i2cdelay(50); // Needed for ESP8266 because it doesn't handle I2C clock stretch correctly bool ok= ccs811.begin(); if( !ok ) Serial.println("setup: CCS811 begin FAILED");
// Print CCS811 versions Serial.print("setup: hardware version: "); Serial.println(ccs811.hardware_version(),HEX); Serial.print("setup: bootloader version: "); Serial.println(ccs811.bootloader_version(),HEX); Serial.print("setup: application version: "); Serial.println(ccs811.application_version(),HEX); // Start measuring ok= ccs811.start(CCS811_MODE_1SEC); if( !ok ) Serial.println("setup: CCS811 start FAILED");
}
void settemp(){
//co sensor uint16_t eco2, etvoc, errstat, raw; ccs811.read(&eco2,&etvoc,&errstat,&raw); // Print measurement results based on status if( errstat==CCS811_ERRSTAT_OK ) { Serial.print("CCS811: "); Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm "); Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb "); Serial.print("raw6="); Serial.print(raw/1024); Serial.print(" uA "); Serial.print("raw10="); Serial.print(raw%1024); Serial.print(" ADC "); Serial.print("R="); Serial.print((1650*1000L/1023)*(raw%1024)/(raw/1024)); Serial.print(" ohm"); Serial.println(); } else if( errstat==CCS811_ERRSTAT_OK_NODATA ) { Serial.println("CCS811: waiting for (new) data"); } else if( errstat & CCS811_ERRSTAT_I2CFAIL ) { Serial.println("CCS811: I2C error"); } else { Serial.print("CCS811: errstat="); Serial.print(errstat,HEX); Serial.print("="); Serial.println( ccs811.errstat_str(errstat) ); }
// Serial.print("Requesting data..."); // sensors.requestTemperatures(); // Serial.println("DONE"); // After we got the temperatures, we can print them here. // Get the temperature from the first sensor only. Serial.print("Var: ");
//Temp is de variable van wat we naar de idx willen zenden int TEMP = (eco2); Serial.println(TEMP); // Here is the Json format that Domoticz expects // /json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP String url = "/json.htm?type=command¶m=udevice&idx="; url += String(275); // IDX van je Device in domoticz url += "&nvalue=0&svalue="; url += String(TEMP); // dit is de waarde die naar de IDX gestuurd word in domoticz
sendToDomoticz(url); P.begin(); // P.print("co"); P.print( TEMP);
}
void loop() { settemp(); P.displayAnimate(); delay (3000);}
//Connect to wifivoid setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.println(""); Serial.println("WiFi connection Established"); Serial.print("IP Adress : "); Serial.print(WiFi.localIP());}
void sendToDomoticz(String url){ Serial.print("Connecting to "); Serial.println(host); Serial.print("Requesting URL: "); Serial.println(url); http.begin(host,port,url); int httpCode = http.GET(); if (httpCode) { if (httpCode == 200) { String payload = http.getString(); Serial.println("Domoticz response "); Serial.println(payload); } } Serial.println("closing connection"); http.end();}