DHT11 sensor Sending data by xbee to xbee

Well after a few hours nuting it out i just about have it working but with one little problem with the reciver end here is the tx code

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
Serial.begin(9600);
}

void loop()
{

float h = dht.readHumidity();
float t = dht.readTemperature();

Serial.print("<T:");
Serial.print(t, DEC); 
Serial.print(">");

delay(2000);

Serial.print("<H:");
Serial.print(h, DEC); 
Serial.print(">");

delay(2000);
}

The Rx code

char inData[24];
byte index;
boolean started = false;
boolean ended = false;

void setup()
{
Serial.begin(9600);
Serial.println("Temperature & Humidity");
}

void loop()
{
  while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = '\0';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ',';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'T')
	{
	   inData[0] = ' ';
	   int windVal = atoi(inData);
           Serial.println(" ");
	   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
	}
	else if(inData[0] == 'H')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = '\0';
  }
}

This is what im getting in the coms on the reviver end

Temp: :16.0000000000,C 
 
Humidity: :42.0000000000,% 
 
Temp: :16.0000000000,C 
 
Humidity: :42.0000000000,% 
 
Temp: :16.0000000000,C 
 
Humidity: :42.0000000000,% 
 
Temp: :16.0000000000,C 
 
Humidity: :42.0000000000,%

the problem is whats all the 000000000000 on the end i just want 16.00C or 42.00%
How can i fix this need help

well i added the lcd to the reciver end works great but all them 0000000000 i dont get

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,21);
char inData[24];
byte index;
boolean started = false;
boolean ended = false;

void setup()
{
Serial.begin(9600);
Serial.println("Temperature & Humidity");
lcd.init();                      
lcd.backlight();
lcd.begin(20, 4);
lcd.setCursor(6, 1);
lcd.print("WELCOME");
delay(3000); 
lcd.clear();
}

void loop()
{
  while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = '\0';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ',';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'T')
	{
	   inData[0] = ' ';
	   int windVal = atoi(inData);
           Serial.println(" ");
	   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           lcd.setCursor(0, 0);
           lcd.print("Temp:"); 
           lcd.setCursor(0, 1);
           lcd.print(inData);
           lcd.print("C");
	}
	else if(inData[0] == 'H')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
           lcd.setCursor(0, 2);
           lcd.print("Humidity:"); 
           lcd.setCursor(0, 3);
           lcd.print(inData);
           lcd.print("%");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = '\0';
  }
}

I fixd it here we go

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
Serial.begin(9600);
}

void loop()
{

float h = dht.readHumidity();
float t = dht.readTemperature();

Serial.print("<T");
Serial.print(t); 
Serial.print(">");

delay(2000);

Serial.print("<H");
Serial.print(h); 
Serial.print(">");

delay(2000);
}

i took out the Serial.print(h, DEC);
and it works great :slight_smile:

Temperature & Humidity
 
Temp: 17.00,C 
 
Humidity: 57.00,% 
 
Temp: 17.00,C 
 
Humidity: 56.00,% 
 
Temp: 17.00,C 
 
Humidity: 55.00,%

i took out the Serial.print(h, DEC);

For floats, the 2nd argument is the number of decimal places to print. You are telling it to print 10 decimal places.

Ok is that good.
here i have the transmitter with temp sensor moisture sensor and voltage but have proplems with the moisture reading on the reciver end. im geting .00 coming up when its on 0 this so my display its a 4line lcd

Temp: 17.00 C
Humidity: 40.00 %
Moisture: 0 .00
Voltage: 5.00 V

This is the code for the TX unit

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

int Sensor1 = A0;
int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.02;    
float R2 =  7399.40;     
int value = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{

float h = dht.readHumidity();
float t = dht.readTemperature();
int Sensor1 = analogRead(A0);
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0;
vin = vout / (R2/(R1+R2));

Serial.print("<T");
Serial.print(t); 
Serial.print(">");

delay(1000);

Serial.print("<H");
Serial.print(h); 
Serial.print(">");

delay(1000);

Serial.print("<M");
Serial.print(Sensor1); 
Serial.print(">");

delay(1000);

Serial.print("<V");
Serial.print(vin); 
Serial.print(">");

delay(5000);
}

and the RX

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,21);
char inData[24];
byte index;
boolean started = false;
boolean ended = false;

