Go Down

Topic: DHT11 sensor Sending data by xbee to xbee (Read 12998 times) previous topic - next topic

vmansmx5

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
Code: [Select]
#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
Code: [Select]
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
Code: [Select]
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

vmansmx5

well i added the lcd to the reciver end works great but all them 0000000000 i dont get
Code: [Select]
#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';
  }
}

vmansmx5

#2
Jul 06, 2012, 01:29 pm Last Edit: Jul 06, 2012, 01:33 pm by vmansmx5 Reason: 1
I fixd it here we go
Code: [Select]
#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  :)
Code: [Select]
Temperature & Humidity

Temp: 17.00,C

Humidity: 57.00,%

Temp: 17.00,C

Humidity: 56.00,%

Temp: 17.00,C

Humidity: 55.00,%

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

vmansmx5

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
Quote

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


This is the code for the TX unit
Code: [Select]
#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
Code: [Select]
#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] = ' ';
  }
}

PaulS

Code: [Select]
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.
Code: [Select]
        inData[index] = '\0';
The art of getting good answers lies in asking good questions.

vmansmx5

#6
Jul 08, 2012, 04:45 am Last Edit: Jul 08, 2012, 04:49 am by vmansmx5 Reason: 1
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);

Code: [Select]
#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] = ' ';
 }
}

vmansmx5

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 <R> For Temp Val
wait for incoming val
then display incoming Val

send this <B> For Humidity Val
wait for incoming val
then display incoming Val

send this <C> For Soil Val
wait for incoming val
then display incoming Val

send this <D> 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.
Code: [Select]
#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] = ' ';
  }
}

vmansmx5

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.

PaulS

Quote
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?
The art of getting good answers lies in asking good questions.

vmansmx5

#10
Jul 09, 2012, 02:29 am Last Edit: Jul 09, 2012, 02:33 am by vmansmx5 Reason: 1
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 <R> For Temp Val
wait for incoming val
then display incoming Val

send this <B> For Humidity Val
wait for incoming val
then display incoming Val

send this <C> For Soil Val
wait for incoming val
then display incoming Val

send this <D> For Volt Val
wait for incoming val
then display incoming Val

then delay for 5mins then start again


PaulS

Quote
So what i want is the reciver to Run like this

send this <R> For Temp Val
wait for incoming val
then display incoming Val

<snip>

send this <D> 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 "<R>" 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 art of getting good answers lies in asking good questions.

vmansmx5

the reciver is sending <R> 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 <B> 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
Code: [Select]
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] = ' ';
  }
}


PaulS

Code: [Select]
  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?
The art of getting good answers lies in asking good questions.

vmansmx5

Quote
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

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

How can this be done?

Go Up