Skip to content

Commit ce49c07

Browse files
Rocketctsandeepmistry
authored andcommitted
Added Example ModbusRTUTemperatureSensor (#4)
1 parent 3b70a0b commit ce49c07

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Modbus RTU Temeperature Sensor
3+
4+
This sketch shows you how to interact with a Modbus RTU temperature and humidity sensor.
5+
It reads the temperature and humidity values every 5 seconds and outputs them to the
6+
serial monitor.
7+
8+
Circuit:
9+
- MKR board
10+
- Winners® Modbus RS485 Temperature and Humidity:
11+
https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN
12+
- External 9-36 V power Supply
13+
- MKR 485 shield
14+
- ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-
15+
- Power supply V+ connected to V+ sensor
16+
- Y connected to A/Y of the Modbus RTU sensor
17+
- Z connected to B/Z of the Modbus RTU sensor
18+
- Jumper positions
19+
- FULL set to OFF
20+
- Z \/\/ Y set to ON
21+
22+
created 8 August 2018
23+
by Riccardo Rizzo
24+
*/
25+
26+
#include <ArduinoModbus.h>
27+
28+
float temperature;
29+
float humidity;
30+
31+
void setup() {
32+
Serial.begin(9600);
33+
while (!Serial);
34+
35+
Serial.println("Modbus Temperature Humidity Sensor");
36+
// start the Modbus RTU client
37+
if (!ModbusRTUClient.begin(9600)) {
38+
Serial.println("Failed to start Modbus RTU Client!");
39+
while (1);
40+
}
41+
}
42+
43+
void loop() {
44+
45+
// send a Holding registers read request to (slave) id 1, for 2 registers
46+
if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) {
47+
Serial.print("failed to read registers! ");
48+
Serial.println(ModbusRTUClient.lastError());
49+
} else {
50+
// If the request succeeds, the sensor sends the readings, that are
51+
// stored in the holding registers. The read() method can be used to
52+
// get the raw temperature and the humidity values.
53+
short rawtemperature = ModbusRTUClient.read();
54+
short rawhumidity = ModbusRTUClient.read();
55+
56+
// To get the temperature in Celsius and the humidity reading as
57+
// a percentage, divide the raw value by 10.0.
58+
temperature = rawtemperature / 10.0;
59+
humidity = rawhumidity / 10.0;
60+
Serial.println(temperature);
61+
Serial.println(humidity);
62+
}
63+
64+
delay(5000);
65+
}

0 commit comments

Comments
 (0)