void setup()
{
Serial.begin(9600);
Serial.println("Temperature & Humidity");
lcd.init();                      
lcd.backlight();
lcd.begin(20, 4);
lcd.setCursor(6, 1);
lcd.print("WELCOME");
delay(3000); 
lcd.clear();
delay(100);
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Temp:"); 
  lcd.setCursor(0, 1);
  lcd.print("Humidity:"); 
  lcd.setCursor(0, 2);
  lcd.print("Moisture:"); 
  lcd.setCursor(0, 3);
  lcd.print("Voltage:"); 
           
  while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = ' ';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ' ';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'T')
	{
	   inData[0] = ' ';
	   int windVal = atoi(inData);
           Serial.println(" ");
	   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           lcd.setCursor(11, 0);
           lcd.print(inData);
           lcd.print("C");
	}
	else if(inData[0] == 'H')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
           lcd.setCursor(11, 1);
           lcd.print(inData);
           lcd.print("%");
        }
        else if(inData[0] == 'M')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Moisture:");
           Serial.print(inData);
         
           Serial.println(" ");
           lcd.setCursor(11, 2);
           lcd.print(inData);
           
           
          
        }
        else if(inData[0] == 'V')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Voltage:");
           Serial.print(inData);
           Serial.print("Volts");
           Serial.println(" ");
           lcd.setCursor(11, 3);
           lcd.print(inData);
           lcd.print("V");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = ' ';
  }
}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ' ';
	}

The last line, inData[index] being assigned a space, is wrong. That array element should be assigned a NULL, instead.

        inData[index] = '\0';

ok i put thet bit in and it works but had to add in a line to clear the last val it stayed on the lcd
lcd.setCursor(11, 2);
lcd.print(" ");
lcd.setCursor(11, 2);
lcd.print(inData);

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,21);
char inData[24];
byte index;
boolean started = false;
boolean ended = false;

void setup()
{
Serial.begin(9600);
Serial.println("Temperature & Humidity");
lcd.init();                      
lcd.backlight();
lcd.begin(20, 4);
lcd.setCursor(6, 1);
lcd.print("WELCOME");
delay(3000); 
lcd.clear();
delay(100);
}

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Temp:"); 
  lcd.setCursor(0, 1);
  lcd.print("Humidity:"); 
  lcd.setCursor(0, 2);
  lcd.print("Moisture:"); 
  lcd.setCursor(0, 3);
  lcd.print("Voltage:"); 
           
  while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = ' ';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = '\0';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'T')
	{
	   inData[0] = ' ';
	   int TempVal = atoi(inData);
           Serial.println(" ");
	   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           lcd.setCursor(11, 0);
           lcd.print(inData);
           lcd.setCursor(18, 0);
           lcd.print("C");
	}
	else if(inData[0] == 'H')
     {
	   inData[0] = ' ';
           int HumidityVal = atoi(inData);
	   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
           lcd.setCursor(11, 1);
           lcd.print(inData);
           lcd.setCursor(18, 1);
           lcd.print("%");
        }
        else if(inData[0] == 'M')
     {
           inData[0] = ' ';
           int MoistureVal = atoi(inData);
           Serial.println(" ");
           Serial.print("Moisture:");
           Serial.print(inData);
           Serial.println(" ");
           lcd.setCursor(11, 2);
           lcd.print("     ");
           lcd.setCursor(11, 2);
           lcd.print(inData);
          
        }
        else if(inData[0] == 'V')
     {
	   inData[0] = ' ';
           int VoltageVal = atoi(inData);
	   Serial.println(" ");
           Serial.print("Voltage:");
           Serial.print(inData);
           Serial.print("Volts");
           Serial.println(" ");
           lcd.setCursor(11, 3);
           lcd.print("          ");
           lcd.setCursor(11, 3);
           lcd.print(inData);
           lcd.setCursor(18, 3);
           lcd.print("V");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = ' ';
  }
}

i have also been working on this code for the transmiter what im tying to do is have the reviver send a code to the transmitter then it reads it then sends the value to the reciver then the reviver displays the value then sends out the next code and so on till it gets all 4 values then it delays then dose it all again.
So what i want is the reciver to send this

send this For Temp Val
wait for incoming val
then display incoming Val

send this For Humidity Val
wait for incoming val
then display incoming Val

