#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "DHT.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SoftwareSerial.h>;
SoftwareSerial mySerial(14, 12); // RX, TX
DHT dht(2, 22); // GPIO , Type
Adafruit_BMP280 bmp280;
MDNSResponder mdns;
const char* ssid = "SSID";
const char* password = "passwd";
ESP8266WebServer server(80);
byte request[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9];
String webPage = "";
int test_pin = 16;
char dht_Temperature[5];
char dht_Humidity [5];
char dht_HeatIndex [5];
char bmp280_Temperature[5];
char bmp280_Pressure[6];
void setup(void){
delay(1000);
Serial.begin(9600);
mySerial.begin(9600);
dht.begin();
bmp280.begin();
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (mdns.begin("esp8266", WiFi.localIP())) {
Serial.println("MDNS responder started");
}
server.on("/", [](){
WebBuild();
server.send(200, "text/html", webPage);
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
void WebBuild() {
dtostrf(dht.readTemperature(), 3, 2, dht_Temperature);
dtostrf(dht.readHumidity(), 3, 2, dht_Humidity);
dtostrf(dht.computeHeatIndex(dht.readTemperature(),dht.readHumidity(),false), 3, 2, dht_HeatIndex);
dtostrf(bmp280.readTemperature(), 3, 2, bmp280_Temperature);
dtostrf(bmp280.readPressure()*0.007500617, 3, 2, bmp280_Pressure);
webPage = "<p>G1Tech: Web Weather Station</p>";
webPage += "<p>DHT22</p>";
webPage += "<p> Temperature: "+String(dht_Temperature)+"C</p>";
webPage += "<p> HeatIndex: "+String(dht_HeatIndex)+"C</p>";
webPage += "<p> Humidity: "+String(dht_Humidity)+"%</p>";
webPage += "<p>BMP280</p>";
webPage += "<p> Temperature: "+String(bmp280_Temperature)+"C</p>";
webPage += "<p> Pressure: "+String(bmp280_Pressure)+"mmHg</p>";
webPage += "<p>MH-Z19</p>";
webPage += "<p> CO2: "+callCO2()+"ppm</p>";
}
String callCO2(){
char mhz19_CO2[4];
mySerial.write(request, 9);
memset(response, 0, 9);
mySerial.readBytes(response, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=response[i];
crc = 255 - crc;
crc++;
if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
return "Err: invalid CRC";
} else {
unsigned int HLconcentration = (unsigned int) response[2];
unsigned int LLconcentration = (unsigned int) response[3];
unsigned int co2 = (256*HLconcentration) + LLconcentration;
Serial.println(co2);
dtostrf(co2, 4, 0, mhz19_CO2);
return String(mhz19_CO2);
}
}