-
Notifications
You must be signed in to change notification settings - Fork 5
/
ESP32_Web_Server.ino
163 lines (131 loc) · 3.55 KB
/
ESP32_Web_Server.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
/////////////////////////////////////////////////////////////////
// ESP32 Web Server Project v1.00 //
// Get the latest version of the code here: //
// http://educ8s.tv/esp32-web-server //
/////////////////////////////////////////////////////////////////
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h> //https://github.com/bbx10/WebServer_tng
#include "Adafruit_BME280.h" //https://github.com/Takatsuki0204/BME280-I2C-ESP32
WebServer server ( 80 );
const char* ssid = "yourSSID";
const char* password = "yourPassword";
#define ALTITUDE 216.0 // Altitude in Sparta, Greece
#define I2C_SDA 27
#define I2C_SCL 26
#define BME280_ADDRESS 0x76 //If the sensor does not work, try the 0x77 address as well
int LEDPIN = 32;
float temperature = 0;
float humidity = 0;
float pressure = 0;
String ledState = "OFF";
Adafruit_BME280 bme(I2C_SDA, I2C_SCL);
void setup()
{
pinMode(LEDPIN, OUTPUT);
Serial.begin(9600);
initSensor();
connectToWifi();
beginServer();
}
void loop() {
server.handleClient();
getTemperature();
getHumidity();
getPressure();
delay(1000);
}
void connectToWifi()
{
WiFi.enableSTA(true);
delay(2000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void beginServer()
{
server.on ( "/", handleRoot );
server.begin();
Serial.println ( "HTTP server started" );
}
void handleRoot(){
if ( server.hasArg("LED") ) {
handleSubmit();
} else {
server.send ( 200, "text/html", getPage() );
}
}
void initSensor()
{
bool status = bme.begin(BME280_ADDRESS);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
}
float getTemperature()
{
temperature = bme.readTemperature();
}
float getHumidity()
{
humidity = bme.readHumidity();
}
float getPressure()
{
pressure = bme.readPressure();
pressure = bme.seaLevelForAltitude(ALTITUDE,pressure);
pressure = pressure/100.0F;
}
void handleSubmit() {
String LEDValue;
LEDValue = server.arg("LED");
Serial.println("Set GPIO ");
Serial.print(LEDValue);
if ( LEDValue == "1" ) {
digitalWrite(LEDPIN, HIGH);
ledState = "On";
server.send ( 200, "text/html", getPage() );
}
else if( LEDValue == "0" )
{
digitalWrite(LEDPIN, LOW);
ledState = "Off";
server.send ( 200, "text/html", getPage() );
} else
{
Serial.println("Error Led Value");
}
}
String getPage(){
String page = "<html lang=en-EN><head><meta http-equiv='refresh' content='60'/>";
page += "<title>ESP32 WebServer - educ8s.tv</title>";
page += "<style> body { background-color: #fffff; font-family: Arial, Helvetica, Sans-Serif; Color: #000000; }</style>";
page += "</head><body><h1>ESP32 WebServer</h1>";
page += "<h3>BME280 Sensor</h3>";
page += "<ul><li>Temperature: ";
page += temperature;
page += "°C</li>";
page += "<li>Humidity: ";
page += humidity;
page += "%</li>";
page += "<li>Barometric Pressure: ";
page += pressure;
page += " hPa</li></ul>";
page += "<h3>GPIO</h3>";
page += "<form action='/' method='POST'>";
page += "<ul><li>LED";
page += "";
page += "<INPUT type='radio' name='LED' value='1'>ON";
page += "<INPUT type='radio' name='LED' value='0'>OFF</li></ul>";
page += "<INPUT type='submit' value='Submit'>";
page += "</body></html>";
return page;
}