from machine import Pin, PWM
import time
import json
# --- Configuration ---
adc = machine.ADC(Pin(26))
THRESHOLD = 500 # "Significant change" threshold for Edge Filtering
last_sent_value = 0
def simulate_cloud_processing(packet):
"""
Simulates what a Cloud Server does once it receives the JSON.
In a real scenario, this would involve a database and AI.
"""
data = json.loads(packet)
val = data["raw_value"]
# Cloud-side 'Heavy' Logic: Predictive analysis
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 Simulation Started...")
while True:
# 1. READ (Perception Layer)
current_value = adc.read_u16()
# 2. EDGE LOGIC (Processing Layer)
# Check if the value has changed significantly since the last 'Cloud' update
diff = abs(current_value - last_sent_value)
if diff > THRESHOLD:
# 3. PACKAGING (Network Layer)
packet = json.dumps({
"id": "VALVE_01",
"raw_value": current_value,
"event": "Significant Change"
})
print(f"EDGE: Change detected ({diff}). Sending to Cloud...")
# 4. SEND (Application Layer)
simulate_cloud_processing(packet)
# Update our 'last sent' baseline
last_sent_value = current_value
else:
# We process this locally, but do NOT send it to the cloud
print(f"EDGE: Minimal change ({diff}). Dropping packet to save bandwidth.")
time.sleep(2)