-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.ino
109 lines (96 loc) · 2.48 KB
/
Client.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Client code to get temperature and humidity
*/
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN 2
// AP Wi-Fi credentials
const char* ssid = "ESP_Sensor_NTW";
const char* password = "espsensorntw";
// Local ESP web-server address
String serverHost = "http://192.168.4.1/feed";
String data;
int counter = 0;
float h;
float t;
// Static network configuration
IPAddress ip(192, 168, 4, 4);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
DHT dht(DHTPIN, DHT11);
WiFiClient client;
void setup() {
ESP.eraseConfig();
WiFi.persistent(false);
Serial.begin(115200);
Serial.println();
Serial.println("**************************");
Serial.println("**************************");
Serial.println("******** BEGIN ***********");
Serial.println("- start DHT sensor");
dht.begin();
delay(500);
Serial.println("- set ESP STA mode");
WiFi.mode(WIFI_STA);
Serial.println("- connecting to wifi");
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
if(counter > 20){
Serial.println("- can't connect, going to sleep");
delay(500);
}
Serial.print(".");
counter++;
}
Serial.println("- wifi connected");
Serial.println("- read DHT sensor");
readDHTSensor();
Serial.println("- build DATA stream string");
buildDataStream();
Serial.println("- send GET request");
sendHttpRequest();
Serial.println();
Serial.println("- end of update");
Serial.println("**************************");
Serial.println("**************************");
}
void sendHttpRequest() {
HTTPClient http;
http.begin(serverHost);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST(data);
http.writeToStream(&Serial);
http.end();
}
void readDHTSensor() {
delay(200);
h = dht.readHumidity();
t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
t = 0.00;
h = 0.00;
}
Serial.println("- temperature read : "+String(t));
Serial.println("- humidity read : "+String(h));
}
void buildDataStream() {
data = "temp1=";
data += String(t);
data += "&hum1=";
data += String(h);
Serial.println("- data stream: "+data);
}
void loop() {
Serial.println("- read DHT sensor");
readDHTSensor();
Serial.println("- build DATA stream string");
buildDataStream();
Serial.println("- send GET request");
sendHttpRequest();
Serial.println();
Serial.println("- end of update");
delay(10000);
}