VirtualWire support for Raspberry Pi

FS1000A and XY-MK-5V 433MHz RF modules are very often first choice for cheap and dirty Do It Yourself wireless communication. Pair of those , allowing one way radio communication, const less than 3 dollars or euros. So they are really cheap. Limited range and transmission speed limits their real life usage, but simple assembly and extremely easy programming are additional advantage over more complex solutions. Specially in Arduino world, with VirtualWire library. I will not write about it right now, there is enough on the internet already.

FS1000A and XY-MK-5V 433MHz RF modules for Raspberry Pi

Unfortunately, problems starts when you want to do cross platform communication based on those modules and VirtualWire library. For example with Arduino and Raspberry Pi. I have found exactly one working implementation of VirtualWire library that is written in Python and works well with Raspberry Pi. And it was hidden. So, with some effort I allowed myself to wrap this code into small python library and this is how piVirtualWire was created.

It depends on pigpio library and requires it installed on Raspberry Pi:

wget abyz.co.uk/rpi/pigpio/pigpio.zip
unzip pigpio.zip
cd PIGPIO
make
make install

And running always in the background sudo /home/pi/PIGPIO/pigpiod

Then, all you need is code.

Transmitter module python code example

This code snippet assumes that FS1000A TX module ADAT (DATA) line is connected to GPIO 4 and uses 1000 baud rate. piVirtualWire library is located in piVirtualWire subfolder.

import piVirtualWire.piVirtualWire as piVirtualWire
import time
import pigpio

if __name__ == "__main__":

    pi = pigpio.pi()
    tx = virtualwire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate

    msg = 42

    tx.put(msg)
    tx.waitForReady()

    tx.cancel()
    pi.stop()

Receiver module python code example

This code snippet assumes that XY-MK-5V is connected to GPIO 18 and uses 1000 baud rate.

import piVirtualWire.piVirtualWire as piVirtualWire
import time
import pigpio

if __name__ == "__main__":

    pi = pigpio.pi()
    rx = piVirtualWire.rx(pi, 18, 1000) # Set pigpio instance, TX module GPIO pin and baud rate

    while True:

            while rx.ready():
                print(rx.get())

            time.sleep(0.5)

    rx.cancel()
    pi.stop()

Happy Arduino to Raspberry Pi radio transmissions!

