Contents
前回の続き
少し間が空いてしまいましたが前回はこちら。
ようやくループできるものが出来たのでご紹介。
使ったversion
python3:Python 3.5.3
今回はpython3だけ使いました。
01 データを取得するコード
初めに試した失敗コード(CPU100%版)
データはループで取得できますが、CPUがpython3により100%になるのと、たまに取得するデータがズレるという問題のあるコードです。
何かに使うかもしれないので残しておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/usr/bin/env python #coding: utf-8 import time import datetime import serial ser = serial.Serial("/dev/ttyUSB0", 115200, timeout=70) with open("test.csv","a") as f: try: while (True): if (ser.inWaiting()>0): data = ser.read(ser.inWaiting()).decode('utf-8') print(data, end='') datetime_now = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S") f.write(datetime_now + data) f.flush() except KeyboardInterrupt: print("Ctrl + C") ser.close() |
このままでは問題なのでこちらの参考サイトを参考にCPU100%を解消しました。
最終的なコード:get_twelite_data.py
CPU100%を解消したコードで2日ほど放置しても取得しても問題なかったものです。
データのズレも今のところ起きていません。
途中にある「print(“{0} {1}”.format(current_time, output_data))」はコメントアウトしても問題ありません。動作確認のために使いました。
実行は普通にpython3 myserialX.pyでも取得できますが、数日放置しておくために以下のようにバックグランドで実行しました。どちらで実行しても「AE-BME280_data.csv」というデータを出力します。
$ nohup python3 get_twelite_data.py &
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#!/usr/bin/env python #coding: utf-8 import time import datetime import serial ser = serial.Serial("/dev/ttyUSB0", 115200) output_data = "" with open("AE-BME280_data.csv","a") as write_file: try: while (True): receive_data = ser.read(1).decode("utf-8") if receive_data == "\n": current_time = datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S") print("{0} {1}".format(current_time, output_data)) write_file.write("{0} {1}".format(current_time, output_data)) write_file.write("\n") write_file.flush() output_data = "" else: output_data = output_data + receive_data except KeyboardInterrupt: print("Ctrl + C") ser.close() |
取得したデータ例 AE-BME280_data.csv
1 2 3 4 5 6 7 |
2017/12/31 16:37:00 ::rc=80000000:lq=75:ct=106B:ed=810E4653:id=1:ba=2665:a1=0820:a2=0419:tm=2040:hu=4800:at=1012 2017/12/31 16:38:00 ::rc=80000000:lq=69:ct=106C:ed=810E4653:id=1:ba=2665:a1=0820:a2=0419:tm=2041:hu=4844:at=1012 2017/12/31 16:38:59 ::rc=80000000:lq=78:ct=106D:ed=810E4653:id=1:ba=2665:a1=0817:a2=0419:tm=2041:hu=4978:at=1012 2017/12/31 16:39:58 ::rc=80000000:lq=78:ct=106E:ed=810E4653:id=1:ba=2665:a1=0820:a2=0419:tm=2042:hu=4987:at=1012 2017/12/31 16:40:58 ::rc=80000000:lq=75:ct=106F:ed=810E4653:id=1:ba=2665:a1=0820:a2=0417:tm=2040:hu=5001:at=1012 2017/12/31 16:41:57 ::rc=80000000:lq=72:ct=1070:ed=810E4653:id=1:ba=2665:a1=0817:a2=0419:tm=2042:hu=5023:at=1012 2017/12/31 16:42:57 ::rc=80000000:lq=84:ct=1071:ed=810E4653:id=1:ba=2665:a1=0817:a2=0419:tm=2042:hu=5008:at=1012 |
02 データをグラフ用に加工するコード:make_graph_data.py
取得したデータ(AE-BME280_data.csv)をグラフとして見ようと思います。
まずは01で取得したデータをグラフ用に加工します。
datetime = line[0:19]だと「2017/12/31 16:39:58」
datetime = line[5:16]だと「12/31 16:39」となります。
次のグラフの表示を見やすくために[5:16]を使っています。
1 2 3 4 5 6 7 8 9 10 11 12 |
#!/usr/bin/env python #coding:utf-8 with open("AE-BME280_data.csv","r") as read_file,open("AE-BME280_graph.csv","w") as write_file: write_file.write("time, temperature, humidity, pressure,\n") for read_line in read_file: divided_line = read_line[19:-1].split(":") #current_time = read_line[0:19] current_time = read_line[5:16] temperature = str(int(divided_line[10].lstrip("tm="))/100) humidity = str(int(divided_line[11].lstrip("hu="))/100) atmospheric_pressure = divided_line[12].lstrip("at=") write_file.write(current_time + "," + temperature + "," + humidity + "," +atmospheric_pressure + "\n") |
取得したデータ例 AE-BME280_data.csv
1 2 3 4 5 6 7 |
time, temperature, humidity, pressure, 12/31 16:37,20.4,48.0,1012 12/31 16:38,20.41,48.44,1012 12/31 16:38,20.41,49.78,1012 12/31 16:39,20.42,49.87,1012 12/31 16:40,20.4,50.01,1012 12/31 16:41,20.42,50.23,1012 |
03 データをグラフ化する
実際にデータをグラフ化するのはこちらのサイトを参考にさせていただきました。
こちらにある「基本的なグラフの描き方」にあるpd.read_csvで読み込むものをAE-BME280_graph.csvに変えただけです。
注意としてSSHから実行するとエラーになります。PVCか直接HDMIでラズパイに接続し、GUIから実行する必要があります。私はPVCを使いました。
簡単に使いまでを書き残すとこんな感じでした。
【ラズパイ側】
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install tightvncserver
tightvncserver
ps -ef | grep tightで5901ポート使っていることを確認。
【Windows側】
UltraVNCのインストール
デフォルトで入れてviewerが入ればいい。
ultraVNCviewerからログイン:5901を忘れないように
パスワード入力してリモートデスクトップにログイン。
そして実際のグラフがこんな感じです。
今年の年末から年明けの取得データです。湿度が夜に急激に上昇しているのは加湿器を使っているからです。ちなみに気圧を追加することも出来るのですが、つまらないグラフになるので止めました。
最後に
無事ループ処理で取得できました。せっかくループ処理するならと思いCPU使用率が100%にならないようにしました。よかったよかった。
あと今回からなるべく簡単なコードでもGitHubに載せようと思います。
今回のコードもこちらに載せております。
コメント