-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeather_Station_IOT.ino
89 lines (70 loc) · 3.03 KB
/
Weather_Station_IOT.ino
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
//Weather Station
#include <DHT.h> // Including library for dht
#include "MQ135.h"
#include <ESP8266WiFi.h>
String apiKey = "3J7EGP6SIP22D52C"; // Enter your Write API key from ThingSpeak
const char *ssid = "*******"; // replace with your wifi ssid and wpa2 key
const char *pass = "******"; // write your wifi password here
const char* server = "api.thingspeak.com";
const int sensorPin= A0;
int air_quality;
#define DHTPIN D1 //Connect to GPIO 2 in NodeMCU Board
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup()
{
Serial.begin(115200);
delay(10);
dht.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
//int gasValue = analogRead(gas);
MQ135 gasSensor = MQ135(A0);
air_quality = gasSensor.getPPM();
if (client.connect(server,80)) // "184.106.153.149" or api.thingspeak.com
{
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(air_quality);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius, Humidity: ");
Serial.print(h);
//Serial.print("%, Gas Value: ");
//Serial.print(h);
Serial.print("%, Air Quality: ");
Serial.print(air_quality);
Serial.println("PPM. Send to Thingspeak.");
}
client.stop();
Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates
delay(1000);
}