Title
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
#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); } } |
[collapse]