Vistx / Modbus-implementation

This repository shows how to implement Modbus RTU communication with ESP-32 Microcontroller

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Modbus RTU implementation

Read more about the Modbus RTU here.

πŸƒ Get things up and running

πŸ”§ Needed Tools

  • ESP-Wroom-32 (or Arduino Uno)
  • USB to Micro USB cable

😏Optional

  • Max 485 Module
  • USB to RS-485 converter
  • Max7219 LED matrix
  • HCSR-04 Ultra Sonic Sensor

πŸ‘¨β€πŸ’» Programming languages

  • C# with Visual Studio 2019
  • C++ with Visual Studio Code and PlatformIO extension installed [tutorial here]

πŸ“– Libraries used in this project

βš™οΈ Hardware Setup Method #1

Connect your USB to USB micro With the Esp32 and plug in to one of your PC Ports (done). Simple yet limited by the USB cable length.

βš™οΈ Hardware Setup Method #2

Important Note: Upload the code before connecting the RX0 and TX0 pins

Connect the max 485 module to the ESP 32 like so :

  • Bridge the RE and DE connections in the breadboard

  • Connect that bridge to the GPIO Pin 4 of the ESP32

  • RX0 (ESP32 ) --> R0 (Max485 Module)

  • TX0 (ESP32 ) --> DI (Max485 Module)

  • Connect A and B pins from the Max485 module to the A(D+) and B(D-) of the USB to RS485 module

1

πŸ§‘β€πŸ’» Software Setup

2

  • Upload the code to your ESP32

3

  • Open GUI: Modbus-implementation / Simple_Example /

4

  • Connect and do Read and Write Holding Registers operations

5

πŸ‘¨β€πŸ« Code Explanation and Important Details

On the ESP32 side of the code, the serial begin baudrate must match the selected baudrate in the GUI application. Also the COMport must not be in use by other software

Also, the slave ID in the ESP32 must match the ID of the called function in C# .

6

Technically, you can connect over 240 slave devices on the same serial connection, all you have to do is change the slave ID for each one and call the specific device by that unique ID from the GUI.

Example 1

πŸ”Œ Wiring

7

Module-->ESP32

  • Vcc --> +5v
  • Gnd --> GND
  • DIN --> GPIO 23
  • CS --> GPIO 5
  • CLK --> GPIO 18

πŸ”Ž Details

Each of the numbers in the Modbus RTU message represents the bit position of the matrix but expressed in decimal form.

8

{img source}

How are the bit values placed?
Take a look at Column 0 and Rows from 0 to 7. The first 2 LED-s are OFF thus 00 , next 4 LED-s are ON so 1111, and lastly 2 LEDs are OFF thus 00. So the entire message is 00111100 in binary, or 60 in decimal form, which in terms represents our first RTU message, audata16[0]=60 next Column 0 is 01000010 in binary, or 66 in decimal form and so on .

Upload the code from Example1/ESP32 Code With VScode/ to the ESP32

πŸ’» Gui

  • Open the programm located here: Example1/GUI Led matrix/WindowsFormsApp1/ with Visual studio 2019
  • or directly : Example1/GUI Led matrix/WindowsFormsApp1//bin/Debug/WindowsFormsApp1.exe
  • Connect with your settings tuch the interactive LEDs and Send(Message)

9

Example 2

πŸ”Œ Wiring

10

  • Vcc --> +5v
  • Gnd --> GND
  • Trig --> GPIO 5
  • Echo --> GPIO 18

πŸ”Ž Details

Timers from Winforms are used to request a register from a slave device (ESP32 in our case), we can see the update interval in which these registers are requested from the slave device, and the response is used to update the user interface.

As stated earlier it's not recommended to use the delay function when using the Modbus slave library so instead we used millis()(how to) , to update Modbus registers and the HCSR 04 readings.

11

Upload the code from Example2/Modbus Rtu With Hc-sr04 to the ESP32

πŸ’» GUI

  • Open the programm located here: Example2/HCSR04DisplayReadings with Visual studio 2019
  • or directly : Example2/HCSR04DisplayReadings/bin/Debug/HCSR04DisplayReadings.exe
  • Connect with your settings and dont forget to set the request interval the unit is miliseconds

12

Multiple ESP-32 Slave devices

13

  • To call the first device (ID 1) change the parameters to the the shown function

1 l

  • To call the second device (ID 2) change the parameters to the the shown function

2 l

  • To call the third device (ID 3) change the parameters to the the shown function

3 l

About

This repository shows how to implement Modbus RTU communication with ESP-32 Microcontroller


Languages

Language:C++ 80.2%Language:C# 19.8%