LoginSignup
2
3

More than 3 years have passed since last update.

Siri+Raspberry PIで部屋の温湿度をサクッと測ろう。

Last updated at Posted at 2019-06-21

1000円以下でデジタル温湿度計を工作します。
材料は、秋月
image.png
送料が500円と高額です。
DHT11 センサーモジュール買うと配線材料ついてきます。
image.png

配線図
image.png
抵抗は、なくても動作したが心配なので10kの抵抗でプルアップ。
image.png

単体テスト

準備

sudo pip install rpi.gpio
git clone https://github.com/szazo/DHT11_Python.git
tree DHT11_Python
DHT11_Python
├── LICENSE.md
├── README.md
├── __init__.py
├── dht11.py
└── dht11_example.py

0 directories, 5 files

上記のdht11.pyをimportする。

test.py
import datetime,time,RPi,dht11
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
instance =dht11. DHT11(pin=14)

while True:
    result = instance.read()
    if result.is_valid():
        print("時刻: " + str(datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S')),end="")
        print(" 温度: %d C " % result.temperature,end="")
        print("湿度: %d %%" % result.humidity)
    time.sleep(2)

実行結果

out.txt
時刻: 2019/06/21 15:11:02 温度: 27 C 湿度: 46 %
時刻: 2019/06/21 15:11:06 温度: 27 C 湿度: 45 %
時刻: 2019/06/21 15:11:08 温度: 27 C 湿度: 45 %
時刻: 2019/06/21 15:11:21 温度: 27 C 湿度: 44 %
時刻: 2019/06/21 15:11:23 温度: 27 C 湿度: 44 %
時刻: 2019/06/21 15:11:25 温度: 27 C 湿度: 44 %
時刻: 2019/06/21 15:11:27 温度: 27 C 湿度: 44 %
時刻: 2019/06/21 15:11:35 温度: 27 C 湿度: 44 %
時刻: 2019/06/21 15:11:37 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:11:39 温度: 28 C 湿度: 45 %
時刻: 2019/06/21 15:11:41 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:11:52 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:11:54 温度: 28 C 湿度: 45 %
時刻: 2019/06/21 15:11:56 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:11:58 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:12:02 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:12:08 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:12:11 温度: 28 C 湿度: 44 %
時刻: 2019/06/21 15:12:13 温度: 28 C 湿度: 44 %

iPhone用Home appテストプログラム

import logging,serial,requests,signal,time,datetime
import RPi.GPIO as GPIO
import dht11
from pyhap.accessory import Accessory, Bridge
from pyhap.accessory_driver import AccessoryDriver
from pyhap.const import (CATEGORY_SENSOR)
logging.basicConfig(level=logging.INFO, format="[%(module)s] %(message)s")
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
instance = dht11.DHT11(pin=14)
global hum
class TemperatureSensor(Accessory):
    category = CATEGORY_SENSOR

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        serv_temp = self.add_preload_service('TemperatureSensor')
        self.char_temp = serv_temp.configure_char('CurrentTemperature')

    @Accessory.run_at_interval(3)
    async def run(self):
        global hum
        result = instance.read()
        if result.is_valid():
            self.char_temp.set_value(result.temperature)
            hum=result.humidity

class HumiditySensor(Accessory):
    category = CATEGORY_SENSOR

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        serv_temp = self.add_preload_service('HumiditySensor')
        self.char_temp = serv_temp.configure_char('CurrentRelativeHumidity')

    @Accessory.run_at_interval(3)
    async def run(self):
        global hum
        self.char_temp.set_value(hum)


def get_bridge(driver):
    bridge = Bridge(driver, 'Bridge')
    bridge.add_accessory(TemperatureSensor(driver, '温度'))
    bridge.add_accessory(HumiditySensor(driver, '湿度'))
    return bridge
driver = AccessoryDriver(port=51826, persist_file='test.state')
driver.add_accessory(accessory=get_bridge(driver))
signal.signal(signal.SIGTERM, driver.signal_handler)
driver.start()

image.png

実行結果は、

image.png

source コードはここに

2
3
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
2
3