Go Down

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

PaulS

Quote
How can this be done?


Code: [Select]
// 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 <R> results in <XXX> coming back. If so, reply will contain the XXX stuff.
The art of getting good answers lies in asking good questions.

vmansmx5

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
Quote

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


Code: [Select]

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
      }
   }
}



PaulS

Code: [Select]
void waitForReply(unsigned long 3000 ,1)
{

3000 is not a valid variable name. Neither is 1, which is untyped.
The art of getting good answers lies in asking good questions.

vmansmx5

Ok so this workd but its sending <R> too many times how can i get it to send 1 time then to wait for reply
Quote
char reply[80];
int index = 0;
boolean started = true;
boolean ended = true;

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

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


}

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
      }
   }
}

PaulS

Quote
Ok so this workd but its sending <R> 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.

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

vmansmx5

OK so been trying this the reciver sends out the <R> and the transmitter gets it then sends back the <T16.00> but the reciver is not geting it or displaying anything
Code: [Select]
char inData[24];
char reply[80];

byte index;
boolean started = true;
boolean ended = true;

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

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



}

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] = ' ';
      }
      else if(aChar == '>')
      {
        ended = true;
      }
      else if(started)
      {
         reply[index] = aChar;
         index++;
         reply[index] = '\0';
      }

      if(started && ended)
      {
        if(inData[0] == 'T')
{
   inData[0] = ' ';
   int windVal = atoi(inData);
           Serial.println(" ");
           Serial.print("GotReply");
   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           
           }

started = false;
ended = false;

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

PaulS

What code is running on the transmitter?

Your initial post contains references to LiquidCrystal that implied that you have an LCD connected to the Arduino. Why not use that to show some debug messages, and perhaps get an idea what is happening?

Remind me what kind of XBees you have, how they are connected to the Arduinos, and how they are configured.
The art of getting good answers lies in asking good questions.

vmansmx5

Ok so what i have is.
The reciver sends out the <R> the tansmiter gets it and sends back the <T13.00> Temp the Test Reciver displays it but the Reciver board dose not just keeps sending <R>

Transmitter:
Arduino Mega + Xbee shield + V1 Xbee + Sensors
Reciver:
Arduino duemilanove + adapter board + V1 xbee
Test Reciver:
MY own Designd PCB with V1 xbee and TFT LCD

All V1 xbee are xbee default baud.

Transmitter:
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] = ' ';
  }
}


Reciver:
Code: [Select]

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

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

