ESP8266 WiFi OneWire AP Client Server HTML Scheme Eagle DS18B20 Thermometer

Wifi thermometer in STA / AP mode with ESP8266


  • The thermometer uses the ESP8266 12E wifi chip on the NodeMCU v3 Lolin development board. In addition to obtaining data from temperature sensors, a Wifi chip is also held by a web server where it displays measured data. The data is always measured when the page is loaded. Inactivity data is not recorded. The web server allows you to run HTML + CSS code, which allows to some extent to make a nice graphical interface for drawing temperatures to the user, for example to a table.
  • There are two versions of this project, while the functionality of the website itself is the same. In both cases, the ESP8266 runs a webserver that draws temperatures into the table. Versions distinguish connectivity. In one case, it is possible to use an existing LAN network to which the board connects and a static or dynamic IP address is held by the webserver. After entering the IP into the browser window, the user gets to the desired page from ESP with temperatures, or when using mDNS it is also possible to use the hostname - domain name.
  • In the latter case, the board transmits its own SSID in the AP mode as an access point. The user accesses the board by entering the network password (included in ESP8266). The board broadcasts the SSID with WPA / WPA2 PSK encryption, or it is possible to broadcast an open wifi network. The data is only accessible within the range of this wifi network outside the LAN of the home. The ESP server also runs a DHCP server that, after successful user authentication, assigns an IP address from the range. The website is located at the gateway"s IP address.
  • Temperature data is recorded from a pair of DS18B20 temperature sensors that are connected to a OneWire bus that allows one-wire data collection with tens of wire to tens to hundreds of meters. DS18B20 are produced in two main versions - the so-called. interior sensor in the transistor housing, or in a waterproof - exterior design in an aluminum tube. OneWire is suitable for interference environments and can accommodate up to 2 56 sensors. Individual sensors are divided by their factory address - serial number. There are 2 main sensor connection options. Normal wiring and parasitic, both can be powered at 3.3 - 5.5V. In normal connection, the result is more reliable, especially for longer distances.
  • Differences are already apparent from the schematics:
    Project parameters:
  • NodeMCU in the role of access point (AP) / or as a client on your LAN
  • Custom SSID and WPA2 PSK encryption, or no encryption
  • Custom IP (static) option to extend the DNS flag
  • Secure website accessible in the ESP network and its impact with a repetitive design
  • Always when the page loads the current info
  • Other possible variations:

    ESP8266 + DS18B20 WiFi teplomer - WPA/WPA2 PSK Client - Webserver:

    /*|---------------------------------|*/
    /*|Projekt: WiFi teplomer - ESP8266 |*/
    /*|Autor: Martin Chlebovec          |*/
    /*|E-mail: martinius96@gmail.com    |*/
    /*|Web: https://arduino.php5.sk     |*/
    /*|Licencia pouzitia: MIT           |*/
    /*|---------------------------------|*/
    
    #include <ESP8266WiFi.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #define ONE_WIRE_BUS 2
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    const char* ssid  = "MenoWifi";
    const char* password = "Heslo";
    WiFiServer server(80);
    String header;
    
    void setup() {
      Serial.begin(115200);
      Serial.println(ip);
      Serial.print("Connecting to ");
      Serial.println(ssid);
      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());
      server.begin();
    }
    
    void loop(){
      WiFiClient client = server.available();
      if (client) {
        sensors.begin(); 
        sensors.requestTemperatures();  
        String currentLine = "";
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();           
            header += c;
            if (c == '\n') { 
              if (currentLine.length() == 0) {
                client.println("HTTP/1.1 200 OK");
                client.println("Content-type:text/html");
                client.println("Connection: close");
                client.println();
                client.println("<head><meta charset='UTF-8'><meta name='author' content='Martin Chlebovec'><meta name='viewport' content='width=device-width, initial-scale=1.0'></head><style>body{  display: table;width: 100%;background: #dedede;text-align: center;}*{ -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */-moz-box-sizing: border-box;    /* Firefox, other Gecko */box-sizing: border-box;         /* Opera/IE 8+ */}.aa_h2{  font:100 5rem/1 Roboto;  text-transform: uppercase;}table{   background: #fff;}table,thead,tbody,tfoot,tr, td,th{  text-align: center;  margin: auto;  border:1px solid #dedede;  padding: 1rem;  width: 50%;}.table    { display: table; width: 50%; }.tr       { display: table-row;  }.thead    { display: table-header-group }.tbody    { display: table-row-group }.tfoot    { display: table-footer-group }.col      { display: table-column }.colgroup { display: table-column-group }.td, .th   { display: table-cell; width: 50%; }.caption  { display: table-caption }.table,.thead,.tbody,.tfoot,.tr,.td,.th{ text-align: center; margin: auto; padding: 1rem;}.table{  background: #fff;  margin: auto;  border:none;  padding: 0; margin-bottom: 5rem;}.th{font-weight: 700;border:1px solid #dedede;&:nth-child(odd){border-right:none;}}.td{font-weight: 300;border:1px solid #dedede;border-top:none;&:nth-child(odd){border-right:none;}}.aa_htmlTable{background: tomato;padding: 5rem;display: table;width: 100%;height: 100vh;vertical-align: middle;}.aa_css{background: skyblue;padding: 5rem;display: table;width: 100%;height: 100vh;vertical-align: middle;}.aa_ahmadawais{display: table;width: 100%;font: 100 1.2rem/2 Roboto;margin: 5rem auto;}</style><div class='aa_htmlTable'><table width=100%><thead><tr><th>Teplota</th><th>Hodnota</th></tr></thead><tbody><tr><td>Dnu</td><td>"+(String)sensors.getTempCByIndex(0)+" °C</td></tr><tr><td>Von</td><td>"+(String)sensors.getTempCByIndex(1)+" °C</td></tr></tbody></table></div>");
                client.println();
                break;
              }else { // if you got a newline, then clear currentLine
                currentLine = "";
              }
              }else if (c != '\r') {  // if you got anything else but a carriage return character,
                currentLine += c;      // add it to the end of the currentLine
              }
            }
          }
      header = "";
      client.stop();
      Serial.println("");
      }
    }
    

    Schéma

    Schéma - WiFi teplomer - ESP8266

    Termostat - Tester - Offline

    //Projekt: Minimálna implementácia - termostat
    //Autor: Martin Chlebovec
    //Hardver: Arduino Uno + DS18B20 (1x OneWire bus)
    //Revizia: 23. Mar. 2020
    
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    #define ONE_WIRE_BUS 5 //datovy vovod OneWire zbernica na pin D5
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensorsA(&oneWire);
    float nastavena_teplota = 24.50;
    float hystereza = 0.50;
    //float hystereza2 = 0.50;
    const int rele = 6;
    //const int rele2 = 7;
    unsigned long timer = 0;
    unsigned long interval = 15000; //ako casto chceme logiku spustit --> v ms (milisekundy)
    void setup() {
      sensorsA.begin();
      Serial.begin(115200);
      Serial.println("UART ready");
      pinMode(rele, OUTPUT);
      // pinMode(rele2, OUTPUT);
    }
    
    void loop() {
      if ((millis() - timer) >= interval || timer == 0) {
        timer = millis();
        sensorsA.requestTemperatures();
        delay(2000); //cakame na teploty
        float  teplota1 = sensorsA.getTempCByIndex(0);
        //float  teplota2 = sensorsA.getTempCByIndex(1);
        float rozdiel = nastavena_teplota - teplota1;
        if (rozdiel > hystereza) {
          digitalWrite(rele, HIGH); //zopnutie rele
          Serial.println("Rele bolo zopnute - Vystup aktivny");
        } else if (rozdiel < (-1 * hystereza)) {
          digitalWrite(rele, LOW); //rozopnutie rele
          Serial.println("Rele bolo rozopnute - Vystup neaktivny");
        }
    
        /*
          float rozdiel2 = nastavena_teplota2 - teplota2;
          if (rozdiel2 > hystereza2) {
          digitalWrite(rele2, HIGH); //zopnutie rele
          } else if (rozdiel2 < (-1 * hystereza2)) {
          digitalWrite(rele2, LOW); //rozopnutie rele
          }
        */
      }
    }