Wednesday, February 19, 2020

Temperature adventures with (RaspPI) ESP32 and MQTT.

 Guest speaker today. Glenn has a farm and has contributed before. Farm automation is especially useful for keeping the work down somewhat and getting information. Here is Glenn's latest project:
---------------------------------------------- 
A while back I began looking at convenient, inexpensive temperature measuring devices. Now the reason behind this is very simple. Going out the back door of our place we have what we call the boot room. We live on a ranch, so needless to say we separate our ‘barn’ clothes from our ‘house’ clothes. The barn clothes hang in the boot room. Well before changing into our barn clothes we would like to know what the exterior temperature is. We don’t have an exterior temperature display near the boot room. Soooo… this is where the ESP32 comes in.

I first started this project as a raspberry pi based project. You see I have a raspberry pi 3 B+ in the horse barn and it measures the temperature of both my workshop and the stall area using DS18B20s. Now just about any of the raspberry pi’s from the 2B on up, or the zero would have worked. The raspberry pi zero W would not have worked in my case here as the device is housed inside a Leviton Structured Wiring panel and there is no wifi within the barn. If you are doing this in a location that has Wifi available then the Pi zero W could certainly fit the bill.

I had adapted code from Dave’s other posts to fulfill the need but that code was originally base on Sqlite and wasn’t feeding to my new MQTT docker container. Time for a change but thanks Dave for the original code!!

This project really contains three parts:
1. Temperature measurement
2. MQTT testing and coding
3. ESP32 implementation.

Temperature measurement.


The DS18B20 is probably the best contribution to the temperature measurement in the history of IoT and home automation. I’m not going to go into depth on this as Dave has several posting on it and they are a wealth of knowledge. These are connected to the raspberry pi as shown in this drawing:
Note: I am only using two Sensors at the moment not the three shown.
Full disclosure here. I am a big fan of Python and Python modules. Have been for a long time. So, when I start a new project I go on a search to see what new modules are out there. I’m an engineer, not a developer. I’m not the guy that writes a lot of code day in and day out.

In this case I came across a module that has really captured my imagination. It is called:
rpi-temperature-mqtt.

It was originally written by a fellow who calls himself HackerCowboy. Here is his Git repository link, https://github.com/hackercowboy/rpi-temperature-mqtt.

I grabbed the code and started playing with it.

First you must install the module onto you device (raspberry pi or ESP32).
pip install rpi-temperature-mqtt

All of the configuration is done in a file you create called config.json. Here is my config.json from the barn:
{
    "mqtt_client_id": "barn",
    "mqtt_host": "10.10.XX.XX",
    "mqtt_port": "1883",
    "wait_update": "60",
    "verbose": "true",
    "sources": [
        {
          "serial": "28-0000055aae0e",
          "topic": "workshop"
        },
         {
           “serial”: “28-0000054de0b5”,
           “topic”: “boxstalls”
          }
      ]
}

As you can see I have two DS18B20s that I am sampling. The code supports multiple devices. The other item of note is that the code provides its own scheduler capability. You set the time period between sampling of the collection of devices with the line:

“wait_update”: “60”,

This says to wait 60 seconds between sampling another set.
It also provides a delay between devices. That line is:

“wait_process”: “10”,

In this case it will wait 10 seconds between samples from the two DS18S20s.
Now to the section labelled “sources”. For each device you will need to add two lines.

“serial”: “your device serial number”,
“topic”: “what you call it in MQTT”

Note that each section must end in a comma after the topic statement. That is except for the last section where the comma is left off.

The other item I use in the config.json file is the verbose option. It lets me see exactly what the module is sending and shows me when the connection is made:

NOTE:

Before you start using rpi-temperature-mqtt you MUST initialize the Device Templates. This is a new change in the later versions of Raspian, etc. If you do not do this rpi-temperature-mqtt WILL NOT WORK. So you must add the following instructions to boot/config.txt

# Add the device tree to initialize w1-gpio.
dtoverlay=w1-gpio

Save it and reboot. If you forget you can manually start it by issuing the command:

sudo dtoverlay w1-gpio

Remember if you don’t do one of these the rpi-thermostat-mqtt code will error and say  it can’t find w1-master…..

MQTTfx:

I use a Ubuntu desktop machine in my lab. As well I have a separate server that runs all my docker containers. So for testing purposes I log into my test device (be they raspiberry pi or ESP320) via SSH and control them.
I found that in order to really see what is going on an MQTT broker monitor is really useful. Enter MQTTfx. This piece of code will run on a variety of platforms and gives you the ability to subscribe to a broker (local or remote) and then publish or subscribe to the topic of interest.

The first thing we need to do is create a connection profile. Select the Broker Status tab. Then from the top Extras tab, edit the connection profile:
  

  
· Enter your new Profile name at the top.
· Enter the ip address of your Broker.
· Enter the port number of your Broker.
· Leave the rest as presented.
· Click Apply, then OK.



You will be returned to the main screen. Now make sure Broker status is selected. From the profile pull down window select your newly created profile and then click on CONNECT:


Once you connect you should see this screen which connects you to the Mosquitto Broker:



Now click on Subscribe.

At this point you need to start the rpi-temperature-mqtt software running on your device by issuing this command:

rpi-temperature-mqtt config.json

Make sure that you do this from the subdirectory that contains config.json.

Now when you switch back to the MQTTfx screen and you select your topic(s) you should see something like this:


Note I’ve selected to view the workshop topic from the pull down. The lower right window displays the last reported value.

Ok, so this post is getting a little long so I’ll end it here. Next we’ll move to the ESP32 and talk about micopython, rpi-temperature-mqtt, and Hazzuh32 (ESP 32) from Adafruit.

Cheers.
Glenn.


2 comments:

  1. Sorry guys but there is an error in the code for the config.json file.
    The comma:
    "topic": "workshop",
    }
    {
    after workshop is in the wrong place. It should look like this:
    "topic": "workshop"
    },
    {
    Sorry it got out before I caught it.

    ReplyDelete
    Replies
    1. I edited the correction into the post as well. That way we can pretend it didn't happen.

      Delete