
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

from machine import Pin import dht import time
import array
sensor = dht.DHT22(Pin(28)) temps = [] time_index = []
def train_linear_regression(x, y): n = len(x) sum_x = sum(x) sum_y = sum(y) sum_xy = sum([x[i]y[i] for i in range(n)]) sum_x2 = sum([x[i]x[i] for i in range(n)]) m = (nsum_xy - sum_xsum_y) / (nsum_x2 - sum_xsum_x) c = (sum_y - m*sum_x) / n return m, c
i = 0 while i < 15: try: sensor.measure() temp = sensor.temperature() print("IoT Temp:", temp) temps.append(temp) time_index.append(i) i += 1 time.sleep(2) except: print("Sensor error")
m, c = train_linear_regression(time_index, temps) print("\nModel trained:") print("Slope (m):", m) print("Intercept (c):", c)
print("\nFuture Predictions:") for future_t in range(15, 20): prediction = m*future_t + c print("Time", future_t, ":", prediction)