Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_AHTX0.h>
- Adafruit_AHTX0 aht;
- #include <Arduino.h>
- #include <ESP8266WiFi.h>
- #include <Hash.h>
- #include <ESPAsyncTCP.h>
- #include <ESPAsyncWebServer.h>
- #include <Adafruit_Sensor.h>
- #include <GyverOLED.h>
- GyverOLED<SSH1106_128x64> oled;
- const char* ssid = "YOUR WIFI SSID";
- const char* password = "YOUR WIFI PASSWORD";
- float t = 0.0;
- float h = 0.0;
- AsyncWebServer server(80);
- unsigned long previousMillis = 0;
- const long interval = 10000;
- const char index_html[] PROGMEM = R"rawliteral(
- <!DOCTYPE HTML><html>
- <head>
- <meta charset=utf-8>
- <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; }
- .dht-labels{
- font-size: 1.5rem;
- vertical-align:middle;
- padding-bottom: 15px;
- }
- </style>
- </head>
- <body>
- <style>
- body {
- background-image: url('https://i.imgur.com/U6G3O4C.jpg');
- background-repeat: no-repeat;
- background-attachment: fixed;
- background-size: cover;
- color: white;
- text-shadow: 0 0 18px #000000, 0 0 18px #000000;
- }
- </style>
- <h1>
- <h2>Cameră mare</h2>
- <p>
- <i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
- <span class="dht-labels">Temperatură</span>
- <span id="temperature">%TEMPERATURE%</span>
- <sup class="units">°C</sup>
- </p>
- <p>
- <i class="fas fa-tint" style="color:#00add6;"></i>
- <span class="dht-labels">Umiditate</span>
- <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("temperature").innerHTML = this.responseText;
- }
- };
- xhttp.open("GET", "/temperature", 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";
- String processor(const String& var){
- if(var == "TEMPERATURE"){
- return String(t);
- }
- else if(var == "HUMIDITY"){
- return String(h);
- }
- return String();
- }
- IPAddress local_IP(192, 168, 0, 202);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- IPAddress primaryDNS(193, 231, 236, 25);
- IPAddress secondaryDNS(193, 231, 236, 30);
- void setup() {
- delay(2000);
- Serial.begin(115200);
- oled.init();
- oled.clear();
- oled.update();
- aht.begin();
- if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
- Serial.println("STA Failed to configure");
- }
- Serial.print("Connecting to ");
- oled.clear();
- oled.setScale(1);
- oled.setCursor(10, 1);
- oled.print("connecting to wifi");
- oled.update();
- oled.setCursor(1, 4);
- oled.setScale(2);
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- oled.print(">");
- oled.update();
- delay(500);
- }
- Serial.println(WiFi.localIP());
- oled.clear();
- oled.setScale(1);
- oled.setCursor(28, 3);
- oled.print("CONNECTED TO");
- oled.setCursor(25, 5);
- oled.print(WiFi.localIP());
- oled.update();
- delay(2000);
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/html", index_html, processor);
- });
- server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", String(t).c_str());
- });
- server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
- request->send_P(200, "text/plain", String(h).c_str());
- });
- server.begin();
- }
- void loop() {
- oled.home();
- sensors_event_t humidity, temp;
- aht.getEvent(&humidity, &temp);
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- if (WiFi.status() != WL_CONNECTED)
- {
- WiFi.disconnect();
- oled.clear();
- oled.setScale(1);
- oled.setCursor(10, 1);
- oled.print("connecting to wifi");
- oled.update();
- oled.setCursor(1, 4);
- oled.setScale(2);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- oled.print(">");
- oled.update();
- delay(500);
- }
- Serial.println(WiFi.localIP());
- oled.clear();
- oled.setScale(1);
- oled.setCursor(28, 3);
- oled.print("CONNECTED TO");
- oled.setCursor(25, 5);
- oled.print(WiFi.localIP());
- oled.update();
- delay(2000);
- }
- float newT = temp.temperature;
- if (isnan(newT)) {
- Serial.println("Failed to read from AHT sensor!");
- }
- else {
- t = newT;
- Serial.println(t);
- oled.clear();
- oled.setScale(3);
- oled.setCursor(8, 1);
- oled.print(t);
- oled.setScale(1);
- oled.print("*");
- oled.setScale(3);
- oled.print("C");
- oled.update();
- }
- float newH = humidity.relative_humidity;
- if (isnan(newH)) {
- Serial.println("Failed to read from AHT sensor!");
- }
- else {
- h = newH;
- Serial.println(h);
- oled.setScale(3);
- oled.setCursor(8, 5);
- oled.print(h);
- oled.setScale(1);
- oled.print(" ");
- oled.setScale(3);
- oled.print("%");
- oled.update();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement