1
1
/*
2
2
Modbus RTU Temeperature Sensor
3
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.
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.
6
7
7
8
Circuit:
8
9
- MKR board
9
- - TModbus RS485 Temperature
10
+ - Modbus RS485 Temperature:
11
+ - External power Supply
10
12
- 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
12
15
- Y connected to A/Y of the Modbus RTU sensor
13
16
- Z connected to B/Z of the Modbus RTU sensor
14
17
- Jumper positions
15
18
- FULL set to OFF
16
- - Z \/\/ Y set to OFF
19
+ - Z \/\/ Y set to ON
17
20
18
- created 16 July 2018
19
- by Sandeep Mistry
21
+ created 8 August 2018
22
+ by Riccardo Rizzo
20
23
*/
21
24
22
25
#include < ArduinoModbus.h>
23
26
24
- float temperature, humidity;
27
+ float temperature;
28
+ float humidity;
25
29
26
30
void setup () {
27
31
Serial.begin (9600 );
@@ -36,19 +40,26 @@ void setup() {
36
40
}
37
41
38
42
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
43
+
44
+ // send a Holding registers read request to (slave) id 1, for 2 registers
40
45
if (!ModbusRTUClient.requestFrom (1 , HOLDING_REGISTERS, 0x00 , 2 )) {
41
46
Serial.print (" failed to read registers! " );
42
47
Serial.println (ModbusRTUClient.lastError ());
43
48
} 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
45
53
short rawtemperature = ModbusRTUClient.read ();
46
54
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
47
58
temperature = rawtemperature / 10.0 ;
48
59
humidity = rawhumidity / 10.0 ;
49
60
Serial.println (temperature);
50
61
Serial.println (humidity);
51
62
}
52
63
53
64
delay (5000 );
54
- }
65
+ }
0 commit comments