Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e19c7a8

Browse files
committed
Added Example ModbusRTUTemperatureSensor
Added ModbusRTUTemperatureSensor example to the library ArduinoModbus
1 parent 3b70a0b commit e19c7a8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Modbus RTU Temeperature Sensor
3+
4+
This sketch show how to use the modbus library, in in order to sent a request to a slave RTU sensor
5+
unit and read the responce packet return by the remote unit.
6+
7+
Circuit:
8+
- MKR board
9+
- TModbus RS485 Temperature
10+
- MKR 485 shield
11+
- ISO GND connected to GND of the Modbus RTU server
12+
- Y connected to A/Y of the Modbus RTU sensor
13+
- Z connected to B/Z of the Modbus RTU sensor
14+
- Jumper positions
15+
- FULL set to OFF
16+
- Z \/\/ Y set to OFF
17+
18+
created 16 July 2018
19+
by Sandeep Mistry
20+
*/
21+
22+
#include <ArduinoModbus.h>
23+
24+
float temperature, humidity;
25+
26+
void setup() {
27+
Serial.begin(9600);
28+
while (!Serial);
29+
30+
Serial.println("Modbus Temperature Humidity Sensor");
31+
// start the Modbus RTU client
32+
if (!ModbusRTUClient.begin(9600)) {
33+
Serial.println("Failed to start Modbus RTU Client!");
34+
while (1);
35+
}
36+
}
37+
38+
void loop() {
39+
//send a reading request to the RTU slave with id=1, type=HOLDING_REGISTERS address=0, number of values to read, nb=2
40+
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) {
41+
Serial.print("failed to read registers! ");
42+
Serial.println(ModbusRTUClient.lastError());
43+
} else {
44+
//if the request goes fine read the value with the read() function
45+
short rawtemperature = ModbusRTUClient.read();
46+
short rawhumidity = ModbusRTUClient.read();
47+
temperature = rawtemperature / 10.0;
48+
humidity = rawhumidity / 10.0;
49+
Serial.println(temperature);
50+
Serial.println(humidity);
51+
}
52+
53+
delay(5000);
54+
}

0 commit comments

Comments
 (0)