Hi,
I used a sketch from the internet to send by RFLink one sensor value to another arduino board. It works well.
But my purpose is to send multiple values, such as light, temperature, CO2-concentration, humidity etc.
So I try to change the sketch for two sensorvalues, but now there are (for me) not-understandable things.
At the recievers side comes the serial data as follow:
983 983
1023 0
2 lines every time.
983 and 1023 (first numbers in the row) are good values.
The other 2 values (983 and 0) are not known. 983 looks like a mirror of the first value.
But were in the code is it going wrong.
Refers it to the char [4]?
Also I don't understand the code at the end of the reciever about buflen.
The int i was in the original sketch.
The int j was invented by myself, because i thought there are two sensors so there must be two charMsg's.
Correct me, please, if I'm wrong.
Gr.
Johan
Here's the code from the transmitter (some words are in dutch...):
#include <VirtualWire.h>
// LED's
const int ledPin = 13;
// Sensors
const int licht = A0;
const int co2 = A1;
int lichtData;
int co2Data;
char lichtCharMsg[4];
char co2CharMsg[4];
void setup() {
// PinModes
// LED
pinMode(ledPin,OUTPUT);
// for debugging
Serial.begin(9600);
// VirtualWire setup
vw_setup(2000); // Bits per sec
}
void loop() {
// Read and store Sensor data
lichtData = analogRead(licht);
co2Data = analogRead(co2);
// Convert integer data to Char array directly
itoa(lichtData,lichtCharMsg,10);
itoa(co2Data,co2CharMsg,10);
// DEBUG
Serial.print("licht Integer: ");
Serial.print(lichtData);
Serial.print(" licht CharMsg: ");
Serial.print(lichtCharMsg);
Serial.print("co2 Integer: ");
Serial.print(co2Data);
Serial.print(" co2 CharMsg: ");
Serial.print(co2CharMsg);
Serial.println(" ");
delay(1000);
// END DEBUG
digitalWrite(13, true); // Turn on a light to show transmitting
vw_send((uint8_t *)lichtCharMsg, strlen(lichtCharMsg));
vw_wait_tx(); // Wait until the whole message is gone
vw_send((uint8_t *)co2CharMsg, strlen(co2CharMsg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off a light after transmission
delay(200);
} // END void loop...
And this is the code from the reciever.
#include <VirtualWire.h>
// LED's
int ledPin = 13;
// Sensors
int lichtData;
int co2Data;
// RF Transmission container
char lichtCharMsg[4];
char co2CharMsg[4];
void setup() {
Serial.begin(9600);
// sets the digital pin as output
pinMode(ledPin, OUTPUT);
// VirtualWire
// Initialise the IO and ISR
// Required for DR3100
vw_set_ptt_inverted(true);
// Bits per sec
vw_setup(2000);
// Start the receiver PLL running
vw_rx_start();
} // END void setup
void loop(){
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
// Non-blocking
if (vw_get_message(buf, &buflen))
{
int i;
// Turn on a light to show received good message
digitalWrite(13, true);
// Message with a good checksum received, dump it.
for (i = 0; i < buflen; i++)
{
// Fill Sensor1CharMsg Char array with corresponding
// chars from buffer.
lichtCharMsg[i] = char(buf[i]);
}
int j;
for (j = 0; j < buflen; j++)
{
co2CharMsg[j] = char(buf[j]);
}
// Null terminate the char array
// This needs to be done otherwise problems will occur
// when the incoming messages has less digits than the
// one before.
lichtCharMsg[buflen] = '\0';
co2CharMsg[buflen] = '\0';
// Convert Sensor1CharMsg Char array to integer
lichtData = atoi(lichtCharMsg);
co2Data = atoi(co2CharMsg);
// DEBUG
Serial.print("Licht: ");
Serial.print(lichtData);
Serial.print("\t");
Serial.print("CO2: ");
Serial.println(co2Data);
// END DEBUG
// Turn off light to and await next message
digitalWrite(13, false);
}
}