Alkaline - Auffrischgerät - Teil 2 - AZ-Delivery

Use batteries longer and actively protect the environment

Connect Home Assistant to the refresh device

Now that we have familiarized ourselves with a fully functional device in the first post, we want to connect it to Home Assistant. Once again, the MQTT protocol will serve us well.

 

One advantage is that we don't have to include a display or controls in the program. The Home Assistant dashboard takes care of these tasks. When the refresh device is ready, we can see this on our cell phone, tablet, or PC and can even have a notification sent to us.

With the Home Assistant app on our cell phone, we always have the current status at our fingertips.

Let's take a look at what's needed to do this.

The sketch

The sketch now comprises five function blocks. The main program has grown quite a bit.

In part 1, we didn't include any libraries. Now there are three in total that are necessary for the WiFi connection and MQTT. We also need .

This is followed by the user configuration in the sketch, where you need to enter the SSID of your WiFi and the corresponding password. Once again, I have assigned a static IP address

.

Next come the credentials for the MQTT server and the MQTT password.

The MQTT topics are created as string variables. The battery voltages are assigned the topics "batt1 ... batt4," and the respective status of a battery is stored in "batt1stat ...batt4stat ."  These statuses are "No battery," "Recharging," and "***Finished***."

We are already familiar with the initialization that follows from Part 1. Here , the array "battstat[]" is added, which records the respective battery status. It is initialized with "No batt." in all four positions.

 

void setup() follows with the initialization of the WiFi and MQTT server. No special features.

void loop() shows only minor changes. At the beginning, the MQTT connection is checked and reestablished if necessary. This is followed by the already familiar process of charging pulse and measurements, now supplemented by the two subroutines Statusmeldungen(); and WiFi_MQTT();

 

The subroutine for status messages

The status messages allow us to see whether a battery is inserted in a compartment, whether the battery is currently being refreshed, or whether the refresh process has been completed.

The sketch <

p class="Text">

The first IF query checks whether the battery voltage is zero. This is equivalent to "no battery is inserted."

 

For your own MQTT programming, it may be interesting to see how strings are transmitted in MQTT. The contents of battstat[] are defined as character strings with up to 16 characters, but are actually shorter. The reason for this is the necessary terminator character '\0'requiredby MQTT.


The WiFi_MQTT subroutine

Here I have summarized the functions that start our WiFi, set the static IP address, establish the connection to the MQTT server, and handle the publishing.

The sketch

Publishing is a simple function. First, it checks whether the connection is still active. If not, it reconnects.

Then, the measured value is transferred for each battery with two decimal places. In MQTT jargon, such a value is called a "payload." The function

snprintf(payload, sizeof(payload), "%.2f", battU[0]);

assembles the payload, which is published to the MQTT server in the following line to the correct topic.  (Unfortunately, there is no English expression for this).

In the following line, the status of the respective battery status is sent. We had already built the correct composition, including the terminator character, for this in "Statusmeldungen.ino".

 

What needs to be done in Home Assistant?

Since MQTT is quite well integrated into Home Assistant and also runs there, we have very little work to do on this page.

As you already know from my previous blog posts here, all you need to do is add the configuration.yaml correctly and restart Home Assistant. This will generate the necessary entities and make them available.

Remember that you must pay close attention to the order of the lines and only use the space bar for spacing, never TAB!

With that in mind, our entry looks like this:


MQTT.yaml

 

If you have already defined MQTT sensors, enter your sensors below. Otherwise, you will receive an error message because

mqtt:

. . sensor:

may only appear once in configuration.yaml. This also applies to all analogous cases.

With the MQTT entry, you may also notice that a

 

                        expire_after: 30

 

is entered. This function has not been around for very long. It is now possible to specify in the sensor definition that, for example, 30 seconds after the last MQTT sensor value, its value is reset. This prevents the impression that a battery is still inserted.

When the device is switched off, the sensors are no longer available and a corresponding message appears in the dashboard.

An addition to Part 1

Not everyone has measuring resistors in their craft box (1% or better). That's why I've now added another variable to the sketch from Part 2

float corr[4] ={1.0, 1.0, 1.09, 1.0}; //Correction values

 

This is a factor by which the respective measured value must be multiplied if the measuring resistors are not exactly correct. This is best measured with a proper multimeter and the correction factor is calculated. I had one resistor too few and promptly picked up a measurement error of a whopping 9%, which was corrected in this way.

If all correction values = 1.0, your resistors are perfect.

If you need or want to use the new function, it is already implemented throughout the new sketch.

 

Practical application

We now have a nice recharger that also does its job well. How do we use it?

Since I have been recharging alkaline batteries for years, I pay attention to what other people do. I met an elderly gentleman who was selling a battery box at a flea market:

 

It has been with me ever since. At the top, it contains a small testing device that provides a fairly rough indication of whether a battery is still usable.

My batteries are arranged so that the AA batteries that have been tested and charged are on the left. They cover the needs of the household, the laboratory, and the workshop.

In the middle and on the right are the AA cells that do not appear to be damaged or leaking, but which I have not yet tested and/or charged.

All of this has led to me having an annual requirement of around 60 alkaline AA batteries in 2025, I haven't bought a single new battery and all my devices are working perfectly. I still have some in stock.

I would be delighted if you too would like to contribute to environmental protection with this project and save money at the same time.

Until the next blog post,
Yours,
Michael Klein

EspFür arduinoGrundlagen softwareProjekte für fortgeschritteneSmart home

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery