-
Notifications
You must be signed in to change notification settings - Fork 129
Added Example ModbusRTUTemperatureSensor #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
e19c7a8
aa7f841
9a9f276
d66cf7c
34f914a
d271e6d
e809061
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
Modbus RTU Temeperature Sensor | ||
|
||
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 | ||
- 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-; | ||
- 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 ON | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You didn't mention the VCC connection for the sensor. |
||
created 8 August 2018 | ||
by Riccardo Rizzo | ||
*/ | ||
|
||
#include <ArduinoModbus.h> | ||
|
||
float temperature; | ||
float 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 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, the sensor sent the readings as bytes packet, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. holding registers are actually 16-bits in size. |
||
// through the read() function is possible read the measurments. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. read the raw values |
||
// the readings is parsed as a short integer from the packets | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nothing is parsed |
||
short rawtemperature = ModbusRTUClient.read(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need an explanation of what the sensor returns and why we need to divide by 10.0. |
||
short rawhumidity = ModbusRTUClient.read(); | ||
|
||
// Is required divide by 10.0 the value readed, because the sensor sent the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. readed -> read, sent -> sends |
||
// readings as an integer obtained multipling the float value readed by 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's a divide not multiply :) the raw values need to be divided by 10 to get the temperature in Celsius and the humidity reading as a percentage. |
||
temperature = rawtemperature / 10.0; | ||
humidity = rawhumidity / 10.0; | ||
Serial.println(temperature); | ||
Serial.println(humidity); | ||
} | ||
|
||
delay(5000); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What voltage and current is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Rocketct from the product page "Working voltage: 9-36V"