7

I'm writing a Raspberry Pi program, to run stuff, take inputs from sensors, save in database and synchronise to a server.

Is there a way to test it on the PC before deploying it to the Raspberry Pi? Because there are too many GPIO operations, I want to make sure it works before putting it on the Raspberry Pi. Some sort of a GPIO emulator or something. I'm on Manjaro Linux.

It doesn't have to be emulator, some sort of an IDE capable of understanding GPIO functions. For instance, a possible output would be:

gpio 1 is running,
sensor 1 is running
sensor 2 isn't running

Something like that, perhaps a Bpython plugin or something. All I want to do is make sure that all my GPIO functions are working the way I want them to work before testing on the Raspberry Pi, because many times, errors on the Raspberry Pi has to do with me not connecting parts correctly. And I don't like to code and fix bugs on the Raspberry Pi.

So if I can verify my script is working correctly on my PC, I can go to the Raspberry Pi, knowing that my script is fine and fix stuff on the board. I just don't want to test my script on the Raspberry Pi anymore.

Peter Mortensen
  • 2,004
  • 2
  • 15
  • 18
Lynob
  • 257
  • 1
  • 2
  • 11

7 Answers7

6

I'm not aware of any ready built solution for your needs.

However you could write your own with not very much effort. How many different calls do you make to the gpio functions? You might find you use less than 10. Just create your own local Python module named the same as your target module and produce stubs for the functions you use.

E.g. if you use function gpio_write() which takes a gpio and a level your stub could just be

def gpio_write(gpio, level):
   print("gpio_write called with gpio={} level={}".format(gpio, level))

Your gpio_read stub could be

def gpio_read(gpio):
   global val
   print("gpio_read called with gpio={}, returning {}".format(gpio, val))
   return val # where val may be read from a file or set externally

This is a fairly standard practice, long used in software development to test software before hardware is available.

By the way my pigpio Python module runs on Windows/Mac/Linux PCs.

joan
  • 71,852
  • 5
  • 76
  • 108
6

GPIO Zero provides a mock pin interface, meaning you can run the same code on your PC and emulate the pins. See examples of how it's used in the test suite.

ben_nuttall
  • 2,471
  • 14
  • 15
3

I made this little library, fedeb95/pin that proved useful for me. Lacks some features, but if it suits you... it worked for me.

Edit: pin is a RPi.GPIO wrapper. Instead of calling method x of RPi.GPIO you call method x of pin. You can specify in a configuration file if your program is running in test mode (wyere you read random values from pins, or values you set with set_value) or on an actual raspberry, where this library simply calls the corresponding RPi.GPIO method. It lacks something, but it's a very simple library, and anyone is free to contribute or tell me what feature he needs and I'll try to add it

fedeb
  • 31
  • 2
2

There's a library you can download here: https://roderickvella.wordpress.com/2016/06/28/raspberry-pi-gpio-emulator/

Or there's a simulator which lets you write and test the python code here: http://blog.withcode.uk/2016/10/rpi-gpio-python-simulator/

pddring
  • 121
  • 1
2

I suppose you could use mocking libraries like Mock.

They are mostly used for Unit Testing, but I'm pretty it would work very well in your case. And you would probably be better of trying it by writting some unit tests.

It replaces on-the-go an existing class, and you just have to write yourself the behaviour you're looking for. It allows you not to change anything in your logic (even the imports), but still get the return values you suppose you would receive from an actual call of the API.

BriceP
  • 123
  • 4
2

Being a newbie to Python and the Pi I didn't understand Joan's answer and needed a simple (bot not elegant!) way of testing my program on the PC without error messages caused by missing GPIO calls. I simply choose what sections of code to run by checking what machine I'm running on. I can then emeluate a GPIO call - like a switch closing - from the PC keyboard.

#!/usr/bin/python3
import socket
my_PC_host_name = 'Dev'
host_name = socket.gethostname()
if (host_name != my_PC_host_name):  # Only run these lines if we're not on the PC
    import RPi.GPIO as GPIO
    # Set GPIO numbering mode and define the input pin
    GPIO.setmode(GPIO.BOARD)  # Uses the same board numbering as on the board
    GPIO.setup(16, GPIO.IN)  # Set up the pin 16 as input

if (host_name != my_PC_host_name):  # Only run these lines if we're not on the PC
    try:
        while True:
            if GPIO.input(16) == 0:
                print ("Switch is open")
            else:
                print ("Switch is closed")             
    finally:
        GPIO.cleanup()  # Cleanup the GPIO ports before ending          
else:  # We're running on the PC
    try:
        while True:
            c = input('Press "o" then "Enter":')
            if c.upper() == 'O':
                print ("Switch is open")
            else:
                print ("Switch is closed")
    finally:           
        print ("Process completed")
M61Vulcan
  • 121
  • 2
1

Just stumbled across this. I've just been using https://github.com/wallysalami/tkgpio It's very good. Not only are GPIO pins simulated but there is also a user interface with buttons, LEDs, buzzers, text display. Only thing I found lacking in my first use was no ability to add plain text to the screen, so I used buzzers but that cost me 2 gpios. [![example using tkgpio][1]][1]

In the example screen shot the red box is my app output and the blue box is a means for me to simulate pulse input rates and buttons to force certain situations. Purchase and Sell are 2 buzzers used to get those 2 text labels on the screen. [1]: https://i.sstatic.net/YCAZd.jpg

Alan
  • 11
  • 1