· 6 years ago · Oct 11, 2019, 03:50 PM
1#include <DHT.h>
2#include <DHT_U.h>
3#include <ESP8266WiFi.h>
4#include <ESP8266WebServer.h>
5#include <SDS011.h>
6#include <SoftwareSerial.h>
7
8
9#define SDS_RX D1
10#define SDS_TX D2
11
12#define DHTPIN D4
13#define DHTTYPE DHT22
14
15const char* ssid = "wifi-ssid";
16const char* password = "password";
17const char* host = "api.thingspeak.com";
18String apiKey = ""; // thingspeak.com api key goes here
19
20float p10,p25;
21int error;
22
23struct Air {
24 float pm25;
25 float pm10;
26 float humidity;
27 float temperature;
28};
29
30
31DHT_Unified dht(DHTPIN, DHTTYPE);
32ESP8266WebServer server(80);
33WiFiClient client;
34SDS011 sds;
35
36void setup() {
37 Serial.begin(9600);
38 sds.begin(SDS_RX,SDS_TX);
39 dht.begin();
40 connectToWiFi();
41}
42
43void loop() {
44 Air airData = readPolution();
45 if (client.connect(host,80) & airData.pm25 > 0.0) {
46 String postStr = apiKey;
47 postStr +="&field1=";
48 postStr += String(airData.pm25);
49 postStr +="&field2=";
50 postStr += String(airData.pm10);
51 postStr +="&field3=";
52 postStr += String(airData.humidity);
53 postStr +="&field4=";
54 postStr += String(airData.temperature);
55 postStr += "\r\n\r\n";
56
57 client.print("POST /update HTTP/1.1\n");
58 client.print("Host: api.thingspeak.com\n");
59 client.print("Connection: close\n");
60 client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
61 client.print("Content-Type: application/x-www-form-urlencoded\n");
62 client.print("Content-Length: ");
63 client.print(postStr.length());
64 client.print("\n\n");
65 client.print(postStr);
66 }
67 client.stop();
68 delay(15000);
69
70 server.handleClient();
71}
72
73void connectToWiFi(){
74 WiFi.mode(WIFI_STA);
75 WiFi.begin(ssid, password);
76
77 Serial.print("Connecting to ");
78 Serial.println(ssid);
79
80 while (WiFi.status() != WL_CONNECTED) {
81 delay(500);
82 Serial.print(".");
83 }
84
85 Serial.println("");
86 Serial.println("WiFi connected");
87 Serial.println("IP address: ");
88 Serial.println(WiFi.localIP());
89 startServer();
90}
91
92void startServer(){
93 server.on("/", handleRoot);
94 server.begin();
95 Serial.println("HTTP server started");
96}
97
98void handleRoot() {
99 Air airData = readPolution();
100 server.send(200, "text/plain", "PM2.5: " + String(airData.pm25) + " (" + String(calculatePolutionPM25(airData.pm25)) + "% normy) | PM10: " + String(airData.pm10) + " (" + String(calculatePolutionPM10(airData.pm10)) + "% normy) | Temperature: " + airData.temperature + " | Humidity: " + airData.humidity);
101}
102
103Air readPolution(){
104 float temperature, humidity;
105 error = sds.read(&p25,&p10);
106 if (!error) {
107 sensors_event_t event;
108 dht.temperature().getEvent(&event);
109 if (isnan(event.temperature)) {
110 Serial.println("Error reading temperature!");
111 } else {
112 temperature = event.temperature;
113 }
114
115 dht.humidity().getEvent(&event);
116 if (isnan(event.relative_humidity)) {
117 Serial.println("Error reading humidity!");
118 } else {
119 humidity = event.relative_humidity;
120 }
121
122 Air result = (Air){normalizePM25(p25/10, humidity), normalizePM10(p10/10, humidity), humidity, temperature};
123 return result;
124 } else {
125 Serial.println("Error reading SDS011");
126 return (Air){0.0, 0.0, 0.0, 0.0};
127 }
128}
129
130//Correction algorythm thanks to help of Zbyszek Kiliański (Krakow Zdroj)
131float normalizePM25(float pm25, float humidity){
132 return pm25/(1.0+0.48756*pow((humidity/100.0), 8.60068));
133}
134
135float normalizePM10(float pm10, float humidity){
136 return pm10/(1.0+0.81559*pow((humidity/100.0), 5.83411));
137}
138
139float calculatePolutionPM25(float pm25){
140 return pm25*100/25;
141}
142
143float calculatePolutionPM10(float pm10){
144 return pm10*100/50;
145}