MODBUS SLAVE MONITOR 1.0
Loading...
Searching...
No Matches
main.cpp File Reference

Modbus RTU Slave Firmware with ADS1015 and real-time RMS calculation (ESP32). More...

#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>
#include "HardwareSerial.h"
#include "ModbusServerRTU.h"

Classes

struct  ADC_Sample
 ADC sample with value and associated channel. More...
struct  RMS_FIFO
 Circular buffer for efficient RMS calculation. More...
struct  SensorData
 Sensor configuration parameters for Modbus discovery. More...

Macros

#define I2C_SDA_PIN   9
 I2C SDA pin for ADS1015 communication.
#define I2C_SCL_PIN   8
 I2C SCL pin for ADS1015 communication.
#define ADS_ALERT_PIN   10
 ADS1015 interrupt pin for completed conversions.
#define SLAVE_ID   1
 Modbus slave device address.
#define NUM_REGISTERS   18
 Total number of Modbus registers available for RMS data.
#define RX_PIN   20
 UART receive pin for RS485.
#define TX_PIN   21
 UART transmit pin for RS485.

Functions

HardwareSerial ModbusSerial (1)
 Serial port instance for Modbus RS485 communication.
ModbusServerRTU MBserver (2000)
 Modbus RTU server with 2000ms timeout.
void IRAM_ATTR on_adc_data_ready ()
 Interrupt service routine for completed ADC conversions.
void task_adquisicion (void *pvParameters)
 ADC data acquisition task with improved multiplexer handling.
void task_procesamiento (void *pvParameters)
 Sample processing and RMS calculation task.
int get_rms_history (int channel, float *output_buffer, int count)
 Gets RMS value history for a specific channel.
void dataUpdateTask (void *pvParameters)
 Modbus register update task with per-channel conversion factors.
ModbusMessage readHoldingRegistersWorker (ModbusMessage request)
 Worker to handle Modbus holding register read requests.
void setup ()
 System initialization function.
void loop ()
 System main loop.

Variables

const int NUM_CHANNELS = 3
 Number of ADC channels to sample.
const int FIFO_SIZE = 320
 Circular FIFO buffer size for RMS calculation.
const int PROCESS_INTERVAL_MS = 1000
 RMS processing interval in milliseconds.
const int RMS_HISTORY_SIZE = 100
 RMS value history size for each channel.
const int MODBUS_UPDATE_INTERVAL_MS = 300
 Modbus register update interval in milliseconds.
const float CONVERSION_FACTOR = 0.618f
 Global conversion factor (maintained for compatibility).
const float CONVERSION_FACTORS [NUM_CHANNELS]
 Channel-specific conversion factors for each ADC channel.
Adafruit_ADS1015 ads
 ADS1015 converter instance.
QueueHandle_t queue_adc_samples
 Communication queue for ADC samples.
RMS_FIFO fifos [NUM_CHANNELS]
 Array of FIFOs for each measurement channel.
float rms_history_ch0 [RMS_HISTORY_SIZE]
 RMS value history for channel 0.
float rms_history_ch1 [RMS_HISTORY_SIZE]
 RMS value history for channel 1.
float rms_history_ch2 [RMS_HISTORY_SIZE]
 RMS value history for channel 2.
volatile int rms_history_head = 0
 Write index for RMS histories.
SemaphoreHandle_t rms_history_mutex
 Mutex for concurrent access protection to RMS histories.
volatile bool adc_data_ready = false
 ADC data ready flag (ISR).
volatile uint8_t current_isr_channel = 0
 Current channel in sampling sequence (ISR).
uint16_t holdingRegisters [NUM_REGISTERS]
 Modbus registers for RMS data.
SemaphoreHandle_t dataMutex
 Mutex for Modbus register protection.
TaskHandle_t dataUpdateTaskHandle
 Handle for Modbus data update task.
SensorData sensor = {1, 3, 10, NUM_REGISTERS, PROCESS_INTERVAL_MS, 1, 1, 0}
 Sensor configuration for discovery responses.

Detailed Description

Modbus RTU Slave Firmware with ADS1015 and real-time RMS calculation (ESP32).

This system acquires analog signals from 3 channels using ADS1015 ADC, calculates real-time RMS values using circular FIFOs, maintains a measurement history and responds to Modbus RTU queries over RS485. Includes channel-specific conversion factors and improved ADC multiplexer handling.

Date
2025-12-04