Skip to content

Commit 046ebe2

Browse files
committed
samples/agriculture: minor improvements in the button/led handling
Signed-off-by: Ruben Moral <ruben.moral@digi.com>
1 parent da95b65 commit 046ebe2

File tree

1 file changed

+55
-50
lines changed
  • samples/demos/end_to_end_agriculture

1 file changed

+55
-50
lines changed

samples/demos/end_to_end_agriculture/main.py

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137

138138
identified = False
139139
finished = False
140-
simulate_temp = False
140+
simulate_temp_hum = False
141141

142142
time_seconds = 0
143143
weather_condition = WEATHER_SUNNY
@@ -147,10 +147,6 @@
147147

148148
sensor = None
149149

150-
LED_PIN_ID = "D9"
151-
152-
led_pin = Pin(LED_PIN_ID, Pin.OUT, value=VALUE_DISABLED)
153-
154150

155151
def read_properties():
156152
"""
@@ -357,10 +353,10 @@ def get_temperature():
357353
"""
358354
# Initialize variables.
359355
global temperature
360-
global simulate_temp
356+
global simulate_temp_hum
361357

362358
# Check if the temperature has to be simulated or read from the I2C sensor.
363-
if simulate_temp or sensor is None:
359+
if simulate_temp_hum or sensor is None:
364360
time_minutes = int(time_seconds) / 60.0
365361

366362
# Get the temperature based on the time of the day.
@@ -402,7 +398,7 @@ def get_temperature():
402398
temperature = sensor.read_temperature(True)
403399
except OSError:
404400
# If the read fails, change to simulation.
405-
simulate_temp = True
401+
simulate_temp_hum = True
406402
return get_temperature()
407403

408404
return "%.2f" % temperature
@@ -417,42 +413,53 @@ def get_moisture():
417413
"""
418414
# Initialize variables.
419415
global moisture
416+
global simulate_temp_hum
420417

421-
time_minutes = int(time_seconds) / 60.0
418+
# Check if the moisture has to be simulated or read from the I2C sensor.
419+
if simulate_temp_hum or sensor is None:
420+
time_minutes = int(time_seconds) / 60.0
422421

423-
# Get the moisture based on the time of the day.
424-
moisture = int(pow(time_minutes, 3) * 0.0000002 -
425-
pow(time_minutes, 2) * 0.000416 +
426-
time_minutes * 0.184 +
427-
52.75)
428-
429-
# Obtain the moisture delta value and determine if it should be added
430-
# or substracted from the calculated one depending on the weather
431-
# condition and the valve position.
432-
if weather_condition == WEATHER_RAINY or valve_pos == 1:
433-
# Calculate a variation delta. Max delta is 25.1 % (20 + 255 * 20)
434-
delta = 20 + (int.from_bytes(os.urandom(1), "big") * 20) / 1000
435-
add = True
436-
elif weather_condition == WEATHER_SUNNY:
437-
# Calculate a variation delta. Max delta is 12.04 % (10 + 255 * 8)
438-
delta = 10 + (int.from_bytes(os.urandom(1), "big") * 8) / 1000
439-
add = False
440-
else:
441-
# Calculate a variation delta. Max delta is 1.02 % (255 * 4)
442-
delta = int.from_bytes(os.urandom(1), "big") * 4 / 1000
443-
add = int.from_bytes(os.urandom(1), "big") > 128
422+
# Get the moisture based on the time of the day.
423+
moisture = int(pow(time_minutes, 3) * 0.0000002 -
424+
pow(time_minutes, 2) * 0.000416 +
425+
time_minutes * 0.184 +
426+
52.75)
444427

445-
# Apply the delta.
446-
if add:
447-
moisture += delta
448-
else:
449-
moisture -= delta
428+
# Obtain the moisture delta value and determine if it should be added
429+
# or substracted from the calculated one depending on the weather
430+
# condition and the valve position.
431+
if weather_condition == WEATHER_RAINY or valve_pos == 1:
432+
# Calculate a variation delta. Max delta is 25.1 % (20 + 255 * 20)
433+
delta = 20 + (int.from_bytes(os.urandom(1), "big") * 20) / 1000
434+
add = True
435+
elif weather_condition == WEATHER_SUNNY:
436+
# Calculate a variation delta. Max delta is 12.04 % (10 + 255 * 8)
437+
delta = 10 + (int.from_bytes(os.urandom(1), "big") * 8) / 1000
438+
add = False
439+
else:
440+
# Calculate a variation delta. Max delta is 1.02 % (255 * 4)
441+
delta = int.from_bytes(os.urandom(1), "big") * 4 / 1000
442+
add = int.from_bytes(os.urandom(1), "big") > 128
450443

451-
# Check limits.
452-
if moisture < PERCENT_MIN:
453-
moisture = PERCENT_MIN
454-
elif moisture > PERCENT_MAX:
455-
moisture = PERCENT_MAX
444+
# Apply the delta.
445+
if add:
446+
moisture += delta
447+
else:
448+
moisture -= delta
449+
450+
# Check limits.
451+
if moisture < PERCENT_MIN:
452+
moisture = PERCENT_MIN
453+
elif moisture > PERCENT_MAX:
454+
moisture = PERCENT_MAX
455+
else:
456+
try:
457+
# Read the humidity from the I2C sensor.
458+
moisture = sensor.read_humidity()
459+
except OSError:
460+
# If the read fails, change to simulation.
461+
simulate_temp_hum = True
462+
return get_moisture()
456463

457464
return "%.2f" % moisture
458465

@@ -540,14 +547,10 @@ def toggle_valve():
540547
Toggles the status of the electronic valve.
541548
"""
542549
global valve_pos
543-
status = valve_pos
544550

545-
if status == 0:
546-
valve_pos = True
547-
else:
548-
valve_pos = False
549-
550-
print("- Toggling valve status to '{}'.".format("Open" if valve_pos == True else "Closed"))
551+
valve_pos = 1 if valve_pos == 0 else 0
552+
print("- Toggling valve status to '{}'.".format("Open" if valve_pos == 1 else "Closed"))
553+
# Turn on/off the LED.
551554
led_pin.value(valve_pos)
552555

553556

@@ -605,7 +608,7 @@ def main():
605608
global sensor
606609
global identified
607610
global finished
608-
global simulate_temp
611+
global simulate_temp_hum
609612

610613
print(" +-----------------------------------+")
611614
print(" | End-to-End IoT Agriculture Sample |")
@@ -617,6 +620,9 @@ def main():
617620
except AssertionError:
618621
pass
619622

623+
# Simulate temperature if no I2C sensor is detected.
624+
simulate_temp_hum = sensor is None
625+
620626
# Configure the Bluetooth advertisement.
621627
config_advertisement()
622628

@@ -633,8 +639,7 @@ def main():
633639
# Sleep 100 ms.
634640
time.sleep_ms(100)
635641

636-
# If the button has been pressed, swap the temperature source
637-
# (reading or simulation).
642+
# If the button has been pressed, toggle the electronic valve.
638643
if not was_btn_pressed and is_button_pressed():
639644
toggle_valve()
640645
status_response = {

0 commit comments

Comments
 (0)