OK, I have the arduino nano hooked up to the DHT22 and was able to load firmware onto it that printed out the temp, humidity, & heat index to the serial monitor, so I know my wiring is good and that the sensor and the nano are functioning properly. Now to modify the openplc arduino firmware to populate those data points into registers.
Below is what my code is looking like using the adafruit DHT library:
So as not to mess with the I/O ports connected to physical pins, I added an additional 3 input registers which I attempt to manually set to the values obtained from the DHT and set the nano up as a generic RTU device in openplc so I could add the 3 additional IRs.
I'm being thrown by the fact I'm getting floats from the DHT but the IRs are word values. Adds/mods to the stock nano openplc code are bolded:
#include <Arduino.h>
#include "Modbus.h"
#include "ModbusSerial.h"
#include <DHT.h>
#include <DHT_U.h>
// DHT setup info
#define DHTPIN 4 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
//ModBus Port information
#define BAUD 115200
#define ID 0
#define TXPIN -1
//Define the number of registers, inputs and coils to be created
#define NUM_DISCRETE_INPUTS 5
#define NUM_INPUT_REGISTERS 6
#define NUM_TOTAL_IRS 9
#define NUM_INPUT_REGISTERS 6
#define NUM_COILS 4
#define NUM_HOLDING_REGISTERS 3
//Create the I/O pin masks
uint8_t pinMask_DIN[] = {2, 3, 4, 5, 6};
uint8_t pinMask_AIN[] = {A0, A1, A2, A3, A4, A5};
uint8_t pinMask_DOUT[] = {7, 8, 12, 13};
uint8_t pinMask_AOUT[] = {9, 10, 11};
//Modbus Object
ModbusSerial modbus;
void configurePins()
{
for (int i = 0; i < NUM_DISCRETE_INPUTS; i++)
{
pinMode(pinMask_DIN[i], INPUT);
}
for (int i = 0; i < NUM_INPUT_REGISTERS; i++)
{
pinMode(pinMask_AIN[i], INPUT);
}
for (int i = 0; i < NUM_COILS; i++)
{
pinMode(pinMask_DOUT[i], OUTPUT);
}
for (int i = 0; i < NUM_HOLDING_REGISTERS; i++)
{
pinMode(pinMask_AOUT[i], OUTPUT);
}
}
void setup()
{
//Setup board I/O
configurePins();
dht.begin();
//Config Modbus Serial (port, speed, rs485 tx pin)
modbus.config(&Serial, BAUD, TXPIN);
//Set the Slave ID
modbus.setSlaveId(ID);
//Add all modbus registers
for (int i = 0; i < NUM_DISCRETE_INPUTS; ++i)
{
modbus.addIsts(i);
}
for (int i = 0; i < NUM_TOTAL_IRS; ++i)
{
modbus.addIreg(i);
}
for (int i = 0; i < NUM_COILS; ++i)
{
modbus.addCoil(i);
}
for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i)
{
modbus.addHreg(i);
}
}
void loop()
{
//Run the main modbus task
modbus.task();
float h = dht.readHumidity();
// Reading temperature or humidity takes about 250 milliseconds!
float f = dht.readTemperature(true);
// Read temperature as Fahrenheit (isFahrenheit = true)
float hif = dht.computeHeatIndex(f, h);
//compute heat index from temp and humidity
modbus.Ireg(6,word(h));
modbus.Ireg(7,word(f));
modbus.Ireg(8,word(hif));
//Update modbus registers
for (int i = 0; i < NUM_DISCRETE_INPUTS; ++i)
{
modbus.Ists(i, digitalRead(pinMask_DIN[i]));
}
for (int i = 0; i < NUM_INPUT_REGISTERS; ++i)
{
modbus.Ireg(i, (analogRead(pinMask_AIN[i]) * 64));
}
for (int i = 0; i < NUM_COILS; ++i)
{
digitalWrite(pinMask_DOUT[i], modbus.Coil(i));
}
for (int i = 0; i < NUM_HOLDING_REGISTERS; ++i)
{
analogWrite(pinMask_AOUT[i], (modbus.Hreg(i) / 256));
}
}
thoughts?