1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

高校生がラズパイでTVを操ってみた with Python

Posted at

皆さん、はじめまして、ムラびとです。今回は、親がgoogle homeraspberry piADRSIR 学習リモコン基板を買ってきたので、rasp berr piをリモコンがわりにしてやろうと、悪戦苦闘している途中経過です。
(※ 今回がQiita初投稿なだけでなく、Pythonで何か作るのも初めてですので、間違いがあるかもしれませんが、その時はご指摘をお願いします。)
コードは、こちらに

目標

Google Homeに喋りかけて、raspberry piを通して、テレビなどを操作したい。

今回したこと

IFTTTからリクエストを受けて色々する前に、
Pythonでラズパイにサーバーを立て、GETやPOSTといったHTTPリクエストを送り、赤外線コードの取得、実行させるようにすること、簡易的なWebサイトの作成、

必要なもの

やりかた

  1. 以下のサイトより、環境構築をします。
    https://qiita.com/Halhira/items/1da2ae543217be26988a

  2. 以下を参考にしていきます。

https://signal-flag-z.blogspot.jp/2018/01/raspberry-pi-adrsir.html
今回は、IR-remocon02-commandline.pyを編集していきますが、ほとんど同じです。
そして、名前をcommand.pyという名前にします。
最終的に以下のようにしていきます。

command.py

from __future__ import print_function
import smbus
import time
from time import sleep
#import commands
import subprocess
import os
import sys
import codecs


# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

# This must match in the Arduino Sketch
#SLAVE_ADDRESS = 0x04
SLAVE_ADDRESS = 0x52
data_numH = 0x31
data_numL = 0x32
data_numHL = [0x00,0x31,0x32]
data_num = 10
memo_no = 0
block = []

#command
R1_memo_no_write = 0x15  #bus-write(ADR,cmd,1)
R2_data_num_read = 0x25 #bus-read(ADR,cmd,3)
R3_data_read           = 0x35 #bus-read(ADR,cmd,n)
W1_memo_no_write  = 0x19 #bus-write(ADR,cmd,1)
W2_data_num_write = 0x29 #bus-write(ADR,cmd,3)
W3_data_write           = 0x39 #bus-read(ADR,cmd,n)
W4_flash_write           = 0x49 #bus-read(ADR,cmd,n)
T1_trans_start             = 0x59 #bus-write(ADR,cmd,1)

############# read command
def read_command(memo_no):
# cmd R1_memo_no_write 0x15 bus-write(ADR,cmd,1)
    #print("memo_no write=",memo_no)
    bus.write_i2c_block_data(SLAVE_ADDRESS, R1_memo_no_write ,memo_no )   #= 0x15  #bus-write(ADR,cmd,1)

# cmd R2_data_num_read 0x25 bus-read(ADR,cmd,3)
    data_numHL = bus.read_i2c_block_data(SLAVE_ADDRESS, R2_data_num_read ,3 )#= 0x25 #bus-read(ADR,cmd,3)
    data_num = data_numHL[1]
    data_num *= 256
    data_num += data_numHL[2]
    #print("data_num =",data_num )

# cmd R3_data_read           0x35 bus-read(ADR,cmd,n)
    block = []
    block_dat  = bus.read_i2c_block_data(SLAVE_ADDRESS, R3_data_read , 1)       #= 0x35 #bus-read(ADR,cmd,n)
    for i in range(data_num ):
     block_dat  = bus.read_i2c_block_data(SLAVE_ADDRESS, R3_data_read , 4)       #= 0x35 #bus-read(ADR,cmd,n)
     block.append(block_dat[0])
     block.append(block_dat[1])
     block.append(block_dat[2])
     block.append(block_dat[3])
    #print(block)  #for denug
    return block

