Skip to content

Method CalibrateSensorsAsync

Namespace: Belay.Core.Examples
Assembly: Belay.Core.dll

CalibrateSensorsAsync(float, float)

Perform sensor calibration routine. This should be done in known environmental conditions.

csharp
[Task(TimeoutMs = 30000)]
[PythonCode("\n        import json\n        import time\n        \n        print('Starting sensor calibration...')\n        \n        # Take multiple readings for averaging\n        temp_readings = []\n        humidity_readings = []\n        \n        for i in range(10):\n            # Read raw temperature\n            raw = temp_adc.read_u16()\n            voltage = (raw / 65535.0) * 3.3\n            temp_c = (voltage - 0.5) * 100\n            temp_readings.append(temp_c)\n            \n            # Read raw humidity (simplified)\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\n                humidity_readings.append(humidity)\n            except:\n                humidity_readings.append({referenceHumidity})  # Use reference if sensor fails\n            \n            time.sleep_ms(500)  # Wait between readings\n            print(f'Calibration reading {i+1}/10...')\n        \n        # Calculate averages\n        avg_temp = sum(temp_readings) / len(temp_readings)\n        avg_humidity = sum(humidity_readings) / len(humidity_readings)\n        \n        # Calculate calibration factors\n        temp_offset = {referenceTemp} - avg_temp\n        humidity_offset = {referenceHumidity} - avg_humidity\n        \n        # Update calibration\n        sensor_calibration['temp_offset'] = temp_offset\n        sensor_calibration['humidity_offset'] = humidity_offset\n        \n        # Save calibration to file\n        with open('calibration.json', 'w') as f:\n            json.dump(sensor_calibration, f)\n        \n        print(f'Calibration complete:')\n        print(f'  Temperature offset: {temp_offset:.2f}°C')\n        print(f'  Humidity offset: {humidity_offset:.1f}%')\n    ")]
Task CalibrateSensorsAsync(float referenceTemp = 25, float referenceHumidity = 50)

Parameters

referenceTemp float

Known temperature for calibration.

referenceHumidity float

Known humidity for calibration.

Returns

Task

Released under the Apache License 2.0.