from machine import Pin
import onewire
import ds18x20
import time
import wifi
import network
import urequests
wifi.connect_ap()
# Function to update ThingSpeak
def update_thingspeak(field1_value):
url = 'http://api.thingspeak.com/update?api_key={}&field1={}'.format("PB3JYS2NU7P9BZSM", field1_value)
response = urequests.get(url)
print(response.text)
response.close()
# Cài đặt cảm biến nhiệt độ
ds_pin = Pin(4) # Thay đổi số chân tùy theo kết nối của bạn
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
# Tìm địa chỉ của cảm biến
roms = ds_sensor.scan()
print('Cảm biến tìm thấy:', roms)
# Giá trị nhiệt độ trước đó
last_temp = 0.0
def read_temperature():
global last_temp
ds_sensor.convert_temp()
time.sleep_ms(750)
for rom in roms:
temp = ds_sensor.read_temp(rom)
print('Nhiệt độ:', temp)
update_thingspeak(temp)
if abs(temp - last_temp) > 2 or temp > 70: # Ngưỡng tăng nhiệt độ
print('Cảnh báo: Có thể có cháy!')
last_temp = temp
while True:
read_temperature()
time.sleep(5) # Đọc cứ mỗi 5 giây
Loading
ds18b20
ds18b20