-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathNode_MCU_iot.ino
65 lines (52 loc) · 1.5 KB
/
Node_MCU_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
#include <DHT.h> // Including library for dht
#include <ESP8266WiFi.h>
String apikey = "3GH0UPMWYROH8UQ9"; // Enter your Write API key from ThingSpeak
const char *ssid = "moto x4 4055"; // replace with your wifi ssid and wpa2 key
const char *password = "9876531578";
const char* server = "api.thingspeak.com";
DHT d(D0, DHT11);
WiFiClient client;
void setup() {
Serial.begin(115200);
delay(10);
d.begin();
Serial.println("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
//www.thingspeak.com
void loop() {
int t=d.readTemperature();
int h=d.readHumidity();
if(client.connect(server,80)){
//GET https://api.thingspeak.com/update?api_key=3GH0UPMWYROH8UQ9&field1=0
String getstr = apikey;
getstr += "&field1=";
getstr += String(t);
getstr += "&field2=";
getstr += String(h);
getstr += "\r\n\r\n";
client.print("GET /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(getstr.length());
client.print("\n\n");
client.print(getstr);
Serial.print("Temperature: ");
Serial.println(t);
Serial.print("Humidity: ");
Serial.print(h);
}
client.stop();
delay(15000);
}