From e19c7a8e03eae5608bcf91a1a709159bc6e6764a Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Tue, 7 Aug 2018 17:04:16 +0200 Subject: [PATCH 1/7] Added Example ModbusRTUTemperatureSensor Added ModbusRTUTemperatureSensor example to the library ArduinoModbus --- .../ModbusRTUTemperatureSensor.ino | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino new file mode 100644 index 0000000..c83615a --- /dev/null +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -0,0 +1,54 @@ +/* + Modbus RTU Temeperature Sensor + + This sketch show how to use the modbus library, in in order to sent a request to a slave RTU sensor + unit and read the responce packet return by the remote unit. + + Circuit: + - MKR board + - TModbus RS485 Temperature + - MKR 485 shield + - ISO GND connected to GND of the Modbus RTU server + - Y connected to A/Y of the Modbus RTU sensor + - Z connected to B/Z of the Modbus RTU sensor + - Jumper positions + - FULL set to OFF + - Z \/\/ Y set to OFF + + created 16 July 2018 + by Sandeep Mistry +*/ + +#include + +float temperature, humidity; + +void setup() { + Serial.begin(9600); + while (!Serial); + + Serial.println("Modbus Temperature Humidity Sensor"); + // start the Modbus RTU client + if (!ModbusRTUClient.begin(9600)) { + Serial.println("Failed to start Modbus RTU Client!"); + while (1); + } +} + +void loop() { + //send a reading request to the RTU slave with id=1, type=HOLDING_REGISTERS address=0, number of values to read, nb=2 + if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) { + Serial.print("failed to read registers! "); + Serial.println(ModbusRTUClient.lastError()); + } else { + //if the request goes fine read the value with the read() function + short rawtemperature = ModbusRTUClient.read(); + short rawhumidity = ModbusRTUClient.read(); + temperature = rawtemperature / 10.0; + humidity = rawhumidity / 10.0; + Serial.println(temperature); + Serial.println(humidity); + } + + delay(5000); +} From aa7f84130bdc921ba34aba5ef3c931f514de5697 Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Wed, 8 Aug 2018 16:35:12 +0200 Subject: [PATCH 2/7] Added comment to the ModbusRTUTemperatureSensor example added some comments to the ModbusRTUTemperatureSensor example --- .../ModbusRTUTemperatureSensor.ino | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index c83615a..1ef2bfd 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -1,27 +1,31 @@ /* Modbus RTU Temeperature Sensor - This sketch show how to use the modbus library, in in order to sent a request to a slave RTU sensor - unit and read the responce packet return by the remote unit. + This sketch shows you how to interact with a Modbus RTU temperature and humidity sensor. + It reads the temperature and humidity values every 5 seconds and outputs them to the + serial monitor. Circuit: - MKR board - - TModbus RS485 Temperature + - Modbus RS485 Temperature: + - External power Supply - MKR 485 shield - - ISO GND connected to GND of the Modbus RTU server + - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-; + - Power supply V+ connected to V+ sensor - Y connected to A/Y of the Modbus RTU sensor - Z connected to B/Z of the Modbus RTU sensor - Jumper positions - FULL set to OFF - - Z \/\/ Y set to OFF + - Z \/\/ Y set to ON - created 16 July 2018 - by Sandeep Mistry + created 8 August 2018 + by Riccardo Rizzo */ #include -float temperature, humidity; +float temperature; +float humidity; void setup() { Serial.begin(9600); @@ -36,14 +40,21 @@ void setup() { } void loop() { - //send a reading request to the RTU slave with id=1, type=HOLDING_REGISTERS address=0, number of values to read, nb=2 + + // send a Holding registers read request to (slave) id 1, for 2 registers if (!ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0x00, 2)) { Serial.print("failed to read registers! "); Serial.println(ModbusRTUClient.lastError()); } else { - //if the request goes fine read the value with the read() function + + // If the request goes fine, the sensor sent the readings as bytes packet, + // through the read() function is possible read the measurments. + // the readings is parsed as a short integer from the packets short rawtemperature = ModbusRTUClient.read(); short rawhumidity = ModbusRTUClient.read(); + + // Is required divide by 10.0 the value readed, because the sensor sent the + // readings as an integer obtained multipling the float value readed by 10 temperature = rawtemperature / 10.0; humidity = rawhumidity / 10.0; Serial.println(temperature); @@ -51,4 +62,4 @@ void loop() { } delay(5000); -} +} \ No newline at end of file From 9a9f2763da42fa32c6da9b3e8e49561002cdbd02 Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Fri, 10 Aug 2018 15:56:32 +0200 Subject: [PATCH 3/7] Added comments on example Added a comment on the example ModbusRTUTemperatureSensor.ino --- .../ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index 1ef2bfd..355d339 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -7,7 +7,8 @@ Circuit: - MKR board - - Modbus RS485 Temperature: + - Winners® Modbus RS485 Temperature and Humidity: + https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN - External power Supply - MKR 485 shield - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-; From d66cf7c5ba13a3db26d3d04d76c21663ba2ac7f0 Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Mon, 20 Aug 2018 09:48:55 +0200 Subject: [PATCH 4/7] Comments changes in ModbusRTUTemperatureSensor Changed comment in ModbusRTUTemperatureSensor example. --- .../ModbusRTUTemperatureSensor.ino | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index 355d339..1792eb5 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -48,14 +48,14 @@ void loop() { Serial.println(ModbusRTUClient.lastError()); } else { - // If the request goes fine, the sensor sent the readings as bytes packet, - // through the read() function is possible read the measurments. - // the readings is parsed as a short integer from the packets + // If the request goes fine, the sensor sends the readings, this are + // stored in the holding register and through the read() function is + // possible get the temperature and the humidity as raw values. short rawtemperature = ModbusRTUClient.read(); short rawhumidity = ModbusRTUClient.read(); - // Is required divide by 10.0 the value readed, because the sensor sent the - // readings as an integer obtained multipling the float value readed by 10 + // Is required divide by 10.0 the raw value to get the temperature in Celsius + // and the humidity reading as a percentage. temperature = rawtemperature / 10.0; humidity = rawhumidity / 10.0; Serial.println(temperature); From 34f914aa5a782dc5d3db1928aeb301b635dc931e Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Tue, 21 Aug 2018 08:44:04 +0200 Subject: [PATCH 5/7] Comments changes ModbusRTUTemperatureSensor.ino Changed comments in the example ModbusRTUTemperatureSensor.ino --- .../ModbusRTUTemperatureSensor.ino | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index 1792eb5..342a784 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -11,8 +11,9 @@ https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN - External power Supply - MKR 485 shield - - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V-; + - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V- - Power supply V+ connected to V+ sensor + - Set the Power supply's voltage to 9 V and the Power supply's current to 1 A - Y connected to A/Y of the Modbus RTU sensor - Z connected to B/Z of the Modbus RTU sensor - Jumper positions @@ -48,14 +49,14 @@ void loop() { Serial.println(ModbusRTUClient.lastError()); } else { - // If the request goes fine, the sensor sends the readings, this are - // stored in the holding register and through the read() function is + // If the request goes fine, the sensor sends the readings, that are + // stored in the holding register and through the read() function it is // possible get the temperature and the humidity as raw values. short rawtemperature = ModbusRTUClient.read(); short rawhumidity = ModbusRTUClient.read(); - // Is required divide by 10.0 the raw value to get the temperature in Celsius - // and the humidity reading as a percentage. + // To get the temperature in Celsius and the humidity reading as + // a percentage, divide the raw value by 10.0. temperature = rawtemperature / 10.0; humidity = rawhumidity / 10.0; Serial.println(temperature); From d271e6d91e8dcd330eca731f95d7e831a77f75e2 Mon Sep 17 00:00:00 2001 From: Riccardo Rizzo Date: Tue, 21 Aug 2018 15:58:29 +0200 Subject: [PATCH 6/7] Comment changes on ModbusRTUTemperatureSensor.ino example Added a comment on the example ModbusRTUTemperatureSensor.ino --- .../ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index 342a784..7a559e3 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -13,7 +13,9 @@ - MKR 485 shield - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V- - Power supply V+ connected to V+ sensor - - Set the Power supply's voltage to 9 V and the Power supply's current to 1 A + - The Working voltage range of the sensor, from product page, is 9-36 V + - Set the Power supply's voltage to a value between 9 - 36 V + - Set the Power supply's current limit to 1 A - Y connected to A/Y of the Modbus RTU sensor - Z connected to B/Z of the Modbus RTU sensor - Jumper positions From e809061aa602032e7e469db6a164f0f74877b097 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Tue, 21 Aug 2018 07:33:28 -0700 Subject: [PATCH 7/7] Update ModbusRTUTemperatureSensor.ino --- .../ModbusRTUTemperatureSensor.ino | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino index 7a559e3..dceb1dd 100644 --- a/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino +++ b/examples/RTU/ModbusRTUTemperatureSensor/ModbusRTUTemperatureSensor.ino @@ -9,13 +9,10 @@ - MKR board - Winners® Modbus RS485 Temperature and Humidity: https://www.banggood.com/Modbus-RS485-Temperature-and-Humidity-Transmitter-Sensor-High-Precision-Monitoring-p-1159961.html?cur_warehouse=CN - - External power Supply + - External 9-36 V power Supply - MKR 485 shield - ISO GND connected to GND of the Modbus RTU sensor and the Power supply V- - Power supply V+ connected to V+ sensor - - The Working voltage range of the sensor, from product page, is 9-36 V - - Set the Power supply's voltage to a value between 9 - 36 V - - Set the Power supply's current limit to 1 A - Y connected to A/Y of the Modbus RTU sensor - Z connected to B/Z of the Modbus RTU sensor - Jumper positions @@ -50,10 +47,9 @@ void loop() { Serial.print("failed to read registers! "); Serial.println(ModbusRTUClient.lastError()); } else { - - // If the request goes fine, the sensor sends the readings, that are - // stored in the holding register and through the read() function it is - // possible get the temperature and the humidity as raw values. + // If the request succeeds, the sensor sends the readings, that are + // stored in the holding registers. The read() method can be used to + // get the raw temperature and the humidity values. short rawtemperature = ModbusRTUClient.read(); short rawhumidity = ModbusRTUClient.read(); @@ -66,4 +62,4 @@ void loop() { } delay(5000); -} \ No newline at end of file +}