import machine
import time
import json
from machine import Pin
# ADC initialization (FIXED)
adc = machine.ADC(Pin(26))
THRESHOLD = 500
last_sent_value = 0
def simulate_cloud_processing(packet):
data = json.loads(packet)
val = data["raw_value"]
# Fixed f-string formatting
if val > 60000:
print(f">>> [CLOUD ALERT] Critical pressure at {val}! shutting down system.")
else:
print(f">>> [CLOUD LOG] Normal operation recorded for {data['id']}")
print("Experiment 4.2: Edge Filtering with Potentiometer Started...")
print("_" * 50)
while True:
current_value = adc.read_u16()
diff = abs(current_value - last_sent_value)
if diff > THRESHOLD:
# Fixed JSON structure (dict, commas, spelling)
packet = json.dumps({
"id": "value_01",
"raw_value": current_value,
"event": "Significant Change"
})
print(f"EDGE: Pot moved. Change = {diff}. Sending to Cloud...")
simulate_cloud_processing(packet)
last_sent_value = current_value
else:
print(f"EDGE: Minor pot movement ({diff}). Data filtered locally.")
time.sleep(2)