sexta-feira, março 20, 2020

Raspberry Oled Screen with DHT22 Temperature Sensor





The code bellow needs improvements






Need enable i2c and install some libraries... (Python 3.x)

pip3 install --upgrade luma.oled
pip3 install Adafruit_DHT

Optional luma.examples

aptitude install python-dev python-pip libfreetype6-dev libjpeg-dev build-essential
aptitude install i2c-tools libopenjp2-7 libtiff5 python3-dev git
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# See LICENSE.rst for details.
# PYTHON_ARGCOMPLETE_OK

"""
Showcase viewport and hotspot functionality.

"""



import datetime
import os
import time
from demo_opts import get_device
from luma.core.virtual import viewport, snapshot
from PIL import ImageFont
import RPi.GPIO as GPIO
import board
from datetime import date
from datetime import datetime
import Adafruit_DHT

tiny_font = ImageFont.truetype(os.path.abspath(os.path.join(
    os.path.dirname(__file__), "fonts", "FreePixel.ttf")), 10)


def right_text(draw, y, width, margin, text):
    x = width - margin - draw.textsize(text, font=tiny_font)[0]
    draw.text((x, y), text=text, font=tiny_font, fill="white")


def title_text(draw, y, width, text):
    x = (width - draw.textsize(text)[0]) / 2
    draw.text((x, y), text=text, fill="yellow")

def position(max):
    forwards = range(0, max)
    backwards = range(max, 0, -1)
    while True:
        for x in forwards:
            yield x
        for x in backwards:
            yield x

def pause_every(interval, generator):
    try:
        while True:
            x = next(generator)
            if x % interval == 0:
                for _ in range(20):
                    yield x
            else:
                yield x
    except StopIteration:
        pass

def render_clock(draw, width, height):
    margin = 3

    now = datetime.now()
    today_date = now.strftime("%d %b %y")
    current_time = now.strftime("%H:%m:%S")

    title_text(draw, margin, width, today_date)
    draw.text((margin + 10, 20), text=current_time, fill="white", font=tiny_font)


def render_temp(draw, width, height):
    humidity, temperature_c = Adafruit_DHT.read_retry(22, 4)
    margin = 3
    
    now = datetime.now()
    today_date = now.strftime("%d %b %y")
    current_time = now.strftime("%H:%m:%S")


    title_text(draw, margin, width, text="Temp DHT22")
    draw.text((margin, 20), text="temperature:", font=tiny_font, fill="white")
    draw.text((margin, 35), text="humidity:", font=tiny_font, fill="white")
    draw.text((margin, 45), text=today_date, font=tiny_font, fill="white")

    right_text(draw, 20, width, margin, text="{0:0.1f}%".format(temperature_c))
    right_text(draw, 35, width, margin, text="{0:0.1f}%".format(humidity))
    right_text(draw, 45, width, margin, text=current_time)
 
def render_temp2(draw, width, height):
    humidity, temperature_c = Adafruit_DHT.read_retry(11, 17)
    margin = 3

    now = datetime.now()
    today_date = now.strftime("%d %b %y")
    current_time = now.strftime("%H:%m:%S")


    title_text(draw, margin, width, text="Temp DHT11")
    draw.text((margin, 20), text="temperature:", font=tiny_font, fill="white")
    draw.text((margin, 35), text="humidity:", font=tiny_font, fill="white")
    draw.text((margin, 45), text=today_date, font=tiny_font, fill="white")

    right_text(draw, 20, width, margin, text="{0:0.1f}%".format(temperature_c))
    right_text(draw, 35, width, margin, text="{0:0.1f}%".format(humidity))
    right_text(draw, 45, width, margin, text=current_time)

def main():
#    widget_width = device.width // 2
    widget_width = device.width
    widget_height = device.height


    temp = snapshot(widget_width, widget_height, render_temp, interval=1.1)
    temp2 = snapshot(widget_width, widget_height, render_temp2, interval=1.0)
    clk = snapshot(widget_width, widget_height, render_clock, interval=1.0)
    clk2 = snapshot(widget_width, widget_height, render_clock, interval=1.0)

    widgets = [temp, temp2, clk, temp2]

    virtual = viewport(device, width=widget_width * len(widgets), height=widget_height)
    for i, widget in enumerate(widgets):
        virtual.add_hotspot(widget, (i * widget_width, 0))

#    for x in pause_every(widget_width, position(widget_width * (len(widgets) - 1))):
#        print("x=",x)
#        virtual.set_position((x, 0))
    x = 0
    while True:
        virtual.set_position((0, 0))
        time.sleep(1)
        #virtual.set_position((128, 0))
        #time.sleep(2)
        #virtual.set_position((256, 0))
        #time.sleep(6)

if __name__ == "__main__":
    try:
        device = get_device()
        main()
    except KeyboardInterrupt:
        pass



systetmd
vi /etc/systemd/system/temperature-widget.service
[Unit]
Description=Temperature sensor to OLED
After=syslog.target

[Service]
Type=simple
WorkingDirectory=/etc/script.d/
RemainAfterExit=yes
ExecStart=/etc/script.d/widget_temp.py
StandardOutput=syslog
StandardError=syslog

[Install]
WantedBy=multi-user.target






Nenhum comentário: