Hi Everyone,
I'm new to Arduino and programing in general. I've been trying to complete a project for quite some time, and just can't seem to get it. Any help or suggestions would be much appreciated. I'm trying to create a weather station that transmits data via XBee and displays the data on the Rx's LCD screen. Below is the hardware I'm using:
The Tx contains:
- Arduino Fio
- XBee Series 1
- DHT - for temperature and humidity
- BMP180 - for barometric pressure, temperature and altitude
The Rx:
- Arduino Uno
- XBee Shield
- XBee Series 1
- LCD screen (16X2)
I believe the Tx is sending the data in the form of an array. It is currently printing in the following format:
<18,64,18,66,18,66,83.20,14,101900.00,764.25,30.09>
which correlates to
<TempC (bmp), TempF (bmp), TempC (dht), TempF (dht), HeatIndexC (dht), HeatIndexF (dht), Humidity, Dew Point C, Pressure Pa, Preasure mmHg, Pressure inHg>
I have read that it's important to send data in the form of an array so the receiver can collect the data, and when Serial is not busy, print it to the LCD. To concatenate the data array I used a combination of the "sprintf" and "dtostrf" functions.
TX CODE POSTED HERE:
#include "DHT.h"
#define DHTPIN 7 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
#include <Wire.h>
#include <Adafruit_BMP085.h>
/***************************************************
BMP085 Barometric Pressure & Temp Sensor
----> https://www.adafruit.com/products/391
These displays use I2C to communicate, 2 pins are required to
interface
****************************************************/
// Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
// Connect GND to Ground
// Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
// Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
// EOC is not used, it signifies an end of conversion
// XCLR is a reset pin, also not used here
Adafruit_BMP085 bmp;
int tempC; // Variable for holding temp in C | originally float, changed to int
int tempF; // Variable for holding temp in F | originally float, changed to int
float pressure; //Variable for holding pressure reading in Pa | originally float, changed to int
float pressuremmHg; // Variable for holding pressure reading in mmHg | originally float, changed to int
float pressureinHg; // Variable for holding pressure reading in inHg| originally float, changed to int
int dewpoint; // Variable for holding pressure reading in *C| originally float, changed to int
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX
char dataPacket [64]; //an array with 12 elements, but 64 total characters
int i; //used as an index into the array
char P1[15]; // these are values created through the "dtostrf" function because the "sprintf" doesn't allow floats
char P2[15]; // these are values created through the "dtostrf" function because the "sprintf" doesn't allow floats
char P3[15]; // these are values created through the "dtostrf" function because the "sprintf" doesn't allow floats
char H1[15]; // these are values created through the "dtostrf" function because the "sprintf" doesn't allow floats
void setup() {
XBee.begin(9600);
Serial.begin(9600);
Serial.println("DHTxx test!");
dht.begin();
bmp.begin();
}
void loop() {
//DHT Values Below:
// Reading temperature or humidity takes about 250 milliseconds! | originally float, changed to int
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default) | originally float, changed to int
int t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true) | originally float, changed to int
int f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default) | originally float, changed to int
int hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)| originally float, changed to int
int hic = dht.computeHeatIndex(t, h, false);
int DP = (t - ((100 - h) / 5)); // Calculates the Dew Point in *C if Humidity >50% (DHT) dd.dd
//DHT Values Below:
tempC = bmp.readTemperature(); // Read Temperature (bmp)
tempF = tempC * 1.8 + 32.; // Convert degrees C to F (bmp)
pressure = bmp.readPressure(); //Read Pressure (bmp)
pressuremmHg = (pressure * .0075); //(bmp)
pressureinHg = (pressure / 3386.39); //(bmp)
dtostrf(pressure,9,2,P1); // due to issues showing pressure in the "sprintf" function because it's a float value. 9 digits, 0 significant digits
dtostrf(pressuremmHg,6,2,P2); // due to issues showing pressure in the "sprintf" function because it's a float value.
dtostrf(pressureinHg,5,2,P3); // due to issues showing pressure in the "sprintf" function because it's a float value.
dtostrf(h,5,2,H1); // due to issues showing pressure in the "sprintf" function because it's a float value.
{
// sprintf(dataPacket, "X%d" ,gx) - this is used to compile multiple datastrings together
// d=integer, f=float
sprintf(dataPacket,"<%d,%d,%d,%d,%d,%d,%s,%d,%s,%s,%s>",tempC,tempF,t,f,hic,hif,H1,DP,P1,P2,P3);
Serial.println(dataPacket);
XBee.print(dataPacket);
delay (2000);
}
}
I'm receiving the data in the receiver but I can't display the values to the LCD screen. I would like to be able to display the following: "TempC: 18" in the LCD and then have it update as new data comes from the Tx.
Questions:
- I believe "TestData" is declared as an array but I haven't been able to extract values using the "strtok" function. Is this the wrong function to use? If so, what is a better function?
- Once the values are extracted from the array, how would you pause Serial and print the values to the lcd? Any suggestions?
RX CODE POSTED HERE:
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX
#include <Wire.h>
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include <stdio.h>
#include <string.h>
char TestData [12];
void setup()
{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear ();
// Set up both ports at 9600 baud. This value is most important
// for the XBee. Make sure the baud rate matches the config
// setting of your XBee.
XBee.begin(9600);
Serial.begin(9600);
}
void loop()
{
if (XBee.available()) {
char TestData = XBee.read(); // If data comes in from XBee, send it out to serial monitor
Serial.print(TestData);
}
}
Any help would be greatly appreciated!
Hope you all have a nice Turkey Day!