RS485 Master / Slave for DHT11 and Lux sensor

Hi, I’m having a bit of a problem with a project to use RS485. I’m new to arduino and I’m trying to write code that will allow me to have a remote arduino which has a DHT11 sensor for temperature and humidity and a LDR for Lux readings. Because of the distance I want to use rs485 protocol to connect this arduino to another arduino and serial print the DHT and Lux value from the remote arduino. The code is as follows (Master):

//RS485 Master program for DHT and Lux values
/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10  //Serial Receive pin
#define SSerialTX        11  //Serial Transmit pin

#define SSerialTxControl 3   //RS485 Direction control

#define RS485Transmit    HIGH
#define RS485Receive     LOW

#define Pin13LED         13
#include <DHT11.h>

// DHT 
#define DHTTYPE DHT11                //define which DHT chip used - DHT11

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
byte bGlobalErr;                     //for passing error code back.
byte dht_dat[4];                     //Array to hold the bytes sent from sensor.
float Lux;            //              |


void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  
  pinMode(Pin13LED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);    
  
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver   
  
  // Start the software serial port, to another device
  RS485Serial.begin(4800);   // set the data rate 

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  digitalWrite(Pin13LED, HIGH);  // Show activity
  
  if (Serial.available())
  {
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit for temperature
    RS485Serial.write('A');
    delay(10);
    RS485Serial.write('Temp');
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit for temperature
    byteReceived = Serial.read();
        if ('Temp' == byteReceived)
         {
         Serial.write("Temp = "); // Send Temp back
	 Serial.write(dht_dat[2]); // Send Temp back
         }
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit for humidity
    RS485Serial.write('A');
    delay(10);
    RS485Serial.write('Humidity');
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit for Humidity
    byteReceived = Serial.read();
        if ('Humidity' == byteReceived)
         {
         Serial.write("Humidity = "); // Send Humidity back
         Serial.write(dht_dat[0]); // Send Humidity back
         }
    digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit for Lux
    RS485Serial.write('A');
    delay(10);
    RS485Serial.write('Lux');        
    digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit for Lux 
    byteReceived = Serial.read();
        if ('Lux' == byteReceived)
         { 
         Serial.write("Lux = "); // Send Lux back
//	 Serial.write(Lux);
         } 
    digitalWrite(Pin13LED, LOW);  // Show activity    
    delay(10);
   
  }
  
  //if (RS485Serial.available())  //Look for data from other Arduino
   //{
   // digitalWrite(Pin13LED, HIGH);  // Show activity
   // byteReceived = RS485Serial.read();    // Read received byte
  //  Serial.write(byteReceived);        // Show on Serial Monitor
   // delay(10);
   // digitalWrite(Pin13LED, LOW);  // Show activity   
  // }  

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********

The code for the remote/slave device:

// Include RHT Library Code
#include <DHT11.h>

/*-----( Import needed libraries )-----*/
#include <SoftwareSerial.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define SSerialRX        10          //Serial Receive pin
#define SSerialTX        11          //Serial Transmit pin
#define SSerialTxControl 3           //RS485 Direction control
#define RS485Transmit    HIGH
#define RS485Receive     LOW
#define Pin13LED         13

// DHT 
#define DHTTYPE DHT11                //define which DHT chip used - DHT11
#define dht_dpin 69                  //pin for DHT11 (Actual pin 15)

//Light Sensor
int lightSensor = 68;                //pin for Photoresistor

/*-----( Declare objects )-----*/
SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX

/*-----( Declare Variables )-----*/
int byteReceived;
int byteSend;
int lightADCReading;                 //variables for measuring the light
double Lux;			          //              |
double lightInputVoltage;            //              |
double lightResistance;              //              |
byte bGlobalErr;                     //for passing error code back.
byte dht_dat[4];                     //Array to hold the bytes sent from sensor.

