Write GPIO Raspberry PI 2 B+ - python

I am new to Raspberry Pi. I need to read and write to the GPIO pins on the Raspberry Pi. I am trying to do this operation in php, python, and the command line. Do I need to download any kind of library to do this in php, python, or command line?

No, controlling GPIOs from userspace is a feature provided by the operating system. You just need to write a number to the GPIO value "file" thats provided by the kernel. It is located under /sys/class/gpio/gpio<number>/value. This should give you a kickstart. There they show how its done on the BeagleBoard with the shell but it is a general concept and it doesnt matter which language you use.

In python you can use the GPIO library
Generally in unix devices are represented as files, so you can open a file in /dev/ and read/write to the devices.
You might get more detailed help on https://raspberrypi.stackexchange.com/

Besides sysfs, the virtual filesystem that allows root to directly read from and write to GPIO pins by echo to or cat from various files in the /sys/class/gpio/ folder (as already mentioned) there are libraries available to do so. Two noteworthy examples are:
wiringPi library provides a cli command gpio to be used by non-privileged users for direct access.
pigs utility, part of the pigpio library also offers comprehensive control of the GPIO pins from the command line (see pigpio for download of source, installation procedures and excellent application examples).

Related

Control java applet using a Joystick/Gamepad on a Raspberry Pi 3

I need to create an arcade cabinet with a clone of an old arcade videogame. A Raspberry PI3 must be used for the:
Running the videogame
External LEDs Control via GPIO
A USB joystick must be used for the control of the main character. I must read the buttons and axis of the joystick from the game. The game is written on java and uses library processing-core.jar (library of the programming language processing). But I cannot find any library to read the button states from Java that runs on Raspberry Pi OS.
I used GameControlPlus - library for processing, but it doesn't work on this processor architecture with this operating system: It can not find right native methods. I tried to emulate my keyboard and mouse via a USB controller. But I need to launch this software and select the configuration file by every launch of the raspberry pi. I founded, that the controller info lays in the dev/controls and the USB controller signals can be read from this directory using evdev, but it uses Python. But I use Java and don't know any another programming languages.

Running a Python animation via USB-Stick on an external "Monitor"?

Basically my project involves displaying a certain animation on a LG TV. The only input I've been given is an USB interface.
From there on the goal is to display an animation or e.g. the current time, date, a countdown or anything alike which could be created with Python libraries.
-> So I need to somehow get a live animation / screensaver running via an USB-Stick on a LG TV. I searched for any Python packages that would enable me to do something like this, but unfortunately I wasn't able to find anything.
Does anyone have ANY idea on how to approach something like this? Feel free to ask any questions if the task isn't understandable.
The easiest method would be to render the animation into a video container that the Smart TV understands. Like mkv or mp4
Python is an interpreted language and it seems wasteful to try get a way for the Smart TV to interpret the Python code.
Unless the animation has interaction - record it on a PC, save to USB and play on TV.
You could also use a Raspberry Pi or Pi Zero with a linux distribution running Python. Then just use the TV as an HDMI monitor.
Do you know the scripts you would like to run?
Install Linux Raspbian (or other) on Raspberry Pi/Pi zero ($35/$11 ea.) at this time enabling or connecting it to WiFi - it sounds like you want values and items from the internet.
Install Python 3 on the Pi: https://installvirtual.com/how-to-install-python-3-8-on-raspberry-pi-raspbian/
Install the dependencies and libraries like: pip install pandas (or whatever libraries you need like matplotlib)
Run your Python script on the Raspberry Pi.
If you want to have the script execute on Startup (I assume you want this installation to be relatively hands-off) - https://www.instructables.com/id/Raspberry-Pi-Launch-Python-script-on-startup/
I would recommend adding a timeout on opening the script, with option to return to the OS and exit the script or you won't have much access to maintain the Pi (it will start executing the script immediately.)
Make sure you know how to do PuTTy access over SSH (or VNC) to maintain the Pi - as you don't want to try debug and code directly on the Pi - especially if this is a public display.

Uploading existing sketch via Python

i have a Arduino which is connected to my PC. I'm sending some hex values over Serial port (with Python/Spyder IDE) to it to control the GPIO pins.
Is there a way to upload the sketch to the Arduino without using the IDE.
I don't want to write the sketch in python. Just want to upload a existing sketch via a Python script.
Thanks for your help!
You can build and upload Arduino sketches with python, there are two worthwhile tools: build_arduino.py and arduino-sketch.
Their pages provide detailed installation and usage instructions.

Control rs232 windows terminal program from python

I am testing a piece of hardware which hosts an ftp server. I connect to the server in order to configure the hardware in question.
My test environment is written in Python 3.
To start the ftp server, I need to launch a special proprietary terminal application on my pc. I must use this software as far as I know and I have no help files for it. I do however know how to use it to launch the ftp server and that's all I need it for.
When I start this app, I go to the menu and open a dialog where I select the com port/speed the hardware is connected to. I then enter the command to launch the ftp server in a console like window within the application. I am then prompted for the admin code for the hardware, which I enter. When I'm finished configuring the device, I issue a command to restart the hardware's software.
In order for me to fully automate my tests, I need to remove the manual starting of this ftp server for each test.
As far as I know, I have two options:
Windows GUI automation
Save the stream of data sent on the com port when using this application.
I've tried to find an GUI automater but pywinauto isn't supporting Python 3. Any other options here which I should look at?
Any suggestions on how I can monitor the com port in question and save the traffic on it?
Thanks,
Barry
Have you looked at pySerial? It's been a few years since I've used it but it was quite good at handling RS-232 communications and it looks like it's compatible with Python 3.x.
Sikuli might provide the kind of GUI automation you need.
I was also able to solve this using WScript, but pySerial was the preferred solution.

Getting Notified on "Ethernet cable plugged in" events in linux

I am writing a Python Application running on linux. I want to be able to register and be notified by the system if a network cable is plugged in/ out.
I am already using pyUdev (python bindings for libUdev) in order to get notified for USB plug in events. However, because the network eth0 module (for example) always remains loaded (regardless of the cable being plugged in) , I dont get the information I require. It only works for if up/down events.
I have read a lot of posts on Windows WMI providing this functionality but none on linux. Am I looking in the right direction?
A python way of doing this (a python library) would be ideal as I want to integrate this with my program.
There are many ways to get the "UP" status of a network interface via polling, ranging from simply parsing ifconfig output, to reading the status of /proc/net/... pseudo-files.
However, for a way to be notified (i.e. not poll yourself) for such events, your best option on Ubuntu is to use the netplug package. It's not installed by default, you need to:
sudo apt-get install netplug
Some documentation is here: http://manpages.ubuntu.com/manpages/lucid/man8/netplugd.8.html
To set it up you need to tweak /etc/netplug/netplugd.conf and /etc/netplug/netplug scripts. For an example of those config files check out the section "Configuring netplug" at http://natisbad.org/dyn-net/index.html
With /etc/netplug/netplug you can have your own python script called when the network goes down/up, so you can generate your kind of event for your application.
Have you looked into netplugd command, I am not sure if a python wrapper exists but it should not be hard to parse the output

Categories