import machine
import time
import json
from machine import Pin
# --- Configuration ----
adc = machine.ADC(Pin(26)) # Potentiometer wiper on GP26
THRESHOLD = 500 # Significant change threshold
last_sent_value = 0
def simulate_cloud_processing(packet):
"""
Simulates what a Cloud Server does once it receives the JSON.
"""
data = json.loads(packet)
val = data["raw_value"]
# Cloud-side 'Heavy' Logic
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:
# 1. READ (Perception Layer)
current_value = adc.read_u16() # 0 → 65535
diff = abs(current_value - last_sent_value)
# 2. EDGE LOGIC (Processing Layer)
if diff > THRESHOLD:
# 3. PACKAGING (Network Layer)
packet = json.dumps({
"id": "VALVE_01",
"raw_value": current_value,
"event": "Significant Change"
})
print(f"EDGE: Pot moved! Change = {diff}. Sending to Cloud...")
# 4. SEND (Application Layer)
simulate_cloud_processing(packet)
last_sent_value = current_value
else:
print(f"EDGE: Minor pot movement ({diff}). Data filtered locally.")
time.sleep(2)