Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: 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.

unit and read the responce packet return by the remote unit.

Circuit:
- MKR board
- TModbus RS485 Temperature
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is extra "T" at the start, we should add the product URL to I think

- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the termination should be set to ON


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't mention the VCC connection for the sensor.

created 16 July 2018
by Sandeep Mistry
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I made this sketch :)

Please update the date and name accordingly.

*/

#include <ArduinoModbus.h>

float temperature, humidity;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's put the variables on one line each please.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to:

// send a Holding registers read request to (slave) id 1, for 2 registers

also make sure if a comment is too long to split it to multiple lines.

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();
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
temperature = rawtemperature / 10.0;
humidity = rawhumidity / 10.0;
Serial.println(temperature);
Serial.println(humidity);
}

delay(5000);
}