DS18B20の測定温度をGoogleスプレッドシートに送る-農家のIoT入門(9)

無事、複数の防水センサーで温度取得が出来た前回。

取得した温度は、RaspberryPi上だけで確認できる状態なので、以前みたいにGoogleスプレッドシートに送って、クラウド上にあげて、どこからでも温度が見られるようにしましょう。

やり方は、以下のそれぞれの回を参照してください。

ラズパイで温湿度を測定しGoogleスプレッドシートに送る-農家のIoT入門(6)
複数のDS18B20防水センサーで温度測定-農家のIoT入門(8)

DS18B20を複数繋ぐ回路

回路は、前回のままです。

ブレッドボードを使って、DS18B20を2個繋いでいます。

卓上でのテスト用に組んだだけですのでブレッドボードを使っていますが、実用試験段階では室内のRaspberryPiから庭までケーブルを伸ばして、地温を計るつもりです。(下の画像みたいな感じで)

Googleスプレッドシートに温度測定データを送る

では、測定した温度をGoogleスプレッドシートに送るコードを書いてみましょう。

第6回と第8回のコードを、無理やり合体させてみました。

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import gspread
  4. import os
  5. import glob
  6. from time import sleep
  7. import time
  8. import datetime
  9. from oauth2client.service_account import ServiceAccountCredentials
  10. os.system('modprobe w1-gpio')
  11. os.system('modprobe w1-therm')
  12. device_file1 = '/sys/bus/w1/devices/28-3c01b607d2db/w1_slave'
  13. device_file2 = '/sys/bus/w1/devices/28-3c01b6073639/w1_slave'
  14. def read_temp_raw1():
  15.     f = open(device_file1, 'r')
  16.     lines1 = f.readlines()
  17.     f.close()
  18.     return lines1
  19. def read_temp_raw2():
  20.     f = open(device_file2, 'r')
  21.     lines2 = f.readlines()
  22.     f.close()
  23.     return lines2
  24. def read_temp1():
  25.     lines1 = read_temp_raw1()
  26.     while lines1[0].strip()[-3:] != 'YES':
  27.         sleep(0.2)
  28.         lines1 = read_temp_raw1()
  29.     equals_pos = lines1[1].find('t=')
  30.     if equals_pos != -1:
  31.         temp_string1 = lines1[1][equals_pos + 2:]
  32.         temp_c1 = float(temp_string1) / 1000.0
  33.         return temp_c1
  34.     
  35. def read_temp2():
  36.     lines2 = read_temp_raw2()
  37.     while lines2[0].strip()[-3:] != 'YES':
  38.         sleep(0.2)
  39.         lines2 = read_temp_raw2()
  40.     equals_pos = lines2[1].find('t=')
  41.     if equals_pos != -1:
  42.         temp_string2 = lines2[1][equals_pos + 2:]
  43.         temp_c2 = float(temp_string2) / 1000.0
  44.         return temp_c2
  45. key_name = './cert/raspberrypi-1-xxx-xxx.json' # GoogleSheet認証キー
  46. sheet_name = 'RaspberryPi-1sheet' # シート名
  47. try:
  48.     if __name__ == '__main__':
  49.         scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
  50.         credentials = ServiceAccountCredentials.from_json_keyfile_name(key_name, scope)
  51.         gc = gspread.authorize(credentials) # JSONキーファイルで認証
  52.         wks = gc.open(sheet_name).sheet1 # sheetをオープン
  53.     while True:
  54.         print(str(datetime.datetime.now()))
  55.         print("t1=" + str(read_temp1()))
  56.         print("t2=" + str(read_temp2()))
  57.         sleep(3)
  58.         datas = [str(datetime.datetime.now()),read_temp1(),read_temp2()]
  59.         wks.append_row(datas)
  60. except KeyboardInterrupt:
  61.     pass

何度みても、無駄の多いコードです・・(^_^;)

ちゃんと勉強しないとですねぇ。

まあ、とりあえず実行してみましょう!

RaspberryPi側は、前回同様t1とt2にわけて温度を3秒毎に表示します。

で、Googleスプレッドシート側には、日付・温度1・温度2と3つのデータを送ります。

なお、わかりやすくなるよう、温度1はストーブの近くに置いて温度に差をつけてみました。

そして、グラフは行の範囲を予め大きくしてあります。

ですので、データが追加されて行が増えていっても、グラフも合わせて広がっていきます。

どういう範囲まで温度を表示させていくかも、今後の課題ですねえ。

 

ともかく、ようやくこれで実用試験の準備が整いました!

次回は実際に地温を計って、離れたところから温度の確認をしてみましょう。