Go Down

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

daniel_e

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));
vw_wait_tx();



At the moment the transmitter reads the data like this:

Code: [Select]
for (i=0;i< buflen;i++) {
Serial.print((char)buf[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!

wildbill

null terminate the string in buffer and use atoi.

jremington

#2
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
   vw_wait_tx();

...

   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



daniel_e

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_wait_tx();
 vw_send((uint8_t *) &humidity, 2); //send an int
 vw_wait_tx();


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?

Thanks!

jremington

Send a two element integer array instead.

Go Up