In this part of the lesson, you will add a relay to your virtual IoT device in addition to the soil moisture sensor, and control it based off the soil moisture level.
The virtual IoT device will use a simulated Grove relay. This keeps this lab the same as using a Raspberry Pi with a physical Grove relay.
In a physical IoT device, the relay would be a normally-open relay (meaning the output circuit is open, or disconnected when there is no signal sent to the relay). A relay like this can handle output circuits up to 250V and 10A.
To use a virtual relay, you need to add it to the CounterFit app
Add the relay to the CounterFit app.
-
Open the
soil-moisture-sensor
project from the last lesson in VS Code if it's not already open. You will be adding to this project. -
Make sure the CounterFit web app is running
-
Create a relay:
-
In the Create actuator box in the Actuators pane, drop down the Actuator type box and select Relay.
-
Set the Pin to 5
-
Select the Add button to create the relay on Pin 5
The relay will be created and appear in the actuators list.
-
The soil moisture sensor app can now be programmed to use the virtual relay.
Program the virtual device.
-
Open the
soil-moisture-sensor
project from the last lesson in VS Code if it's not already open. You will be adding to this project. -
Add the following code to the
app.py
file below the existing imports:from counterfit_shims_grove.grove_relay import GroveRelay
This statement imports the
GroveRelay
from the Grove Python shim libraries to interact with the virtual Grove relay. -
Add the following code below the declaration of the
ADC
class to create aGroveRelay
instance:relay = GroveRelay(5)
This creates a relay using pin 5, the pin you connected the relay to.
-
To test the relay is working, add the following to the
while True:
loop:relay.on() time.sleep(.5) relay.off()
The code turns the relay on, waits 0.5 seconds, then turns the relay off.
-
Run the Python app. The relay will turn on and off every 10 seconds, with a half second delay between turning on and off. You will see the virtual relay in the CounterFit app close and open as the relay is turned on and off.
Now that the relay is working, it can be controlled in response to soil moisture readings.
Control the relay.
-
Delete the 3 lines of code that you added to test the relay. Replace them with the following code in its place:
if soil_moisture > 450: print("Soil Moisture is too low, turning relay on.") relay.on() else: print("Soil Moisture is ok, turning relay off.") relay.off()
This code checks the soil moisture level from the soil moisture sensor. If it is above 450, it turns on the relay, turning it off if it goes below 450.
💁 Remember the capacitive soil moisture sensor reads the lower the soil moisture level, the more moisture there is in the soil and vice versa.
-
Run the Python app. You will see the relay turn on or off depending on the soil moisture levels. Change the Value or the Random settings for the soil moisture sensor to see the value change.
Soil Moisture: 638 Soil Moisture is too low, turning relay on. Soil Moisture: 452 Soil Moisture is too low, turning relay on. Soil Moisture: 347 Soil Moisture is ok, turning relay off.
💁 You can find this code in the code-relay/virtual-device folder.
😀 Your virtual soil moisture sensor controlling a relay program was a success!