3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Raspberry Piで水温測定→IoT可視化サービスに送信

3
Posted at

忘備録

今後センサーの繋ぎ方とか追記予定

1-wireをラズパイ側で許可
↓
Raspberry piで水温計測
↓
IoT可視化サービスThingspeakに送信

下記を参考にした。thingspeakデータ送信部分を追加した。

参考
https://thepihut.com/blogs/raspberry-pi-tutorials/18095732-sensors-temperature-with-the-1-wire-interface-and-the-ds18b20

使用水温計はDS18B20。amazonで購入。そのままだとラズパイに繋げない。被覆を剥がして、半田ごて使ってラズパイにつなぐためのジャンパワイヤと接続。

import os
import glob
import requests
import time
import sys
 
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
 
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
 
url = 'https://api.thingspeak.com/update'
api = '●●●●'

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines
 
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        return temp_c

try:
    
    while True:
        param = {'api_key':api,
                 'field3':read_temp()}  #field●はthingspeakの個人の設定
        requests.get(url, params = param)
        time.sleep(600)   #10分おきに計測。
except:
    sys.exit()
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?