1
0

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 1 year has passed since last update.

【高先さんの電子工作】第20回:温度が高くなったら自動でファンを回してLINEに通知する

Last updated at Posted at 2023-09-21

概要

実演していますが、ざっと以下のようなことをやっています。

  • サーミスタを使って温度を測定して、一定以上の温度になったらファンが自動で起動します。
  • ファンが起動するとサーミスタの温度が下がって、一定以下の温度になるとファンが自動で停止します。
  • ファンが起動したり、停止したりするとLINEに通知が来るようにしています。

電子回路

ファンを動作させる部分については以下の記事を参考にして下さい。

サーミスタを扱う部分については以下の記事を参考にして下さい。

プログラム

設定ファイルに以下の情報を書き込みます。

config.json
{
    "ssid": "2.4GHz帯のWi-FiのSSID",
    "password": "Wi-Fiのパスワード",
    "access_token": "LINE Notifyのアクセストークン"
}

メインとなるプログラムです。

main.py
from machine import ADC, Pin
import utime
import math
import ujson
import urequests
import network

# 温度の閾値
limit = 26.0

thermistor = ADC(28)
led = Pin('LED', Pin.OUT)
moter_enable = Pin(0, Pin.OUT, value=0)
moter_pin1 = Pin(1, Pin.OUT, value=1)
moter_pin2 = Pin(2, Pin.OUT, value=0)

config = {} 
with open('config.json', 'r') as f:
    config = ujson.load(f)
    
ssid = config['ssid']
password = config['password']
access_token = config['access_token']

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)

while not wlan.isconnected():
    print('Waiting for connection...')
    utime.sleep(1)
    
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')


def line_notify(message):
    endpoint = 'https://notify-api.line.me/api/notify'
    headers = {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type' : 'application/x-www-form-urlencoded'
    }
    data = f'message={message}'.encode('utf-8')
    response = urequests.post(endpoint, headers=headers, data=data)
    response.close()


while True:
    # サーミスタの温度を取得する
    temperature_value = thermistor.read_u16()
    Vr = 3.3 * float(temperature_value) / 65535
    Rt = 10000 * Vr / (3.3 - Vr)
    temp = 1/(((math.log(Rt / 10000)) / 3950) + (1 / (273.15+25)))
    Cel = temp - 273.15
    print (' サーミスタ: %.2f C' % (Cel))
    
    if Cel > limit:
        led.on()
        if moter_enable.value() == 0:
            line_notify(f'温度が{limit}°Cを超えたのでファンを回します。')
            moter_enable.value(1)
    else:
        led.off()
        if moter_enable.value() == 1:
            line_notify(f'温度が{limit}°C以下になったのでファンを停止します。')
            moter_enable.value(0)
    utime.sleep(1)
    
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?