Created
November 12, 2020 09:41
-
-
Save harakka/58273b0128c94c6667c31cf41748effa to your computer and use it in GitHub Desktop.
Hacky script to display data on Raspi-connected oled screen and motioneye overlay. Need to rewrite this into separate polling/data collection and consumer components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Original file: https://github.com/adafruit/Adafruit_Python_SSD1306/blob/master/examples/stats.py | |
# Modified by Antti 'harakka' Riikonen for personal use, to display readings | |
# from AM2302 sensor, the library for which is also from Adafruit. You rock. | |
# Original license follows. | |
# Copyright (c) 2017 Adafruit Industries | |
# Author: Tony DiCola & James DeVito | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
import time | |
import signal | |
import os | |
import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
import Adafruit_DHT | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
import subprocess | |
# Signal handler turns screen and camera overlay off and dies | |
def handleSignal(signal, frame): | |
print 'Signal ',signal,' received, shutting gracefully.' | |
disp.command(Adafruit_SSD1306.SSD1306_DISPLAYOFF) | |
urlencoded_text = "Temp%3A%20NA%5CnHum%3A%20%20NA".format(temperature,humidity) | |
with open(os.devnull, "w") as void: | |
subprocess.call(["/usr/bin/curl", "--silent", "--show-error", "http://localhost:7999/1/config/set?text_left={}".format(urlencoded_text)], stdout=void) | |
exit(0) | |
''' | |
# Set up interrupt signals for capture | |
# Actually we don't need this since the finally block will handle cleanupsignal.signal(signal.SIGHUP, handleSignal) | |
signal.signal(signal.SIGINT, handleSignal) | |
signal.signal(signal.SIGQUIT, handleSignal) | |
signal.signal(signal.SIGABRT, handleSignal) | |
signal.signal(signal.SIGBUS, handleSignal) | |
signal.signal(signal.SIGFPE, handleSignal) | |
signal.signal(signal.SIGSEGV, handleSignal) | |
signal.signal(signal.SIGTERM, handleSignal) | |
''' | |
# Temperature sensor setup | |
sensor = Adafruit_DHT.AM2302 | |
DHTPin = 4 | |
# Raspberry Pi pin configuration: | |
RST = None # on the PiOLED this pin isnt used | |
# Note the following are only used with SPI: | |
DC = 23 | |
SPI_PORT = 0 | |
SPI_DEVICE = 0 | |
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) | |
# Note you can change the I2C address by passing an i2c_address parameter like: | |
# disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_address=0x3C) | |
# Alternatively you can specify an explicit I2C bus number, for example | |
# with the 128x32 display you would use: | |
# disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=2) | |
# Initialize library. | |
disp.begin() | |
# Clear display. | |
disp.clear() | |
disp.display() | |
# Set brightness | |
disp.set_contrast(1) | |
# Create blank image for drawing. | |
# Make sure to create image with mode '1' for 1-bit color. | |
width = disp.width | |
height = disp.height | |
image = Image.new('1', (width, height)) | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
# Draw some shapes. | |
# First define some constants to allow easy resizing of shapes. | |
padding = -2 | |
top = padding | |
bottom = height-padding | |
# Move left to right keeping track of the current x position for drawing shapes. | |
x = 0 | |
# Load default font. | |
#font = ImageFont.load_default() | |
if os.access("/home/pi/MiniSet2.ttf", os.R_OK): | |
font = ImageFont.truetype("/home/pi/MiniSet2.ttf", 8) | |
else: | |
font = ImageFont.load_default() | |
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script! | |
# Some other nice fonts to try: http://www.dafont.com/bitmap.php | |
# font = ImageFont.truetype('Minecraftia.ttf', 8) | |
try: | |
while True: | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load | |
# cmd = "hostname -I | cut -d\' \' -f1" | |
IP1 = subprocess.check_output("ip -4 -h addr show dev wlan0 | grep inet | xargs | cut -d \' \' -f 2", shell = True ) | |
IP2 = subprocess.check_output("ip -4 -h addr show dev eth0 | grep inet | xargs | cut -d \' \' -f 2", shell = True ) | |
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" | |
CPU = subprocess.check_output(cmd, shell = True ) | |
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'" | |
MemUsage = subprocess.check_output(cmd, shell = True ) | |
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'" | |
Disk = subprocess.check_output(cmd, shell = True ) | |
# Try to grab a sensor reading. Use the read_retry method which will retry up | |
# to 15 times to get a sensor reading (waiting 2 seconds between each retry). | |
humidity, temperature = Adafruit_DHT.read_retry(sensor, DHTPin) | |
# Note that sometimes you won't get a reading and | |
# the results will be null (because Linux can't | |
# guarantee the timing of calls to read the sensor). | |
# If this happens try again! | |
if humidity is None and temperature is None: | |
humidity = "N/A" | |
temperature = "N/A" | |
else: | |
humidity = "{0:0.1f}".format(humidity) | |
temperature = "{0:0.1f}".format(temperature) | |
# Write the text. | |
draw.text((x, top), "wifi: " + str(IP1), font=font, fill=255) | |
draw.text((x, top+8), "eth0: " + str(IP2), font=font, fill=255) | |
draw.text((x, top+16), str(CPU), font=font, fill=255) | |
draw.text((x, top+24), str(MemUsage), font=font, fill=255) | |
draw.text((x, top+32), str(Disk), font=font, fill=255) | |
draw.text((x, top+40), ('Temperature: {0} C'.format(temperature)), font=font, fill=255) | |
draw.text((x, top+48), ('Humidity: {0}%'.format(humidity)), font=font, fill=255) | |
#draw.text((x, top+25), str(Disk), font=font, fill=255) | |
# Display image. | |
disp.image(image) | |
disp.display() | |
# Send temp and humidity to camera text overlay | |
urlencoded_text = "Temp%3A%20{0}%20C%5CnHum%3A%20%20{1}%20pct".format(temperature,humidity) | |
with open(os.devnull, "w") as void: | |
subprocess.call(["/usr/bin/curl", "--silent", "--show-error", "http://localhost:7999/1/config/set?text_left={}".format(urlencoded_text)], stdout=void) | |
time.sleep(5) | |
finally: | |
disp.command(Adafruit_SSD1306.SSD1306_DISPLAYOFF) | |
urlencoded_text = "Temp%3A%20NA%5CnHum%3A%20%20NA".format(temperature,humidity) | |
with open(os.devnull, "w") as void: | |
subprocess.call(["/usr/bin/curl", "--silent", "--show-error", "http://localhost:7999/1/config/set?text_left={}".format(urlencoded_text)], stdout=void) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment