Загрузка данных


import time
import network
import onewire
import ds18x20

from machine import Pin, ADC
from micropyserver import MicroPyServer

_init()

# DS18B20
ow = onewire.OneWire(Pin(4))
ds = ds18x20.DS18X20(ow)
roms = ds.scan()

# Фоторезистор
adc = ADC(0)

# WiFi
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(
    essid="SmartHome",
    password="12345678"
)

time.sleep(2)

print("IP:", ap.ifconfig()[0])

server = MicroPyServer()

def index(request, params):

    temp = "N/A"

    try:
        ds.convert_temp()
        time.sleep_ms(750)

        for rom in roms:
            temp = str(round(ds.read_temp(rom), 1))

    except:
        pass

    light = adc.read()

    if light > 150:
        status = "Bright"
    else:
        status = "Dark"

    html_file = open("page.html")
    html = html_file.read()
    html_file.close()

    html = html.replace("{TEMP}", temp)
    html = html.replace("{LIGHT}", str(light))
    html = html.replace("{STATUS}", status)

    server.send(
        html,
        content_type="Content-Type: text/html"
    )

server.add_route("/", index)

print("Server started")
server.start()