-
Notifications
You must be signed in to change notification settings - Fork 1
/
espwifi.ino
66 lines (57 loc) · 1.52 KB
/
espwifi.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
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <WiFiClient.h>
#define DHTPINA D4
const char* ssid = "parter";
const char* pass = "setrometal";
const char* host = "192.168.2.55";
DHT dht(DHTPINA, DHT22);
float dhtUmid,dhtTemp;
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
dht.begin();
}
void loop() {
readData();
Serial.print("Temp,umid:");
Serial.print(dhtTemp);
Serial.print(",");
Serial.println(dhtUmid);
send_data();
Serial.println("Going to sleep.");
ESP.deepSleep(60e6*10);
}
void readData(void){
dhtTemp = dht.readTemperature();
dhtUmid = dht.readHumidity();
}
void send_data(){
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/add_data_espwifi.php?";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + "umid=" +dhtUmid + "&&" + "temp=" +dhtTemp + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
}