ESP32 MicroPython project with several DS18B20 probes, publish the measurements to Domoticz using the HTTP JSON API

After several articles to present the principles of MicroPython on ESP8266 and ESP32 development board, it’s time to get down to business. I propose to take the Arduino reading code of several Dallas DS18B20 temperature measurement probes in MicroPython (previous article). Here, we will use the uPiCraft code editor already presented previously in this tutorial and this one (management of the WiFi connection). uPiCraft contains some add-ons including a module to scan the One-Wire bus and read the temperature very simply. We will also discover how to connect to a Domoticz server and publish the metrics. If you do not want to use uPiCraft, the DS18x20 module source code is available further down the article. If you do not have the MicroPython firmware installed on an ESP8266 or ESP32 card, follow this tutorial with the esptool.py script or this one with uPiCraft.

Necessary material

The code works was on ESP8266 as well as on ESP32. The One-Wire bus requires only one wire to communicate with the micro-controller. The code can retrieve the temperature of 2 Dallas DS18B20 probes but you can connect up to 100. It should be enough to cover an entire house!

Circuit with multiple Dallas DS18B20 probes

The connection is very simple. The One-Wire bus is usually located on a yellow wire. Red is used for power, black for GND as usual. For the bus to work, it must be “de-wormed”. For this, a resistor (4K7 in general) is placed between the + 5V and the data bus. I also tried other resistors (5K7) and a power supply + 3V3 successfully. Obviously, everything will depend on the cable length. The longer the cabling, the more rigorous it will be to supply the power and the de-interference of the signal.

For this tutorial, the DS18B20 is plugged into the D4 pin of the ESP8266. All the codes in this tutorial can be used on an Arduino Uno or ESP32.

Copy the DS18X20 library for MicroPython on the ESP32 / ESP8266

The uPiCraft IDE comes with some additional libraries to the native MicroPython modules. The DS18X20 library is stored with the other libraries in the side navigation bar in the uPi_Lib section.

Log in to your MicroPython development board. If you missed the previous episodes, go to the Tools menu and select Serial to choose the COM port to which the card is connected. Then click on the connect button.

To copy the library to the map, simply drag and drop on the connected device. The other method is to open the library and then choose the Download option in the Tools menu.

If you are not using the uPiCraft IDE, create a new file named DS18X20.pu and paste the source code. It is also available on GitHub. This driver allows you to create an object attached to the One-Wire probe pin. Several methods are available:

  • scan
  • convert_temp
  • read_scratch
  • write_scratch
  • read_temp

Identify Dallas DS18B20 probes in MicroPython

The first thing to do is to individually identify the probes connected on the One-Wire bus. It is still more practical to know if we measure the temperature in the room or outside.

Create a new script and paste the following code. Change the pin of the One-Wire bus. Save it as DS18B20_scanner.py (for example) and press F5 to upload and execute the script. The script runs in a loop by scanning the bus every second. To stop the script, place the cursor in the console (bottom of the screen) and press CTRL + C. Connect the probes successively to locate them. The last hex code will be used to identify the probes separately. There is very little chance that two probes have the same code. If so, use another part of the code

Read the temperature of DS18B20 probes in MicroPython

Now that each probe has been identified, let’s look at how to recover the temperature. The scan method retrieves the identifiers of the probes attached to the OneWire bus. Each identifier will be needed to read the temperature individually. Here, an infinite loop (while True 🙂 makes it possible to record the temperature of each probe every second. To interrupt the loop, press the stop icon.

Prepare virtual appliances on Domoticz

Go to the Domoticz server to create two virtual devices of the temperature type and get the Idx of each probe. Follow this tutorial to learn how to do it.

domoticz ds18b20 esp8266 esp32 arduino idx device sensor

Log in to Domoticz and publish metrics using an HTTP request

That’s it, everything is ready. We know how to identify Dallas DS18B20 probes, read the temperature individually. All that remains is to send the measurements to a home automation server. Here we will publish it on a Domoticz server using the JSON API. The complete technical documentation is here.

I modified Ralph’s source code published on the MicroPython forum and added other physical quantities. To make HTTP requests, we have the MicroPython socket (module) library (online documentation). 5 lines of code are enough to connect and send an HTTP request in MicroPython.

Create a new script and paste the following source code. Save it as domoticz.py. Copy it on the development board. To use this module, we must initialize a Domoticz object by passing it the IP address of the server, the port and the identification key (optional). Then, to send measurements, it will suffice to call the method corresponding to the – measured quantity. For the DS18B20, we will use the setTemperature method. Each method waits

  • The identifier (Idx) of the virtual appliance
  • The value (s)
Warning. The name of the library must be different from the class, otherwise the module can not be called. You will have an error object is not callable. For this, you can play capitalize for example. 

The method assembles the query in the format expected by the Domoticz JSON API. Then, the sendRequest method is called to publish the measurements to the server. The sendRequest method is waiting for the server to return. If the server is not available, the return 0 method. Otherwise, it returns the HTML code of the server. If the Domoticz server has accepted the request, code 200 is returned.

If you followed the previous tutorial on managing the WiFi connection in MicroPython, copy the ConnectWifi.py library to your card, otherwise go and get it here.

Now create a new script and paste the code below. Change the following settings

  • pinDs18b20, pin to which the OneWire bus is connected
  • insideTemp, identifier of the inner probe (for example), code found with the previous scanner script
  • outsideTemp, ditto for outdoor sensor
  • hold_time_sec, waiting time in seconds between two publications of my
  • Idx_inside, Idx Domoticz of the inner probe
  • Idx_outside, ditto for the external probe
  • domoticzIP, IP address of the Domoticz server
  • domoticzPort, the port, default 8080

To identify each probe, we will test the end of the identifier. The identifier is stored in a hexadecimal array.

Finally, before executing the HTTP request, it is necessary to convert the float measure to a string. For this, we have the format function of MicroPython.

Full project code

 upicraft ds18b20 domoticz http request micropython

Domoticz virtual devices update in seconds.

Here, this article concludes this small series of tutorials on MicroPython. I hope you like it. We will resume the tutorials a little later to drive the robotic arm presented a few weeks ago. If you have special needs or problems, the comments are there for that.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

Tags:

DIY Projects