def moisture_reading(channel): i = 0 data = [] while i < 100: value = mcp.read_adc_difference(channel) log_util.log_debug(__name__, "Moisture mesure {}: {}".format(i, value)) if value != 0: measure = (value / 1023) * 100 data.append(measure) i += 1 return round(100 - (sum(data) / len(data)), 2)
def readings_subscribe_action(client, userdata, message): log_util.log_info(__name__, 'Received a message: {}'.format(message.payload)) byte_payload = message.payload.decode('utf8').replace("'", '"') data = json.loads(byte_payload) if automatic: log_util.log_debug(__name__, "Automatic is on, checking the moisture level...") if sensors.moisture_level(data['Items']): data = {"action": "ON", "requester": "AUTOMATIC"} water.watering_invocation(data) else: log_util.log_info( __name__, 'The soil is too wet, the moisture level is {}'.format( data['Items']['moisture1']))
def light_reading(channel): log_util.log_debug(__name__, "Light sensor") i = 0 data = [] while i < 10: measure = 0.0 # Output on the pin for GPIO.setmode(GPIO.BOARD) GPIO.setup(channel, GPIO.OUT) GPIO.output(channel, GPIO.LOW) sleep(0.1) # Change the pin back to input GPIO.setup(channel, GPIO.IN) # Count until the pin goes high while GPIO.input(channel) == GPIO.LOW: measure += 1 if measure != 0: data.append(measure) i += 1 return round((sum(data) / len(data)), 2)
def status_subscribe_action(client, userdata, message): global automatic log_util.log_info(__name__, 'Received a message: {}'.format(message.payload)) byte_payload = message.payload.decode('utf8').replace("'", '"') data = json.loads(byte_payload) status = data['status'] if status == 'A': log_util.log_debug(__name__, "Turned ON automatic") automatic = True elif status == 'M': automatic = False log_util.log_debug(__name__, "Turned OFF automatic") elif status == 'F': watering = {"action": "OFF", "requester": "MANUAL"} log_util.log_debug(__name__, "Turned OFF Manual") water.watering_invocation(watering) elif status == 'O': watering = {"action": "ON", "requester": "MANUAL"} log_util.log_debug(__name__, "Turned ON Manual") water.watering_invocation(watering)
def air_sensor_reading(channel): log_util.log_debug(__name__, "Air sensor") i = 0 data_humidity = [] data_temperature = [] while i < 1: humidity_unvalidated, temperature_unvalidated = Adafruit_DHT.read_retry(DHT_SENSOR, channel) log_util.log_debug(__name__, humidity_unvalidated) log_util.log_debug(__name__, temperature_unvalidated) if humidity_unvalidated and temperature_unvalidated: data_humidity.append(humidity_unvalidated) data_temperature.append(temperature_unvalidated) i += 1 return round((sum(data_humidity) / len(data_humidity)), 2), round((sum(data_temperature) / len(data_temperature)), 2)
my_rpi = AWSIoTMQTTClient("Raspberry_Listener") my_rpi.configureEndpoint(host, 8883) my_rpi.configureCredentials(rootCAPath, privateKeyPath, certificatePath) my_rpi.configureOfflinePublishQueueing(-1) # Infinite offline Publish queueing my_rpi.configureDrainingFrequency(2) # Draining: 2 Hz my_rpi.configureConnectDisconnectTimeout(10) # 10 sec my_rpi.configureMQTTOperationTimeout(5) # 5 sec # Connect and subscribe to AWS IoT my_rpi.connect() my_rpi.subscribe('smartgarden/status', 1, status_subscribe_action) my_rpi.subscribe('smartgarden/watering', 1, watering_subscribe_action) my_rpi.subscribe('smartgarden/readings', 1, readings_subscribe_action) my_rpi.subscribe('smartgarden/maxdata', 1, max_data_update_action) if len(repository_dynamo.get_status()) > 0 and repository_dynamo.get_status( )[0]['status'] == 'A': log_util.log_debug(__name__, "First status of the system is Automatic") automatic = True # Publish to the same topic in a loop forever while True: try: pass except KeyboardInterrupt as e: log_util.log_error( __name__, "User request to stop the system: {}".format(str(e))) break