Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container

Introduction

It has become a confusing title, but it looks like this スクリーンショット 2020-02-11 14.56.17.png

environment

Development PC: Mac Mojave RaspBerry Pi4 memory 4GB SD card 16GB

Write Ubuntu 19.10 to SD

https://ubuntu.com/download/raspberry-pi I downloaded the 64-bit version from and wrote it using balena Etchar.

Install docker on Ubuntu

Enter Ubuntu with ssh or something

sudo apt-get install docker-ce

You can install it with.

sudo usermod -aG docker ubuntu

You can docker without using sudo.

Make a base image

You can write a python script on the development PC and then copy it to dockerImage with Dockerfile, but it was difficult to understand, so once you make a base with Dockerfile, enter a container and create an image from that container.

Dockerfile

[Building a Docker environment for TensorFlow with Raspberry Pi](https://qiita.com/kohbis/items/53c27ce6c62a7039aaa2#raspberrypi-%E3%81%AB-docker-%E3%82%92%E3%81%88%E3 % 81% 84% E3% 82% 84) I referred to this article, almost above. I wanted to put rpi.gpio, but I didn't know how to put it, so I just wrote it.

FROM resin/rpi-raspbian:stretch

RUN echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi firmware" > /etc/apt/sources.list

RUN apt-get update -y
RUN apt-get install -y --no-install-recommends \
        vim git less wget \
        build-essential \
        libatlas-base-dev \
        python3-pip python3-dev python3-setuptools\
        python3-scipy python3-h5py \
        libraspberrypi-bin \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --upgrade pip setuptools
RUN pip3 install rpi.gpio

Then build

docker build -t rpi/rpi:0.1 .

Make sure you can do it with docker images! It should be done.

Start a container and write a python script

Start the container from the image you created earlier.

docker run --name rpi_test -ti --privileged rpi/rpi:0.1 /bin/bash

Then, it will be inside the container, so install what you want. Because it is a nano school

sudo apt-get update
sudo apt-get install nano

Go to home and make a script. I think it's better to set a working directory around here ...

cd home
nano test.py

It is a program that puts an LED on GPIO17 and makes an appropriate L flickering.


import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
while True:
    GPIO.output(11, True)
    time.sleep(2)
    GPIO.output(11, False)
    time.sleep(2)

After saving the script

exit

Please log out with

docker ps -a
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                     PORTS               NAMES
e700d8490be8        rpi/rpi:0.1           "/usr/bin/entry.sh /…"   11 minutes ago      Exited (0) 6 seconds ago                       rpi_test

The container is stopped. To make an image from this container

docker commit rpi_test rpi/rpi:0.2
docker images
REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE
rpi/rpi              0.2                 50f7f2a406e7        2 minutes ago       622MB

You now have an image with the script saved.

Start-up

sudo docker run --privileged -it -d --name rpi -w /home rpi/rpi:0.2 python3 test.py

The container moves in the background and keeps fluttering! If you want to stop it, stop with docker stop rpi

Summary

I want to get sensor data soon

Recommended Posts

Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
I set the environment variable with Docker and displayed it in Python
Put Scipy + Matplotlib in Ubuntu on Vagrant and display the graph with X11 Forwarding
Install CaboCha in Ubuntu environment and call it with Python.
Fill the string with zeros in python and count some characters from the string
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
Put Python3 in Docker container of Amazon Linux2
Put Docker in Windows Home and run a simple web server with Python
Install selenium on Mac and try it with python
Get mail from Gmail and label it with Python3
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Get the matched string with a regular expression and reuse it when replacing on Python3
Put Python 3.x on Ubuntu
Recursively get the Excel list in a specific folder with python and write it to Excel.
[Python] I installed the game from pip and played it
Life game with Python [I made it] (on the terminal & Tkinter)
How is the progress? Let's get on with the boom ?? in Python
Control the motor with a motor driver using python on Raspberry Pi 3!
How to install OpenCV on Cloud9 and run it in Python
Install mecab on Sakura shared server and call it from python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
How to use python put in pyenv on macOS with PyCall
Read a file in Python with a relative path from the program
Install lp_solve on Mac OS X and call it with python.
From ubuntu installation to running kinect with docker and ros (overview)
Operate Firefox with Selenium from python and save the screen capture
Put python, numpy, opencv3 in ubuntu14
Until you put Python in Docker
Pass the selected item in Tablacus Explorer from JScript to python and rename it all at once
[Small story] I can't find it even though I apt search to install Python on ubuntu with Docker.
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Deploy Python face recognition model on Heroku and use it from Flutter ②
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
Deploy a Python app on Google App Engine and integrate it with GitHub
Put OpenCV in OS X with Homebrew and input / output video with python
Put your own image data in Deep Learning and play with it
Send email with SES in Python and short message with SMS on SNS
Use python on Raspberry Pi 3 to light the LED with switch control!
Enclose the cat result in double quotes and put it in a variable
Scrap the published csv with Github Action and publish it on Github Pages
Set cron from 1 on Ubuntu 16.04 (Sakura VPS) and execute python program regularly
How to make a container name a subdomain and make it accessible in Docker
Create and edit spreadsheets in any folder on Google Drive with python
Put MeCab binding for Python with pip on Windows, mac and Linux
Get and convert the current time in the system local timezone with python
Deploy and use the prediction model created in Python on SQL Server
Read the file with python and delete the line breaks [Notes on reading the file]
Compare nighttime and daytime returns on the Nikkei Stock Average with python
Make a decision tree from 0 with Python and understand it (4. Data structure)
Deep Learning from scratch-Chapter 4 tips on deep learning theory and implementation learned in Python
[python] Send the image captured from the webcam to the server and save it
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
Use the LibreOffice app in Python (2) Manipulate calc (from macros and externals)
Exclusive control with lock file in Python
Behind the flyer: Using Docker with Python
Build Python3 and OpenCV environment on Ubuntu 18.04
Python virtual environment and packages on Ubuntu
Display Python 3 in the browser with MAMP
A memo with Python2.7 and Python3 on CentOS
Dealing with "years and months" in Python