Raspberry Pi systemdによる自動起動
Q&A
Closed
解決したいこと
Raspberry Pi Python 3.7で電源onで自動起動させ
wifi接続してサーバーにデータを送りたいのですが、
うまくうごきません。
自動起動以外(サービス起動及びパイソンプログラム起動)の時は問題なく動作します。
解決方法を教えてください。
発生している問題・エラー
a.service - do websens
Loaded: loaded (/etc/systemd/system/a.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-10-12 10:36:51 JST; 30min ago
Process: 844 ExecStart=/usr/bin/python3.7 /opt/mybin/web2.py (code=exited, status=1/FAILURE)
Main PID: 844 (code=exited, status=1/FAILURE)
10月 12 10:36:51 raspberrypi python3.7[844]: return session.request(method=method, url=url, **kwargs)
10月 12 10:36:51 raspberrypi python3.7[844]: File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
10月 12 10:36:51 raspberrypi python3.7[844]: resp = self.send(prep, **send_kwargs)
10月 12 10:36:51 raspberrypi python3.7[844]: File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
10月 12 10:36:51 raspberrypi python3.7[844]: r = adapter.send(request, **kwargs)
10月 12 10:36:51 raspberrypi python3.7[844]: File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
10月 12 10:36:51 raspberrypi python3.7[844]: raise ConnectionError(e, request=request)
10月 12 10:36:51 raspberrypi python3.7[844]: requests.exceptions.ConnectionError: HTTPConnectionPool(host='192.168.180.133', port=80): Max retries exceeded with url: /wifi2/sensor.php (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xb5da3290>: Failed to establish a new connection: [Errno 101] Network is unreachable'))
10月 12 10:36:51 raspberrypi systemd[1]: a.service: Main process exited, code=exited, status=1/FAILURE
10月 12 10:36:51 raspberrypi systemd[1]: a.service: Failed with result 'exit-code'.
該当するソースコード1(web2.py)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
from datetime import datetime
def uploadSensorValues(red,green,blue,cData,ax,ay,az,tt,gx,gy,gz):
url = 'http://192.168.180.133/wifi2/sensor.php'
sensorsdata = 0,datetime.now().strftime("%Y/%m/%d"),datetime.now().strftime("%H:%M:%S"),red,green,blue,cData,ax,ay,az,tt,gx,gy,gz,0
print("送り")
print (json.dumps(sensorsdata))
r = requests.post(url, data=json.dumps(sensorsdata)stoptimeout=20)
def main():
uploadSensorValues(28, 39, 47, 56, 128,200,256,512,300,233,300)
if __name__ == '__main__':
該当するソースコード2(a.service)
[Unit]
Description=do websens
After=bluetooth.target bluetooth.service
[Service]
ExecStart=/usr/bin/python3.7 /opt/mybin/web2.py
Restart=alway
Type=simple
[Install]
WantedBy=bluetooth.target
サービス起動で動かしてうまくいったとき
pi@raspberrypi:~ $ sudo systemctl stop a
pi@raspberrypi:~ $ sudo systemctl start a
pi@raspberrypi:~ $ sudo systemctl status a
● a.service - do websens
Loaded: loaded (/etc/systemd/system/a.service; enabled; vendor preset: enable
Active: inactive (dead) since Mon 2020-10-12 11:26:03 JST; 33s ago
Process: 3048 ExecStart=/usr/bin/python3.7 /opt/mybin/web2.py (code=exited, st
Main PID: 3048 (code=exited, status=0/SUCCESS)
10月 12 11:26:02 raspberrypi systemd[1]: Started do websens.
10月 12 11:26:03 raspberrypi python3.7[3048]: 送り
10月 12 11:26:03 raspberrypi python3.7[3048]: [0, "2020/10/12", "11:26:02", 28,
10月 12 11:26:03 raspberrypi systemd[1]: a.service: Succeeded.
自分で試したこと
[Install]WantedBy=bluetooth.targetは最初はmulti-user.targetにしていたのですが、systemd-analyze plot > plot.svgでサービスの立ち上げをチェックしa.serviceを一番最後にするために変更しました。
0