Docsity

IOT Practical 8 code TE AIDS SPPU 2024 Pattern, Study Guides, Projects, Research of Internet and Information Access

Toggle sidebar
bg1
from machine import Pin
import dht
import time
# ML part
import array
# SENSOR SETUP
sensor = dht.DHT22(Pin(28))
temps = []
time_index = []
# SIMPLE LINEAR REGRESSION (NO sklearn in Pico)
# y = mx + c
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 = (n*sum_xy - sum_x*sum_y) / (n*sum_x2 - sum_x*sum_x)
c = (sum_y - m*sum_x) / n
return m, c
# MAIN LOOP (IoT DATA COLLECTION)
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")
# ML TRAINING
m, c = train_linear_regression(time_index, temps)
print("\nModel trained:")
print("Slope (m):", m)
print("Intercept (c):", c)
# FUTURE PREDICTION
print("\nFuture Predictions:")
for future_t in range(15, 20):
prediction = m*future_t + c
print("Time", future_t, ":", prediction)
OUTPUT:

Partial preview of the text

Download IOT Practical 8 code TE AIDS SPPU 2024 Pattern and more Study Guides, Projects, Research Internet and Information Access in PDF only on Docsity!

from machine import Pin import dht import time

ML part

import array

SENSOR SETUP

sensor = dht.DHT22(Pin(28)) temps = [] time_index = []

SIMPLE LINEAR REGRESSION (NO sklearn in Pico)

y = mx + c

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

MAIN LOOP (IoT DATA COLLECTION)

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")

ML TRAINING

m, c = train_linear_regression(time_index, temps) print("\nModel trained:") print("Slope (m):", m) print("Intercept (c):", c)

FUTURE PREDICTION

print("\nFuture Predictions:") for future_t in range(15, 20): prediction = m*future_t + c print("Time", future_t, ":", prediction)

OUTPUT: