0% found this document useful (0 votes)
18 views

Code

This Arduino code configures a Modbus slave device to communicate sensor data from a DHT11 temperature and humidity sensor to a Modbus master over serial. It initializes a register bank to store digital input, output, analog input and output registers. Temperature and humidity readings are periodically taken from the DHT11 sensor and written to analog input and output registers. Digital status registers are also updated based on the temperature. The Modbus slave protocol is run to handle communication with the Modbus master.

Uploaded by

Dariel Tema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Code

This Arduino code configures a Modbus slave device to communicate sensor data from a DHT11 temperature and humidity sensor to a Modbus master over serial. It initializes a register bank to store digital input, output, analog input and output registers. Temperature and humidity readings are periodically taken from the DHT11 sensor and written to analog input and output registers. Digital status registers are also updated based on the temperature. The Modbus slave protocol is run to handle communication with the Modbus master.

Uploaded by

Dariel Tema
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <modbus.

h>
#include <modbusDevice.h>
#include <modbusRegBank.h>
#include <modbusSlave.h>
#include <SimpleDHT.h>
#include <TimerOne.h>

// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11;
// int pinRelay1 =7;
// int pin_led1=8;
/*
This example code shows a quick and dirty way to get an
arduino to talk to a modbus master device with a
device ID of 1 at 9600 baud.
*/
// #define interval 5000
//Setup the brewtrollers register bank
//All of the data accumulated will be stored here
modbusDevice regBank;
//Create the modbus slave protocol handler
modbusSlave slave;
void setup()
{

//Assign the modbus device ID.


regBank.setId(1);
Timer1.initialize(500000);
Timer1.attachInterrupt(waktu);
//Timer1.start();
// pinMode(pinRelay1,OUTPUT);
// pinMode(pin_led1,OUTPUT);
/*
modbus registers follow the following format
00001-09999 Digital Outputs, A master device can read and write to these
registers
10001-19999 Digital Inputs, A master device can only read the values from these
registers
30001-39999 Analog Inputs, A master device can only read the values from these
registers
40001-49999 Analog Outputs, A master device can read and write to these
registers

Analog values are 16 bit unsigned words stored with a range of 0-32767
Digital values are stored as bytes, a zero value is OFF and any nonzer value is
ON

It is best to configure registers of like type into contiguous blocks. this


allows for more efficient register lookup and and reduces the number of messages
required by the master to retrieve the data
*/

//Add Digital Output registers 00001-00016 to the register bank


regBank.add(1);
regBank.add(2);
regBank.add(3);
regBank.add(4);
regBank.add(5);
regBank.add(6);
regBank.add(7);
regBank.add(8);
regBank.add(9);
regBank.add(10);
regBank.add(11);
regBank.add(12);
regBank.add(13);
regBank.add(14);
regBank.add(15);
regBank.add(16);

//Add Digital Input registers 10001-10008 to the register bank


regBank.add(10001);
regBank.add(10002);
regBank.add(10003);
regBank.add(10004);
regBank.add(10005);
regBank.add(10006);
regBank.add(10007);
regBank.add(10008);

//Add Analog Input registers 30001-10010 to the register bank


regBank.add(30001);
regBank.add(30002);
regBank.add(30003);
regBank.add(30004);
regBank.add(30005);
regBank.add(30006);
regBank.add(30007);
regBank.add(30008);
regBank.add(30009);
regBank.add(30010);

//Add Analog Output registers 40001-40020 to the register bank


regBank.add(40001);
regBank.add(40002);
regBank.add(40003);
regBank.add(40004);
regBank.add(40005);
regBank.add(40006);
regBank.add(40007);
regBank.add(40008);
regBank.add(40009);
regBank.add(40010);
regBank.add(40011);
regBank.add(40012);
regBank.add(40013);
regBank.add(40014);
regBank.add(40015);
regBank.add(40016);
regBank.add(40017);
regBank.add(40018);
regBank.add(40019);
regBank.add(40020);
/*
Assign the modbus device object to the protocol handler
This is where the protocol handler will look to read and write
register data. Currently, a modbus slave protocol handler may
only have one device assigned to it.
*/
slave._device = &regBank;

// Initialize the serial port for coms at 9600 baud


slave.setBaud(9600);
regBank.set(1, 0);
regBank.set(2, 0);
regBank.set(3, 0);
regBank.set(4, 0);
regBank.set(5, 0);
regBank.set(6, 0);
regBank.set(7, 0);
regBank.set(8, 0);

regBank.set(10001, 0);
regBank.set(10002, 0);
regBank.set(10003, 0);
regBank.set(10004, 0);
regBank.set(10005, 0);
regBank.set(10006, 0);
regBank.set(10007, 0);
regBank.set(10008, 0);

regBank.set(30001,0);
regBank.set(30002,0);
regBank.set(30003,0);
regBank.set(30004,0);
regBank.set(30005,0);
regBank.set(30006,0);
regBank.set(30007,0);
regBank.set(30008,0);
regBank.set(30009,0);
regBank.set(30010,0);

regBank.set(40001,0);
regBank.set(40002,0);
regBank.set(40003,0);
regBank.set(40004,0);
regBank.set(40005,0);
regBank.set(40006,0);
regBank.set(40007,0);
regBank.set(40008,0);
regBank.set(40009,0);
regBank.set(40010,0);
}
byte temperature = 0;
byte humidity = 0;

void waktu(){
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) !=
SimpleDHTErrSuccess);
regBank.set(40001,(int)temperature);
regBank.set(40002,(int)humidity);

regBank.set(30001,(int)temperature);
regBank.set(30002,(int)humidity);

if ((int)temperature>0){
regBank.set(00001,(byte)1);
regBank.set(00002,(byte)1);

regBank.set(10001,(byte)1);
regBank.set(10002,(byte)1);
} else {
regBank.set(00001,(byte)0);
regBank.set(00002,(byte)0);

regBank.set(10001,(byte)0);
regBank.set(10002,(byte)0);
}
slave.run();
}

void loop()
{
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) !=
SimpleDHTErrSuccess);//{
// Serial.print("Read DHT11 failed, err="); Serial.print(err);
// return;
// }
// regBank.set(40001,(int)temperature);
// regBank.set(40002,(int)humidity);

// regBank.set(30001,(int)temperature);
// regBank.set(30002,(int)humidity);
slave.run();
}

You might also like