22 thoughts on “VirtualWire support for Raspberry Pi

      1. Libraries get imported successfully but on executing the program i get the error as follows

        AttributeError: ‘NoneType’ object has no attribute ‘sendall’

        My program is :

        import piVirtualWire.piVirtualWire as piVirtualWire
        import time
        import pigpio

        pi = pigpio.pi()
        tx = piVirtualWire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate
        while(1):
        msg = ’42’

        tx.put(msg)
        tx.waitForReady()

        tx.cancel()
        pi.stop()

        ***************************

        i saved PIGPIO in home/pi/try/
        and VirtualWire in
        home/pi/try/PIGPIO/

        my code is saved in
        home/pi/try/PIGPIO

        Please help

        1. here is the complete error :

          Traceback (most recent call last):
          File “/home/pi/try/PIGPIO/send_data.py”, line 10, in
          tx.put(msg)
          File “/home/pi/try/PIGPIO/piVirtualWire/piVirtualWire.py”, line 120, in put
          self.pi.wave_add_generic(self.wf)
          File “/home/pi/try/PIGPIO/pigpio.py”, line 2113, in wave_add_generic
          self.sl, _PI_CMD_WVAG, 0, 0, len(pulses)*12, extents))
          File “/home/pi/try/PIGPIO/pigpio.py”, line 1000, in _pigpio_command_ext
          sl.s.sendall(ext)
          AttributeError: ‘NoneType’ object has no attribute ‘sendall’

        2. I also tried this code

          import piVirtualWire.piVirtualWire as piVirtualWire
          import time
          import pigpio

          pi = pigpio.pi()
          tx = piVirtualWire.tx(pi, 4, 9600) # Set pigpio instance, TX module GPIO pin and baud rate

          msg = ’42’

          tx.put(msg)
          tx.waitForReady()

          tx.cancel()
          pi.stop()

  1. Unfortunatly the description could be better :-/

    I installed it like you wrote,…
    when I write bash receive.py (your code) i get errors,…
    receive.py: line 1: import: command not found
    receive.py: line 2: import: command not found
    receive.py: line 3: import: command not found
    receive.py: line 7: syntax error near unexpected token `(‘
    receive.py: line 7: ` pi = pigpio.pi()’

    So I have absolute no Idea how to get it work, and the idea from this script is really awesome.

    1. Hi JAN,

      Are you sure your python is running correctly? `import command not found` looks like genuine python problem.
      Either way, piVirtualWire has to be in a subfolder piVirtualWire in the same catalog as receive.py is located.

      1. Hey,
        there was a problem with the script, right.

        But now I get the error:
        ./receive.py: line 1: syntax error near unexpected token `(‘
        ./receive.py: line 1: `pi = pigpio.pi()’

        PIGPIO is located /home/pi/PIGPIO
        VirtualWire is located /home/pi/PIGPIO/piVirtualWire

        Thanks
        Jan

        1. Hey, the script is running now ๐Ÿ™‚
          There was a problem with the pigpio installation.

          Now I have to find out how to send data via Arduino to my Raspberry ๐Ÿ™‚

  2. Hey, I just came across your post while trying to connect two Raspi 3 with a pair of FS1000A / XY-MK-5V. Your/the code looks straight forward, but sadly I’m not able to make them talk to each other. The RF modules are working in an Arduino setup, so I assume they are fine.

    Did you use any additional resistors or did you simply connect the data line(s) straight to the GPIO pin?

    And dod you power both sender and receiver from the 5V or the 3.3V line?

    Thanks,
    chris

      1. Ok. That’s what I tried as well – without success yet. So I need to continue troubleshooting. But thanks so far!

  3. 1.
    In the Transmitter-snippet
    tx = virtualwire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate should be
    tx = VirtualWire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate
    2. If I run (the corrected) snippet (called it TX.py) I get:
    Traceback (most recent call last):
    File “TX.py”, line 12, in
    tx.put(msg)
    File “/home/dfstk/piVirtualWire/piVirtualWire.py”, line 94, in put
    if len(data) > MAX_MESSAGE_BYTES:
    TypeError: object of type ‘int’ has no len()

    What can I do??

    Thanks
    Dieter

    1. sorry, correction
      1.
      In the Transmitter-snippet
      tx = virtualwire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate
      should be
      tx = piVirtualWire.tx(pi, 4, 1000) # Set pigpio instance, TX module GPIO pin and baud rate

      Dieter

      1. Same error here.

        Fixed by making mes a string instead of an int

        mess = ’42’
        instead of
        mes = 42

        However, I am still not getting data across to my arduino or to a pi…

        1. If I put an led across vcc and pin 18, it lights up after I send data via the RF.

          Sorry about all the posts….trying to debug…

  4. The library doesn’t work anymore for me too. Raspberry PI 3. I can’t recieve or send anything from the raspberry pi.

    But my both arduino can send and see messages.

    Any idea about the cause ?

        1. I have both the receiver and transmitter hooked up to the pi. If I wire the ADAT directly to the GPIO 18 pin, the data is received. My transmitter and receiver are less than an inch apart, but the communication will not happen via RF.

  5. I use an Arduino to send data and a Raspberry-pi 3 to receive. For me it it was up & running after a 1/2h. But my antenna need to be better tunned (only +/-4m with a 17cm antenna) and the breadboard and processor noise are certenly also annoyance.
    ps take a look at the pin numbering ….

Leave a Reply

Your email address will not be published.