send this For Soil Val
wait for incoming val
then display incoming Val

send this For Volt Val
wait for incoming val
then display incoming Val

then delay for 5mins then start again

This works great if i send them out the coms using my xbee coms board
Can anyone help me with this

This is the transmitter code.

#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);

char inData[24];
byte index;
boolean started = false;
boolean ended = false;

int Sensor1 = A0;
int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.02;    // !! resistance of R1 !!
float R2 =  7399.40;     // !! resistance of R2 !!
// variable to store the value 
int value = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{

 while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = ' ';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ' ';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'R')
	{
           inData[0] = ' ';
           float t = dht.readTemperature(); 
	   Serial.print("<T");
           Serial.print(t); 
           Serial.print(">"); 
	}
	else if(inData[0] == 'B')
     {
           inData[0] = ' ';
           float h = dht.readHumidity();
	   Serial.print("<H");
           Serial.print(h); 
           Serial.print(">");
        }
        else if(inData[0] == 'C')
     {
	   inData[0] = ' ';  
           int Sensor1 = analogRead(A0);
           Serial.print("<M");
           Serial.print(Sensor1); 
           Serial.print(">");
        }
        else if(inData[0] == 'D')
     {
           inData[0] = ' ';
           value = analogRead(analogInput);
           vout = (value * 5.0) / 1024.0;
           vin = vout / (R2/(R1+R2));
	   Serial.print("<V");
           Serial.print(vin); 
           Serial.print(">");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = ' ';
  }
}

ok so i put on a TFT color LCD looks Great just been playing around with it here are some photos of my workbench the photos are not the best.

IMG_0054.JPG

IMG_0055.JPG

This works great if i send them out the coms using my xbee coms board
Can anyone help me with this

So, what is the problem?

Well not sure on the reciver code how to set it up like this.
i can send the R B C D by my xbee adapter board and the transmitter sends back the values.
But how to set up the reviver part im not sure can i put in a delay that waits for incoming val then sends out the next code......
So what i want is the reciver to Run like this

send this For Temp Val
wait for incoming val
then display incoming Val

send this For Humidity Val
wait for incoming val
then display incoming Val

send this For Soil Val
wait for incoming val
then display incoming Val

send this For Volt Val
wait for incoming val
then display incoming Val

then delay for 5mins then start again

So what i want is the reciver to Run like this

send this For Temp Val
wait for incoming val
then display incoming Val

send this For Volt Val
wait for incoming val
then display incoming Val

then delay for 5mins then start again

I'm confused. What are you sending "" to? What is sending data? Why does the receiver need to wait?

In my mind, the receiver should do nothing until it receives a letter command. It should then process that command, which means sending the appropriate output. There is no reason for it to delay at all.

The application that sends the letter commands should only send them once every 5 minutes.

the reciver is sending for temp to the transmitter then the transmitter sends back the value for the of temp
the reciver then displays the value.
then the reciver sends For Humidity to the transmitter then the transmitter sends back the value for the of temp
the reciver then displays the value.
and so on
I tryd this but it keeps sending then out and cant get the value back delays dont work

void loop()
{
  lcd.setCursor(0, 0);
  lcd.print("Temp:"); 
  lcd.setCursor(0, 1);
  lcd.print("Humidity:"); 
  lcd.setCursor(0, 2);
  lcd.print("Moisture:"); 
  lcd.setCursor(0, 3);
  lcd.print("Voltage:"); 
  
  Serial.print("<R>");
  
  Serial.print("<B>");
 
  Serial.print("<C>");
   
  Serial.print("<D>");
  
  
           
  while(Serial.available() > 0)
  {
	char aChar = Serial.read();
	if(aChar == '<')
	{
	    started = true;
	    index = 0;
	    inData[index] = ' ';
	}
	else if(aChar == '>')
	{
	    ended = true;
	}
	else if(started)
	{
	    inData[index] = aChar;
	    index++;
	    inData[index] = ' ';
	}
  }

  if(started && ended)
  {
	// Use the value
	if(inData[0] == 'T')
	{
	   inData[0] = ' ';
	   int windVal = atoi(inData);
           Serial.println(" ");
	   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           lcd.setCursor(11, 0);
           lcd.print(inData);
           lcd.print("C");
	}
	else if(inData[0] == 'H')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
           lcd.setCursor(11, 1);
           lcd.print(inData);
           lcd.print("%");
        }
        else if(inData[0] == 'M')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Moisture:");
           Serial.print(inData);
         
           Serial.println(" ");
           lcd.setCursor(11, 2);
           lcd.print(inData);
           
           
          
        }
        else if(inData[0] == 'V')
     {
	   inData[0] = ' ';
           int temp = atoi(inData);
	   Serial.println(" ");
           Serial.print("Voltage:");
           Serial.print(inData);
           Serial.print("Volts");
           Serial.println(" ");
           lcd.setCursor(11, 3);
           lcd.print(inData);
           lcd.print("V");
     }

	started = false;
	ended = false;

	index = 0;
	inData[index] = ' ';
  }
}
  Serial.print("<R>");
  
  Serial.print("<B>");
 
  Serial.print("<C>");
   
  Serial.print("<D>");

