Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby

Introduction

I thought about doing something with the Raspberry Pi that I had left for a long time, but I can't think of a theme ... (sweat)

As a result of thinking for a while, I decided to try how versatile PyCall I have been using from the other day can be used, so I put PyCall in Raspberry Pi and made a circuit to detect ON / OFF of the switch and run it with Ruby Saw.

[Reference site] Raspberry Pi GPIO input / output sample (Python, C language, shell script)

Install PyCall

Since Ruby 2.1 was included immediately after the initial installation, I will use it as it is. Install PyCall with the following command.

python


$ sudo apt install -y ruby-dev
$ sudo gem install --pre pycall

Check the source code for Python

Let's take a look at the source code published on the reference site mentioned above.

gpio_input.py


#!/usr/bin/python
 
import RPi.GPIO as GPIO
import time
 
IO_NO = 4
 
print("press ^C to exit program ...\n")
 
GPIO.setmode(GPIO.BCM)
GPIO.setup(IO_NO, GPIO.IN)
 
try:
 while True:
  print(GPIO.input(IO_NO))
  time.sleep(1)
except KeyboardInterrupt:
 print("detect key interrupt\n")
 
GPIO.cleanup()
print("Program exit\n")

When I try it out ...

python


$ python gpio_input.py
press ^C to exit program ...
1
1
1
0
0
1
1
1
0
1
1
^Cdetect key interrupt

Program exit
$

It seems that when you press the button, it becomes 0, and when you release it, 1 is displayed. For the time being, I found that the wiring was correct.

Rewrite using PyCall

If you use the sample code as it is, it will be a little dull, so let's change it so that it is displayed while counting the number of ON / OFF times.

gpio_input.rb


require 'pycall/import'
include PyCall::Import

pyimport 'RPi.GPIO', as: 'gpio'

io_no = 4

status = 1
buff = 1

cnt = 0

puts "press ^C to exit program ...\n"

gpio.setmode.(gpio.BCM)
gpio.setup.(io_no, gpio.IN)

# 「Ctrl」+Stop at "C"
Signal.trap(:INT) do
  puts "detect key interrupt\n"
  gpio.cleanup.()
  puts "Program exit\n"
  exit 0
end

loop do
  status = gpio.input.(io_no)
  if buff != status
    if status == 1
      puts "OFF"
    else
      cnt += 1
      puts "ON(#{cnt})"
    end
  end
  
  buff = status
  sleep 0.5
end

By the way, in Ruby, variables starting with uppercase letters cannot be defined (I've heard that ...), so I change them to lowercase letters.

Operation check

Execute the command and press the button several times ...

python


$ ruby gpio_input.rb
press ^C to exit program ...
ON(1)
OFF
ON(2)
OFF
ON(3)
OFF
^Cdetect key interrupt

Program exit
$

It was done (^-^)

If you use a gem called PI Piper, you can do it with Ruby without any problem, so you don't need to use PyCall ...

Well, what should I do next?

Recommended Posts

Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Install pyenv on Raspberry Pi and version control Python
I tried using the Python library from Ruby with PyCall
Access google spreadsheet using python on raspberry pi (for myself)
Try using ArUco on Raspberry Pi
Detect "brightness" using python on Raspberry Pi 3!
Install python library on Lambda using [/ tmp]
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
Try tweeting arXiv's RSS feed on twitter from Raspberry Pi with python
Initial settings for using GrovePi + starter kit and camera on Raspberry Pi
Detect slide switches using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Sound the buzzer using python on Raspberry Pi 3!
Install Python and libraries for Python on MacOS Catalina
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Initial settings for using Python3.8 and pip on CentOS8
Install psycopg2 (pgsql library for python3) on Apple Silicon
Install selenium on Mac and try it with python
Try debugging Python on Raspberry Pi with Visual Studio.
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Compile and install MySQL-python for python2.7 on amazon linux
Output to "7-segment LED" using python on Raspberry Pi 3!
Try casting videos and websites from Raspberry Pi to Chromecast and Nest Hub using CATT
Use kintone API SDK for Python on Raspberry Pi (easily store data in kintone from Raspberry Pi)
Python on Ruby and angry Ruby on Python
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Run AWS IoT Device SDK for Python on Raspberry Pi
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install mecab on Sakura shared server and call it from python
Notes on using MeCab from Python
Install ghoto2 on Raspberry Pi (memo)
Quickly install OpenCV 2.4 (+ python) on OS X and try the sample
Cross-compile for Raspberry Pi Zero on Debian-Create your own shared library
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
Install fabric on Ubuntu and try
Install Python on CentOS using Pyenv
Install confluent-kafka for Python on Ubuntu
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Install OpenMedia Vault 5 on Raspberry Pi 4
Install Python on CentOS using pyenv
Try using Amazon DynamoDB from Python
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Install ROS and ROS module for Roomba on RaspberryPi3 and try to run it
Stream processing for Python and SensorTag, Kafka, Spark Streaming --Part 1: Raspberry Pi 3
Notes for using OpenCV on Windows10 Python 3.8.3.
Try fishing for smelt with Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Try calling Python from Ruby with thrift
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
How to install NumPy on Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
The temperature is automatically measured using Raspberry Pi 3 and automatically uploaded to the server (Docker python3 + bottle + MySQL) for viewing!
I tried running Flask on Raspberry Pi 3 Model B + using Nginx and uWSGI