0% found this document useful (0 votes)
7 views2 pages

Mode Bus Rtu

Uploaded by

rfid035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Mode Bus Rtu

Uploaded by

rfid035
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Here is the Arduino Modbus code utilizing the Modbus Master to read the temperature

and humidity values from the sensor.

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

#define RX_PIN 10
#define TX_PIN 11

SoftwareSerial debugSerial(RX_PIN, TX_PIN);

// Modbus register array


uint16_t holdingRegs[2];

ModbusMaster node;

void setup() {
// Initialize SoftwareSerial for debugging
debugSerial.begin(9600);
Serial.begin(9600);

// Initialize Modbus communication


node.begin(1, Serial); // Modbus slave ID and hardware serial port
}

void loop() {
// Read 6 holding registers starting at address 0
uint8_t result = node.readHoldingRegisters(0, 2);

if (result == node.ku8MBSuccess) {
// Store the received data into the holdingRegs array
for (int i = 0; i < 2; i++) {
holdingRegs[i] = node.getResponseBuffer(i);
}

// Print the received data


debugSerial.println("Reading holding registers:");
debugSerial.print("Temp (reg): ");
debugSerial.println(holdingRegs[0]);
debugSerial.print("Hmd (reg): ");
debugSerial.println(holdingRegs[1]);

// Convert and print the values


float temperatureCelsius = holdingRegs[0] / 10.0;
float humidityPercentage = holdingRegs[1] / 10.0;

debugSerial.print("Temp: ");
debugSerial.print(temperatureCelsius);
debugSerial.println(" C");
debugSerial.print("Hmd: ");
debugSerial.print(humidityPercentage);
debugSerial.println(" %");

debugSerial.println();
debugSerial.println("- - - - - - - - - - ");
debugSerial.println();
} else {
debugSerial.println("Failed to read holding registers!");
}
// Add a delay of 1000ms before next request
delay(1000);
}

You might also like