Temperature Sensor

The central heating system based on Raspberry Pi required a method to monitor the lounge temperature, prompting the exploration of the ESP8266 microcontroller due to the Raspberry Pi’s bulky nature. The ESP8266 in Feather format, being compact, was chosen as it seamlessly fits into a small earthenware pot, blending well with the lounge decor, and features Wi-Fi connectivity for network access without the need for cumbersome wiring. Additionally, the ESP8266 can function as a web-server, enabling the creation of webpages, and utilises a DS18B20 to measure ambient temperature, displaying the data on a webpage for the Raspberry Pi to determine when to activate the boiler.

This of course has already been done by others. A good description can be had at Random Nerd Tutorials, at the following address: https://randomnerdtutorials.com/esp8266-ds18b20-temperature-sensor-web-server-with-arduino-ide/

This is the program I used:

// ESP8266 Temperature Relay
// Arduino Temperature Sensor Program
// ++++++++++++++++++++++++++++++++++
// + Sketch Name: temp-transmitterC +
// ++++++++++++++++++++++++++++++++++
// Sketch for reading temperature from DS18820
// and putting it on a webpage
// so that node-RED can read it
// (simpler than MQTT)
// Alex February 15th 2021

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DS18B20.h>

#ifndef STASSID
#define STASSID "BTHub5-8929"
#define STAPSK "8bc9278546"
#endif

// Set the Static IP address stuff
IPAddress local_IP(192, 168, 1, 214);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);


#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);
DS18B20 sensor(&oneWire);


const char* ssid = STASSID;
const char* password = STAPSK;

ESP8266WebServer server(80);

// const int led = 13;

float temperature = 0 ; //define variable temperature
// We now create a string to send over the internet.
// by converting the number temperature into the string page
// C++ has no command for converting a float into a string.
String getPage(){
String page = ""; // clear the string variable page
page = temperature;
return page;
}



void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}

void setup(void)
{

// Enter DS18B20 settings
sensor.setResolution(12);
sensor.requestTemperatures();

// Enter serial settings (USB)
Serial.begin(115200);

// Enter network settings
WiFi.hostname("ESP8266");
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); // Station (STA) mode is used to get ESP
//module connected to a Wi-Fi network established by an access point.

// Now wait for connection to intranet
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("."); // send dots to serial until connected
}
// we are now connected to the intranet

// start the server and the temperature sensor
server.on("/", handleRoot); // what to do when a user accesses our address
server.onNotFound(handleNotFound); // do this if page is not on site
server.begin(); // start the server
sensor.begin(); // start the DS18820 acquisition program
}

// the following happens when a client accesses 192.168.1.214
void handleRoot() {
server.send ( 20, "text/html", getPage() ); // 20 byte buffer Send the page
}

// all of setup is now complete so next
// we repeatedly do the following:
void loop(void) {
sensor.requestTemperatures(); //ask for a temperature
while (!sensor.isConversionComplete()); // wait until sensor data is valid
temperature = sensor.getTempC(); // read current temperature
Serial.println(temperature); // print CRLF followed by the temperature to the USB
delay(2000);
server.handleClient(); //deal with the client (if we have one)
}
// End of code
• ESP8266 Temperature Relay
• Function Nodes
• Home
• node-RED heating flow
• Target Temperature
/* This was compiled by the Mac on 28 Feb 2023 by Alex */
/* Thanks to */
/* https://iotstarters.com/temperature-monitoring-with-esp-dash-webserver/ */
#include <Arduino.h>
#if defined(ESP8266)
/* ESP8266 Dependencies */
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#elif defined(ESP32)
/* ESP32 Dependencies */
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#endif
#include <ESPDash.h>
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to GPIO 13
#define ONE_WIRE_BUS 13

// 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);

// NETWORK CREDENTIALS
// (enclose ssid and p/w using inverted commas only)
const char* ssid = "< internet hub name >";
const char* password = "< password to log into router >";


/* Start Webserver */
AsyncWebServer server(80);
/* Attach ESP-DASH to AsyncWebServer */
ESPDash dashboard(&server);

Card tempC(&dashboard, TEMPERATURE_CARD, "Temperature in C", "°C");
/* Card tempF(&dashboard, TEMPERATURE_CARD, "Temperature in F", "°F"); */

void setup() {
Serial.begin(115200);
/* Connect WiFi */
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Connection Failed!\n");
return;
}
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
/* Start AsyncWebServer */
server.begin();
Serial.println("Filename is ESP8266.B.works. It just prints the temperature as numbers down the seriaL port.");
}

void loop()
{
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
/* Serial.print("Temp. in Celsius: "); */
Serial.println(temperatureC);

/* float temperatureF = sensors.getTempFByIndex(0); */
/* Serial.print("Temp. in Fahrenheit: "); */
/* bSerial.println(temperatureF); */

/* Update Card Values */
tempC.update(temperatureC,"°C");
/* tempF.update(temperatureF,"°F"); */
/* Send Updates to our Dashboard (realtime) */
dashboard.sendUpdates();
delay(3000);
}