Skip to content

Instantly share code, notes, and snippets.

@psygo
Created May 10, 2021 14:45
Show Gist options
  • Save psygo/34956885ac4d8be03c8fed3f5cfae603 to your computer and use it in GitHub Desktop.
Save psygo/34956885ac4d8be03c8fed3f5cfae603 to your computer and use it in GitHub Desktop.
Édouard's Raspberry Pi Course

Édouard's Raspberry Pi Course

Tags: Raspberry Pi, Electronics

Raspberry Pi For Beginners - 2021 Complete Course | Udemy

  • The current limit for the Raspberry Pi is near 15 mA.
  • When using an infinite loop, use time.sleep to consume less CPU usage.
  • hostname -I for the IP address on the network.
  • df -h for the available space on the SD card.
  • sudo raspi-config for more Rasp configs.
  • Use yagmail to send and receive emails (SMTP).
  • You can use systemd to start your app when the Raspberry Pi boots up.
     [Unit]
     Description = Program that detects movements, takes a photo and sends an email with it
     After=multi-user.target
     
     [Service]
     ExecStart=/user/bin/python3 /home/pi/Documents/python_programs/project_step4.py
     User=pi
     
     [Install]
     WantedBy=multi-user.target
    • Then sudo systemctl enable project.service
  • Taking photos: raspistill -o camera/img.jpg
    • You can also use rotation, width and height arguments.
    • And you can also record videos with raspivid
    • In Python:
     import picamera
     
     camera = PiCamera()
     camera.resolution = (1280, 720)
     camera.rotation = 180
     time.sleep(2)
     
     file_name = "path/to/file"
     camera.capture(file_name)
     print("Done.")
     
     # Or:
     camera.start_recording(file_name)
     camera.wait_recording(10)
     camera.stop_recording()

Python Management

  • pip3 list to list all of the installed packages.
  • except KeyboardInterrupt: captures the Ctrl + C exception.
# Use `"w"` to write
with open("/home/pi/text_file", "r") as f:
    # print(f.read())
	for line in f:
	    print(line)
if os.path.exists("/home/pi/text_file"):
    print("File exists")
	os.remove("/home/pi/text_file")

Web Server

from flask import Flask
import RPi.GPIO as GPIO

BUTTON_PIN = 26

GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN)

app = Flask(__name__)

@app.route("/")
def index():
    return "Hello from Flask"
	
@app.route("/push-button")
def push_button_state():
    if GPIO.input(BUTTON_PIN) == GPIO.HIGH:
		return ""
	else:
	    return "Button is not pressed"

app.run(host="0.0.0.0", port=8500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment