-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfan_pwm_Weather_client_server_BME680
378 lines (307 loc) · 9.88 KB
/
fan_pwm_Weather_client_server_BME680
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//https://handyman.dulare.com/client-server-communication-using-esp8266/
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#define SEALEVELPRESSURE_HPA (1014.66)
Adafruit_BME680 bme; // I2C
//D1 -> //SCL/SCK
//D2 -> //SDA/SDI
const char* ssid = ""; // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = ""; // The password of the Wi-Fi network
const char* getHost = ""; // The data receiver host name (not URL)
const int httpGetPort = 80; // The data receiver host port
String getReceiverURL = "/"; // The data receiver script
int mysql_channel = 15;
int interruptCounter = 0; // counter of interrupt executions
int count_loop = 0;
float t_avg = 0;
float h_avg = 0;
float p_avg = 0;
float g_avg = 0;
int period = 180 ;
int t1 = 0;
int t2 = 0;
int send_data = 0;
int fanPulse = D3; //D3 I chose port D3 for the tachymeter signal, as it has a built in 10K Pullup resistor, which is needed for reading the tachymeter pulse.
int fanControl = D4; // OLD D2
unsigned long pulseDuration;
WiFiServer server(80); // set WiFiServer on port 80
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
while (!Serial);
Serial.println(F("BME680 test"));
if (!bme.begin(0x77))
{
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
pinMode(fanPulse, INPUT);
digitalWrite(fanPulse,HIGH);
pinMode(fanControl, OUTPUT);
analogWrite(fanControl,500);
delay(10);
Serial.println('\n');
WiFi.hostname("ESPboard-counter");
WiFi.begin(ssid, password); // Connect to the network
Serial.print("Connecting to ");
Serial.print(ssid); Serial.println(" ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++i); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
// start HTTP server
server.begin();
Serial.printf("Web server started, open %s in a web browser\n", WiFi.localIP().toString().c_str());
}
// send data to the receiver host
void postData(float t, float h, float p_avg, float g_avg) {
WiFiClient clientGet;
double frequency = readPulse();
// We now create and add parameters:
String src = "ESP";
int vint = interruptCounter;
String postStr = "?esp";
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field_measure1=1";
postStr +="&field_measure2=2";
postStr +="&field_measure4=4";
postStr +="&field_measure5=5";
postStr +="&field_measure3=3";
postStr +="&field_measure9=9";
postStr +="&channel=";
postStr += String(mysql_channel);
postStr +="&ip=";
postStr += String(WiFi.localIP().toString().c_str());
postStr +="&field4=";
postStr += String(frequency/4);
postStr +="&field5=";
postStr += String(frequency/4*60);
postStr +="&field3=";
postStr += String(p_avg);
postStr +="&field9=";
postStr += String(g_avg);
String getReceiverURLtemp = getReceiverURL + postStr;
Serial.println("-------------------------------");
Serial.print(">>> Connecting to host: ");
Serial.println(getHost);
Serial.println(getReceiverURLtemp);
if (!clientGet.connect(getHost, httpGetPort)) {
Serial.print("Connection failed: ");
Serial.print(getHost);
} else {
clientGet.print("GET " + getReceiverURLtemp + " HTTP/1.1\r\n" +
"Host: " + getHost + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeoutP = millis();
while (clientGet.available() == 0) {
if (millis() - timeoutP > 10000) {
Serial.print(">>> Client Timeout: ");
Serial.println(getHost);
clientGet.stop();
return;
}
}
//just checks the 1st line of the server response. Could be expanded if needed.
while(clientGet.available()){
String retLine = clientGet.readStringUntil('\r');
Serial.print(">>> Host returned: ");
Serial.println(retLine);
// reset counter if successully connected
if (retLine == "HTTP/1.1 200 OK") {
Serial.println(">>> Communication successful");
interruptCounter -= vint;
Serial.print(">>> Changed interrupt counter to: ");
Serial.println(interruptCounter);
} else {
Serial.println(">>> Communication failed!!!");
}
break;
}
} //end client connection if else
Serial.print(">>> Closing host: ");
Serial.println(getHost);
clientGet.stop();
}
// prepare a web page to be send to a client (web browser)
String prepareHtmlPage()
{
String htmlPage =
String("HTTP/1.1 200 OK\r\n") +
"Content-Type: text/html\r\n" +
"Connection: close\r\n" + // the connection will be closed after completion of the response
"Refresh: 30\r\n" + // refresh the page automatically every 5 sec
"\r\n" +
"<!DOCTYPE HTML>" +
"<html><body>" +
"<p>Interrupt counter: " + String(interruptCounter) + "</p>" +
"<p>Current time: " + String(millis()) + "</p>" +
"<br><br>"+
"Click <a href=\"/fan=high\">here</a> fan high<br>"+
"<br><br>"+
"Click <a href=\"/fan=mid\">here</a> fan mid<br>"+
"<br><br>"+
"Click <a href=\"/fan=normal\">here</a> fan normal<br>"+
"<br><br>"+
"Click <a href=\"/fan=low\">here</a> fan lowh<br>"+
"<br><br>"+
"Click <a href=\"/send_data=1\">here</a> send data<br>"+
"</body></html>" +
"\r\n";
return htmlPage;
}
// handle HTTP request to this board
void handle_request(WiFiClient client) {
Serial.println("\n[Client connected]");
while (client.connected())
{
// read line by line what the client (web browser) is requesting
if (client.available())
{
String line = client.readStringUntil('\r');
Serial.println("URL: ");
Serial.print(line);
Serial.println("");
if (line.indexOf("/fan=high") != -1) {
speedFan(1200);
Serial.println("HIGH");
client.println(prepareHtmlPage());
break;
}
if (line.indexOf("/fan=mid") != -1) {
speedFan(700);
Serial.println("MID");
client.println(prepareHtmlPage());
break;
}
if (line.indexOf("/fan=normal") != -1) {
speedFan(500);
Serial.println("Normal");
client.println(prepareHtmlPage());
break;
}
if (line.indexOf("/fan=low") != -1) {
speedFan(200);
Serial.println("LOW");
client.println(prepareHtmlPage());
break;
}
if (line.indexOf("/setspeed=") != -1) {
int t1 = line.indexOf("/setspeed=");
String rz_speed = line.substring(t1+10);
t1 = rz_speed.indexOf(" ");
rz_speed = rz_speed.substring(0,t1);
delay(100);
Serial.println("NEW SPEED:");
Serial.println(rz_speed);
speedFan(rz_speed.toInt());
Serial.println(rz_speed);
client.println(prepareHtmlPage());
break;
}
if (line.indexOf("/send_data=1") != -1) {
send_data = 1;
client.println(prepareHtmlPage());
break;
}
// wait for end of client's request, that is marked with an empty line
if (line.length() == 1 && line[0] == '\n')
{
client.println(prepareHtmlPage());
break;
}
}
}
delay(100); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("[Client disonnected]");
}
void speedFan(int speed_fan){
Serial.println("speed fan:");
Serial.print(speed_fan);
readPulse();
analogWrite(fanControl,speed_fan);
readPulse();
}
int readPulse() {
pulseDuration = pulseIn(fanPulse, LOW);
double frequency = 1000000/pulseDuration;
Serial.print("pulse duration:");
Serial.println(pulseDuration);
Serial.print("time for full rev. (microsec.):");
Serial.println(pulseDuration*2);
Serial.print("freq. (Hz):");
Serial.println(frequency/2);
Serial.print("RPM:");
Serial.println(frequency/2*60);
return frequency;
}
// main loop of the board
void loop() {
unsigned long current_time = millis();
Serial.println(count_loop);
Serial.println("");
if (! bme.performReading())
{
Serial.println("Failed to perform reading :(");
return;
}
Serial.print("Temperature = ");
Serial.print(bme.temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.pressure / 100.0);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.humidity);
Serial.println(" %");
Serial.print("Gas = ");
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(" KOhms");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.println();
delay(2000);
count_loop++;
t_avg = t_avg+bme.temperature;
h_avg = h_avg+bme.humidity;
p_avg = p_avg+(bme.pressure / 100.0);
g_avg = g_avg+(bme.gas_resistance / 1000.0);
if(count_loop == period || send_data == 1){
t_avg = t_avg/count_loop;
h_avg = h_avg/count_loop;
p_avg = p_avg/count_loop;
g_avg = g_avg/count_loop;
postData(t_avg,h_avg,p_avg,g_avg);
count_loop = 0;
t_avg = 0;
h_avg = 0;
p_avg = 0;
g_avg = 0;
send_data = 0;
}
// wait for a client (web browser) to connect
WiFiClient client = server.available();
if (client){
handle_request(client);
}
//delay(20000);
delay(100);
}