Elektroda.com
Elektroda.com
X

How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial

p.kaczmarek2 1854 1
This content has been translated flag-pl » flag-en View the original version here.
  • How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial
    I will show here how you can connect Arduino R4 WiFi to Home Assistant via MQTT in order to receive and send data via MQTT publish. I will use the ArduinoMqttClient library for this. The sample code that I will post here first connects to our WiFi network, then connects to our MQTT broker via login and password, and finally subscribes to the given MQTT topic and listens to messages on it, while also publishing data from itself.

    The theme is based on the new Arduino R4 WiFi board:
    How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial
    On the software side I used ArduinoMqttClient:
    https://www.arduino.cc/reference/en/libraries/arduinomqttclient/
    https://github.com/arduino-libraries/ArduinoMqttClient
    The library must be installed via Libraries:
    How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial
    Then we can run the sample code that we'll write together in a moment.
    At the beginning, we attach the headers and define the constants (directions for WiFi and MQTT server):
    Code: c
    Log in, to see the code

    Next, we create instances of WiFi and MQTT clients globally:
    Code: c
    Log in, to see the code

    Next, in setup, we run everything:
    Code: c
    Log in, to see the code

    The above code activates the serial port so you can watch what the Arduino is doing in the Serial Monitor.
    Then we start WiFi:
    Code: c
    Log in, to see the code

    In the loop, we try to connect as a client to the wireless network. When the connection is successful, we print our IP:
    Code: c
    Log in, to see the code

    Then we run MQTT:
    Code: c
    Log in, to see the code

    We try to connect in a loop. The first connection fails with error -2. I based the code on ArduinoMqttClient examples and Arduino Forum discussions and it seems that everyone has this problem.
    Then we subscribe to the MQTT topics that interest us and set the callback that the MQTT library will call when it receives some data:
    Code: c
    Log in, to see the code

    Without executing subscribe for a given topic, we will not receive data sent to it!
    The continuation takes place in the aforementioned callback. It is called only when we receive some data:
    Code: c
    Log in, to see the code

    In this callback, you can freely process the received data, switch the relay, etc.

    There is still the main loop - you need to refresh the MQTT client there, I also publish the time from Arduino there:
    Code: c
    Log in, to see the code

    That's all. We save and upload to Arduino. Home Assistant assumes it's already on. We enter the MQTT tool, Home Assistant sees the information published by Arduino:
    How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial
    Arduino also sees what HA will post:
    How to connect Arduino R4 WiFi to Home Assistant via MQTT? ArduinoMqttClient tutorial
    Communication works fine both ways.
    Full code:
    Spoiler:
    Code: c
    Log in, to see the code


    Summary
    Arduino R4 WiFi has just entered the market and already offers ready-made mechanisms that allow you to easily connect to an MQTT broker, for example, such as in Home Assistant. Subscribing to an MQTT topic and publishing data on a selected topic also works perfectly fine. Therefore, nothing stands in the way of being able to easily connect our projects to the IoT network and I mean even our older projects, from before R4, because the migration from Uno R3 to R4 WiFi is also not that difficult.
    Do you see any potential uses for MQTT on R4? I invite you to the discussion.

    Cool? Ranking DIY
    Do you have a problem with Arduino? Ask question. Visit our forum Arduino.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 5827 posts with rating 5811, helped 278 times. Been with us since 2014 year.
  • #2
    krzbor
    Level 25  
    I wonder how it will behave in the event of loss of communication with the broker, i.e. in the case of:
    - network (or WiFi) problem
    - problem with the broker itself (e.g. temporary shutdown of the computer with the broker, restart of the broker, etc.)
    I'm afraid that the device with this client will not come back to life without a reboot.

    By the way - what MQTT client do you recommend for ESP8266 (Arduino)? I found something like this link . It has interesting properties:

    Connecting to a WiFi network.
    Connecting to a MQTT broker.
    Automatically detecting connection lost either from the WiFi client or the MQTT broker and it will retry a connection automatically.
    Subscribing/unsubscribing to/from MQTT topics by a friendly callback system.
    Supports wildcards (+, #) in subscriptions
    Provide a callback handling to advise once everything is connected (Wifi and MQTT).
    Provide a function to enable printing of useful debug information related to MQTT and Wifi connections.
    Provide some other useful utilities for MQTT and Wifi management.
    Provide a function to enable an HTTP Update server secured by a password to allow remote update.
    Provide a function to enable OTA secured by a password to allow remote update.

    I would like to create something universal on Arduino based on WiFiManager (it works well for me), OTA (it works well for me too) and of course the MQTT client - here I need a proven, reliable library resistant to everything that may happen (problem with WiFi, network, broker, etc.). If you can recommend something (something that works long and reliably) then write.