Skip to content

Method GetCurrentReadingAsync

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

GetCurrentReadingAsync()

Read current environmental conditions from all sensors.

csharp
[Task]
[PythonCode("\n        import json\n        import time\n        \n        def read_temperature():\n            # Read temperature sensor (TMP36 or similar)\n            raw = temp_adc.read_u16()\n            voltage = (raw / 65535.0) * 3.3\n            # TMP36: 10mV/°C with 500mV offset at 0°C\n            temp_c = (voltage - 0.5) * 100\n            # Apply calibration\n            temp_c = temp_c * sensor_calibration['temp_scale'] + sensor_calibration['temp_offset']\n            return round(temp_c, 2)\n        \n        def read_humidity():\n            # Read humidity from SHT30 via I2C\n            try:\n                # SHT30 command: measure with high repeatability\n                i2c.writeto(0x44, bytes([0x2C, 0x06]))\n                time.sleep_ms(15)  # Wait for measurement\n                data = i2c.readfrom(0x44, 6)\n                \n                # Convert raw data to humidity percentage\n                temp_raw = (data[0] << 8) | data[1]\n                humid_raw = (data[3] << 8) | data[4]\n                humidity = (humid_raw * 100.0) / 65535.0\n                \n                # Apply calibration\n                humidity += sensor_calibration['humidity_offset']\n                return round(max(0, min(100, humidity)), 1)\n            except:\n                # Fallback: estimate from temperature (very rough)\n                temp = read_temperature()\n                return max(30, min(70, 60 - temp * 0.5))\n        \n        def read_light_level():\n            # Read light sensor (photoresistor or photodiode)\n            raw = light_adc.read_u16()\n            # Convert to 0-100 light level scale\n            light = (raw / 65535.0) * 100 * sensor_calibration['light_scale']\n            return round(light, 1)\n        \n        try:\n            # Take readings\n            temperature = read_temperature()\n            humidity = read_humidity()\n            light = read_light_level()\n            timestamp = time.ticks_ms()\n            \n            # Update statistics\n            globals()['reading_count'] = globals().get('reading_count', 0) + 1\n            globals()['last_reading_time'] = timestamp\n            \n            # Return structured data\n            reading = {\n                'temperature': temperature,\n                'humidity': humidity,\n                'lightLevel': light,\n                'timestamp': timestamp,\n                'readingId': reading_count\n            }\n            \n            json.dumps(reading)\n            \n        except Exception as e:\n            globals()['error_count'] = globals().get('error_count', 0) + 1\n            raise Exception(f'Sensor reading failed: {str(e)}')\n    ")]
Task<EnvironmentReading> GetCurrentReadingAsync()

Returns

Task<EnvironmentReading>

Current environmental reading with temperature, humidity, and light levels.

Released under the Apache License 2.0.