import math import random import sched, time import socket, pickle from threading import Timer class Conditions: def __init__(self): self.temperature = random.uniform(25.0, 38.0) self.air_humidity = random.uniform(0.2, 0.6) self.soil_humidity = random.uniform(0.2, 0.6) self.air_speed = random.randint(16, 30) def new_conditions(self): self.temperature = random.uniform(25.0, 38.0) self.air_humidity = random.uniform(0.2, 0.6) self.soil_humidity = random.uniform(0.2, 0.6) self.air_speed = random.randint(16,30) return self #function that calculates if the differnce between the values exceeds 10% def calculate_difference(previous, current): if abs((previous - current) / previous) > 0.1: return True else: return False #function that decides if the new measurements need to be sent over the server def needs_send(previous, current): if (calculate_difference(previous.temperature, current.temperature) or calculate_difference(previous.air_humidity, current.air_humidity) or calculate_difference(previous.soil_humidity, current.soil_humidity) or calculate_difference(previous.air_speed, current.air_speed)) : return True #copy function def copy(obj1, obj2): obj1.temperature = obj2.temperature obj1.air_humidity = obj2.air_humidity obj1.soil_humidity = obj2.soil_humidity obj1.air_speed = obj2.air_speed #function that checks if data has been sent in 5 minutes def send_is_true(data_sent, current_conditions): if data_sent == True: pass else: print("Data has not been sent for 5 minutes\n") print("Sending current data") my_pickled_object = pickle.dumps(current_conditions) s.send(my_pickled_object) #socket connection s = socket.socket() port = 3125 s.connect(('localhost', port)) #initializing conditions objects counter = 0 previous_conditions = Conditions() current_conditions = Conditions() #initializing scheduler f = sched.scheduler(time.time, time.sleep) #initializing data for time checker data_sent = False t = Timer(300.0, send_is_true) t.start() def send_data(sc, counter, previous_conditions, current_conditions, data_sent): if counter == 0: #is this first time sending? previous_conditions = previous_conditions.new_conditions() elif counter == 1: #data sets are over 1 current_conditions = current_conditions.new_conditions() else: copy(previous_conditions, current_conditions) current_conditions = current_conditions.new_conditions() if counter != 0: #will print data to console when we have > 1 conditions print("\n\n---" + str(counter) + "---") print("\nPrevious temperature: " + str(int(previous_conditions.temperature)) + " Celcius") print("Current temperature: " + str(int(current_conditions.temperature)) + " Celcius\n\n") print(f"Previous soil humidity: " + str(int(previous_conditions.soil_humidity*100)) + " %") print(f"Current soil humidity: " + str(int(current_conditions.soil_humidity*100)) + " %\n\n") print(f"Previous air humidity: " + str(int(previous_conditions.air_humidity*100)) + "%") print(f"Current air humidity: " + str(int(current_conditions.air_humidity*100)) + "%\n\n") print("Previous air speed: " + str(previous_conditions.air_speed) + " km/h") print("Current air speed: " + str(current_conditions.air_speed) + " km/h\n\n") if needs_send(previous_conditions, current_conditions): print("It needs to be send.") my_pickled_object = pickle.dumps(current_conditions) data_sent = True s.send(my_pickled_object) else: print("It doesn't need to be send.") counter += 1 f.enter(30, 1, send_data, (sc,counter, previous_conditions, current_conditions, data_sent)) f.enter(30, 1, send_data, (f,counter, previous_conditions, current_conditions, data_sent)) f.run() s.close()