間欠動作

概要

RaspberryPiを間欠動作させる際の設定関連。

外観

回路

Raspberry Piの設定

起動時にスクリプト実行

startup.sh

sleep 30
python /home/pi/send_data.py
mode=$(python /home/pi/is_shutdown.py)
if [ $mode = 1 ]; then
    sudo shutdown -h now
fi

is_shutdown.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import gpiozero
def main():
    btn = gpiozero.Button(21)
    if btn.is_pressed:
        return '1'
    else:
        return '0'
if __name__ == '__main__':
    ret = main()
    sys.stdout.write(ret)

send_data.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib.parse,urllib.request

def send_data_post(value):
    url = 'https://hogehoge/'
    params = {'input':1, 'temp':str(value) }
    params = urllib.parse.urlencode(params)
    params_byte = params.encode('utf-8')

    res = urllib.request.urlopen(url,params_byte)
    r = res.read().decode('utf-8')
    #print(r)
    i = r.find('<body>') + 6
    result_code = int(r[i:i+2])
    return result_code

def send_data_get(value):
    url = 'https://hogehoge'
    url += '&d1=' + str(value)
    res = urllib.request.urlopen(url)
    r = res.read().decode('utf-8')
    result_code = int(r[0:1])
    return result_code

def main():
    r = send_data_get(1.0)
    if(r==1):
        print('success!')
    else:
        print('error!')

def loop():
    import time
    while True:
        main()
        time.sleep(30*60)

main()
#loop()

startup.shを所定のフォルダに移動させて、

$ sudo mv ./startup.sh /usr/local/bin/

/etc/rc.localを編集します。

$ sudo nano /etc/rc.local

exit 0の前に、startup.shを追記

sudo bash startup.sh
exit 0

ページトップへ