So the compilation worked and I believe my wiring is all correct, however I am not getting the result I was "expecting".
What I thought would happen was this: that the temp data would be stored in register DI2 and then my LCD would print the data from that register.
Here is my modification of your original example:
#include <LiquidCrystal.h>
#include <DHT.h>
int rs=7;
int en=8;
int d4=9;
int d5=10;
int d6=11;
int d7=12;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
#define Type DHT11
int sensePin=2;
DHT HT(sensePin,Type);
float tempC;
// On a Mega you can simply use
// a Reference to an existing HW Serial:
// HardwareSerial &mySerial = Serial3;
// If you don't need debugging output, you can use
// a reference to Serial
// HardwareSerial &mySerial = Serial;
HardwareSerial &mySerial = Serial1;
constexpr byte modbus_format = SERIAL_8N1; // leave it on SERIAL_8N1 for SoftSerial
constexpr uint32_t modbus_baud = 19200; // use only slow speeds with SoftSerial
constexpr byte modbus_enable_pin = 4; // The GPIO used to control the MAX485 TX pin. Set to 255 if not needed (i.e. a selfsensing module or RS232).
/* **************************************************** *
* Modbus
* **************************************************** */
#include <NoiascaModbusServerSimple.h> // Modbus Slave library (low level RS484 communication)
#include <NoiascaModbusCommon.h> // predefined Modbus registers for the library examples
constexpr byte modbus_slave_id = 2; // Modbus address of this slave
// FC3/FC16 holding registers - not used in this example
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0 which is for Function 3 Register 40001
DI2, // a digitlRead of a GPIO
DO13, // a digitalWrite to GPIO
ADC0, // analogRead
ADC1, // analogRead
ADC2, // analogRead
ADC3, // analogRead
HOLDING_REGS_SIZE // leave this one
};
unsigned int holdingRegs[HOLDING_REGS_SIZE]; // FC3/FC16 register array
ModbusServer modbusServer(modbus_slave_id, mySerial);
/*
update the holding register (read the pins)
and update the pins according to
this function should be called over and over again in loop
*/
void updateRegisters()
{
// write new values to GPIO
digitalWrite(13, holdingRegs[DO13]);
// update data to be read by the master
holdingRegs[DI2] = HT.readTemperature();
holdingRegs[ADC0] = analogRead(A0);
holdingRegs[ADC1] = analogRead(A1);
holdingRegs[ADC2] = analogRead(A2);
holdingRegs[ADC3] = analogRead(A3);
}
/*
if you need to control the transmit pin manually
this callback will be called before the transmission starts
*/
void cbPreTransmission()
{
digitalWrite(modbus_enable_pin, HIGH);
}
/*
if you need to control the transmit pin manually
this callback will be called after the transmission has ended
*/
void cbPostTransmission()
{
digitalWrite(modbus_enable_pin, LOW);
}
/*
this functions can be called,
when a Modbus Client writes a value with FC6 or several values with FC16
This example only shows a debug messages to indicate Master activity.
*/
void cbRegisterWritten(const uint16_t reg, const uint16_t value)
{
Serial.print(F("write register:")); Serial.print(reg); Serial.print(F(" value:")); Serial.println(value);
}
/*
this functions can be called,
when a Modbus Client read values from this Modbus Server (FC3).
If you don't need the debug messages don't set the callback.
This example only shows a debug messages to indicate Master activity.
*/
void cbRegisterRead(const uint16_t reg, const uint16_t count)
{
Serial.print(F("read from start register:")); Serial.print(reg); Serial.print(F(" count:")); Serial.println(count);
}
/* **************************************************** *
* Arduino default setup and loop
* **************************************************** */
void setup()
{
lcd.begin(16,2);
HT.begin();
delay(100);
Serial.begin(115200); // only used for debugging
Serial.println(F("Modbus LCD Slave OOP")); // comment this lines when you use the hardware serial for Modbus
Serial.flush();
mySerial.begin(modbus_baud); // start the Serial for Modbus
modbusServer.begin(&mySerial, modbus_baud, modbus_format, modbus_slave_id, HOLDING_REGS_SIZE, holdingRegs);
pinMode(modbus_enable_pin, OUTPUT); // if you need a TX enable pin, set it to Output
modbusServer.setPreTransmission(cbPreTransmission);
modbusServer.setPostTransmission(cbPostTransmission);
modbusServer.setRegisterWritten(cbRegisterWritten);
modbusServer.setRegisterRead(cbRegisterRead);
// init your local hardware
pinMode(13, OUTPUT);
}
void loop()
{
modbusServer.update(); // call modbus in loop
updateRegisters(); // handle GPIOs (outputs like relais or LEDs and Inputs)
// do other things non blocking in loop
lcd.setCursor(0,0);
lcd.print(DI2);
delay(1000);
lcd.clear();
}
first, please tell:
Did the HelloWorld Example from Modbus Slave Library work like expected?
Did the HelloWorld Example react on all provided registers / addresses?
Is your Modbus working?
And then ask yourself:
is the DHT working?
does the example sketch of DHT Sensor work?
is the LCD working?
does the example sketch of LCD work?
put a lcd.print("end setup"); at the end in your setup
does it get printed?
If you can answer all questions with a clear yes, I can give one hint,
analyze this line
what should this line print to the lcd?
What is the content of DI2?
What do you expect there instead?
I have NOT tested the HelloWorld example from the library. In my mind I was adding my own LCD and temp reader in order to test it.
How do I test it as it is (without editing it)?
what should this line print to the lcd?
What is the content of DI2?
What do you expect there instead?
My expectation of the line lcd.print(DI2); was this:
DHT reads temperature --> DHT sends temperature data to the DI2 "digital in" register --> LCD prints the data from the DI2 register.
Actually begins the slave and activates/starts up the registers.
Please correct me if this understanding in not correct and also please explain how I am to correctly understand the creation of registers and register data in this code (?).
Hi Noiasca,
I spoke with a person at a company I know who is familiar with Modbus. He recommended I use the library ModbusRtu since it can make both a slave and master and is quite simple to use. So I have switched to that library and learned the functions I need.
However the help you have given me has been very valuable and made me understand more about coding in general and Modbus, and has also made it much easier for me to understand the other library.
Thank you for your help & instructions