質問編集履歴
3
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -415,3 +415,13 @@
|
|
415
415
|
psコマンドはどのように使ったらよいでしょうか?
|
416
416
|
|
417
417
|
[http://www.raspberrypirulo.net/entry/2017/07/17/Pythonプログラムを起動時に実行する方法%28Systemd%29](http://www.raspberrypirulo.net/entry/2017/07/17/Pythonプログラムを起動時に実行する方法%28Systemd%29)
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
psコマンドは以下でしたが、見れませんでした。
|
422
|
+
|
423
|
+
[http://tech.nikkeibp.co.jp/it/article/COLUMN/20060227/230860/](http://tech.nikkeibp.co.jp/it/article/COLUMN/20060227/230860/)
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
一番は起動時に実行されたpythonプログラムのターミナルが表示されたら、どこでエラーが出ているのかわかるのですが。。。
|
2
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -405,3 +405,13 @@
|
|
405
405
|
pythonファイル自体はターミナルで実行して動いているのですが、自動起動しません。
|
406
406
|
|
407
407
|
なぜでしょう。
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
### 追加
|
412
|
+
|
413
|
+
psコマンドを使うとフォアグラウンドで見ることができるようですが、
|
414
|
+
|
415
|
+
psコマンドはどのように使ったらよいでしょうか?
|
416
|
+
|
417
|
+
[http://www.raspberrypirulo.net/entry/2017/07/17/Pythonプログラムを起動時に実行する方法%28Systemd%29](http://www.raspberrypirulo.net/entry/2017/07/17/Pythonプログラムを起動時に実行する方法%28Systemd%29)
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -26,6 +26,334 @@
|
|
26
26
|
|
27
27
|
|
28
28
|
|
29
|
+
pythonファイル
|
30
|
+
|
31
|
+
```python
|
32
|
+
|
33
|
+
#!/usr/bin/env python
|
34
|
+
|
35
|
+
# -*- coding: utf-8 -*-
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
# +++ モジュール +++
|
40
|
+
|
41
|
+
from threading import Thread
|
42
|
+
|
43
|
+
import RPi.GPIO as GPIO
|
44
|
+
|
45
|
+
import datetime
|
46
|
+
|
47
|
+
import schedule
|
48
|
+
|
49
|
+
import pandas
|
50
|
+
|
51
|
+
import time
|
52
|
+
|
53
|
+
import csv
|
54
|
+
|
55
|
+
import os
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
# +++ 変数 +++
|
60
|
+
|
61
|
+
# リマインド時間
|
62
|
+
|
63
|
+
spc_time = "7:20"
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
# 確認時間
|
68
|
+
|
69
|
+
start_time = "00:00:00"
|
70
|
+
|
71
|
+
end_time = "08:30:00"
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
# +++ クラス +++
|
76
|
+
|
77
|
+
class GarbageDayReminder(object):
|
78
|
+
|
79
|
+
# イニシャライズ
|
80
|
+
|
81
|
+
def __init__(self):
|
82
|
+
|
83
|
+
pass
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
# 日付処理
|
88
|
+
|
89
|
+
def date(self):
|
90
|
+
|
91
|
+
# 変数
|
92
|
+
|
93
|
+
FormatDay = "%Y/%m/%d "
|
94
|
+
|
95
|
+
FormatTime = "%H:%M:%S"
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
# 今日の日時を取得
|
100
|
+
|
101
|
+
now = datetime.datetime.now()
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
# 明日の日時を取得
|
106
|
+
|
107
|
+
tmr = now + datetime.timedelta(days=1)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# 1990-01-01対策の現在日付を追加
|
112
|
+
|
113
|
+
date = now.strftime(FormatDay)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# 今日、明日を振り分ける時間を作成
|
118
|
+
|
119
|
+
start = datetime.datetime.strptime(date + start_time, FormatDay + FormatTime)
|
120
|
+
|
121
|
+
end = datetime.datetime.strptime(date + end_time, FormatDay + FormatTime)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
# 今日、明日日付取得の分岐
|
126
|
+
|
127
|
+
if start <= now <= end:
|
128
|
+
|
129
|
+
# 今日日付の取得
|
130
|
+
|
131
|
+
today = "{}/{}/{}".format(now.year, now.month, now.day)
|
132
|
+
|
133
|
+
return today ,"today"
|
134
|
+
|
135
|
+
else:
|
136
|
+
|
137
|
+
# 明日の日付を取得
|
138
|
+
|
139
|
+
tommorow = "{}/{}/{}".format(tmr.year, tmr.month, tmr.day)
|
140
|
+
|
141
|
+
return tommorow ,"tommorow"
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
# csv処理
|
146
|
+
|
147
|
+
def csv(self,date):
|
148
|
+
|
149
|
+
# データが格納されている作業ディレクトリまでパス指定
|
150
|
+
|
151
|
+
os.chdir("/opt/GarbageDayReminder")
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
# csvファイルを読み込む
|
156
|
+
|
157
|
+
df = pandas.read_csv("garbage_day.csv")
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# 何のゴミの日か取得
|
162
|
+
|
163
|
+
garbage = df[df["日付"] == date]["ゴミ"].values[0]
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
return garbage
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
# リマインダー処理
|
172
|
+
|
173
|
+
def reminder(self, now, garbage):
|
174
|
+
|
175
|
+
# 辞書
|
176
|
+
|
177
|
+
gb_dict = {"燃":"燃やすごみ",\
|
178
|
+
|
179
|
+
"不":"燃やさないごみ",\
|
180
|
+
|
181
|
+
"缶":"空き缶、スプレー缶、空きびん、廃蛍光管",\
|
182
|
+
|
183
|
+
"資":"プラスチック製容器梱包、ペットボトル",\
|
184
|
+
|
185
|
+
"紙":"紙類ごみ",\
|
186
|
+
|
187
|
+
"草":"せんてい枝、庭の草", \
|
188
|
+
|
189
|
+
"粗":"粗大ごみ"}
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
# 今日、明日の切替
|
194
|
+
|
195
|
+
if now == "today":
|
196
|
+
|
197
|
+
now = "今日"
|
198
|
+
|
199
|
+
elif now == "tommorow":
|
200
|
+
|
201
|
+
now = "明日"
|
202
|
+
|
203
|
+
else:
|
204
|
+
|
205
|
+
return
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# リマインド
|
210
|
+
|
211
|
+
if garbage in gb_dict:
|
212
|
+
|
213
|
+
msg = "/opt/jtalk/jtalk.sh " + "ぽーーん!" + now + "は" + gb_dict[garbage] + "の日です"
|
214
|
+
|
215
|
+
os.system(msg)
|
216
|
+
|
217
|
+
else:
|
218
|
+
|
219
|
+
msg = "/opt/jtalk/jtalk.sh " + "ぽーーん!" + now + "のごみの日はありません"
|
220
|
+
|
221
|
+
os.system(msg)
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
# 中間処理
|
226
|
+
|
227
|
+
def intermediate(self):
|
228
|
+
|
229
|
+
# 日付処理
|
230
|
+
|
231
|
+
date, now = gdr.date()
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
# csv処理
|
236
|
+
|
237
|
+
garbage = gdr.csv(date)
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
# リマインダー処理
|
242
|
+
|
243
|
+
gdr.reminder(now, garbage)
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
# 指定時間処理
|
248
|
+
|
249
|
+
def specified_time(self):
|
250
|
+
|
251
|
+
# 指定時間に中間処理を実行
|
252
|
+
|
253
|
+
schedule.every().day.at(spc_time).do(gdr.intermediate)
|
254
|
+
|
255
|
+
while True:
|
256
|
+
|
257
|
+
schedule.run_pending()
|
258
|
+
|
259
|
+
time.sleep(1)
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
# チェック処理
|
264
|
+
|
265
|
+
def button(self):
|
266
|
+
|
267
|
+
GPIO.setmode(GPIO.BCM)
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
#GPIO12pinを入力モードとし、pull up設定とします
|
272
|
+
|
273
|
+
GPIO.setup(12,GPIO.IN,pull_up_down=GPIO.PUD_UP)
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
while True:
|
278
|
+
|
279
|
+
GPIO.wait_for_edge(12, GPIO.FALLING)
|
280
|
+
|
281
|
+
sw_counter = 0
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
while True:
|
286
|
+
|
287
|
+
sw_status = GPIO.input(12)
|
288
|
+
|
289
|
+
if sw_status == 0:
|
290
|
+
|
291
|
+
sw_counter = sw_counter + 1
|
292
|
+
|
293
|
+
if sw_counter >= 30:
|
294
|
+
|
295
|
+
print("長押し")
|
296
|
+
|
297
|
+
gdr.intermediate()
|
298
|
+
|
299
|
+
break
|
300
|
+
|
301
|
+
else:
|
302
|
+
|
303
|
+
print("短押し")
|
304
|
+
|
305
|
+
break
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
time.sleep(0.01)
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
print(sw_counter)
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
# メイン処理
|
318
|
+
|
319
|
+
def main(self):
|
320
|
+
|
321
|
+
# 指定時間処理(マルチスレッド)
|
322
|
+
|
323
|
+
t = Thread(target=gdr.specified_time)
|
324
|
+
|
325
|
+
t.start()
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
# デバッグ用
|
330
|
+
|
331
|
+
#gdr.intermediate()
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
# ボタン処理
|
336
|
+
|
337
|
+
gdr.button()
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
# +++ 初期動作 +++
|
342
|
+
|
343
|
+
if __name__ == "__main__":
|
344
|
+
|
345
|
+
# GarbageDayReminderクラスをインスタンス化して実行
|
346
|
+
|
347
|
+
gdr = GarbageDayReminder()
|
348
|
+
|
349
|
+
gdr.main()
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
```
|
354
|
+
|
355
|
+
|
356
|
+
|
29
357
|
python.serviceを以下フォルダに置く
|
30
358
|
|
31
359
|
```ここに言語を入力
|
@@ -71,3 +399,9 @@
|
|
71
399
|
sudo systemctl enable GarbageDayReminder.service
|
72
400
|
|
73
401
|
```
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
pythonファイル自体はターミナルで実行して動いているのですが、自動起動しません。
|
406
|
+
|
407
|
+
なぜでしょう。
|