Created
May 2, 2020 14:43
-
-
Save MDevolution/b64de8db4b06d0069c3ca9b63f3a7f3f to your computer and use it in GitHub Desktop.
Wemos + Si7010 - Simple WebServer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <Hash.h> | |
#include <ESPAsyncTCP.h> | |
#include <ESPAsyncWebServer.h> | |
#include "Adafruit_Si7021.h" | |
Adafruit_Si7021 sensor = Adafruit_Si7021(); | |
// Replace with your network credentials | |
const char* ssid = ""; | |
const char* password = ""; | |
// Create AsyncWebServer object on port 80 | |
AsyncWebServer server(80); | |
//============================================================ | |
// Read Temperature Function | |
//============================================================ | |
String readTemperature() { | |
float temp = sensor.readTemperature(); | |
Serial.print("Temperature: "); | |
Serial.println(temp); | |
return String(temp); | |
} | |
String readHumidity() { | |
float hum = sensor.readHumidity(); | |
Serial.print("Humidity: "); | |
Serial.println(hum); | |
return String(hum); | |
} | |
//============================================================ | |
// WebServer HTML | |
//============================================================ | |
const char index_html[] PROGMEM = R"rawliteral( | |
<!DOCTYPE HTML><html> | |
<head> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> | |
<style> | |
html { | |
font-family: Arial; | |
display: inline-block; | |
margin: 0px auto; | |
text-align: center; | |
} | |
h2 { font-size: 3.0rem; } | |
p { font-size: 3.0rem; } | |
.units { font-size: 1.2rem; } | |
.ds-labels{ | |
font-size: 1.5rem; | |
vertical-align:middle; | |
padding-bottom: 15px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>ESP Si7021 WebServer</h2> | |
<p> | |
<i class="fas fa-thermometer-half" style="color:#d69400;"></i> | |
<span class="ds-labels">Temperature </span><br> | |
<span id="temperaturec">%TEMPERATURE%</span> | |
<sup class="units">°C</sup> | |
</p> | |
<p> | |
<i class="fas fa-tint" style="color:#00add6;"></i> | |
<span class="ds-labels">Humidity </span><br> | |
<span id="humidity">%HUMIDITY%</span> | |
<sup class="units">%</sup> | |
</p> | |
</body> | |
<script> | |
setInterval(function ( ) { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
document.getElementById("temperaturec").innerHTML = this.responseText; | |
} | |
}; | |
xhttp.open("GET", "/temperaturec", true); | |
xhttp.send(); | |
}, 10000) ; | |
setInterval(function ( ) { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
document.getElementById("humidity").innerHTML = this.responseText; | |
} | |
}; | |
xhttp.open("GET", "/humidity", true); | |
xhttp.send(); | |
}, 10000 ) ; | |
</script> | |
</html>)rawliteral"; | |
// Replaces placeholder with DHT values | |
String processor(const String& var){ | |
//Serial.println(var); | |
if(var == "TEMPERATURE"){ | |
return readTemperature(); | |
}else if(var == "HUMIDITY"){ | |
return readHumidity(); | |
} | |
return String(); | |
} | |
void setup(){ | |
// Serial port for debugging purposes | |
Serial.begin(115200); | |
Serial.println(); | |
sensor.begin(); | |
// Connect to Wi-Fi | |
WiFi.begin(ssid, password); | |
Serial.println("Connecting to WiFi"); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(); | |
// Print ESP Local IP Address | |
Serial.println(WiFi.localIP()); | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send_P(200, "text/html", index_html, processor); | |
}); | |
server.on("/temperaturec", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send_P(200, "text/plain", readTemperature().c_str()); | |
}); | |
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){ | |
request->send_P(200, "text/plain", readHumidity().c_str()); | |
}); | |
// Start server | |
server.begin(); | |
} | |
void loop(){ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment