-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_thermometer.ino
227 lines (204 loc) · 5.19 KB
/
wifi_thermometer.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#define DHTPIN D6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// wifi setup every row : SSID, Password
const char* wifi_ssid[][2] = {
{"SSID","password"},
};
const int wifi_ssid_max = 1;
const String privateAjaxUrl = ""; // link to your json processor website
const String stationID = "D1_dht22_001";
int stationTemp = 0;
int stationHeatIndex = 0;
int stationHumidity = 0;
unsigned long stationMillis = 0; // timestamp for json data
int sleepSeconds = 600;
/**
* Set wifi connection
*/
void setWifiConnection() {
byte available_networks = 0;
byte foundSsid = false;
do {
Serial.println("Searching routers");
do {
available_networks = WiFi.scanNetworks();
if (available_networks == 0) {
Serial.print(".");
delay(1000);
}
} while (available_networks == 0);
if (available_networks>0) {
foundSsid = false;
Serial.println("Wifi connections available");
for (int network = 0; network < available_networks; network++) {
for (int f = 0; f<wifi_ssid_max; f++) {
if (WiFi.SSID(network) == wifi_ssid[f][0]) {
Serial.print("Found ");
Serial.println(wifi_ssid[f][0]);
WiFi.begin(wifi_ssid[f][0], wifi_ssid[f][1]);
network = available_networks;
foundSsid = true;
}
}
}
byte wifiCounter = 30;
while ((foundSsid == true) && (WiFi.status() != WL_CONNECTED) && ( wifiCounter > 0)) {
wifiCounter --;
delay(500);
Serial.print(".");
}
}
} while (WiFi.status() != WL_CONNECTED);
}
/*
* Url Encode for sending post/get to webserver
* @param string original string
* @return string encoded string
*/
String urlencode(String str) {
String encodedString="";
char c;
char code0;
char code1;
char code2;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == ' '){
encodedString+= '+';
} else if (isalnum(c)){
encodedString+=c;
} else{
code1=(c & 0xf)+'0';
if ((c & 0xf) >9){
code1=(c & 0xf) - 10 + 'A';
}
c=(c>>4)&0xf;
code0=c+'0';
if (c > 9){
code0=c - 10 + 'A';
}
code2='\0';
encodedString+='%';
encodedString+=code0;
encodedString+=code1;
//encodedString+=code2;
}
yield();
}
return encodedString;
}
/*
* Weather information in json format
*/
String jsonOutput() {
String j = "";
j+="{\n";
j += "\"station_id\":\"";
j += stationID;
j+="\",\n";
j += "\"millis\":";
j += stationMillis;
j+=",\n";
j += "\"humidity\":";
j += stationHumidity;
j+=",\n";
j += "\"temperature\":";
j += stationTemp;
j+=",\n";
j += "\"heatIndex\":";
j += stationHeatIndex;
j+="\n";
j+="}\n";
return j;
}
/*
* Send json data with POST method to my webserver
*/
void sendRawAjax() {
HTTPClient http;
http.begin( privateAjaxUrl );
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST("json=" + urlencode(jsonOutput()));
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
Serial.println(jsonOutput());
}
/**
* DHT data read and set global variable
*/
void readDHT() {
String message = "";
int h = round(dht.readHumidity());
int t = round(dht.readTemperature());
if (isnan(h) || isnan(t) ) {
Serial.println("Failed to read from DHT sensor!");
} else {
int hic = round(dht.computeHeatIndex(t, h, false));
if ((t>-50) && (t<60)) {
stationTemp = t;
}
if ((hic>-50) && (hic<60)) {
stationHeatIndex = hic;
}
if ((h>0) && (h<150)) {
stationHumidity = h;
}
}
}
/**
* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SYSTEM SETUP
*/
void setup() {
Serial.begin(74880);
setWifiConnection();
Serial.println("");
Serial.print("Connected to ");
Serial.println(WiFi.SSID());
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway IP address: ");
Serial.println(WiFi.gatewayIP());
dht.begin();
}
/**
* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SYSTEM LOOP
*/
void loop() {
if (WiFi.status() != WL_CONNECTED) {
setWifiConnection();
}
Serial.println("Waiting 10 seconds for the sensor.");
delay(10000);
readDHT();
stationMillis = millis();
sendRawAjax();
Serial.print("Humidity: ");
Serial.print(stationHumidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(stationTemp);
Serial.print(" *C ");
Serial.print("Heat index: ");
Serial.print(stationHeatIndex);
Serial.println(" *C ");
Serial.print("Going to sleep till ");
Serial.print(sleepSeconds);
Serial.println(" seconds. ");
ESP.deepSleep(sleepSeconds * 1000000);
}