[PYTHON] Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]

Abstract Use Lambda and Kinesis to create intelligent T-shirts with LED matrices. The first part will explain how to make a T-shirt.

The T-shirt is linked to the service built on AWS via MQTT, calculates the discomfort index based on the data collected from the temperature / humidity sensor, and sends a message to the LED matrix to display when certain conditions are met. I will.

Slide022.png

If the discomfort index is 85 or higher, the T-shirt is hot and steamy, and I would like to display the message "Kuse".

Slide047.png

Then, I will explain how to make a T-shirt.

Raspberry Pi Type B+ A Raspberry Pi is used to control the LED matrix and temperature / humidity sensors. A 40-pin IO is available on the Raspberry Pi Type B +. The LED was connected via GPIO and the temperature / humidity sensor was connected via USB. The Raspberry Pi sells for around 5,000 yen. It is convenient to have one in the family (* ´Д`) Raspberry Pi

Slide024.png

LED Matrix parts

The LED matrix is connected to 64 (8x8) LEDs and is controlled from the Raspberry Pi via GPIO. You can easily control 64 LEDs with GPIO 16 pin by using a technique called Dynamic lighting.

This time, 64 LEDs were soldered to the board one by one. If you use a flexible board, you will feel less discomfort when you install it on a T-shirt. It is also a good point that you can easily cut it with scissors. Flexible board

Slide027.png

The wiring itself is the same as the device sold by Akizuki Denshi, so I think that it can be mounted without difficulty by soldering with reference to the circuit diagram below. However, it takes a lot of time and effort, so I only have patience. Akizuki Denshi LED Matrix alt

With reference to the above circuit diagram, the LED anode (13,3,4,10,6,11,15,16 on the circuit diagram) and cathode (left 9,14,8,12,1,7, circuit diagram) A total of 16 pins (2,5) will be connected to the GPIO of the Raspberry pi. GPIO has an output of 3.3 V, so convert the LED current and connect a resistor of about 300 Ω directly.

GPIO (19, 13, 16, 5, 22, 27, 17, 4) → R (300Ω) x8 → Anode (13, 3, 4, 10, 6, 11, 15, 16 on the circuit diagram) → LEDx64 → Cathode (Circuit diagram left 9, 14, 8, 12, 1, 7, 2, 5) → GPIO (18, 23, 24, 25, 12, 16, 20, 21)

By the way, I connected the Raspberry pi and the LED matrix with a flat cable. Flat cables are sold in meters at parts stores. The Raspberry pi's GPIO connector is 40 pin, so it's convenient to connect with a 40-core flat cable. Flat cable body

Crimping the connector and flat cable is expensive due to the special equipment, but it is surprisingly easy to crimp with a vise. I put my weight on it with disposable chopsticks and crimped it. Flat Cable Connector

LED Matrix font

The characters displayed on the LED matrix are 8x8 dots. One way is to create a matrix that represents the characters on your own and display them in dots, but this time we will use an 8-dot font called the Misaki font. Misaki font

Slide028.png

Download BDF file to convert it to Python code. Convert in BDF format as follows.

font_converter.py


import sys
print 'codes = {'
data = u''
for line in sys.stdin:
	items = line[0:-1].split(' ')
	if items[0] == 'STARTCHAR':
		code_char = items[1]
	if items[0] == 'ENCODING':
		code = items[1]
		data = u''
#·····to be continued

So, I noticed around here, but as it is, the code list of JIS array is output, so it is necessary to convert it to UTF8 array. However, I didn't have much time, so I decided to support only alphanumeric characters, hiragana, and katakana (not kanji), and converted it to a UTF8 array using the engineer's final secret Excel. Since the conversion was in the middle of the conversion, only the font conversion code of the output result is left.

font.py


codes = {
        32: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  ],
        33: [ 0x40, 0x40, 0x40, 0x40, 0x00, 0x40, 0x00, 0x00,  ],
        34: [ 0xa0, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  ],
        35: [ 0xa0, 0xe0, 0xa0, 0xa0, 0xe0, 0xa0, 0x00, 0x00,  ],
        36: [ 0x40, 0xe0, 0xc0, 0x60, 0xe0, 0x40, 0x00, 0x00,  ],
        37: [ 0x00, 0x80, 0x20, 0x40, 0x80, 0x20, 0x00, 0x00,  ],
#〜〜〜〜〜
        8736: [ 0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x7e, 0x00,  ],
        8735: [ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7e, 0x00,  ],
        8895: [ 0x00, 0x02, 0x06, 0x0a, 0x12, 0x22, 0x7e, 0x00,  ],
        8757: [ 0x00, 0x44, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,  ],
        8745: [ 0x00, 0x3c, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,  ],
        8746: [ 0x42, 0x42, 0x42, 0x42, 0x42, 0x3c, 0x00, 0x00,  ],
}

https://gist.github.com/shimy/f23b54c0f57d7b325e23

LED Matrix Display Program

You can display a message in the LED matrix in GPIO with the following code. The following video will be helpful for the principle of LED matrix. You can also understand GPIO control from Python. Raspberry Pi Project: The LED Matrix

control.py


# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import font as font
import os
import sys

###### GPIO Initialization #####
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)

cathodes = [4,17,27,22,5,6,13,19]
anodes = [18,23,24,25,12,16,20,21]

for cathode in cathodes:
    GPIO.setup(cathode, GPIO.OUT)
    GPIO.output(cathode, 0)

for anode in anodes:
    GPIO.setup(anode, GPIO.OUT)
    GPIO.output(anode, 0)

###### Display message #####
def display_message(str):
    sleeptime = 0.001
    speed = 8
    half = 4
    full = 8
    matrix_height = 8
    matrix_width = 0

    for idx in range(len(str)):
        char_code = ord(str[idx])
        if char_code < 1000:
            matrix_width += half
        else:
            matrix_width += full

    matrixes = matrix_height*['']

    for idx in range(len(str)):
        char_code = ord(str[idx])
        if font.codes.has_key(char_code):
            hex_pattern = font.codes[char_code]
        else:
            hex_pattern = font.codes[9632]

        for n in range(matrix_height):
            if char_code < 1000:
                bits_str = format(hex_pattern[n]>>4, '04b')
            else:
                bits_str = '{0:08b}'.format(hex_pattern[n])
            bits_str = bits_str.replace('0', '.').replace('1', 'o')
            bits_str = bits_str.replace('.', '1').replace('o', '0')
            matrixes[n] += bits_str

    try:
        pattern = matrix_height*['']
        for cnt in range(len(matrixes[0])-matrix_height):
            
            for row_no in range(matrix_height):
                pattern[row_no] = matrixes[row_no][cnt:cnt+matrix_height]

            os.system('clear')
            print '\n'.join(pattern).replace('0', 'o').replace('1', ' ')

            for pause in range(speed):
                for i in range(8):
                    for j in range(8):
                        GPIO.output(cathodes[j], int(pattern[i][j]))
                    
                    GPIO.output(anodes[i],1)
                    time.sleep(sleeptime)
                    GPIO.output(anodes[i],0)

    except KeyboardInterrupt:
        GPIO.cleanup()

##### main #####
str = u"Hello Sekai"
display_message(str)

Temperature / humidity sensor

For the temperature / humidity sensor, we used USBRH, which is sold by Straberry Linux Co, Ltd. https://strawberry-linux.com/catalog/items?code=52002

Slide025.png

You can get the temperature and humidity by running usbrh as follows:

$ sudo usbrh
$ 29.68 41.41

You can find out how to install and use it by referring to the following articles. http://hitoriblog.com/?p=9835 http://www.infiniteloop.co.jp/blog/2013/02/raspberrypitem/

T-shirt test

Screen Shot 2014-12-22 at 12.19.21 AM.png https://www.youtube.com/watch?v=YxEnnobXkWY

Summary

It's an article for the Lambda Advent Calendar, but I haven't talked about Lambda yet ... I'm exhausted, so let's meet in the second part.

Disclaimer

This is an individual opinion and has nothing to do with the company.

Recommended Posts

Let's make an IoT shirt with Lambda, Kinesis, Raspberry Pi [Part 1]
Make an umbrella reminder with Raspberry Pi Zero W
Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Make a wash-drying timer with a Raspberry Pi
Operate an oscilloscope with a Raspberry Pi
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
Let's make an app that can search similar images with Python and Flask Part1
Easy IoT to start with Raspberry Pi and MESH
Try to visualize the room with Raspberry Pi, part 1
Let's do Raspberry Pi?
DigitalSignage with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Make an autonomous driving robot car with Raspberry Pi3 B + and ultrasonic distance sensor HC-SR04
Play with the Raspberry Pi Zero WH camera module Part 1
Create an LCD (16x2) game with Raspberry Pi and Python
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Mutter plants with Raspberry Pi
Let's make Othello with wxPython
Let's make an Errbot plugin
Let's make Splatoon AI! part.1
Let's make dice with tkinter
Let's access your Raspberry Pi from outside your home with VPN (WireGuard)
Let's make a WEB application for phone book with flask Part 1
Let's make a WEB application for phone book with flask Part 2
Make a Kanji display compass with Raspberry Pi and Sense Hat
Make an effector for video conferencing with Spout + OpenCV + dlib (Part 1)
Let's make a WEB application for phone book with flask Part 4
[Raspberry Pi] Stepping motor control with Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
Serial communication with Raspberry Pi + PySerial
Let's make a breakout with wxPython
Let's make Othello AI with Chainer-Part 1-
Try L Chika with raspberry pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Let's make Othello AI with Chainer-Part 2-
Make Scrapy an exe with Pyinstaller
Using a webcam with Raspberry Pi
Make an MQTT Publisher Box Part 2
[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)
Let's make an image recognition model with your own data and play!
Create a socket with an Ethernet interface (eth0, eth1) (Linux, C, Raspberry Pi)