void setup()                         //Every block is in a separate function. 
{
  pinMode(dht_dpin,OUTPUT);
  digitalWrite(dht_dpin,HIGH);
  
  // Start the built-in serial port, probably to Serial Monitor
  Serial.begin(9600);
  Serial.println("SerialRemote");  // Can be ignored
  
  pinMode(Pin13LED, OUTPUT);   
  pinMode(SSerialTxControl, OUTPUT);  
  digitalWrite(SSerialTxControl, RS485Receive);  // Init Transceiver
  
  // Start the software serial port, to another device
  RS485Serial.begin(4800);   // set the data rate  
}

void loop()
{
  fotoLoop();                      //Light measurements loop through this one
  ReadDHT();                       //Temperature and Humidity Readings
   //Copy input data to output  
  if (RS485Serial.available()) 
  {
    byteSend = RS485Serial.read();   // Read the byte 
    
    digitalWrite(Pin13LED, HIGH);  // Show activity
    delay(100);              
    digitalWrite(Pin13LED, LOW);   
    //if (-1 != byteSend)
   // {
      if ('A' == byteSend)
       {  
           if ('Temp' == byteSend)
           {
              digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit    
	      RS485Serial.write(dht_dat[0]); // Send Temp back
	      digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit      
	   }   
           if ('Humidity' == byteSend)
           {
	      digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit                
	      RS485Serial.write(dht_dat[2]); // Send Humidity back
	      digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit
           }
           if ('Lux' == byteSend)
           { 
              digitalWrite(SSerialTxControl, RS485Transmit);  // Enable RS485 Transmit  
	      RS485Serial.write(Lux); // Send Lux back
	      digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit
           }   
      delay(100);   
      //digitalWrite(SSerialTxControl, RS485Receive);  // Disable RS485 Transmit      
      }
    // }  
//    delay(100);
  }// End If RS485SerialAvailable 

  Serial.print("LUX = ");
  Serial.println(Lux);
  Serial.print("Humidity = ");
  Serial.println(dht_dat[0]);
  Serial.print("Temp = ");
  Serial.println(dht_dat[2]);
  delay(2500); 
}
              
void ReadDHT()
{
  bGlobalErr=0;
  byte dht_in;
  byte i;
  digitalWrite(dht_dpin,LOW);
  delay(23);
  digitalWrite(dht_dpin,HIGH);
  delayMicroseconds(40);
  pinMode(dht_dpin,INPUT);
  dht_in=digitalRead(dht_dpin);

  if(dht_in)
  {
    bGlobalErr=1;//dht start condition 1 not met
    return;
  }

  delayMicroseconds(80);
  dht_in=digitalRead(dht_dpin);

  if(!dht_in)
  {
    bGlobalErr=2;//dht start condition 2 not met
    return;
  }

  delayMicroseconds(80);
  for (i=0; i<5; i++)
    dht_dat[i] = read_dht_dat();

  pinMode(dht_dpin,OUTPUT);

  digitalWrite(dht_dpin,HIGH);

  byte dht_check_sum =
    dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];

  if(dht_dat[4]!= dht_check_sum)
  {
    bGlobalErr=3;
  }
};


byte read_dht_dat()
{
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++)
  {
    while(digitalRead(dht_dpin)==LOW);
    delayMicroseconds(45);

    if (digitalRead(dht_dpin)==HIGH)
      result |=(1<<(7-i));

    while (digitalRead(dht_dpin)==HIGH);
  }
  return result;
}


void fotoLoop()
{
  lightADCReading = analogRead(lightSensor);
  // Calculating the voltage of the ADC for light
  lightInputVoltage = 5.0 * ((double)lightADCReading / 1024.0);
  // Calculating the resistance of the photoresistor in the voltage divider
  lightResistance = (10.0 * 5.0) / lightInputVoltage - 10.0;
  // Calculating the intensity of light in lux       
  Lux = 255.84 * pow(lightResistance, -10/9);
}

Currently I’m not getting any data from the DHT11 or LUX sensors over the rs485 network. Any help would be greatful

Regards

Anthony

Hello,

Did u solve the problem? I have the same doubt.

Thanks

Get the simple examples running first using only the single character keyboard entry then expand from there one step at a time.