質問編集履歴

1

プログラムコードを修正しました(エラー部分だけが含まれるように簡素化)

2019/10/25 07:42

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -86,380 +86,96 @@
86
86
 
87
87
  ```html
88
88
 
89
- #Graph the temperature, humidity, pressure and upload it to the FTP server.
89
+ #!/usr/bin/python
90
90
 
91
- #coding: utf-8
91
+ # -*- coding: utf-8 -*-
92
-
93
- #2017/09/23
94
-
95
- #2017/10/04
96
-
97
- #2017/10/26
98
92
 
99
93
 
100
94
 
101
- import numpy as np
95
+ #Save the temperature, humidity, and atmospheric pressure obtained from bme280 in csv
102
96
 
103
- import matplotlib
97
+ #csv format:
104
98
 
105
- matplotlib.use("Agg")
99
+ #year,month,date,hour,min,val
100
+
101
+
102
+
103
+
106
104
 
107
105
  import matplotlib.pyplot as plt
108
106
 
109
107
  import csv
110
108
 
111
- from pylab import *
112
109
 
113
- from ftplib import FTP
114
-
115
- import re
116
-
117
- import requests
118
-
119
- import os
120
-
121
-
122
-
123
- from linebot import LineBotApi
124
-
125
- from linebot.models import TextSendMessage
126
110
 
127
111
 
128
112
 
129
113
  MAX_RANGE=50
130
114
 
131
- URL_PATH="/var/www/html/"
115
+ URL_PATH="/home/pi/dev/data/"
132
116
 
133
117
 
134
118
 
119
+ temp=[0 for i in range(MAX_RANGE)]
120
+
135
- def drawingTemp():
121
+ date=[0 for i in range(MAX_RANGE)]
122
+
123
+ span=[]
136
124
 
137
125
 
138
126
 
139
- temp=[0 for i in range(MAX_RANGE)]
127
+ for i in range(MAX_RANGE):
140
128
 
141
- date=[0 for i in range(MAX_RANGE)]
129
+ span.append(1000/MAX_RANGE*i)
142
-
143
- span=[]
144
130
 
145
131
 
146
132
 
147
- for i in range(MAX_RANGE):
133
+ fp=open(URL_PATH+"temp.csv","rb")
148
134
 
135
+ reader=csv.reader(fp)
136
+
137
+ for row in reader:
138
+
139
+ temp.pop(0)
140
+
149
- span.append(1000/MAX_RANGE*i)
141
+ temp.append(float(row[5]))
142
+
143
+ date.pop(0)
144
+
145
+ #month/date_hour:minute
146
+
147
+ date.append("%02d"%int(row[1])+"/"+"%02d"%int(row[2])+"_"+"%02d"%int(row[3])+":"+"%02d"%int(row[4]))
148
+
149
+ fp.close()
150
150
 
151
151
 
152
152
 
153
- fp=open(URL_PATH+"temp.csv","rb")
153
+ fig=plt.figure(figsize=(16,9))
154
154
 
155
- reader=csv.reader(fp)
155
+ ax=fig.add_subplot(1,1,1)
156
156
 
157
- for row in reader:
157
+ plt.title("Temperature")
158
158
 
159
- temp.pop(0)
159
+ plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
160
160
 
161
- temp.append(float(row[5]))
161
+ plt.xlabel("month/date_hour:minute")
162
162
 
163
- date.pop(0)
163
+ plt.ylabel("degree Celsius")
164
164
 
165
- #month/date_hour:minute
165
+ plt.plot(temp,color="m")
166
166
 
167
- date.append("%02d"%int(row[1])+"/"+"%02d"%int(row[2])+"_"+"%02d"%int(row[3])+":"+"%02d"%int(row[4]))
167
+ plt.xticks(range(MAX_RANGE),date)
168
168
 
169
- fp.close()
169
+ grid(True)
170
+
171
+ labels=ax.set_xticklabels(date,rotation=90,fontsize="small")
170
172
 
171
173
 
172
174
 
173
- if(temp[MAX_RANGE-1]<=20.0):
175
+ plt.show()
174
176
 
175
- sendMsgToLine("WARNING:temp is under 20C!!")
177
+ plt.savefig(URL_PATH+"temp.png",bbox_inches="tight")
176
178
 
177
179
 
178
180
 
179
- fig=plt.figure(figsize=(16,9))
180
-
181
- ax=fig.add_subplot(1,1,1)
182
-
183
- plt.title("Temperature")
184
-
185
- plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
186
-
187
- plt.xlabel("month/date_hour:minute")
188
-
189
- plt.ylabel("degree Celsius")
190
-
191
- plt.plot(temp,color="m")
192
-
193
- plt.xticks(range(MAX_RANGE),date)
194
-
195
- grid(True)
196
-
197
- labels=ax.set_xticklabels(date,rotation=90,fontsize="small")
198
-
199
-
200
-
201
- plt.savefig(URL_PATH+"temp.png",bbox_inches="tight")
202
-
203
-
204
-
205
- def drawingPressure():
206
-
207
-
208
-
209
- pressure=[0 for i in range(MAX_RANGE)]
210
-
211
- date=[0 for i in range(MAX_RANGE)]
212
-
213
- span=[]
214
-
215
-
216
-
217
- for i in range(MAX_RANGE):
218
-
219
- span.append(1000/MAX_RANGE*i)
220
-
221
-
222
-
223
- fp=open(URL_PATH+"pressure.csv","rb")
224
-
225
- reader=csv.reader(fp)
226
-
227
- for row in reader:
228
-
229
- pressure.pop(0)
230
-
231
- pressure.append(float(row[5]))
232
-
233
- date.pop(0)
234
-
235
- #month/date_hour:minute
236
-
237
- date.append("%02d"%int(row[1])+"/"+"%02d"%int(row[2])+"_"+"%02d"%int(row[3])+":"+"%02d"%int(row[4]))
238
-
239
- fp.close()
240
-
241
-
242
-
243
- #Warn if the air pressure acquired in the last 6 times keeps falling
244
-
245
- flg=True
246
-
247
- for i in range(MAX_RANGE-6,MAX_RANGE-1):
248
-
249
- if(pressure[i]<pressure[i+1]):
250
-
251
- flg=False
252
-
253
- break
254
-
255
- if(flg):
256
-
257
- sendMsgToLine("WARNING:Barometric pressure keeps falling for 30 consecutive minutes")
258
-
259
-
260
-
261
- fig=plt.figure(figsize=(16,9))
262
-
263
- ax=fig.add_subplot(1,1,1)
264
-
265
- plt.title("Pressure")
266
-
267
- plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
268
-
269
- plt.xlabel("month/date_hour:minute")
270
-
271
- plt.ylabel("Hectopascals")
272
-
273
- plt.plot(pressure,color="c")
274
-
275
- plt.xticks(range(MAX_RANGE),date)
276
-
277
- grid(True)
278
-
279
- labels=ax.set_xticklabels(date,rotation=90,fontsize="small")
280
-
281
-
282
-
283
- plt.savefig(URL_PATH+"pressure.png",bbox_inches="tight")
284
-
285
-
286
-
287
- def drawingHum():
288
-
289
-
290
-
291
- hum=[0 for i in range(MAX_RANGE)]
292
-
293
- date=[0 for i in range(MAX_RANGE)]
294
-
295
- span=[]
296
-
297
-
298
-
299
- for i in range(MAX_RANGE):
300
-
301
- span.append(1000/MAX_RANGE*i)
302
-
303
-
304
-
305
- fp=open(URL_PATH+"hum.csv","rb")
306
-
307
- reader=csv.reader(fp)
308
-
309
- for row in reader:
310
-
311
- hum.pop(0)
312
-
313
- hum.append(float(row[5]))
314
-
315
- date.pop(0)
316
-
317
- #month/date_hour:minute
318
-
319
- date.append("%02d"%int(row[1])+"/"+"%02d"%int(row[2])+"_"+"%02d"%int(row[3])+":"+"%02d"%int(row[4]))
320
-
321
- fp.close()
322
-
323
-
324
-
325
- if(hum[MAX_RANGE-1]>=70.0):
326
-
327
- sendMsgToLine("WARNING:Hum is over 70%!!")
328
-
329
-
330
-
331
- if(hum[MAX_RANGE-1]<40.0):
332
-
333
- sendMsgToLine("WARNING:Hum is under 40%!!")
334
-
335
-
336
-
337
- fig=plt.figure(figsize=(16,9))
338
-
339
- ax=fig.add_subplot(1,1,1)
340
-
341
- plt.title("Humidity")
342
-
343
- plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)
344
-
345
- plt.xlabel("month/date_hour:minute")
346
-
347
- plt.ylabel("percent")
348
-
349
- plt.plot(hum,color="g")
350
-
351
- plt.xticks(range(MAX_RANGE),date)
352
-
353
- grid(True)
354
-
355
- labels=ax.set_xticklabels(date,rotation=90,fontsize="small")
356
-
357
-
358
-
359
- plt.savefig(URL_PATH+"hum.png",bbox_inches="tight")
360
-
361
-
362
-
363
- def sendMsgToLine(msg):
364
-
365
- Channel_Access_Token="XXXXXXXXXX"
366
-
367
- User_Id="XXXXXXXXXX"
368
-
369
- Line_Bot_Api=LineBotApi(Channel_Access_Token)
370
-
371
- Line_Bot_Api.push_message(User_Id,TextSendMessage(text=msg))
372
-
373
-
374
-
375
- print("start drawing Temp...")
376
-
377
- drawingTemp()
378
-
379
- print("successful")
380
-
381
-
382
-
383
- print("start drawing Pressure...")
384
-
385
- drawingPressure()
386
-
387
- print("successful")
388
-
389
-
390
-
391
- print("start drawing Hum...")
392
-
393
- drawingHum()
394
-
395
- print("successful")
396
-
397
-
398
-
399
- print("connect to http server...")
400
-
401
- ftp=FTP("FTP_Server","usrid","passwd")
402
-
403
- print("successful")
404
-
405
-
406
-
407
- #only first time
408
-
409
- #send to html
410
-
411
- #fp=open("../../var/www/html/index.html","rb")
412
-
413
- #ftp.storbinary("STOR public_html/index.html",fp)
414
-
415
- #fp.close()
416
-
417
-
418
-
419
- #send temp to http
420
-
421
- print("send temp.png to http server...")
422
-
423
- fp=open(URL_PATH+"temp.png","rb")
424
-
425
- ftp.storbinary("STOR public_html/temp.png",fp)
426
-
427
- fp.close()
428
-
429
- print("successful")
430
-
431
-
432
-
433
- #send pressure to http
434
-
435
- print("send pressure.png to http server...")
436
-
437
- fp=open(URL_PATH+"pressure.png","rb")
438
-
439
- ftp.storbinary("STOR public_html/pressure.png",fp)
440
-
441
- fp.close()
442
-
443
- print("successful")
444
-
445
-
446
-
447
- #send hum to http
448
-
449
- print("send hum.png to http server...")
450
-
451
- fp=open(URL_PATH+"hum.png","rb")
452
-
453
- ftp.storbinary("STOR public_html/hum.png",fp)
454
-
455
- fp.close()
456
-
457
- print("successful")
458
-
459
-
460
-
461
- ftp.close()
462
-
463
- print("all complete!")
464
-
465
181
  ```