import machine
import math
import utime
import random
import time
class Temperature_Sensor:
"""
Class representing a temperature sensor.
"""
# Set Beta value for temperature sensor
BETA = 3950
# Set Resistor value for temperature sensor
R1 = 10000
# Set reference voltage for temperature sensor
V_REF = 3.3
# Initialise ADC with temperature sensor connected
adc = machine.ADC(machine.Pin(28))
def get_temperature(self):
"""
Get temperature from the sensor in Celsius.
Returns:
float: Temperature in Celsius.
"""
analog_value = self.adc.read_u16()
voltage = (analog_value / 65535) * self.V_REF
resistance = self.R1 / ((self.V_REF / voltage) - 1)
kelvin = 1 / (1 / 298.15 + 1 / self.BETA * math.log(resistance / 10000))
celsius = kelvin - 273.15
return round(celsius * self.get_random_number(), 2)
def get_random_number(self):
"""
Get a random number between 0.9 to 1.1 with a chance of 5% that the number is between 0.1 and 1.6.
Returns:
float: Random number.
"""
if(random.randint(0, 100) < 50):
return random.uniform(0.1, 1.6)
else:
return random.uniform(0.9, 1.1)
class Temperature_Data:
"""
Class for storing temperature data.
"""
temperature_storage = []
events = []
smallest_temp = 1000
highest_temp = -1000
def log_temperature(self, temperature):
"""
Log temperature and update internal storage.
Args:
temperature (float): Temperature to log.
"""
if len(self.temperature_storage) > 99:
self.temperature_storage.pop(0)
self.temperature_storage.append(temperature)
self.log_event(temperature)
self.log_smallest_temp(temperature)
self.log_highest_temp(temperature)
def log_event(self, temperature):
"""
Log events based on temperature thresholds.
Args:
temperature (float): Temperature to check for event.
"""
if temperature < 10 or temperature > 30:
localtime = time.localtime()
if len(self.events) > 99:
self.events.pop(0)
self.events.append({"Temp": temperature, "Localtime": localtime})
def log_smallest_temp(self, temperature):
"""
Log the smallest temperature.
Args:
temperature (float): Temperature to compare.
"""
if temperature < self.smallest_temp:
self.smallest_temp = temperature
def log_highest_temp(self, temperature):
"""
Log the highest temperature.
Args:
temperature (float): Temperature to compare.
"""
if temperature > self.highest_temp:
self.highest_temp = temperature
def get_last_temp(self):
"""
Get the last logged temperature.
Returns:
float: Last logged temperature.
"""
if len(self.temperature_storage) > 0:
return self.temperature_storage[-1]
else:
return 0
def get_last_event(self):
"""
Get the last logged event.
Returns:
dict: Last logged event.
"""
if len(self.events) > 0:
return self.events[-1]
else:
return 0
def get_highest_temperature(self):
"""
Get the highest recorded temperature.
Returns:
float: Highest recorded temperature.
"""
return self.highest_temp
def get_smallest_temp(self):
"""
Get the smallest recorded temperature.
Returns:
float: Smallest recorded temperature.
"""
return self.smallest_temp
def main():
temperature_sensor = Temperature_Sensor()
temperature_data = Temperature_Data()
while True:
curr_temp = temperature_sensor.get_temperature()
temperature_data.log_temperature(curr_temp)
print("\n",curr_temp)
utime.sleep(1)
if __name__ == "__main__":
main()