From 981ffb4e3b28406cf13123dfc9f518188897fd7b Mon Sep 17 00:00:00 2001 From: Victor Coman Date: Tue, 16 Nov 2021 12:22:46 +0100 Subject: [PATCH 1/4] samples/agriculture: do not disable BLE after provisioning Signed-off-by: Victor Coman --- samples/demos/end_to_end_agriculture/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/demos/end_to_end_agriculture/main.py b/samples/demos/end_to_end_agriculture/main.py index 0770bf1..9de4cde 100644 --- a/samples/demos/end_to_end_agriculture/main.py +++ b/samples/demos/end_to_end_agriculture/main.py @@ -299,7 +299,7 @@ def relay_frame_callback(relay_frame): elif operation == OP_FINISH: print("- BLE: Finish request received.") # Disable BLE interface. This operation does not require a response. - xbee.atcmd(AT_CMD_BT, VALUE_DISABLED) + # xbee.atcmd(AT_CMD_BT, VALUE_DISABLED) # Write settings in the device. xbee.atcmd(AT_CMD_WR) finished = True From f1d3d7a1fd01a4e51949e646eeb4db473727ca5e Mon Sep 17 00:00:00 2001 From: Victor Coman Date: Thu, 25 Nov 2021 09:59:35 +0100 Subject: [PATCH 2/4] samples/agriculture: change functionality of button DIO0 - Before when you pressed the button, it was switching between simulation and real data. - Now it always works with real data, and the button just toggles the electronic water valve to open or close. Signed-off-by: Victor Coman --- samples/demos/end_to_end_agriculture/main.py | 41 +++++++++++++++----- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/samples/demos/end_to_end_agriculture/main.py b/samples/demos/end_to_end_agriculture/main.py index 9de4cde..08b7ba4 100644 --- a/samples/demos/end_to_end_agriculture/main.py +++ b/samples/demos/end_to_end_agriculture/main.py @@ -137,7 +137,7 @@ identified = False finished = False -simulate_temp = True +simulate_temp = False time_seconds = 0 weather_condition = WEATHER_SUNNY @@ -531,6 +531,22 @@ def set_status_value(status_id, status_value): led_pin.value(valve_pos) +def toggle_valve(): + """ + Toggles the status of the electronic valve. + """ + global valve_pos + status = valve_pos + + if status == 0: + valve_pos = 1 + else: + valve_pos = 0 + + print("- Toggling valve status to '{}'.".format("Open" if valve_pos == 1 else "Closed")) + # set_valve_open(new_status) + + def get_mac(): """ Returns the XBee MAC address of the device. @@ -613,15 +629,22 @@ def main(): # Sleep 100 ms. time.sleep_ms(100) - if sensor is not None: - # If the button has been pressed, swap the temperature source - # (reading or simulation). - if not was_btn_pressed and is_button_pressed(): - simulate_temp = not simulate_temp - print("- Temperature source changed to %s" - % ("simulation" if simulate_temp else "reading")) + # If the button has been pressed, swap the temperature source + # (reading or simulation). + if not was_btn_pressed and is_button_pressed(): + toggle_valve() + status_response = { + ITEM_OP: OP_STATUS, + ITEM_PROP: get_sensors_values() + } + print("- Reporting status data: %s" % status_response) + try: + xbee.transmit(xbee.ADDR_COORDINATOR, + ujson.dumps(status_response)) + except Exception as e: + print(" - Transmit failure: %s" % str(e)) - was_btn_pressed = is_button_pressed() + was_btn_pressed = is_button_pressed() # Blink identification LED if necessary. if identified: From da95b65740c5fd6e8eed590ffa56229a49657444 Mon Sep 17 00:00:00 2001 From: Victor Coman Date: Thu, 9 Dec 2021 13:46:37 +0100 Subject: [PATCH 3/4] samples/agriculture: add LED for valves (turns on the LED when open and turns off when closed) Signed-off-by: Victor Coman --- samples/demos/end_to_end_agriculture/main.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/samples/demos/end_to_end_agriculture/main.py b/samples/demos/end_to_end_agriculture/main.py index 08b7ba4..461adfc 100644 --- a/samples/demos/end_to_end_agriculture/main.py +++ b/samples/demos/end_to_end_agriculture/main.py @@ -147,6 +147,10 @@ sensor = None +LED_PIN_ID = "D9" + +led_pin = Pin(LED_PIN_ID, Pin.OUT, value=VALUE_DISABLED) + def read_properties(): """ @@ -539,12 +543,12 @@ def toggle_valve(): status = valve_pos if status == 0: - valve_pos = 1 + valve_pos = True else: - valve_pos = 0 + valve_pos = False - print("- Toggling valve status to '{}'.".format("Open" if valve_pos == 1 else "Closed")) - # set_valve_open(new_status) + print("- Toggling valve status to '{}'.".format("Open" if valve_pos == True else "Closed")) + led_pin.value(valve_pos) def get_mac(): From 046ebe26c4482291f4d5e00998ac930eb87039a8 Mon Sep 17 00:00:00 2001 From: Ruben Moral Date: Thu, 19 May 2022 12:12:46 +0200 Subject: [PATCH 4/4] samples/agriculture: minor improvements in the button/led handling Signed-off-by: Ruben Moral --- samples/demos/end_to_end_agriculture/main.py | 105 ++++++++++--------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/samples/demos/end_to_end_agriculture/main.py b/samples/demos/end_to_end_agriculture/main.py index 461adfc..f065bac 100644 --- a/samples/demos/end_to_end_agriculture/main.py +++ b/samples/demos/end_to_end_agriculture/main.py @@ -137,7 +137,7 @@ identified = False finished = False -simulate_temp = False +simulate_temp_hum = False time_seconds = 0 weather_condition = WEATHER_SUNNY @@ -147,10 +147,6 @@ sensor = None -LED_PIN_ID = "D9" - -led_pin = Pin(LED_PIN_ID, Pin.OUT, value=VALUE_DISABLED) - def read_properties(): """ @@ -357,10 +353,10 @@ def get_temperature(): """ # Initialize variables. global temperature - global simulate_temp + global simulate_temp_hum # Check if the temperature has to be simulated or read from the I2C sensor. - if simulate_temp or sensor is None: + if simulate_temp_hum or sensor is None: time_minutes = int(time_seconds) / 60.0 # Get the temperature based on the time of the day. @@ -402,7 +398,7 @@ def get_temperature(): temperature = sensor.read_temperature(True) except OSError: # If the read fails, change to simulation. - simulate_temp = True + simulate_temp_hum = True return get_temperature() return "%.2f" % temperature @@ -417,42 +413,53 @@ def get_moisture(): """ # Initialize variables. global moisture + global simulate_temp_hum - time_minutes = int(time_seconds) / 60.0 + # Check if the moisture has to be simulated or read from the I2C sensor. + if simulate_temp_hum or sensor is None: + time_minutes = int(time_seconds) / 60.0 - # Get the moisture based on the time of the day. - moisture = int(pow(time_minutes, 3) * 0.0000002 - - pow(time_minutes, 2) * 0.000416 + - time_minutes * 0.184 + - 52.75) - - # Obtain the moisture delta value and determine if it should be added - # or substracted from the calculated one depending on the weather - # condition and the valve position. - if weather_condition == WEATHER_RAINY or valve_pos == 1: - # Calculate a variation delta. Max delta is 25.1 % (20 + 255 * 20) - delta = 20 + (int.from_bytes(os.urandom(1), "big") * 20) / 1000 - add = True - elif weather_condition == WEATHER_SUNNY: - # Calculate a variation delta. Max delta is 12.04 % (10 + 255 * 8) - delta = 10 + (int.from_bytes(os.urandom(1), "big") * 8) / 1000 - add = False - else: - # Calculate a variation delta. Max delta is 1.02 % (255 * 4) - delta = int.from_bytes(os.urandom(1), "big") * 4 / 1000 - add = int.from_bytes(os.urandom(1), "big") > 128 + # Get the moisture based on the time of the day. + moisture = int(pow(time_minutes, 3) * 0.0000002 - + pow(time_minutes, 2) * 0.000416 + + time_minutes * 0.184 + + 52.75) - # Apply the delta. - if add: - moisture += delta - else: - moisture -= delta + # Obtain the moisture delta value and determine if it should be added + # or substracted from the calculated one depending on the weather + # condition and the valve position. + if weather_condition == WEATHER_RAINY or valve_pos == 1: + # Calculate a variation delta. Max delta is 25.1 % (20 + 255 * 20) + delta = 20 + (int.from_bytes(os.urandom(1), "big") * 20) / 1000 + add = True + elif weather_condition == WEATHER_SUNNY: + # Calculate a variation delta. Max delta is 12.04 % (10 + 255 * 8) + delta = 10 + (int.from_bytes(os.urandom(1), "big") * 8) / 1000 + add = False + else: + # Calculate a variation delta. Max delta is 1.02 % (255 * 4) + delta = int.from_bytes(os.urandom(1), "big") * 4 / 1000 + add = int.from_bytes(os.urandom(1), "big") > 128 - # Check limits. - if moisture < PERCENT_MIN: - moisture = PERCENT_MIN - elif moisture > PERCENT_MAX: - moisture = PERCENT_MAX + # Apply the delta. + if add: + moisture += delta + else: + moisture -= delta + + # Check limits. + if moisture < PERCENT_MIN: + moisture = PERCENT_MIN + elif moisture > PERCENT_MAX: + moisture = PERCENT_MAX + else: + try: + # Read the humidity from the I2C sensor. + moisture = sensor.read_humidity() + except OSError: + # If the read fails, change to simulation. + simulate_temp_hum = True + return get_moisture() return "%.2f" % moisture @@ -540,14 +547,10 @@ def toggle_valve(): Toggles the status of the electronic valve. """ global valve_pos - status = valve_pos - if status == 0: - valve_pos = True - else: - valve_pos = False - - print("- Toggling valve status to '{}'.".format("Open" if valve_pos == True else "Closed")) + valve_pos = 1 if valve_pos == 0 else 0 + print("- Toggling valve status to '{}'.".format("Open" if valve_pos == 1 else "Closed")) + # Turn on/off the LED. led_pin.value(valve_pos) @@ -605,7 +608,7 @@ def main(): global sensor global identified global finished - global simulate_temp + global simulate_temp_hum print(" +-----------------------------------+") print(" | End-to-End IoT Agriculture Sample |") @@ -617,6 +620,9 @@ def main(): except AssertionError: pass + # Simulate temperature if no I2C sensor is detected. + simulate_temp_hum = sensor is None + # Configure the Bluetooth advertisement. config_advertisement() @@ -633,8 +639,7 @@ def main(): # Sleep 100 ms. time.sleep_ms(100) - # If the button has been pressed, swap the temperature source - # (reading or simulation). + # If the button has been pressed, toggle the electronic valve. if not was_btn_pressed and is_button_pressed(): toggle_valve() status_response = {