-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp01.cpp
57 lines (45 loc) · 1.35 KB
/
esp01.cpp
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
#include <Adafruit_Sensor.h>
#include <DHT.h>
// dht sensor library from adafruit
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "ssid";
const char* password = "password";
String serverName = "/upload.php";
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
void loop() {
// put your main code here, to run repeatedly:
float temp = dht.readTemperature();
float hum = dht.readHumidity();
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
} else {
Serial.printf("Temperature %.2f C, Humidity %.2f %% \n",temp,hum);
}
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
http.addHeader("Content-Type", "application/json");
char buffer[80];
sprintf(buffer,"{\"room\": \"kitchen\", \"temperature\": %.2f, \"humidity\": %.2f }",temp,hum);
int httpResponseCode = http.POST(buffer);
Serial.println(buffer);
Serial.println(httpResponseCode);
http.end();
}
delay(5000);
}