[PYTHON] Make an umbrella reminder with Raspberry Pi Zero W

I'm an amateur like electronic work, but to get started

Make an umbrella reminder using. There is a weather app on your mobile phone that says "When it's about to rain, what time is it in the morning?", But when I go out, I sometimes forget it. .. It's like putting it in the front door and displaying the probability of precipitation on the LCD triggered by the input of the motion sensor. I also want to use a power battery. In some cases, consider reducing power consumption. It was completed for the time being! There is nothing technically advanced.

-[x] How to use LCD -[x] How to use the motion sensor -[x] Acquisition of precipitation probability -[x] Combination of each function -[x] Power supply battery -[] Power consumption reduction

Figure of completion

It turned out to be something like this. If you are properly prepared at the entrance, you will be notified properly! I would like to verify how long this battery will last. 4AF816CA-8529-40AA-8761-2ECC9EB76447-2791-000001FB8590F7A4.jpg

What you prepared

I bought the GPIO Hammer Header because I'm not confident in soldering. The OSOYOO electronic component set is recommended for people with no experience in electronic work. It's cheap, the tutorial is easy, and I'm satisfied.

How to use LCD

See below. http://osoyoo.com/ja/2016/04/10/drive-16x2-lcd-with-raspberry-pi/ http://osoyoo.com/ja/2016/06/01/drive-i2c-lcd-screen-with-raspberry-pi/

How to use the motion sensor

See below. http://osoyoo.com/ja/2016/07/14/motionsensor-pi/

Obtaining the probability of precipitation

I created the following child article. Getting Precipitation Probability from XML in Python

Combination of each function

It was completed, but by the time I got to this point, I connected the VCC and GND of the LCD in reverse and broke one: cry:

--Improvements -Requests.get () is called once for each of ~~ 12-18h and 18-24h. ~~ --2017/08/13 Addendum: Fixed using the tuple comprehension (strictly, generator?) Taught by @shiracamus. --I don't know how to find out if it is backlit. --I tried bus.read_byte (), but for some reason I can't see only the backlight information.

umbrella_reminder.py


#!/usr/bin/env python2
# coding: utf-8

# If motion sensor detected a person, get and show today's rain fall chance
# of Yokohama (East of Kanagawa) on LCD for 30 seconds.
# If not, wait 1 second and check again.


# for general use
import time

# for motion sensor (referred to as "MS")
import RPi.GPIO as GPIO

# for LCD
import smbus

# for rain fall chance
import datetime
import requests
import xml.etree.ElementTree as ET


# constants for motion sensor

MS_GPIO_INPUT_PIN = 14


# constants for LCD

LCD_I2C_ADDR                = 0x3F
LCD_CHR_DISP_WIDTH          = 16
LCD_CHR_DRAM_WIDTH          = 0x28
LCD_NUM_LINES               = 2
LCD_PULSE_WAIT              = 0.0005
LCD_DELAY_WAIT              = 0.0005

I2C_CMD_MODE                = 0x00
I2C_CHR_MODE                = 0x01
I2C_ENABLE_BIT              = 0x04
I2C_BACKLIGHT_OFF           = 0x00
I2C_BACKLIGHT_ON            = 0x08


bus = smbus.SMBus(1)


def initialize_gpio_for_ms():

    GPIO.setwarnings(False)
    GPIO.cleanup()
    GPIO.setwarnings(True)

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(MS_GPIO_INPUT_PIN, GPIO.IN, pull_up_down = GPIO.PUD_UP)


def initialize_lcd():

    send_byte_to_lcd(0x33, I2C_CMD_MODE) # establish 4 bits mode
    send_byte_to_lcd(0x32, I2C_CMD_MODE) # establish 4 bits mode

    send_byte_to_lcd(0x01, I2C_CMD_MODE) # 000001 clear display, return cursor to the home position
    # send_byte_to_lcd(0x02, I2C_CMD_MODE) # 000010 return cursor to the home position
    send_byte_to_lcd(0x06, I2C_CMD_MODE) # 000110 cursor move direction: increment, display shift: disabled
    send_byte_to_lcd(0x0C, I2C_CMD_MODE) # 001100 display on, cursor off, blink off
    # send_byte_to_lcd(0x10, I2C_CMD_MODE) # 010000 move cursor without writing, not used for initialization
    send_byte_to_lcd(0x28, I2C_CMD_MODE) # 101000 4 bits mode, use 2 lines, use 5 dots font
    time.sleep(LCD_DELAY_WAIT)


def send_byte_to_lcd(lcd_data, i2c_mode):

    first_data  = ((lcd_data & 0xF0) << 0) | i2c_mode
    second_data = ((lcd_data & 0x0F) << 4) | i2c_mode

    write_and_toggle_lcd_enable(first_data)
    write_and_toggle_lcd_enable(second_data)


def write_and_toggle_lcd_enable(data):

    time.sleep(LCD_DELAY_WAIT)
    bus.write_byte(LCD_I2C_ADDR, (data | I2C_ENABLE_BIT))
    time.sleep(LCD_PULSE_WAIT)
    bus.write_byte(LCD_I2C_ADDR, (data & ~I2C_ENABLE_BIT))
    time.sleep(LCD_DELAY_WAIT)


def turn_off_lcd():

    send_byte_to_lcd(0x08, (I2C_CMD_MODE | I2C_BACKLIGHT_OFF))


