11/*
22 Modbus RTU Temeperature Sensor
33
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.
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.
67
78 Circuit:
89 - MKR board
9- - TModbus RS485 Temperature
10+ - Modbus RS485 Temperature:
11+ - External power Supply
1012 - MKR 485 shield
11- - ISO GND connected to GND of the Modbus RTU server
13+ - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-;
14+ - Power supply V+ connected to V+ sensor
1215 - Y connected to A/Y of the Modbus RTU sensor
1316 - Z connected to B/Z of the Modbus RTU sensor
1417 - Jumper positions
1518 - FULL set to OFF
16- - Z \/\/ Y set to OFF
19+ - Z \/\/ Y set to ON
1720
18- created 16 July 2018
19- by Sandeep Mistry
21+ created 8 August 2018
22+ by Riccardo Rizzo
2023*/
2124
2225#include < ArduinoModbus.h>
2326
24- float temperature, humidity;
27+ float temperature;
28+ float humidity;
2529
2630void setup () {
2731 Serial.begin (9600 );
@@ -36,19 +40,26 @@ void setup() {
3640}
3741
3842void 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
43+
44+ // send a Holding registers read request to (slave) id 1, for 2 registers
4045 if (!ModbusRTUClient.requestFrom (1 , HOLDING_REGISTERS, 0x00 , 2 )) {
4146 Serial.print (" failed to read registers! " );
4247 Serial.println (ModbusRTUClient.lastError ());
4348 } else {
44- // if the request goes fine read the value with the read() function
49+
50+ // If the request goes fine, the sensor sent the readings as bytes packet,
51+ // through the read() function is possible read the measurments.
52+ // the readings is parsed as a short integer from the packets
4553 short rawtemperature = ModbusRTUClient.read ();
4654 short rawhumidity = ModbusRTUClient.read ();
55+
56+ // Is required divide by 10.0 the value readed, because the sensor sent the
57+ // readings as an integer obtained multipling the float value readed by 10
4758 temperature = rawtemperature / 10.0 ;
4859 humidity = rawhumidity / 10.0 ;
4960 Serial.println (temperature);
5061 Serial.println (humidity);
5162 }
5263
5364 delay (5000 );
54- }
65+ }
0 commit comments