/* DHT-22 sensor with 12c 16x2 LCD with Arduino uno
Temperature and humidity sensor displayed in LCD
based on: http://www.ardumotive.com/how-to-use-dht-22-sensor-en.html and
https://www.ardumotive.com/i2clcden.html for the i2c LCD library by Michalis Vasilakis
Recompile by adhitadhitadhit
Notes: use LCD i2c Library from link above, i'm not sure why but new Liquidcristal library from Francisco Malpartida isn't working for me
other thing, check your */
//Libraries
#include <dht.h> // sensor library using lib from https://www.ardumotive.com/how-to-use-dht-22-sensor-en.html
#include <LiquidCrystal_I2C.h> // LCD library using from https://www.ardumotive.com/i2clcden.html for the i2c LCD library
#include <Wire.h>
#include <ModbusRTUSlave.h>
#include <SoftwareSerial.h>
dht DHT;
//Constants
#define DHT22_PIN 2 // DHT 22 (AM2302) - pin used for DHT22
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 after finding it from serial monitor (see comment above) for a 16 chars and 2 line display
#define SLAVE_ID 1 // Slave ID (1-127)
const byte dePin = 4;
const byte rxPin = 1;
const byte txPin = 0;
SoftwareSerial mySerial(rxPin, txPin);
ModbusRTUSlave modbus(mySerial, dePin); // Use default baud rate
uint16_t holdingRegisters[2];
//Variables
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setBacklight(HIGH);
modbus.configureHoldingRegisters(holdingRegisters, 2); // Configure registers
}
void loop()
{
// ModbusRtu.poll(); // Poll for Modbus requests
int chk = DHT.read22(DHT22_PIN);
//Read data and store it to variables hum and temp
hum = DHT.humidity;
temp= DHT.temperature;
//Print temp and humidity values to LCD
lcd.setCursor(0,0);
lcd.print("Humidity: ");
lcd.print(hum);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Temp: ");
lcd.print(temp);
lcd.println(" C");
delay(2000); //Delay 2 sec between temperature/humidity check.
if (hum > 50){
digitalWrite(8, HIGH);
digitalWrite(7, LOW);
delayMicroseconds(500);
}
else(hum < 30);
{
digitalWrite(8, LOW);
digitalWrite(7, HIGH);
}
// Store values in Modbus registers (correct way)
holdingRegisters[0] = temp * 10; // Multiply by 10 for 1 decimal place
holdingRegisters[1] = hum;
// uint8_t cbWrite(Modbus::ResultCode event, uint16_t transactionID, void* data) {
//return 0;
modbus.poll(); // Check for Modbus requests
/*
// Store temperature and humidity values in Modbus registers
t_reg = DHT.temperature * 10; // Multiply by 10 for 1 decimal place
h_reg = DHT.humidity;
// Function to handle Modbus read requests
uint8_t cbWrite(Modbus::ResultCode event, uint16_t transactionID, void* data) {
return 0;
}
// Function to handle Modbus write requests (not used in this example)
uint16_t cbRead(Modbus::ResultCode event, uint16_t transactionID, void* data) {
return 0;
} */
}