from machine import Pin, I2C, UART
import ssd1306
import dht
from MPU6050 import MPU6050
import time
import network
import urequests
# OLED Setup
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
#Button Setup
yes_button = Pin(4, Pin.IN, Pin.PULL_UP)
no_button = Pin(5, Pin.IN, Pin.PULL_UP)
# Temperature Sensor Setup
tempsensor = dht.DHT22(Pin(15))
# Accelerometer Setup
mpu = MPU6050()
# UART setup for communication with computer
uart = UART(1, baudrate=115200,tx=Pin(10),rx=Pin(9))
# To Connect to Internet
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
# To check if connected to Wifi
print("Connecting to WiFi", end="")
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
# Constants for simulation
DATA_COLLECTION_INTERVAL_SEC = 5 # Data collection interval in seconds should be 1200 seconds,but for faster simulation, it is smaller
NUM_READINGS_PER_TWOHOURS = 6 # Number of readings in 2 hours
# Arraylists and variables for temperature data
temperature_readings = []
sum_temperature = 0.0
temperature_count = 0
# Arraylists for accelerometer data
accelerometer_readings = []
sum_col1 = 0
sum_col2 = 0
sum_col3 = 0
while True:
# Simulating Sensor Data Collection (just to show how it would look like on the actual device)
#Collecting temperature
tempsensor.measure()
temperature = tempsensor.temperature()
print('The temperature is ', temperature)
#Collecting accelerometer
acc = mpu.read_accel_data()
print('The accelerometer data is ', acc)
#EDA and HRV or BVP from PPG should be collected as well
# Store temperature readings
temperature_readings.append(temperature)
sum_temperature += temperature
temperature_count += 1
# Store accelerometer readings
accelerometer_readings.append(acc)
# Check if two hours passed
if temperature_count >= NUM_READINGS_PER_TWOHOURS:
# Calculate average temperature
average_temperature = sum_temperature / NUM_READINGS_PER_TWOHOURS
print("Average Temperature in the last two hours:", average_temperature)
for row in accelerometer_readings:
sum_col1 += row['x']
sum_col2 += row['z']
sum_col3 += row['y']
average_acc_x = sum_col1 / 6
average_acc_z = sum_col2 / 6
average_acc_y = sum_col3 / 6
print("Average Accelerometer (X, Y, Z) in the past two hours:",
average_acc_x, average_acc_y, average_acc_z)
data_point = [average_temperature, average_acc_x, average_acc_y, average_acc_z] #average of eda and HRV or BVP also
uart.write("{}\n".format(data_point)) #send to computer for predicting
predicted_mood = uart.read() # to receive data from computer
prediction = "4 = meditation" #Predicted Mood Inputted for Simulating
print(f'Current mood is {prediction}')
oled.fill(0)
oled.show()
oled.text(f'Current mood is {prediction}\n Yes or No', 0, 0)
oled.show()
time.sleep(0.3)
timeout = 15 # Timeout in seconds
start_time = time.ticks_ms()
while time.ticks_diff(time.ticks_ms(), start_time) < timeout * 1000:
if not yes_button.value():
oled.fill(0) #clear display
oled.show()
oled.text('Confirmed',10,10)
oled.show()
#Send value to Cloud
urequests.get(f'https://hook.eu2.make.com/5c2ygdvc3oski16rgygby453zme8xvsl?Name=Mood&Value={prediction}')
time.sleep(1)
break
elif not no_button.value():
oled.fill(0)
oled.show()
oled.text('Choose Correct Mood',0,0)
oled.show()
time.sleep(1)
for i in range(5):
oled.fill(0)
oled.show()
oled.text(f'{str(i)}',0,0)
oled.show()
time.sleep(1)
# Wait for button press to select mood
while True:
for i in range(5):
if not yes_button.value():
prediction = str(i)
print("User corrected mood: ", i)
# Send data to Cloud
urequests.get(f'https://hook.eu2.make.com/5c2ygdvc3oski16rgygby453zme8xvsl?Name=Mood&Value={prediction}')
time.sleep(0.2) #Debouncing delay
break
break
else:
oled.fill(0)
oled.show()
oled.text('Confirm Yes or No',0,0)
oled.show()
time.sleep(1)
# Reset variables for the next 2 hours
temperature_readings = []
sum_temperature = 0.0
temperature_count = 0
accelerometer_readings = []
sum_col1 = 0
sum_col2 = 0
sum_col3 = 0
#Reset oled
oled.fill(0)
oled.show()
time.sleep(DATA_COLLECTION_INTERVAL_SEC)