import machine
import network
import time
import urequests
import ujson
import dht
# WiFi credentials
WIFI_SSID = "EMBEDDED_MEKA"
WIFI_PASSWORD = "kucingmeow2"
#ThingSpeak API settings
THINGSPEAK_API_KEY = "H4B6K1RG5MCD2JIW"
THINGSPEAK_URL = "https://api.thingspeak.com/update?api_key=H4B6K1RG5MCD2JIW"
potentiometer_1 = machine.ADC(26)
potentiometer_2 = machine.ADC(27)
dht_sensor = dht.DHT22(machine.Pin(4))
def read_room_temperature_humidity():
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
return temperature, humidity
def interpret_conditions(HR, SP, Temp, Hum):
HR_condition = "Not engaging" if 60 <= HR <= 79 else "Engaging" if 79 < HR < 100 else "Highly Engaging"
HR_percentage = 0 if HR_condition == "Not engaging" else 20 if HR_condition == "Engaging" else 40
SP_condition = "Booing" if SP<=3750 else "Cheering"
SP_percentage = 0 if SP_condition == "Booing" else 40
temp_condition = "Optimal" if 22 <= Temp <= 26 else "Too Cold" if Temp < 22 else "Too Hot"
temp_percentage = 0 if temp_condition in ["Too Cold", "Too Hot"] else 10
hum_condition = "Optimal" if 40 <= Hum <= 60 else "Too Dry" if Hum < 40 else "Too Wet"
hum_percentage = 0 if hum_condition in ["Too Dry", "Too Wet"] else 10
print(f"Heart Rate Condition: {HR_condition} (+{HR_percentage}%)")
print(f"Sound Pitch Condition: {SP_condition} (+{SP_percentage}%)")
print(f"Temperature Condition: {temp_condition} (+{temp_percentage}%)")
print(f"Humidity Condition: {hum_condition} (+{hum_percentage}%)")
total_percentage = HR_percentage + SP_percentage + temp_percentage + hum_percentage
return total_percentage
def send_data_to_thingspeak(HR, SP, Temp, Hum, ENG):
data ={
"api_key": THINGSPEAK_API_KEY,
"field2": HR,
"field3": SP,
"field4": Temp,
"field5": Hum,
"field6": ENG,
}
response = urequests.post(THINGSPEAK_URL,data=ujson.dumps(data),headers={"Content-Type":"application/json"})
response.close()
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(1)
print("Connected to Wi-Fi")
if __name__== "__main__":
try:
while True:
pot1 = potentiometer_1.read_u16()
HR = (pot1) * (60) / (65535) + 60
pot2 = potentiometer_2.read_u16()
SP = (pot2) * (7500) / (65535) + 500
Temp, Hum = read_room_temperature_humidity()
#Assuming lowest pitch = 500 and highest pitch = 8000
#Assuming high sound pitch is cheering,low sound pitch is booing
print(f"Heart Rate:{HR} Sound Pitch:{SP} Temperature:{Temp} Humidity:{Hum}")
ENG = interpret_conditions(HR, SP, Temp, Hum)
print(f"Total Participant Engagement: {ENG}%")
send_data_to_thingspeak(HR, SP, Temp, Hum, ENG)
print("Data sent to ThingSpeak")
time.sleep(15)
except KeyboardInterrupt:
pass