[solved] - VirtualWire - Best Use

Good Morning Folks, so from several times i'm play with virtual wire for send data via "ASK Amplitude Shift Keying".

What I have learned is that:

  • the library send-out data using:
  const char *msg = "hello";
  vw_send((uint8_t *)msg, strlen(msg));

Where msg could be defined in this way:

char msg[6] = "hello";

or in this way:

char msg[6];
       msg[0]='h';
       msg[1]='e';
       msg[2]='l';
       msg[3]='l';
       msg[4]='0';
       msg[5]='\0';

and Receive message in this way:

    char msgrx[10];
    uint8_t buf[VW_MAX_MESSAGE_LEN];        // define the buffer
    uint8_t buflen = VW_MAX_MESSAGE_LEN;  // buffer length
     if (vw_get_message(buf, &buflen)) // Non-blocking
       {
	int i;
	   Serial.print("Got: ");
	   for (i = 0; i < buflen; i++)
	       {
	         msgrx[i]=buf[i];
               }
        }

Well at the end inside my variable msgrx I will have "hello" + 5 + '\0' .
Then when I have my message into the variable i did all my check for example:

if (strcmp(msgrx, "hello")  == 0)
     {
       Serial.println("Work are equal..."
     }
  • Is this the correct use for the library ?
  • Could I improve in something ?

VirtualWire is the only library that can be used for send data via ASK Amplitude Shift Keying or could be used something else ?

thanks have nice day,
gnux

You could use the Manchester library

I use it with a Attiny85

Thanks for the information,
for example if would like to send-out the number "156" like a byte is possible ?

thanks
Gnux

for example if would like to send-out the number "156" like a byte is possible ?

Yes, below are my test sketch.
It just sends numbers starting at 1000 and counting up.

I was going to use it as a outdoor wireless thermometer, but ehhhm my temp sensor suddenly send out smoke (I had it reversed)

#include <MANCHESTER.h>
unsigned int data = 1000;

void setup() 
{  

MANCHESTER.SetTxPin(PB4);
//  MANCHESTER.SetTimeOut(1000);
}

void loop(){
data++;

MANCHESTER.Transmit(data);

delay(500);
}

But with virtual wire i mean ...

so I've seen this

char array = (char)byte_array;

for convert byte into char

could be correct ?

thanks
gnux

so I've seen this

char array = (char)byte_array;

for convert byte into char

It does not convert the byte array to a char array. It uses a different kind of pointer (char) to point to the data in the byte_array. Whatever is using array treads the data in byte_array as though it is char data. This works when byte_array only contains data that can be interpreted as char data.

A byte and a char are the same size (8 bits), but the byte is unsigned and the char is signed, so the two do not have the same range of values. As long as the data is in the overlapping range (0 to 127), the byte data is interpreted the same when interpreting as a char.

So then with virtual wire is only possible send out number from 0 to 127 correct in only one byte ...

is possible pass directly the value without pass from the past char variable since the moment that want a uint8_t ? in this way i think could be more linear ...

Thanks for the support
gnux

So then with virtual wire is only possible send out number from 0 to 127 correct in only one byte ...

No, you can send any value from 0 to 255. However, if the byte contains a value like 240, interpreting that as a character will not give the desired results.

If you ARE sending bytes, why are you casting to char?

Hi Paul,
so I would like send-out a byte ... kindly could you give me an example-code for send-out for example the number "240" like a byte ? and receive it like a byte ?

I ask to this because with the code i think will be more clear and maybe i can provide to you more information :slight_smile:

thanks for the support,
gnux

so I would like send-out a byte ... kindly could you give me an example-code for send-out for example the number "240" like a byte ?

byte stuff = 240;
vw_send(&stuff, 1);

and receive it like a byte ?

    uint8_t buf[VW_MAX_MESSAGE_LEN];        // define the buffer
    uint8_t buflen = VW_MAX_MESSAGE_LEN;  // buffer length
     if (vw_get_message(buf, &buflen)) // Non-blocking
       {

buf[0] will contain 240.

Thanks 1000000 :slight_smile:

very useful :slight_smile:

Then into buf I can assign it to another byte variable :slight_smile:
I'll try and i'll let you know :slight_smile:
enjoy the weekend,
Gnux