DS18b20 temperature sensor with ESP-01 module for ioBroker

In this article, I will show that we can measure temperature using the Dallas DS18b20 OneWire thermometer sensor and transfer data with the ESP8266-01 module to ioBroker using the MQTT protocol.


Things used:

ESP8266-Esp-01 Module

ESP-01S Breadboard Adapter

Programmer USB to ESP-01 Adapter ESP8266

DS18B20 Digital Thermometer Temperature Sensor

FT232RL FTDI USB to TTL Serial Converter 3.3V 5.5V

Resistor Kit Assortment, 0 Ohm-1M Ohm


The ESP8266-01 module is a small, cost-effective module with 2 pins for the serial data line and 2 general purpose I / O. they can be used to use i2c or OneWire devices. We are now using the latter to connect the DS18b20 temperature sensor.

We need another 4.7K resistor, the data line needs to be pulled up to the power supply.

Attention! The z ESP8266-01 module can only withstand 3.3V, it does not tolerate 5V well, the smoke comes out immediately.

Let’s see the connection:

ESP8266-01 and DS18b20
ESP8266-01 and DS18b20

If you don’t already have it, be sure to install the required directories and then upload the following Arduino sketch:

/****************************************************************/

//  https://myhomethings.eu
//  Board: Generic ESP8266 module
//  Flash size: 1M (no SPIFFS)

/****************************************************************/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>


const char* ssid = "ssid";
const char* password = "Password";
const char* mqtt_server = "MQTT.Server.IP.Address";

unsigned long previousMillis = 0;
unsigned long lastMsg = 0;
char msg[25];

OneWire oneWire(2);
DallasTemperature sensors(&oneWire);
WiFiClient espClient;
PubSubClient client(espClient);

void setup() 
{
  sensors.begin();
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  client.setServer(mqtt_server, 1883);
}

void loop() 
{
  if (!client.connected()) 
  {
    reconnect();
  }
  client.loop();

  unsigned long Millis = millis();
  if (Millis - lastMsg > 30000)
  {
    lastMsg = Millis;
    char MsgTemp[10];
    sensors.requestTemperatures();
    float h = (sensors.getTempCByIndex(0));
    dtostrf(h, 4, 2, MsgTemp);
    client.publish("DS18b20_topic", MsgTemp);
  }
}

void reconnect() 
{
  while (!client.connected()) 
  {
    String clientId = "ESP8266_DS18b20";
    if (client.connect(clientId.c_str()))
    {
      // pass
    } 
    else 
    {
      delay(6000);
    }
  }
}

The ESP-01 does not have an on-board serial USB interface circuit, so this must be prepared separately as shown below. The ESP-01 GPIO 0 pin must be drawn to GND for programming! (blue wire in the picture below) This is not required for normal serial communication.

ESP8266-01 and the FTDI FT232RL module
ESP8266-01 and the FTDI FT232RL module

Or you can buy an adapter like this:

USB to ESP8266 ESP-01S from Amazon
USB to ESP8266 ESP-01S from Amazon (Ad)

You can find more interesting things here .