# Modbus Project for Vivarox EMS

Modbus Project for Vivarox EMS
This is a specification and estimation of the required hardware and costs.
More detail will be added and real time testing is required.

## Arduino Option:

To connect to Modbus and read information onto an SD card using an Arduino.

### Hardware needed:

1. Arduino Board

   Recommended: Arduino MEGA 2560 (for more memory and I/O pins) or Arduino UNO (for simpler).

   - [Arduino MEGA @ R377.20](https://www.robotics.org.za/MEGA-16U2?search=Arduino%20MEGA%202560)
   - [UNO R3 with 16U2 USB Interface @ R151.00](https://www.robotics.org.za/UNOR3-16U2?search=%20Arduino%20UNO)

2. RS485 to TTL Module

   Allows communication between the Arduino and Modbus devices using the RS485 protocol.

   - [RS485 Module (TTL -> RS485) @ R25.30](https://www.robotics.org.za/RS485-MOD)
   - [MAX485 Bus Transceiver (4 Pack) @ R16.00](https://www.robotics.org.za/MAX485-DIP?search=MAX485)

3. SD Card Module

   Allows the Arduino to read from and write data to an SD card.
   Standard SD card module that interfaces via SPI (Serial Peripheral Interface).

   - [Micro SD Card Module @ R25.00](https://www.diyelectronics.co.za/store/memory/512-micro-sd-card-module.html?srsltid=AfmBOoptww8c6kx53xbZWiP2_C_qOE3r9xinyoCO-AZHrZkNQiyxU17c)

4. RTC Module
   To keep track of the current date and time, even when the Arduino is powered off. This will allow your Arduino to timestamp the data it reads from Modbus and writes to the SD card.

   - [DS3231 Real Time Clock Module @ R55.20](https://www.robotics.org.za/DS3231-MOD?search=DS3231)

5. Power Supply / Batteries

   To power the Arduino and connected peripherals (RS485 module and SD card module).

   - [AC Adapter 9V with barrel jack @ R60](https://www.robotics.org.za/AC-9V-2A-2155?search=%20Power%20Supply)
   - [Panasonic CR2032 3V 225mAh Coin Cell Battery (5 Pack) @ R43.00](https://www.robotics.org.za/CR2032)

### Wiring

#### RS485 Module to Arduino:

1. RO (Receiver Output) to Arduino RX
2. DI (Driver Input) to Arduino TX
3. DE (Driver Enable) & RE (Receiver Enable) to an Arduino digital pin (control this pin in the code to switch between sending and receiving).
4. VCC to 5V on Arduino
5. GND to GND on Arduino
6. A & B (RS485 differential pair) to Modbus device.

#### SD Card Module to Arduino:

1. VCC to 5V on Arduino
2. GND to GND on Arduino
3. MOSI to MOSI (pin 51 on MEGA, pin 11 on UNO)
4. MISO to MISO (pin 50 on MEGA, pin 12 on UNO)
5. SCK to SCK (pin 52 on MEGA, pin 13 on UNO)
6. CS (Chip Select) to any available digital pin (e.g., pin 4).

#### Wiring the RTC Module

1. VCC to 5V on the Arduino.
2. GND to GND on the Arduino.
3. SDA to SDA (pin 20 on MEGA, pin A4 on UNO).
4. SCL to SCL (pin 21 on MEGA, pin A5 on UNO).

### Software

- Modbus Library: SimpleModbus or ModbusMaster.
- SD Library: The Arduino IDE comes with an SD library for interfacing with the SD card.
- RTC Library: Use the RTClib by Adafruit, which is compatible with both DS3231 and DS1307 modules.

#### Programming Workflow

- Initialize Modbus Communication:
- Initialize RTC Module: Set up the RTC to ensure it has the correct time.
- Use the Modbus library to set up communication parameters (e.g., baud rate, parity).
- Read Data from Modbus:
- Use the Modbus library to read the required data from the Modbus registers (e.g., meters, input registers).
- Get the Current Time from the RTC: Get the current date and time for logging.
- Write Data to SD Card:
- Use the SD library to open a file on the SD card, write the Modbus data, and then close the file.

#### Arduino Source Examples

[Example Arduino Prototype](https://git.warky.dev/wdevs/vivarox-ems-modbus/src/branch/main/Arduino/basic_read.cpp)

#### Addition Software Notes

- Ability to reset and write logs to newly inserted SD card.
- Writing to files stamped by day.

## Costs

This estimated cost of the hardware from supplier like Micro Robotics, excluding the labour to assemble:

R617.00 per unit using the Arduino MEGA
R374,5 per unit using the Arduino UNO

## Raspberry Pi Option:

[Check this page for the Raspberry PI option](https://git.warky.dev/wdevs/vivarox-ems-modbus/src/branch/main/raspberry-pi-option.md)
