Hi I new to this forum and would like some help with a program i am trying to write. I have a 433 Mhz transmitter and receiver which i'm trying to use to send Temperature and Humidity values from a DHT11 sensor and also values for Lux via analogue A0.
I have figured out how to make the transmitter send constant data for these values in the following form, , and . I studied another project to do it this way.
I have also had luck receiving this data and have tried to extend the project so that when one of these values is detected it displays Temp: XX, Humidity: XX etc on the serial port. I'm have problems get the code to except IF statements and print the values correctly.
I have played about so much i think i have messed up the code for the receiver
#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
char inData[24];
byte index;
char reply[80];
boolean started = true;
boolean ended = true;
// Init VWMsgBuf.
uint8_t VWMsgBuf[VW_MAX_MESSAGE_LEN];
// Init VWMsgStr.
String VWMsgStr;
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
uint8_t VWBufLen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(VWMsgBuf, &VWBufLen)) {
// Flash a light to show received good message.
digitalWrite(13, true);
// Message with a good checksum received, dump it.
VWMsgStr = "";
for (int i = 0; i < VWBufLen; i++) {
//.write(message[i]);
while(Serial.available() > 0)
{
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] = ' ';
}
}
}
Serial.println();
}
}
TX code used
#define DEBUG
#include <VirtualWire.h>
// Init VWMsgBuf.
char VWMsgBuf[VW_MAX_MESSAGE_LEN];
// Init VWMsgStr. for humidy, temperature and Lux
String VWMsgStr; //for humidity
String VWMsgStrT; // for temperature
String VWMsgStrL; // for Lux
#include <wire.h>
#include <dht11.h>
// Init DHT11.
dht11 DHT11;
// DHT11 OUT to digital I/O pin.
#define DHT11_PIN 2
//
unsigned int Luxdata;
// setup() runs once after reset.
void setup() {
#ifdef DEBUG
// Init serial for debugging.
Serial.begin(9600);
#endif
// Init BPS.
vw_setup(2000);
// Send setup() message.
//VWMsgStr = "REMOTE1: setup()!";
VWTX(VWMsgStr);
VWTXT(VWMsgStrT);
VWTXL(VWMsgStrL);
Lux();
//#endif
}
// loop() runs continuously after setup().
void loop() {
VWTX(VWMsgStr);
delay(1000);
VWTXT(VWMsgStrT);
delay(1000);
VWTXL(VWMsgStrL);
#ifdef DEBUG
Serial.println(VWMsgStr);
delay(1000);
Serial.println(VWMsgStrT);
delay(1000);
Serial.println(VWMsgStrL);
#endif
// Send DHT11 message.
int rc = DHT11.read(DHT11_PIN);
switch (rc) {
case DHTLIB_OK:
VWMsgStr = "<H" + String(DHT11.humidity) + ">";
VWMsgStrT = "<T" + String(DHT11.temperature) + ">";
//VWMsgStr += " TempF: " + String(fahrenheit(DHT11.temperature));
break;
case DHTLIB_ERROR_CHECKSUM:
VWMsgStr = "REMOTE1: Checksum!";
break;
case DHTLIB_ERROR_TIMEOUT:
VWMsgStr = "REMOTE1: Time Out!";
break;
default:
VWMsgStr = "REMOTE1: Unknown!";
break;
}
VWMsgStrL = "<L" + String(Luxdata) + ">";
}
// Virtual Wire Transmit msgStr
void VWTX(String VWMsgStr) {
VWMsgStr.toCharArray(VWMsgBuf, VW_MAX_MESSAGE_LEN);
uint8_t VWMsgBufLen = strlen(VWMsgBuf);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)VWMsgBuf, strlen(VWMsgBuf));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(1000);
}
// Virtual Wire Transmit msgStr for temperature
void VWTXT(String VWMsgStrT) {
VWMsgStrT.toCharArray(VWMsgBuf, VW_MAX_MESSAGE_LEN);
uint8_t VWMsgBufLen = strlen(VWMsgBuf);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)VWMsgBuf, strlen(VWMsgBuf));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(1000);
}// Virtual Wire Transmit msgStr for temperature
void VWTXL(String VWMsgStrL) {
VWMsgStrL.toCharArray(VWMsgBuf, VW_MAX_MESSAGE_LEN);
uint8_t VWMsgBufLen = strlen(VWMsgBuf);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)VWMsgBuf, strlen(VWMsgBuf));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(1000);
}
/* Main program */
void Lux(){
/* Temporarily holds the value read from analogue input A0 */
unsigned int Luxdata;
/* Read the analogue input A0... */
Luxdata = analogRead(A0);
}
Im really stuck and would really appreciate some help
Cheers