def turn_on_lcd():

    send_byte_to_lcd(0x0C, (I2C_CMD_MODE | I2C_BACKLIGHT_ON))


def move_cursor(line, offset):

    if line < 0 or LCD_NUM_LINES <= line:
        print('invalid line')
        return False

    if offset < 0 or LCD_CHR_DRAM_WIDTH <= offset:
        print('invalid offset')
        return False

    if   line == 0:
        line_data = 0x00
    elif line == 1:
        line_data = 0x40

    data = 0x80 + line_data + offset
    send_byte_to_lcd(data, (I2C_CMD_MODE | I2C_BACKLIGHT_ON))

    return True


def show_text_to_line(message, line):

    if move_cursor(line, 0) == False:
        return

    message = message.ljust(LCD_CHR_DISP_WIDTH)

    for i in range(LCD_CHR_DISP_WIDTH):
        send_byte_to_lcd(ord(message[i]), (I2C_CHR_MODE | I2C_BACKLIGHT_ON))


def get_yokohama_rain_fall_chances():

    today = datetime.datetime.today().strftime("%Y/%m/%d")

    url = 'http://www.drk7.jp/weather/xml/14.xml'
    response = requests.get(url)

    root = ET.fromstring(response.content)

    return ((period.get('hour'), period.text)
            for area in root.iter('area')
            if area.get('id').encode('utf-8') == 'Eastern'
            for info in area.findall('info')
            if info.get('date') == today
            for period in info.find('rainfallchance').findall('period'))


def main():

    initialize_gpio_for_ms()
    initialize_lcd()

    target_periods = '12-18', '18-24'

    while True:

        motion_detected = True if GPIO.input(MS_GPIO_INPUT_PIN) != 0 else False

        if motion_detected:

            print('motion detected')

            turn_on_lcd()

            curr_line = 0
            for period, rain_fall_chance in get_yokohama_rain_fall_chances():
                if period in target_periods:
                    text = period + 'h ' + rain_fall_chance + '%'
                    show_text_to_line(text, curr_line)
                    print(text)
                    curr_line += 1
                    if curr_line >= LCD_NUM_LINES:
                        break

            time.sleep(30)

        else:

            print('motion not detected')

            turn_off_lcd()
            time.sleep(1)


if __name__ == '__main__':

    try:
        main()

    except KeyboardInterrupt:
        pass

    finally:
        send_byte_to_lcd(0x01, I2C_CMD_MODE)
        turn_on_lcd()
        show_text_to_line('Goodbye!', 0)
        GPIO.cleanup()

Power supply battery

Looking at various things, I was worried that the protection circuit works and the output stops in the case of a small current in recent mobile batteries, but I was worried that I could use my own battery normally. .. I wonder if the protection circuit is not working because it is cheap? Or is there a decent amount of current flowing? I also want to measure the power consumption. ANKER Astro Slim2

Reduced power consumption

I haven't done it yet.

Recommended Posts

Make an umbrella reminder with Raspberry Pi Zero W
Let's make a cycle computer with Raspberry Pi Zero (W, WH)
Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]
Discord bot with python raspberry pi zero with [Notes]
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
Control brushless motors with GPIOs on Raspberry Pi Zero
Mutter plants with Raspberry Pi
Play with the Raspberry Pi Zero WH camera module Part 1
Create an LCD (16x2) game with Raspberry Pi and Python
Make an autonomous driving robot car with Raspberry Pi3 B + and ultrasonic distance sensor HC-SR04
[Raspberry Pi] Stepping motor control with Raspberry Pi
Servo motor control with Raspberry Pi
OS setup with Raspberry Pi Imager
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Try L Chika with raspberry pi
Make a Kanji display compass with Raspberry Pi and Sense Hat
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Make Scrapy an exe with Pyinstaller
Using a webcam with Raspberry Pi
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)
Measure SIM signal strength with Raspberry Pi
Make an audio interface controller with pyusb (2)
Make an audio interface controller with pyusb (1)
Hello World with Raspberry Pi + Minecraft Pi Edition
Build a Tensorflow environment with Raspberry Pi [2020]
Make Raspberry Pi speak Japanese using OpenJtalk
Get BITCOIN LTP information with Raspberry PI
Try fishing for smelt with Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Improved motion sensor made with Raspberry Pi
Try Object detection with Raspberry Pi 4 + Coral
Working with sensors on Mathematica on Raspberry Pi
Use PIR motion sensor with raspberry Pi
Infer Custom Vision model with Raspberry Pi
Create a car meter with raspberry pi
Inkbird IBS-TH1 value logged with Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
I tried to build an environment of Ubuntu 20.04 LTS + ROS2 with Raspberry Pi 4
Quickly create an environment where you can play with Raspberry Pi 4 (target 1 hour)
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
[Python] Make a game with Pyxel-Use an editor-
Make a monitoring device with an infrared sensor
I tried L-Chika with Raspberry Pi 4 (Python edition)
Enjoy electronic work with GPIO on Raspberry Pi
MQTT RC car with Arduino and Raspberry Pi
Power on / off your PC with raspberry pi
CSV output of pulse data with Raspberry Pi (CSV output)
Observe the Geminids meteor shower with Raspberry Pi 4
Get CPU information of Raspberry Pi with Python
Make DHT11 available on Raspberry Pi + python (memo)
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Play with your Ubuntu desktop on your Raspberry Pi 4