-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp32-turbidity.ino
73 lines (66 loc) · 1.75 KB
/
esp32-turbidity.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
/*
ESP32 Turbidity Sensor
by Roland Pelayo
for TeachMeMicro
Rev 1.0 - May 22, 2020
Full tutorial on https://www.teachmemicro.com/esp32-turbidity-Sensor
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include "mainpage.h"
#include "jscript.h"
//provide your own WiFi SSID and password
const char* ssid = "<your WiFI SSID>";
const char* password = "<your WiFi Password>";
WebServer server(80);
//For storing data as string
String text= "";
//Sensor data
double turbidity;
void setup(void) {
//For debugging
Serial.begin(9600);
//Use ESP32 as WiFi Station
WiFi.mode(WIFI_STA);
//Initiate WiFi Connection
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
//Print your WiFi's SSID (might be insecure)
Serial.println(ssid);
Serial.print("IP address: ");
//Print your local IP address (needed for browsing the app)
Serial.println(WiFi.localIP());
//Page for reading data. Sensor is read in this part
server.on("/data", [](){
delay(100);
int val = analogRead(36);
turbidity = map(val, 0, 2800, 5, 1);
text = (String)turbidity;
Serial.println(val);
server.send(200, "text/plain", text);
});
//Home page. Contents of 'page' is in mainpage.h
server.on("/", []() {
server.send(200, "text/html", page);
});
//JavaScript! Contents of 'javascript' is in jscript.h
server.on("/jscript.js", []() {
server.send(200, "text/javascript", javascript);
});
//start web server
server.begin();
//Just stating things
Serial.println("HTTP server started");
}
void loop(void) {
//Make the ESP32 always handle web clients
server.handleClient();
}