Send a bunch of requests on every pass through loop. Then, don't wait for a response or for any data to have arrived. OK, I think I see the problem.

You need to create a blocking function, called after each request for data, that waits for a reply.

You are sending R, B, C, and D, and expecting T, H, M, and V. Why is that?

You are sending R, B, C, and D, and expecting T, H, M, and V. Why is that?

Becouse i want the reciver to send out R,B,C,D to get the value back by T,H,M,V

You need to create a blocking function, called after each request for data, that waits for a reply.

How can this be done?

How can this be done?

// Global variables
char reply[80];
int index = 0;

void waitForReply(unsigned long howLongToWait)
{
   index = 0;
   unsigned long startTime = millis();
   while(millis() - startTime < howLongToWait)
   {
      char aChar = Serial.read();
      if(aChar == '<')
      {
         started = true;
         index = 0;
         reply[index] = '\0';
      }
      else if(aChar == '>')
      {
        ended = true;
      }
      else if(started)
      {
         reply[index] = aChar;
         index++;
         reply[index] = ' ';
      }

      if(started && ended)
      {
         break; // Exit the while loop
      }
   }
}

This assumes that sending results in coming back. If so, reply will contain the XXX stuff.

Just tying this out not sure if this is the right way to go about it but must be missing somthing when i click verify i get this message

test_RX1:0: error: expected ',' or '...' before numeric constant
test_RX1:17: error: expected ',' or '...' before numeric constant

char reply[80];
int index = 0;
boolean started = true;
boolean ended = true;

void setup() {
  Serial.begin(9600);
}

void loop() {
  
 Serial.print("<R>");

 waitForReply(1);
}

void waitForReply(unsigned long 3000 ,1)
{
   index = 0;
   unsigned long startTime = millis();
   while(millis() - startTime < 3000)
   {
      char aChar = Serial.read();
      if(aChar == '<')
      {
         started = true;
         index = 0;
         reply[index] = '\0';
      }
      else if(aChar == '>')
      {
        ended = true;
      }
      else if(started)
      {
         reply[index] = aChar;
         index++;
         reply[index] = ' ';
      }

      if(started && ended)
      {
        Serial.print("GotReply"); 
        break; // Exit the while loop
      }
   }
}
void waitForReply(unsigned long 3000 ,1)
{

3000 is not a valid variable name. Neither is 1, which is untyped.

Ok so this workd but its sending too many times how can i get it to send 1 time then to wait for reply

char reply[80];
int index = 0;
boolean started = true;
boolean ended = true;

void setup() {
Serial.begin(9600);
}

void loop() {

Serial.print("");

}

void waitForReply(unsigned long howLongToWait)
{
index = 0;
unsigned long startTime = millis();
while(millis() - startTime < 3000)
{
char aChar = Serial.read();
if(aChar == '<')
{
started = true;
index = 0;
reply[index] = '\0';
}
else if(aChar == '>')
{
ended = true;
}
else if(started)
{
reply[index] = aChar;
index++;
reply[index] = ' ';
}

if(started && ended)
{
Serial.print("GotReply");
break; // Exit the while loop
}
}
}

Ok so this workd but its sending too many times how can i get it to send 1 time then to wait for reply

Maybe you need to actually call the function.

void loop()
{  // Moved down here where it belongs
  Serial.print("<R>");
  waitForReply(1000);
}

Why does the function take an argument, how long to wait, and then wait for a hardcoded amount of time?