Superfunk2000
Published © GPL3+

ESP826 DS18B20 Server

Scan the OneWire bus for DS18B20 temperature sensors, and delivers the data serial, via web server and MQTT.

BeginnerFull instructions provided288
ESP826 DS18B20 Server

Things used in this project

Story

Read more

Schematics

Wiring

Code

ESP DS18B20 Temperatur Server

Arduino
/*
  ESP DS18B20 Temperatur Server by joerg Mahn
 Scan the OneWire bus for DS18B20 temperature sensors,
 and delivers the data serial, via web server and MQTT.

  Created: 01.12.2019
*/

#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <MQTT.h>

// GPIO Pin
#define ONE_WIRE_BUS 4

const int  max_sensors    = 30;
const char *modul         = "DS18B20 Server";
const char *zone          = "Dachgeschoss";
const char *ssid          = "************";
const char *password      = "************";
const char *topic         = "home/temp/dg/";
const char *MQTTServer    = "************";
const char *MQTTUsername  = "************";
const char *MQTTPassword  = "************";

char arr_room[5][15] = {
	"Raum01",	// Index 0
	"Raum02",	// Index 1
	"Raum03",	// Index 2
	"Raum04",	// Index 3
	"Raum05"	// Index 4
};

const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
	<meta http-equiv="refresh" content="10">
	<title>%modul% (%zone%)</title>
	<style>
		html {
			font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
			border-collapse: collapse;
		}
		td, th {
			border: 1px solid #ddd;
			padding: 8px;
		}
		tr:nth-child(even){
			background-color: #f2f2f2;
		}
		tr:hover {
			background-color: #ddd;
		}
		th {
			padding-top: 12px;
			padding-bottom: 12px;
			text-align: left;
			background-color: #4CAF50;
			color: white;
		}
	</style>
</head>
<body>
	<center>
		<h2>ESP DS18B20 Server</h2>
		<h2>Zone: %zone%</h2>
		<table>
			<tr><th>Index</th><th>Adresse</th><th>Raum</th><th>Temperatur</th></tr>
			<tr><td>%i0%</td><td>%a0%</td><td>%r0%</td><td>%t0%</td></tr>
			<tr><td>%i1%</td><td>%a1%</td><td>%r1%</td><td>%t1%</td></tr>
			<tr><td>%i2%</td><td>%a2%</td><td>%r2%</td><td>%t2%</td></tr>
			<tr><td>%i3%</td><td>%a3%</td><td>%r3%</td><td>%t3%</td></tr>
			<tr><td>%i4%</td><td>%a4%</td><td>%r4%</td><td>%t4%</td></tr>
		</table>
	</center>
</body>
</html>)rawliteral";

// Configuration OneWire
float arr_temperatur[max_sensors];
DeviceAddress arr_address[max_sensors];
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int numberOfDevices;

// Configuration MQTT
char status[30] = "";
WiFiClient MQTTnet;
MQTTClient MQTT;

// Configuration Webserver
char MACAdresse[18] = "";
AsyncWebServer server(80);

float readDSTemperatureC(int index) {
	if (index < numberOfDevices) {
		sensors.requestTemperatures(); 
		float tempC = sensors.getTempCByIndex(index);
		delay(100);
		if(tempC == -127.00) return 85;
		return tempC;
	}
}

String processor(const String& var){
	char return_msg[40];
	char char_temp[6];
	char char_add[17];
	for (int i=0; i<numberOfDevices;i++) {
    // Index
	if (var == "i" + (String) i) {
		return (String) i;
	}
	// Sensor-Address
	if (var == "a" + (String) i) {
		char buffer[20];
		array_to_string(arr_address[i],8,buffer);
		return (String) buffer;
	}
	// Room-Name
	if (var == "r" + (String) i) {    
		return (String) arr_room[i];
	}
	// Temperature
	if (var == "t" + (String) i) {    
		dtostrf(arr_temperatur[i], 2, 1, char_temp);
		return (String) char_temp + " &deg;C"; 
	}
	if (var == "zone") return zone;
	if (var == "modul") return modul;
  }
}

