NodeMCU ESP32 - Code Page


//=================================================================================================================================
// ESP CODE
//---------------------------------------------------------------------------------------------------------------------------------
// *** Import required libraries
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// *** OneWire databus is connected to GPIO 4
#define ONE_WIRE_BUS 4

// *** Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// *** Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

// *** Variables to store temperature values
String temperatureC = "";
String temperatureF = "";

// *** Timer variables
unsigned long lastTime = 0;  
unsigned long timerDelay = 30000;

// *** Replace ssid and password with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

// *** Create AsyncWebServer object on port 80
AsyncWebServer server(80);

// *** Read temperature in degrees Celsius
String readDSTemperatureC() {
  // Call sensors.requestTemperatures() to issue a global temperature 
  // and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  float tempC = sensors.getTempCByIndex(0);
  // In case the sensor is not able to get a valid reading, it returns -127. 
  // So, we have an if statement that returns two dashes (–-) in case the 
  // sensor fails to get the readings.
  if(tempC == -127.00) {
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Celsius: ");
    Serial.println(tempC); 
  }
  return String(tempC);
}

// *** Read temperature in degrees Fahrenheit
String readDSTemperatureF() {
  // Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures(); 
  float tempF = sensors.getTempFByIndex(0);
  // In case the sensor is not able to get a valid reading, it returns -196. 
  // So, we have an if statement that returns two dashes (–-) in case the 
  // sensor fails to get the readings.
  if(int(tempF) == -196){
    Serial.println("Failed to read from DS18B20 sensor");
    return "--";
  } else {
    Serial.print("Temperature Fahrenheit: ");
    Serial.println(tempF);
  }
  return String(tempF);
}

//=================================================================================================================================
// Web Page
//---------------------------------------------------------------------------------------------------------------------------------
// *** Build the web page
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 DS18B20 Server</h2>
    <p>
      <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
      <span class="ds-labels">Temperature Celsius</span>
      <span id="temperaturec">%TEMPERATUREC%</span>
      <sup class="units">°C</sup>
    </p>
    <p>
      <i class="fas fa-thermometer-half" style="color:#059e8a;"></i> 
      <span class="ds-labels">Temperature Fahrenheit</span>
      <span id="temperaturef">%TEMPERATUREF%</span>
      <sup class="units">°F</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("temperaturef").innerHTML = this.responseText;
      }
    };
    xhttp.open("GET", "/temperaturef", true);
    xhttp.send();
  }, 10000) ;
  </script>
</html>)rawliteral";


//
//=================================================================================================================================
// ESP CODE continues
//---------------------------------------------------------------------------------------------------------------------------------
// *** Processor replaces placeholder with DS18B20 values
String processor(const String& var){
  //Serial.println(var);
  if(var == "TEMPERATUREC"){
    return temperatureC;
  }
  else if(var == "TEMPERATUREF"){
    return temperatureF;
  }
  return String();
}

// *** Setup routine
void setup(){
  Serial.begin(115200); // Serial port for debugging purposes
  Serial.println();
   
  sensors.begin();      // Start up the DS18B20 library

  temperatureC = readDSTemperatureC();
  temperatureF = readDSTemperatureF();

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.println("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  
  Serial.println(WiFi.localIP()); // Print ESP Local IP Address

  // Routes for root / web page
  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", temperatureC.c_str());
  });
  server.on("/temperaturef", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", temperatureF.c_str());
  });
  
  server.begin(); // Start server
} // setup()

// *** main loop
void loop(){
  if ((millis() - lastTime) > timerDelay) {
    temperatureC = readDSTemperatureC();
    temperatureF = readDSTemperatureF();
    lastTime = millis();
  }  
} // main loop()
//=================================================================================================================================
// ESP CODE ends
//---------------------------------------------------------------------------------------------------------------------------------