################# write command
def write_command(memo_no,block2):
    #f = open(filename,'r')
    #block2 =f.read()
    #f.close()
    #print(block2)
    #print(len(block2))
    str_tmp = ""
    int_tmp = []
    #for i in range(len(block2)/2): #TypeError: 'float' object cannot be interpreted as an integer
    for i in range(int(len(block2)//2)):
    
        str_tmp = block2[i*2] + block2[i*2+1]
        int_tmp.append( int(str_tmp, 16))
    #print(int_tmp)  
    #print(len(int_tmp))
# cmd W1_memo_no_write 0x19 bus-write(ADR,cmd,1)
    bus.write_i2c_block_data(SLAVE_ADDRESS, W1_memo_no_write ,memo_no )   #= 
# cmd W2_data_num_write 0x29 bus-write(ADR,cmd,3)
    data_num = int(len(int_tmp)//4)  #for test
    data_numHL = [0x31,0x32] #for test
    data_numHL[0] = int(data_num//256)
    data_numHL[1] = int(data_num%256)
    #print(data_numHL ,data_numHL[0] ,data_numHL[1])
    bus.write_i2c_block_data(SLAVE_ADDRESS, W2_data_num_write ,  data_numHL)   #= 
    #TypeError: Third argument must be a list of at least one, but not more than 32 integers
# cmd W3_data_write           0x39 bus-read(ADR,cmd,n)
    #print(data_num)
    data_numHL = [0x31,0x32,0x33,0x34] #for test 
    for i in range(data_num):
         data_numHL[0] = int_tmp[i*4+0]
         data_numHL[1] = int_tmp[i*4+1]
         data_numHL[2] = int_tmp[i*4+2]
         data_numHL[3] = int_tmp[i*4+3]
         bus.write_i2c_block_data(SLAVE_ADDRESS, W3_data_write , data_numHL)   #= 
# cmd W4_flash_write           0x49 bus-read(ADR,cmd,n)
    bus.write_i2c_block_data(SLAVE_ADDRESS, W4_flash_write,memo_no)   #=

# #############trans command
def trans_command(block2):
    #f = open(filename,'r')
    #block2 =f.read()
    #f.close()
    print(block2)
    print(len(block2))
    str_tmp = ""
    int_tmp = []
    for i in range(int(len(block2)//2)):
    #for i in range(len(block2)/2):
        str_tmp = block2[i*2] + block2[i*2+1]
        int_tmp.append( int(str_tmp, 16)) #python /home/pi/I2C0x52-IR/IR-remocon02-commandline.py t 
    print(int_tmp)  
    print(len(int_tmp))
# cmd W2_data_num_write 0x29 bus-write(ADR,cmd,3)
    data_num = int(len(int_tmp)//4)  #for test
    data_numHL = [0x31,0x32] #for test
    data_numHL[0] = int(data_num//256)
    data_numHL[1] = int(data_num%256)
    bus.write_i2c_block_data(SLAVE_ADDRESS, W2_data_num_write ,  data_numHL)   #= 
# cmd W3_data_write           0x39 bus-read(ADR,cmd,n)
    print(data_num)
    data_numHL = [0x31,0x32,0x33,0x34] #for test 
    for i in range(data_num):
         data_numHL[0] = int_tmp[i*4+0]
         data_numHL[1] = int_tmp[i*4+1]
         data_numHL[2] = int_tmp[i*4+2]
         data_numHL[3] = int_tmp[i*4+3]
         bus.write_i2c_block_data(SLAVE_ADDRESS, W3_data_write , data_numHL)   #= 
 # cmd T1_trans_start             0x59 bus-write(ADR,cmd,1)
    memo_no = [0x00 ] #for dummy
    bus.write_i2c_block_data(SLAVE_ADDRESS, T1_trans_start,memo_no )   #= 

3.Flask のインストール

サーバーを立てるためのPythonライブラリーですが、今回はFlaskをいうのを使っていきたいと思います。

sudo apt-get install python-pip
sudo pip install flask

4. サーバーサイドの作成
app.pyというファイルを作り、中身を書いていきます。

app.py
# -*- coding: utf-8 -*-

from flask import Flask, flash, redirect, render_template, request, session, abort,jsonify
import numpy as np
from command import trans_command,read_command
app = Flask(__name__)
IRCodes=[] 

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/addcode',methods=['GET','POST'])
def addCode():
    print('requested')
    if request.method == 'POST':
        IRcode={}
        IRcode['name']=request.form['name']	
        IRcode['code']=request.form['code']
        IRCodes.append(IRcode)
	print(IRCodes)
       	return jsonify(IRcode)
    else:
        return render_template('index.html')
@app.route('/code',methods=['GET','POST'])
def transIRCode():
    print(request.form['code'])
    trans_command(request.form['code'])
    return 'success'
@app.route('/code-from/<int:memo_no>')
def codeFrom(memo_no):
    print(memo_no,type(memo_no))
    str=''
    for d in read_command([memo_no]):
        str+='{:02X}'.format(d)
    print(str)
    return str
if __name__ == '__main__':
    app.debug = True 
    app.run(host='0.0.0.0') 

5.  sudo python app.py でサーバーを立て、curlなどで確認
6.  終わり

終わりに

急ぎ足でしたが、ご閲覧ありがとうございました。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?