void printAddress(DeviceAddress deviceAddress) {
	for (uint8_t i = 0; i < 8; i++){
	if (deviceAddress[i] < 16) Serial.print("0");
		Serial.print(deviceAddress[i], HEX);
	}
}

void array_to_string(byte array[], unsigned int len, char buffer[]) {
    for (unsigned int i = 0; i < len; i++) {
        byte nib1 = (array[i] >> 4) & 0x0F;
        byte nib2 = (array[i] >> 0) & 0x0F;
        buffer[i*2+0] = nib1  < 0xA ? '0' + nib1  : 'A' + nib1  - 0xA;
        buffer[i*2+1] = nib2  < 0xA ? '0' + nib2  : 'A' + nib2  - 0xA;
    }
    buffer[len*2] = '\0';
}

void MQTTconnect() {
	Serial.println(F("\nConnecting to MQTT Broker..."));
	if (MQTT.connect(MACAdresse, MQTTUsername, MQTTPassword)) {
		Serial.println(F("Connected"));
		MQTT.publish(status, "online");
	} else {
		Serial.println(F("Error!"));
		MQTT.publish(status, "error");
	}
	Serial.println();
}

void setup(){
	Serial.begin(9600);
	delay(1000);
	Serial.println();
	Serial.println(modul);
	Serial.print(F("Zone: ")); Serial.println(zone);

	// Verbinde WLAN
	byte mac[6];
	WiFi.begin(ssid, password);
	Serial.print(F("Searching WLAN ")); Serial.print(ssid); Serial.print(F("."));
	while (WiFi.status() != WL_CONNECTED) {
		delay(500);
		Serial.print(F("."));
	}
	Serial.println();
	WiFi.macAddress(mac);
	Serial.print(F("MAC-Address: ")); array_to_string(mac, 6, MACAdresse); Serial.println(MACAdresse);
	Serial.print(F("IP-Address: ")); Serial.println(WiFi.localIP());
	Serial.println();

	// Starte DS18B20
	Serial.println(F("Searching DS18B20 Devices..."));
	sensors.begin();
	numberOfDevices = sensors.getDeviceCount();
	Serial.print(numberOfDevices, DEC); Serial.println(" Devices found:");

  // Ermittele Sensoren-Adressen
	for(int i=0;i<numberOfDevices; i++){
		// Search the wire for address
		if(sensors.getAddress(arr_address[i], i)){
			Serial.print(F("Device "));
			Serial.print(i, DEC);
			Serial.print(F(" with "));
			printAddress(arr_address[i]);
			Serial.println();
		} else {
			Serial.print(F("Ghost Device "));
			Serial.print(i, DEC);
			Serial.print(F(" found, but without Address."));
		}
	}

	// Indexing Webserver and start
	server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
		request->send_P(200, "text/html", index_html, processor);
	});
	server.begin();

	// Connecting MQTT Brooker
	MQTT.begin(MQTTServer, MQTTnet);
	// Last will and testament
	strcpy(status, topic); strcat(status, "status");
	MQTT.setWill(status, "offline", true, 0);
	MQTTconnect();
}
 
void loop(){
	char fulltopic[50];
	char char_temp[6];

	MQTT.loop();
	if (!MQTT.connected()) MQTTconnect();
	for (int i=0; i<numberOfDevices;i++) {

		// get Temperature
		sensors.getAddress(arr_address[i], i);
		arr_temperatur[i] = readDSTemperatureC(i);
		dtostrf(arr_temperatur[i], 3, 1, char_temp);
		delay(750);

		// Serial Output
		Serial.print(F("Index: "));Serial.print(i);
		Serial.print(F(", Adresse: ")); printAddress(arr_address[i]);
		Serial.print(F(", Raum: ")); Serial.print(arr_room[i]);
		Serial.print(F(", Temperatur: ")); Serial.print(arr_temperatur[i]); Serial.println(F(" C"));

		// publish to MQTT Brooker
		strcpy(fulltopic, topic); strcat(fulltopic, arr_room[i]);
		MQTT.publish(fulltopic, char_temp);
	}
	Serial.println("");
	delay(10000);
}

Credits

Superfunk2000

Superfunk2000

0 projects • 4 followers

Comments