VirtualWire - Transmitting more than 8 bit / 1byte

Hello!

I wrote a program to transmit a value from one arduino via VirtualWire to another.
The data i am trying to send is an integer with information about the wattage of an appliance.

I am trying to send data ranging from 0 - 1000 (eg. 10 bits) but i am only able to send 255. I understand that VirtualWire is only able to send 8 bits at a time, but my question is: How to transmit 10 bits instead of 8?

My code is as follows:

Transmitter:

     char *msg = "0";
     msg[0] = kwh;                                  // The integer with wattage value
     vw_send((uint8_t *)msg, strlen(msg));
     vw_wait_tx();

Receiver:

  uint8_t buf[VW_MAX_MESSAGE_LEN];       
  uint8_t buflen = VW_MAX_MESSAGE_LEN;  
  if (vw_get_message(buf, &buflen))       
  {
      int i;
      for (i = 0; i < buflen; i++)
      {
        // Skriver data til LCD
        lcd.clear();
        lcd.print("Data:");
        lcd.setCursor(6, 0);  lcd.print("     ");
        lcd.setCursor(6, 0);  lcd.print(buf[i], DEC);  // Prints received value to LCD
        lcd.setCursor(9, 0);  lcd.print("Watt");
        
        RXdata = buf[i];    // Moves the value to Integer RXdata  
        }
      
   }

I`ve tried playing around with arrays and characters but i have not been able to get it to work

Any help is appreciated :slight_smile:

vw_send((uint8_t *)msg, strlen(msg));

Since vw_send wants to send an array of uint8_t, of a specified length, it doesn't really make sense to declare the array of type char.

char *msg = "0";

Declare it of the correct type and size, populate it, and send it.

uint8_t msg[2];
memcpy(msg, kwh, 2);
vw_send(msg, 2);

I am getting a:

error: invalid conversion from 'int' to 'const void*'

In function 'void loop()':
error: invalid conversion from 'int' to 'const void*

with the code:

     uint8_t msg[2];
     memcpy(msg, kwh, 2);
     vw_send(msg, 2);
     vw_wait_tx();

kwh is defined as int

?

Add an explicit cast:

memcpy(msg, [glow](const void *)[/glow]kwh, 2);

This worked, i´m getting no compiler errors.

But how do i go around doing it on the receiver side? - It does not receive properly.

This VirtualWire library is over my head i feel....

Ok i have this code on the transmitting side:

     uint8_t msg[2];
     memcpy(msg, (const void *)kwh, 2);
     vw_send(msg, 2);
     vw_wait_tx();

and this on the receiving:

  uint8_t buf[VW_MAX_MESSAGE_LEN];  
  uint8_t buflen = VW_MAX_MESSAGE_LEN; 
  if (vw_get_message(buf, &buflen))  
  {
      int i;
      for (i = 0; i < buflen; i++)
      {
        Serial.print(buf[i], DEC);

        }

If i define kwh as 0 i get 1980
1 gives 066
2 gives 660
100 gives 00

What i am receiving is not coherent to what i am transmitting...

Is there something wrong with the way i am receiving?

How many bytes did you get as a result of the call to vw_get_message? You are not printing any sort of identifier before or after the byte. How do you know where the printed data for one byte ends and the data for the next byte begins?

If the purpose of sending an array of bytes is to send an integer, why are you printing the individual bytes that make up that integer? If you were printing them on the sender to, so you had something to compare the bytes to, that might be a meaningful thing to do. As is, I can't see much sense in it.

I got 2 bytes and no i am not using any form of identifier.

I tried this:

  uint8_t buf[VW_MAX_MESSAGE_LEN];        
  uint8_t buflen = VW_MAX_MESSAGE_LEN;   
  if (vw_get_message(buf, &buflen))     
  {
     
    Serial.print("Hi Nibble: ");
    Serial.println(buf[1], DEC);
    Serial.print("Lo Nibble: ");
    Serial.println(buf[2], DEC);
      
   }

And its clear that i`m not getting anything. - It prints the serial text so its getting some sort of message. But theres no data. It just prints 0 nomatter what.

I am Sorry if i havent been clear - Im a beginner at this - The thing i am trying to do, is to transfer 2 bytes at a time with the VirtualWire library.

If you have a better idea on how to do it, then please share - i appreciate your help :slight_smile:

Check out reply #3:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1268969540

Thanks a lot! That Post solved my problem :slight_smile: