Skip to content

Example: read temperature and humidity via Modbus RTU from MD02. #154

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

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Changes from all commits
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,76 @@
/*
Modbus RTU Client MD02 Temperature Humidity sensor

This sketch creates a Modbus RTU Client and demonstrates
how to use read temperature and humidity values as sensed
by the RTU Modbus capable MD02 sensor.
*/

/**************************************************************************************
* INCLUDE
**************************************************************************************/

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

/**************************************************************************************
* CONSTANTS
**************************************************************************************/

static unsigned int const MODBUS_BAUDRATE = 9600;
static float const MODBUS_BIT_DURATION = 1.f / MODBUS_BAUDRATE;
static float const MODBUS_PRE_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;
static float const MODBUS_POST_DELAY_BR = MODBUS_BIT_DURATION * 9.6f * 3.5f * 1e6;

static int const MODBUS_DEVICE_ID = 1;
static int const MODBUS_DEVICE_TEMPERATURE_REGISTER = 0x0001;
static int const MODBUS_DEVICE_HUMIDITY_REGISTER = 0x0002;

/**************************************************************************************
* SETUP/LOOP
**************************************************************************************/

void setup()
{
Serial.begin(9600);
while (!Serial);

RS485.setDelays(MODBUS_PRE_DELAY_BR, MODBUS_POST_DELAY_BR);

if (!ModbusRTUClient.begin(MODBUS_BAUDRATE, SERIAL_8N1))
{
Serial.println("Failed to start Modbus RTU Client!");
for (;;) { }
}

ModbusRTUClient.setTimeout(2 * 1000UL); /* 2 seconds. */
}

void loop()
{
if (!ModbusRTUClient.requestFrom(MODBUS_DEVICE_ID, INPUT_REGISTERS, MODBUS_DEVICE_TEMPERATURE_REGISTER, 1)) {
Serial.print("failed to read temperature register! ");
Serial.println(ModbusRTUClient.lastError());
return;
}
if (ModbusRTUClient.available())
{
int16_t const temperature_raw = ModbusRTUClient.read();
float const temperature_deg = temperature_raw / 100.f;
Serial.println(temperature_deg);
}

if (!ModbusRTUClient.requestFrom(MODBUS_DEVICE_ID, INPUT_REGISTERS, MODBUS_DEVICE_HUMIDITY_REGISTER, 1)) {
Serial.print("failed to read humidity register! ");
Serial.println(ModbusRTUClient.lastError());
return;
}
if (ModbusRTUClient.available())
{
int16_t const humidity_raw = ModbusRTUClient.read();
float const humidity_per_cent = humidity_raw / 100.f;
Serial.println(humidity_per_cent);
}

delay(1000);
}
Loading