

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


import time import random import json from machine import Pin from dht import DHT
dht_sensor = DHT22(Pin(2)) alert_led = Pin(3, Pin.OUT)
class VirtualMQTTBroker: """Simulates a secure cloud message broker entirely within chip memory.""" def init(self): self.subscriptions = {} def subscribe(self, topic, callback_function): self.subscriptions[topic] = callback_function print(" [Network Layer] Channel Subscribed - > Ready for data link.") def publish(self, topic, message_string): print(" [Network Layer] Packet routing through Virtual Infrastructure Broker...")
if topic in self.subscriptions: self.subscriptions[topic](topic, message_string.encode('utf-8'))
virtual_broker = VirtualMQTTBroker()
BASELINE_TEMP = 24. BASELINE_HUMID = 45. SAFETY_RADIUS_THRESHOLD = 20. def evaluate_cloud_level_ml(temp, humid): """Calculates geometric variance to isolate malicious tampering or critical events.""" variance_distance = ((temp - BASELINE_TEMP)2 + (humid - BASELINE_HUMID)2)**0. print(" [Cloud ML Engine] Computed Variance Metric Score: {:.2f}".format(variance_distance)) if variance_distance > SAFETY_RADIUS_THRESHOLD: return "OUTLIER_ANOMALY" return "STABLE_DATA" def incoming_packet_subscriber_loop(topic, msg): """Acts as the Cloud analytics pipeline consumer interface loop.""" print("\n [Ingested Event: Cloud Data Intake Pipeline]") try: data = json.loads(msg.decode()) t_in = data["temp"] h_in = data["humid"] p_type = data["privacy_mode"]
print(" Decoded - > Temp: {}°C, Humid: {}% [Privacy: {}]".format(t_in, h_in, p_type))
ml_decision = evaluate_cloud_level_ml(t_in, h_in) print(" Analytical Verdict Result:", ml_decision)
if ml_decision == "OUTLIER_ANOMALY": print(" Threat Verified! Enforcing local isolation loop.") alert_led.value(1) # Flash warning hazard indicators red else: print(" Operational parameters stabilized within nominal limits.") alert_led.value(0) # Clear system logs parameters except Exception as error: print(" Payload parser verification logic failure event:", error)
print("Initializing Privacy-Aware Intelligent AIoT Application...") print("Starting Virtual Secure Network Gateway Stack...") time.sleep(0.5) print("Device IP Allocated: 10.10.0.1 (Localized Wokwi Sandbox Sandbox Mode)")
TELEMETRY_TOPIC = "aiot/integrated/telemetry" virtual_broker.subscribe(TELEMETRY_TOPIC, incoming_packet_subscriber_loop) print("System Pipeline Connected. Local Cloud-Loop Active via In-Memory Secure Bridge.")
while True: try:
dht_sensor.measure() raw_t = dht_sensor.temperature() raw_h = dht_sensor.humidity()
noise_variance = (random.getrandbits(8) / 255.0 - 0.5) * 0. anonymized_t = round(raw_t + noise_variance, 2)
sensor_payload = { "temp": anonymized_t, "humid": raw_h, "privacy_mode": "Differential_Noise_Injection", "timestamp_node_tick": time.ticks_ms() } print("\n [Edge Sensor Node Telemetry Update]") print(" Raw Sensor: T={:.1f}°C, H={:.1f}%".format(raw_t, raw_h)) print(" Anonymized Outbound Stream:", json.dumps(sensor_payload))