Thanyasit / ESP32_Modbus_MQTT

ESP32 Modbus(KP835) MQTT to Nodered

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESP32_Modbus_MQTT

Description

This code is written for the ESP32 and is intended to read values from a Modbus device and publish them to an MQTT broker. The code uses the ModbusMaster library to communicate with the Modbus device and the PubSubClient library to publish to the MQTT broker.
2022

Usage

The code begins by including the necessary libraries and defining some constants. The setup_wifi() function connects to a WiFi network using the WiFi library. The reconnect() function attempts to connect to an MQTT broker using the PubSubClient library.

The preTransmission() and postTransmission() functions are used to control the MAX485 transceiver, which is used to communicate with the Modbus device. The setup() function initializes the serial communication, connects to the WiFi network and MQTT broker, and reads values from the Modbus device. It then publishes these values to the MQTT broker using the client.publish() function.

The code also includes functions for converting 32-bit integers to floats and floats to 32-bit integers. These functions are used to convert the Modbus values to a format that can be published to the MQTT broker.

To put the ESP32 into deep sleep mode, the esp_deep_sleep_start() function is used. The esp_sleep_enable_timer_wakeup() function is used to set the sleep interval.

To use this code, you will need to modify the following constants in the setup() function:

ssid - the name of your WiFi network password - the password for your WiFi network mqtt_server - the IP address of your MQTT broker You will also need to modify the following lines in the client.connect() function in the reconnect() function:

"NO4" - this is the client ID used to connect to the MQTT broker. You can change this to any unique string. Note that the code is written to read and publish values from a specific Modbus device. You will need to modify the code to read and publish values from your own Modbus device. You can use the readInputRegisters() function to read values from your Modbus device, and the client.publish() function to publish these values to the MQTT broker.

Example of using ESP32 and Max485

Example to node-red

License

This code is licensed under the MIT License. See the LICENSE file for details.

About

ESP32 Modbus(KP835) MQTT to Nodered

License:MIT License


Languages

Language:C++ 81.8%Language:Python 5.5%Language:Processing 4.4%Language:Ruby 4.3%Language:C 3.3%Language:Makefile 0.7%