void loop() {
 
 
Serial.print("<R>");
waitForReply(2000);
delay(1000);


}

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)
      {
        if(inData[0] == 'T')
{
   inData[0] = ' ';
   int windVal = atoi(inData);
           Serial.println(" ");
           Serial.print("GotReply");
   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           
           }

started = false;
ended = false;

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


vmansmx5

Test Reciver:
Code: [Select]

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SPI.h>
#include <SD.h>

// TFT display and SD card will share the hardware SPI interface.
// Hardware SPI pins are specific to the Arduino board type and
// cannot be remapped to alternate pins.  For Arduino Uno,
// Duemilanove, etc., pin 11 = MOSI, pin 12 = MISO, pin 13 = SCK.
#define SD_CS    4  // Chip select line for SD card
#define TFT_CS  10  // Chip select line for TFT display
#define TFT_DC   9  // Data/command line for TFT
#define TFT_RST  8  // Reset line for TFT (or connect to +5V)
#define bkl 2      //LCD Black Light
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

char inData[24];
byte index;
boolean started = false;
boolean ended = false;
\
void setup(void) {
  Serial.begin(9600);
  pinMode(TFT_CS, OUTPUT);
  digitalWrite(TFT_CS, HIGH); 
  pinMode(bkl, OUTPUT);
  digitalWrite(bkl, LOW);
  // Our supplier changed the 1.8" display slightly after Jan 10, 2012
  // so that the alignment of the TFT had to be shifted by a few pixels
  // this just means the init code is slightly different. Check the
  // color of the tab to see which init code to try. If the display is
  // cut off or has extra 'random' pixels on the top & left, try the
  // other option!

  // If your TFT's plastic wrap has a Red Tab, use the following:
  tft.initR(INITR_REDTAB);   // initialize a ST7735R chip, red tab
  // If your TFT's plastic wrap has a Green Tab, use the following:
  //tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab

  Serial.print("Initializing SD card...");
  if (!SD.begin(SD_CS)) {
    Serial.println("failed!");
    return;
  }
  Serial.println("OK!");

  bmpDraw("welcome.bmp", 0, 0);
  delay(5000);
  bmpDraw("b8.bmp", 0, 0);
  testfillrects(ST7735_WHITE);
  tft.fillCircle(15, 103, 5, ST7735_BLACK);
  tft.fillCircle(15, 121, 5, ST7735_BLACK);
  tft.fillCircle(15, 139, 5, ST7735_BLACK);
}

void loop() {

  delay(500);
  tft.fillCircle(15, 121, 5, ST7735_BLACK);
  tft.drawLine(0, 25, tft.width()-1, 25, ST7735_GREEN);
  tft.setCursor(25, 6);
  tft.setTextColor(ST7735_GREEN);
  tft.setTextSize(2);
  tft.print("DISPLAY");
  tft.setCursor(5, 30);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Temperature");
  tft.setCursor(115, 30);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("C");
  tft.setCursor(5, 40);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Humidity");
  tft.setCursor(115, 40);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("%");
  tft.setCursor(5, 50);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Moisture");
  tft.setCursor(5, 60);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Voltage");
  tft.setCursor(115, 60);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("V");
  tft.setCursor(5, 70);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("0.1 deg C");
  tft.setCursor(5, 80);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Pressure");
  tft.setCursor(115, 80);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Pa");
  tft.drawLine(0, 90, tft.width()-1, 90, ST7735_GREEN);
 
  tft.setCursor(28, 99);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(1.7);
  tft.print("TX");
 
  tft.setCursor(28, 119);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(1.7);
  tft.print("RX");
 
  tft.setCursor(28, 136);
  tft.setTextColor(ST7735_RED);
  tft.setTextSize(1.7);
  tft.print("SD");

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 windVal = atoi(inData);
           Serial.println(" ");
   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +25/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 30);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(inData);
           
}
else if(inData[0] == 'H')
     {
   inData[0] = ' ';
           int temp = atoi(inData);
   Serial.println(" ");
           Serial.print("Humidity:");
           Serial.print(inData);
           Serial.print("%");
           Serial.println(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +46/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 40);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(inData);
           
        }
        else if(inData[0] == 'M')
     {
   inData[0] = ' ';
           int temp = atoi(inData);
   Serial.println(" ");
           Serial.print("Moisture:");
           Serial.print(inData);
           Serial.println(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +67/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 50);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(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(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +86/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 60);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(inData);
        }
        else if(inData[0] == 'F')
     {
   inData[0] = ' ';
           int temp = atoi(inData);
   Serial.println(" ");
           Serial.print("Temperature:");
           Serial.print(inData);
           Serial.print("*0.1 deg C");
           Serial.println(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +106/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 70);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(inData);
        }
        else if(inData[0] == 'P')
     {
   inData[0] = ' ';
           int temp = atoi(inData);
   Serial.println(" ");
           Serial.print("Pressure:");
           Serial.print(inData);
           Serial.print(" Pa");
           Serial.println(" ");
           tft.fillCircle(15, 121, 5, ST7735_RED);
           tft.fillRect(tft.width()/80 +142/2, tft.height()/10 +126/2, 40, 9, ST7735_WHITE);
           tft.setCursor(70, 80);
           tft.setTextColor(ST7735_RED);
           tft.setTextSize(1);
           tft.println(inData);
     }

started = false;
ended = false;

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

void testfillrects(uint16_t color1) {
 
    tft.fillRect(tft.width()/2 -130/2, tft.height()/2 -110/2 , 150, 65, color1);
   
 
}

PaulS

Is it just me, or is the receiver code actually on the transmitter, and vice verso?\

Only the "receiver" keeps sending <R>. The "transmitter" keeps reading serial data and replying.
The art of getting good answers lies in asking good questions.

vmansmx5

yes the receiver is sendind the <R> and waiting for the reply back from the transmitter it sends back the value of the senseor for the reciver to display the value on the com window but im not geting a replay back. the test reciver gets the reply back?

PaulS

Lets just call the devices A and B. B sends <R>. A receives <R>, and stores R in inData, setting started and ended to true. After the while loop ends, started and ended are true, so A responds with <Txx.xx>.

So, what does device A do, if is receives serial data:
Code: [Select]
   while(millis() - startTime < 3000)
   {
      char aChar = Serial.read();

First, it tries to read data that might not be there, yet. That's not good.

But, let's assume that the data is quick in coming back (not a valid assumption, but, bear with me).
Code: [Select]
      if(started && ended)
      {
        if(inData[0] == 'T')
{
   inData[0] = ' ';
   int windVal = atoi(inData);
           Serial.println(" ");
           Serial.print("GotReply");
   Serial.print("Temp:");
           Serial.print(inData);
           Serial.print("C");
           Serial.println(" ");

Suppose that inData does contain Txx.xx. What do you do with that data? Yep, send it to the other XBee. What good is that? You need to separate the serial data being sent to the serial monitor from the serial data being sent to the XBee. That means that you need either a Mega with multiple hardware serial ports or you need to use NewSoftSerial/SoftwareSerial to talk to the XBee and Serial to talk to the Serial Monitor.
The art of getting good answers lies in asking good questions.

vmansmx5

Ok so B Sends <R> Then A recives it then it sends <Txx.xx> back to A and A should display the value
If i have The xbee adaptorboad plugd in the the usb i can send <R> and get back the value on the coms window
but A is not geting the value back and displaying it so it must be the code on A thats not reading in the value

PaulS

Please re-read the last 5 words of reply #21.
The art of getting good answers lies in asking good questions.

vmansmx5

and how they are configured.
as in the Xbees thay are the default program

Go Up