Created
September 11, 2016 13:41
-
-
Save RyosukeKamei/c66dbed6742f6d7b16fffe69b9837814 to your computer and use it in GitHub Desktop.
Raspberry Pi 3(ラズパイ)を使って温度を自動計測し、自動的にサーバ上(Docker python3+bottle+MySQL)にアップして閲覧する! ref: http://qiita.com/RyosukeKamei/items/075ba9687396319c0be5
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
USE measurement; | |
CREATE TABLE `temperatures` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`server_id` int(11) NOT NULL, | |
`temperature` int(11) NOT NULL, | |
`careted_at` datetime NOT NULL, | |
`careted_user` int(11) NOT NULL, | |
`updated_at` datetime NOT NULL, | |
`updated_user` int(11) NOT NULL, | |
KEY `id` (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
INSERT INTO `temperatures` | |
(`id`, `server_id`, `temperature`, `careted_at`, `careted_user`, `updated_at`, `updated_user`) | |
VALUES | |
(1, 1, 29, NOW(), 1, NOW(), 1); |
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
# 準備 | |
# $ sudo apt-get install libi2c-dev | |
# $ sudo sh -c 'echo "options i2c_bcm2708 combined=1" >> /etc/modprobe.d/i2c.conf' | |
# GPIOを制御するライブラリ | |
import wiringpi | |
# タイマーのライブラリ | |
import time | |
# I2Cデバイスからの読み取りに必要なライブラリを呼び出す | |
import os | |
import struct | |
# URLアクセス | |
import urllib.request | |
# I2Cのインスタンスを作成 | |
wiringpi.wiringPiSetup() | |
i2c = wiringpi.I2C() | |
# I2Cの設定 | |
# 通信する機器のI2Cアドレスを指定 | |
temperture_dev = i2c.setup(0x48) | |
# 温度を16ビットのデータ取得 | |
# その他めレジスタ0x03に設定 | |
i2c.writeReg8(temperture_dev, 0x03, 0x80) | |
while True: | |
# 温度センサーの2バイト分を読み取る | |
temperture_data = struct.unpack('2B', os.read(temperture_dev, 2)) | |
# 値が2バイトずつ分かれるので1つにまとめる | |
temperture = ( ( temperture_data[0] << 8 ) + temperture_data[1] ) | |
# 負の値の場合は数値を変換 | |
if ( temperture_data[0] >= 0x80 ): | |
temperture = temperture - 65536 | |
# 取得した値を128で割って温度を算出 | |
temperture = temperture / 128 | |
# 温度表示 | |
print ( "温度 " , temperture , "C" ) | |
response = urllib.request.urlopen('http://172.17.0.4:8080/input_temperature?server_id=1&temperature=' + str(temperture) + '&user_id=1') | |
data = response.read() | |
print ( "サーバレスポンス : ", data ) | |
# 1分ごと | |
time.sleep(60) |
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
$ docker-compose up -d |
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
$ git clone git@github.com:RyosukeKamei/rpi-python-bottle.git |
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
[mysqld] | |
innodb_strict_mode | |
innodb_file_format = Barracuda | |
innodb_file_per_table | |
innodb_large_prefix = 1 | |
character-set-server=utf8mb4 | |
skip-character-set-client-handshake | |
max_allowed_packet = 32m | |
skip-networking = 0 | |
[client] | |
default-character-set=utf8mb4 |
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
setl expandtab | |
setl tabstop=4 | |
setl shiftwidth=4 | |
setl softtabstop=0 | |
autocmd BufWritePre * :%s/\s\+$//ge | |
setlocal textwidth=80 |
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
# bottleのライブラリ | |
from bottle import route, run, request | |
# MySQLドライバはmysql.connector | |
import mysql.connector | |
# 補足 | |
# 本当はテンプレートを入れるとHTMLが綺麗になります。 | |
# その辺は後日… | |
# hostのIPアドレスは、$ docker inspect {データベースのコンテナ名}で調べる | |
# MySQLのユーザやパスワード、データベースはdocker-compose.ymlで設定したもの | |
# user : MYSQL_USER | |
# password : MYSQL_PASSWORD | |
# database : MYSQL_DATABASE | |
connector = mysql.connector.connect ( | |
user = 'bottle', | |
password = 'bottle', | |
host = '172.17.0.3', | |
database = 'measurement' | |
) | |
@route('/list') | |
def list(): | |
# 温度を表示 | |
cursor = connector.cursor() | |
cursor.execute("select `id`, `temperature`, `careted_at` from temperatures") | |
disp = "<table>" | |
# ヘッダー | |
disp += "<tr><th>ID</th><th>温度</th><th>登録日</th></tr>" | |
# 一覧部分 | |
for row in cursor.fetchall(): | |
disp += "<tr><td>" + str(row[0]) + "</td><td>" + str(row[1]) + "</td><td>" + str(row[2]) + "</td></tr>" | |
disp += "</table>" | |
cursor.close | |
return "DBから取得 "+disp | |
@route('/input_temperature') | |
def input_temperature(): | |
# 温度を入力 | |
cursor = connector.cursor() | |
cursor.execute("INSERT INTO `temperatures` (`server_id`, `temperature`, `careted_at`, `careted_user`, `updated_at`, `updated_user`) VALUES (" + request.query.server_id + ", " + request.query.temperature + ", NOW(), " + request.query.user_id + ", NOW(), " + request.query.user_id + ")") | |
# コミット | |
connector.commit(); | |
cursor.close | |
return "OK" | |
# コネクターをクローズ | |
connector.close | |
# サーバ起動 | |
run(host='0.0.0.0', port=8080, debug=True, reloader=True) |
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
$ docker build -t hypriot/rpi-python . |
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
$ docker run --name rpi-python-bottle-app -it hypriot/rpi-python bash |
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
$ docker images |
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
$ cd rpi-python-bottle |
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
$ docker ps -a |
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
# mysql -u bottle -pbottle measurement < /docker-entrypoint-initdb.d/create_table.sql |
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/local/bin/python3 /home/bottle/server.py |
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
$ docker exec -it rpi-python-bottle-db bash |
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
# (Contrl + p, Control + q) |
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
$ cd {git cloneしたフォルダ}/rpi-python-bottle | |
$ sudo python3 digital_temperature_sensor_for_api.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment