Method StartContinuousMonitoringAsync
Namespace: Belay.Core.Examples
Assembly: Belay.Core.dll
StartContinuousMonitoringAsync(int)
Start continuous environmental monitoring in a background thread. Data is collected at the specified interval and can trigger alerts.
csharp
[Thread(Name = "env_monitor", AutoRestart = true)]
[PythonCode("\n import _thread\n import time\n import json\n \n def continuous_monitor():\n print('Starting continuous environmental monitoring...')\n globals()['monitoring_active'] = True\n \n while globals().get('monitoring_active', False):\n try:\n # Take sensor readings\n temp_raw = temp_adc.read_u16()\n temp_voltage = (temp_raw / 65535.0) * 3.3\n temperature = (temp_voltage - 0.5) * 100\n temperature = temperature * sensor_calibration['temp_scale'] + sensor_calibration['temp_offset']\n \n light_raw = light_adc.read_u16()\n light = (light_raw / 65535.0) * 100 * sensor_calibration['light_scale']\n \n # Try humidity reading\n try:\n i2c.writeto(0x44, bytes([0x2C, 0x06]))\n time.sleep_ms(15)\n data = i2c.readfrom(0x44, 6)\n humid_raw = (data[3] << 8) | data[4]\n humidity = (humid_raw * 100.0) / 65535.0 + sensor_calibration['humidity_offset']\n except:\n humidity = 50.0 # Default if sensor fails\n \n # Update statistics\n globals()['reading_count'] = globals().get('reading_count', 0) + 1\n globals()['last_reading_time'] = time.ticks_ms()\n \n # Check alert thresholds\n alerts = []\n thresholds = alert_thresholds\n \n if temperature > thresholds['max_temp']:\n alerts.append(f'High temperature: {temperature:.1f}°C')\n elif temperature < thresholds['min_temp']:\n alerts.append(f'Low temperature: {temperature:.1f}°C')\n \n if humidity > thresholds['max_humidity']:\n alerts.append(f'High humidity: {humidity:.1f}%')\n \n if light < thresholds['min_light']:\n alerts.append(f'Low light: {light:.1f}')\n \n # Print current readings\n timestamp = time.ticks_ms()\n print(f'[{timestamp}] T:{temperature:.1f}°C H:{humidity:.1f}% L:{light:.1f}')\n \n # Print alerts if any\n for alert in alerts:\n print(f'ALERT: {alert}')\n \n # Add to data buffer (keep last 100 readings)\n if 'data_buffer' not in globals():\n globals()['data_buffer'] = []\n \n data_buffer.append({\n 'temp': round(temperature, 1),\n 'humidity': round(humidity, 1), \n 'light': round(light, 1),\n 'timestamp': timestamp,\n 'alerts': alerts\n })\n \n # Keep buffer size manageable\n if len(data_buffer) > 100:\n data_buffer.pop(0)\n \n # Wait for next reading\n time.sleep_ms({intervalMs})\n \n except Exception as e:\n print(f'Monitoring error: {e}')\n globals()['error_count'] = globals().get('error_count', 0) + 1\n time.sleep_ms(5000) # Back off on error\n \n print('Continuous monitoring stopped')\n \n # Start the monitoring thread\n _thread.start_new_thread(continuous_monitor, ())\n ")]
Task StartContinuousMonitoringAsync(int intervalMs = 5000)
Parameters
intervalMs
int
Data collection interval in milliseconds.