Hello, can someone modify this code in such a way that the values will move to the real-time database of Firebase? I need to access all 8 parameter values in the real-time database of google firebase.
// Get the Modbus master library at https://github.com/4-20ma/ModbusMaster
// Get the DHT sensor library at https://github.com/adafruit/DHT-sensor-library and https://github.com/adafruit/DHT-sensor-library
#include <ModbusMaster.h>
#include <HardwareSerial.h>
#include <DHT.h>
const int RO_PIN = 13; // Receive (data in) pin
const int DI_PIN = 12; // Transmit (data out) pin
const int RE_PIN = 22;
const int DE_PIN = 23;
const int DHT_PIN = 4; // DHT data pin
const int DHT_TYPE = DHT11; // DHT sensor type (DHT11, DHT21, DHT22)
HardwareSerial swSerial(2); // Use Serial2 for communication
ModbusMaster node;
DHT dht(DHT_PIN, DHT_TYPE);
// Put the MAX485 into transmit mode
void preTransmission()
{
digitalWrite(RE_PIN, HIGH);
digitalWrite(DE_PIN, HIGH);
}
// Put the MAX485 into receive mode
void postTransmission()
{
digitalWrite(RE_PIN, LOW);
digitalWrite(DE_PIN, LOW);
}
void setup()
{
Serial.begin(9600);
// Configure the MAX485 RE & DE control signals and enable receive mode
pinMode(RE_PIN, OUTPUT);
pinMode(DE_PIN, OUTPUT);
digitalWrite(DE_PIN, LOW);
digitalWrite(RE_PIN, LOW);
// Modbus communication runs at 9600 baud
swSerial.begin(9600, SERIAL_8N1, RO_PIN, DI_PIN);
// Modbus slave ID of the NPK sensor is 2
node.begin(2, swSerial);
// Callbacks to allow us to set the RS485 Tx/Rx direction
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
// Initialize the DHT sensor
dht.begin();
delay(1000);
}
void loop()
{
uint8_t result;
// Remove any characters from the receive buffer
// Ask for 7x 16-bit words starting at register address 0x0000
result = node.readHoldingRegisters(0x0000, 7);
if (result == node.ku8MBSuccess)
{
Serial.println("Reply:");
Serial.print("Temp: ");
Serial.print(node.getResponseBuffer(0) / 10.0);
Serial.println(" F");
Serial.print("Moisture: ");
Serial.print(node.getResponseBuffer(1) / 10.0);
Serial.println("%");
Serial.print("EC: ");
Serial.print(node.getResponseBuffer(2));
Serial.println(" uS/cm");
Serial.print("pH: ");
Serial.print(node.getResponseBuffer(3) / 100.0);
Serial.println("");
Serial.print("N: ");
Serial.print(node.getResponseBuffer(4) * 10);
Serial.println(" mg/kg");
Serial.print("P: ");
Serial.print(node.getResponseBuffer(5) * 10);
Serial.println(" mg/kg");
Serial.print("K: ");
Serial.print(node.getResponseBuffer(6) * 10);
Serial.println(" mg/kg");
}
else
{
printModbusError(result);
}
// Read temperature and humidity from DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if any valid data was obtained from the DHT sensor
if (!isnan(temperature) && !isnan(humidity)) {
//Serial.print("DHT Temperature: ");
//Serial.print(temperature);
//Serial.println(" °C");
Serial.print("DHT Humidity: ");
Serial.print(humidity);
Serial.println("%");
} else {
Serial.println("Failed to read data from DHT sensor");
}
delay(2000);
}
// Print out the error received from the Modbus library
void printModbusError(uint8_t errNum)
{
switch (errNum)
{
case node.ku8MBSuccess:
Serial.println(F("Success"));
break;
case node.ku8MBIllegalFunction:
Serial.println(F("Illegal Function Exception"));
break;
case node.ku8MBIllegalDataAddress:
Serial.println(F("Illegal Data Address Exception"));
break;
case node.ku8MBIllegalDataValue:
Serial.println(F("Illegal Data Value Exception"));
break;
case node.ku8MBSlaveDeviceFailure:
Serial.println(F("Slave Device Failure"));
break;
case node.ku8MBInvalidSlaveID:
Serial.println(F("Invalid Slave ID"));
break;
case node.ku8MBInvalidFunction:
Serial.println(F("Invalid Function"));
break;
case node.ku8MBResponseTimedOut:
Serial.println(F("Response Timed Out"));
break;
case node.ku8MBInvalidCRC:
Serial.println(F("Invalid CRC"));
break;
default:
Serial.println(F("Unknown Error"));
break;
}
}