Skip to content

Instantly share code, notes, and snippets.

@AO8
AO8 / pi_lapse.py
Created April 6, 2017 19:20
Timelapse photo with timestamp Python script for Raspberry Pi
from picamera import PiCamera, Color
from time import sleep
import datetime as dt
camera = PiCamera()
# 8 hour timeplase with image taken every 10 seconds
for i in range(2880):
camera.annotate_background = Color("black")
camera.annotate_text = dt.datetime.now().strftime("PiLapse %m-%d-%Y %H:%M:%S")
@AO8
AO8 / video.py
Created May 7, 2017 16:45
Record h264 timestamped video in Python with PiCamera
from picamera import PiCamera, Color
from time import sleep
import datetime as dt
camera = PiCamera()
camera.annotate_background = Color("black")
start = dt.datetime.now()
camera.start_recording("recording.h264")
while (dt.datetime.now() - start).seconds < 3600: # total seconds for length of recording
@AO8
AO8 / motiontxt.py
Last active May 12, 2017 15:12
Use a PIR sensor, Raspberry Pi, and PiCamera to detect motion, take a timestamped photo, then send a text message alert from Gmail with Python.
# Allow less secure apps to access your Gmail account
# https://en.wikipedia.org/wiki/List_of_SMS_gateways
# 10digitNumber@mms.att.net for AT&T
# 10digitNumber@pm.sprint.com for Sprint
# 10digitNumber@tmomail.net for T-Mobile
# 10digitNumber@vzwpix.com for Verizon
from gpiozero import MotionSensor
@AO8
AO8 / send_mms.py
Last active June 7, 2017 15:39
Send a text message from Gmail with Python
# allow less secure apps to access your Gmail at: https://support.google.com/accounts/answer/6010255?hl=en
# https://en.wikipedia.org/wiki/List_of_SMS_gateways
# ten_digit_number@mms.att.net for AT&T
# ten_digit_number@pm.sprint.com for Sprint
# ten_digit_number@tmomail.net for T-Mobile
# ten_digit_number@vzwpix.com for Verizon
import smtplib
username = "your_address@gmail.com"
@AO8
AO8 / fizz_buzz.py
Created June 23, 2017 20:46
Simple FizzBuzz Python Solution
def fizz_buzz():
for num in range(1,101):
if num % 5 == 0 and num % 3 == 0:
print("FizzBuzz")
elif num % 3 == 0:
print("Fizz")
elif num % 5 == 0:
print("Buzz")
@AO8
AO8 / send_img.py
Last active June 23, 2017 20:48
Take a timestamped photo with PiCamera, then attach and send from Gmail with Python
# Enable less secure apps in Gmail
import os
import smtplib
import email
import sys
import picamera
import time
from datetime import datetime
from email.mime.multipart import MIMEMultipart
@AO8
AO8 / starts_with.py
Last active September 27, 2017 16:19
Simple practice exercise using Python's startswith() and index() methods.
words = ["hello", "Break", "cat", "blue", "brown", "zoo", "baby", "pie", "BOOM!"]
def b_count(lst):
# prints index and string of every element in a list that starts with the letter 'b' or 'B'
for i in lst:
if i.startswith("b") or i.startswith("B"):
print("Index " + str(lst.index(i)) + ": " + i)
b_count(words)
@AO8
AO8 / send_msg.py
Last active October 1, 2017 22:53
Send Gmail with Python
# Allow less secure apps to access your gmail account
import email
import smtplib
fromaddr = "yourAddress@gmail.com"
toaddr = "toAddress@domain.com"
msg = "Content of your email message."
username = "yourGmailAddress"
@AO8
AO8 / eight_ball.py
Last active October 5, 2017 15:37
Magic 8 Ball with Python. Reads from included txt file.
import random
with open("eight_ball_answers.txt", "r") as f:
answers = f.read() # read in text as string
answers = answers.strip().replace('"', '') # remove white space and double quotes
answers = answers.strip('[]').split(', ') # remove the square brackets and split on separator
question = input("Ask a question or enter \"N\" to quit: ")
while not(question.upper().startswith("N")):
@AO8
AO8 / halloween_motion_scare.py
Created October 9, 2017 20:12
A fun Halloween-themed Python project, perfect for unsuspecting trick-or-treaters. Use a Raspberry Pi, the Pygame package, and a cheap PIR sensor to play a random .wav file when motion is detected.
# tutorial to connect PIR sensor to RPi at: https://www.raspberrypi.org/learning/parent-detector/worksheet/
# download the Pygame package at: http://www.pygame.org/download.shtml
# download free .wav audio files at: http://soundbible.com/
# this scream horror .wav file is a favoriate: http://soundbible.com/1627-Female-Scream-Horror.html
from gpiozero import MotionSensor
import pygame.mixer
from pygame.mixer import Sound
import random
from time import sleep