Go Down

Topic: Using a sent variable (Read 472 times) previous topic - next topic


Hallo everybody,

i am working on a home automation solution using several pro minis, that (in the first step) send data from sensors via 433 RF (VirtualWire) to a transmitter. Everything works perfectly, but I don't want to send too much obvious data through the network ('window is open', 'window is closed'). Instead I want to use little 4-digit-codes, which the transmitter should 'decode' and write a status in the console.

Something like
1234 = Bathroom window is open
9876 = Living room window is closed

The Challenge: I am able to send variables through VirtualWire, but only as char and I don't know how to convert the char-data back into a variable that can be used to print a status into the console.

I am sending the data as followed:

Code: [Select]

int windowClosed = 1043;
int windowOpen = 7293;
char msg[24];
sprintf(msg, "%i.%i", windowClosed);
vw_send((uint8_t *)msg, strlen(msg));

At the moment the transmitter reads the data like this:

Code: [Select]
for (i=0;i< buflen;i++) {

But I want to do something like that:

Code: [Select]
if ((char *)buf == 1043) {
      Serial.println("The bathroom window is closed");

Of course the '1043' can not be used like that at the moment, because it is still text. Does anyone know how I can convert the digits into a variable again, so that I can use it like in my example?

Thanks for help!


null terminate the string in buffer and use atoi.


May 10, 2017, 06:04 pm Last Edit: May 10, 2017, 06:05 pm by jremington
You don't need to send character data. Send and receive the integer value directly, like this:

Code: [Select]
   vw_send((uint8_t *) &windowClosed, 2); //send an int


   msglen = VW_MAX_MESSAGE_LEN;  //need to reset every time
   if (vw_get_message((uint8_t *) &data, &msglen)) // "msglen" should end up as 2 for an int variable


Alright @jremington. Works perfectly! I am thrilled :-)

But here comes the next challenge: I am now playing around with temperature and humidity. Thanks to your help I am now able to send the sensor-data directly as an integer, like this:

Code: [Select]
vw_send((uint8_t *) &temp, 2); //send an int
 vw_send((uint8_t *) &humidity, 2); //send an int

The receiver gets the data and I am able to see it in the serial monitor.

But how can my receiver differentiate between the two variables I sent (temperature and humidity)?
It receives both variables as 'data'.

What's the best approach here?



Send a two element integer array instead.

Go Up