Skip to content

Sensor base class and temperature sensor scripts #17

Merged
merged 4 commits into from
Mar 10, 2026
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions co2_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import random
import time
from sensor_base import SensorBase


class CO2Sensor(SensorBase):
"""
CO2 sensor that simulates gradual drift in CO2 concentration.
Values range from 400 ppm (fresh air) to 1500 ppm (poor air quality).
"""

def __init__(self, room_id):
super().__init__("co2", room_id)

# Start somewhere within the normal CO2 range
self.current_value = random.uniform(400, 800)

def read_value(self):
"""
Simulate gradual CO2 drift rather than random jumps.
"""

# Small drift step
drift = random.uniform(-50, 50)

self.current_value += drift

# Clamp value within realistic range
self.current_value = max(400, min(1500, self.current_value))

return round(self.current_value, 2)

def generate_co2_reading(self):
"""
Generate a formatted JSON CO2 reading.
"""
value = self.read_value()
return self.generate_reading(value, "ppm")


# ------------------------
# Manual Test
# ------------------------

if __name__ == "__main__":

sensor = CO2Sensor("Room101")

print("Testing CO2 drift:\n")

for _ in range(5):
print(sensor.generate_co2_reading())
time.sleep(1)
33 changes: 33 additions & 0 deletions sensor_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
from datetime import datetime, timezone


class SensorBase:
"""
This will be the base class for all sensor types.
Stores information about the sensor and provides a method to generate
a formatted JSON sensor reading.
"""

def __init__(self, sensor_type: str, room_id: str):
"""
This function initializes the sensor with its type and room ID.
"""
self.sensor_type = sensor_type
self.room_id = room_id

def generate_reading(self, value, unit):
"""
This function generates a JSON formatted sensor reading
with the value read from the sensor and unit of measurement.
"""

message = {
"sensor_type": self.sensor_type,
"room": self.room_id,
"value": value,
"unit": unit,
"timestamp": datetime.now().astimezone().isoformat() #to show local timezone
}

return json.dumps(message)
52 changes: 52 additions & 0 deletions temperature_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import random
import time
from sensor_base import SensorBase


class TemperatureSensor(SensorBase):
"""
Temperature sensor that simulates gradual temperature drift.
"""

def __init__(self, room_id):
super().__init__("temperature", room_id)

# Start somewhere within the realistic range
self.current_value = random.uniform(18, 30)

def read_value(self):
"""
Simulate gradual drift in temperature rather than random jumps.
Each reading changes slightly from the previous one.
"""

# Small drift step
drift = random.uniform(-0.5, 0.5)

self.current_value += drift

# Clamp value within realistic range
self.current_value = max(18, min(30, self.current_value))

return round(self.current_value, 2)

def generate_temperature_reading(self):
"""
Generate a formatted JSON temperature reading.
"""
value = self.read_value()
return self.generate_reading(value, "C")


# ------------------------
# Manual Test
# ------------------------

if __name__ == "__main__":
sensor = TemperatureSensor("Room101")

print("Testing temperature drift:\n")

for _ in range(5):
print(sensor.generate_temperature_reading())
time.sleep(1) #to implement a natural delay between readings