// Load Wi-Fi library
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
// Replace with your network credentials
const char* ssid = "YOURSSID";
const char* password = "YOURPASSWORD";
// Set your Static IP address
IPAddress local_IP(192, 168, 0, 121);
// Set your Gateway IP address
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 0, 0);
#include "DHT.h"
#define DHTPIN 5 // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
#define NONVAL -9999
#define ANALOGPIN 0
#define PWMPIN 12
DHT dht(DHTPIN, DHTTYPE);
String HTTPURL = "http://192.168.0.117/hzcontroller/raumluft01.php";
void setup() {
//serial initialize.
Serial.begin(9600);
while(!Serial) { }
// Configures static IP address
//if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS))
if (!WiFi.config(local_IP, gateway, subnet))
{
Serial.println("WiFi Failed to configure");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
// https://randomnerdtutorials.com/solved-reconnect-esp8266-nodemcu-to-wifi/
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
Serial.println("");
Serial.print("MAC: ");
Serial.println(WiFi.macAddress());
Serial.print("IP : ");
Serial.println(WiFi.localIP());
//Initialise Sensors
dht.begin();
//MHZ 19
pinMode(PWMPIN, INPUT);
//MQ 135, analog Read braucht keine Initialisierung
Serial.println("Device Started");
}
char *ftoa(char *a, double f, int precision)
{
long p[] = {0,10,100,1000,10000,100000,1000000,10000000,100000000};
char *ret = a;
long heiltal = (long)f;
itoa(heiltal, a, 10);
while (*a != '\0') a++;
*a++ = '.';
long desimal = abs((long)((f - heiltal) * p[precision]));
itoa(desimal, a, 10);
return ret;
}
int loopCnt = 0;
void loop() {
char hs[20], ts[20];
char mqr[20], mzr[20], mzv[20];
// Report every 60 seconds.
if(loopCnt > 60) {
Serial.println("");
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed
if (isnan(h)) h = NONVAL;
if (isnan(h)) t = NONVAL;
ftoa(hs,h,2);
ftoa(ts,t,2);
Serial.print("Luftfeuchte: ");
Serial.print(hs);
Serial.print(" %\t");
Serial.print("Temperatur: ");
Serial.print(ts);
Serial.println(" *C ");
//https://www.codrey.com/electronic-circuits/how-to-use-mq-135-gas-sensor/
int anval = analogRead(ANALOGPIN); //0-1023, bei 0-3.3V, Achtung(!) Sensor liefert bis 5V => VoltageDivider notwendig
Serial.print("MQ-135 (raw): ");
Serial.println(anval);
//Wir rechnen den Wert nicht linear in ppm um, vgl WebLink, wenn dann richtib oder üner Library
//https://unsinnsbasis.de/co2-sensor-mhz19b/
int pwm_value = pulseIn(PWMPIN, HIGH, 2200000UL);
Serial.print("MHZ19B (pwm): ");
Serial.print(pwm_value);
//int ppm = (((pwm_value / 1000.0)-2)*5000.0) / 1000;
//int ppm = (((pwm_value / 1000.0)-2)*5.0) / 1.0;
int ppm = ((pwm_value / 1000)- 2) * 5;
Serial.print(" ppm: ");
Serial.println(ppm);
//Daten zum melden fertig machen
itoa(anval,mqr,10);
itoa(pwm_value,mzr,10);
itoa(ppm,mzv,10);
String queryString = HTTPURL + "?room=WOZI&h=" + String(hs) + "&t=" + String(ts) + "&mqr=" + String(mqr) + "&mzr=" + String(mzr) + "&mzv=" + String(mzv);
Serial.println(queryString);
//melden
//https://randomnerdtutorials.com/esp8266-nodemcu-http-get-post-arduino/
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, queryString.c_str());
// Send HTTP GET request
//int httpResponseCode = 0;
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
//String payload = http.getString();
//Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
loopCnt = 0;
}
delay(1000); // 1s
if ((loopCnt % 10) == 0)
Serial.print(".");
loopCnt ++;
}