質問編集履歴
1
実行ファイルのコード表示・具体的な現状
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,9 +1,132 @@
|
|
1
|
-
・パソコン(Windows)につないだ状態での自動実行は可能です。
|
2
|
-
|
1
|
+
まずraspberry pi ZEROで人感センサが反応したときGmailが送信されるスクリプトが以下のものです。
|
3
|
-
・実行ファイルはpyファイルです。
|
4
2
|
|
5
|
-
・試したこととしては「while ! /sbin/ifconfig eth0 | grep -q 'inet [0-9]'; do sleep 3 done」を使用コマンドの前に入れる(無線 LAN の場合は、eth0 を wlan0に変更)ことで実行できるという情報があったため、試したのですが、メールが送られてきません。
|
6
|
-
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12256934807
|
7
3
|
|
4
|
+
```code
|
5
|
+
import requests
|
6
|
+
import RPi.GPIO as GPIO
|
7
|
+
import time
|
8
|
+
|
9
|
+
|
10
|
+
import smtplib
|
11
|
+
from email.mime.text import MIMEText
|
12
|
+
from email.utils import formatdate
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
SLEEPTIME = 60
|
17
|
+
|
18
|
+
|
19
|
+
GPIO_PIN = 16
|
20
|
+
GPIO.setmode(GPIO.BCM)
|
21
|
+
GPIO.setup(GPIO_PIN,GPIO.IN)
|
22
|
+
|
23
|
+
def main():
|
24
|
+
sendAddress = '×××@gmail.com'
|
25
|
+
password = '×××'
|
26
|
+
|
27
|
+
subject = '見守りIoTデバイス 在室通知'
|
28
|
+
bodyText = '在室が検知されました'
|
29
|
+
fromAddress = '×××@gmail.com'
|
30
|
+
toAddress = '×××@gmail.com'
|
31
|
+
|
32
|
+
# SMTPサーバに接続
|
33
|
+
smtpobj = smtplib.SMTP('smtp.gmail.com', 587)
|
34
|
+
smtpobj.starttls()
|
35
|
+
smtpobj.login(sendAddress, password)
|
36
|
+
|
37
|
+
# メール作成
|
38
|
+
msg = MIMEText(bodyText)
|
39
|
+
msg['Subject'] = subject
|
40
|
+
msg['From'] = fromAddress
|
41
|
+
msg['To'] = toAddress
|
42
|
+
msg['Date'] = formatdate()
|
43
|
+
|
44
|
+
# 作成したメールを送信
|
45
|
+
smtpobj.send_message(msg)
|
46
|
+
smtpobj.close()
|
47
|
+
|
48
|
+
try:
|
49
|
+
while True:
|
50
|
+
if(GPIO.input(GPIO_PIN) == GPIO.HIGH):
|
51
|
+
main()
|
52
|
+
time.sleep(SLEEPTIME)
|
53
|
+
|
54
|
+
finally:
|
55
|
+
GPIO.cleanup()
|
56
|
+
```
|
8
|
-
|
57
|
+
この実行ファイルは「kk.py」という名前で保存しました。
|
58
|
+
|
59
|
+
その後、ラズパイが起動した時、自動でこのプログラムが実行可能にするため、ラズパイのターミナル内で「$ sudo nano /etc/rc.local」コマンドを入力すると以下の文面が表示されます。
|
60
|
+
|
61
|
+
```
|
62
|
+
#!/bin/sh -e
|
63
|
+
#
|
64
|
+
# rc.local
|
65
|
+
#
|
66
|
+
# This script is executed at the end of each multiuser runlevel.
|
67
|
+
# Make sure that the script will "exit 0" on success or any other
|
68
|
+
# value on error.
|
69
|
+
#
|
70
|
+
# In order to enable or disable this script just change the execution
|
71
|
+
# bits.
|
72
|
+
#
|
73
|
+
# By default this script does nothing.
|
74
|
+
|
75
|
+
# Print the IP address
|
76
|
+
_IP=$(hostname -I) || true
|
77
|
+
if [ "$_IP" ]; then
|
78
|
+
printf "My IP address is %s\n" "$_IP"
|
79
|
+
fi
|
80
|
+
|
81
|
+
python3 /home/pi/Desktop/kk.py
|
82
|
+
|
83
|
+
exit 0
|
84
|
+
|
85
|
+
```
|
86
|
+
この構文のうち、下から2行目の
|
87
|
+
「python3 /home/pi/Desktop/kk.py」は自分で付け足しました。
|
9
88
|
|
89
|
+
|
90
|
+
これで編集を完了し保存した後、ラズパイを再起動すると、センサが反応したとき自動でメールが送信されました。
|
91
|
+
|
92
|
+
|
93
|
+
それでここからが本題で、これらはパソコンにラズパイを接続して起動することで自動実行されていますが、これをパソコンでではなく、モバイルバッテリーにさして起動する際、自動実行できるようにしたいわけです。
|
94
|
+
|
95
|
+
|
96
|
+
そこで試したことが、「$ sudo nano /etc/rc.local」にて、実行ファイルの前に「while ! /sbin/ifconfig eth0 | grep -q 'inet [0-9]'; do sleep 3 done」を加えることで改善されるという情報をもとに内容を修正しました。
|
97
|
+
※無線 LAN の場合は、eth0 を wlan0に変更するみたいなので、自分の場合はwlan0を使用していますhttps://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q12256934807
|
98
|
+
|
99
|
+
```
|
100
|
+
#!/bin/sh -e
|
101
|
+
#
|
102
|
+
# rc.local
|
103
|
+
#
|
104
|
+
# This script is executed at the end of each multiuser runlevel.
|
105
|
+
# Make sure that the script will "exit 0" on success or any other
|
106
|
+
# value on error.
|
107
|
+
#
|
108
|
+
# In order to enable or disable this script just change the execution
|
109
|
+
# bits.
|
110
|
+
#
|
111
|
+
# By default this script does nothing.
|
112
|
+
|
113
|
+
# Print the IP address
|
114
|
+
_IP=$(hostname -I) || true
|
115
|
+
if [ "$_IP" ]; then
|
116
|
+
printf "My IP address is %s\n" "$_IP"
|
117
|
+
fi
|
118
|
+
|
119
|
+
while ! /sbin/ifconfig wlan0 | grep -q 'inet [0-9]'; do sleep 3
|
120
|
+
|
121
|
+
python3 /home/pi/Desktop/kk.py
|
122
|
+
|
123
|
+
exit 0
|
124
|
+
|
125
|
+
```
|
126
|
+
これで、モバイルバッテリーにさして起動して試したところメールが来ませんでした。
|
127
|
+
|
128
|
+
なぜできないのか原因がわからないというのが現状です。
|
129
|
+
どなたかご教授よろしくお願いします。
|
130
|
+
|
131
|
+
|
132
|
+
|