chapter two (lesson one) commits (#58)
* Chapter two(lesson one) commit * Update README.md * Update pi-temp.md * Update virtual-device-temp.md * fixes * fixes * Update README.md * chapter two(lesson 2) * Update README.md * Chapter two (lesson three) * changed images Co-authored-by: Jim Bennett <jim.bennett@microsoft.com>
This commit is contained in:
Родитель
8a4cd49d39
Коммит
ce7eeba3a7
|
@ -47,7 +47,7 @@ When learning about plants, most students are taught about the necessity of wate
|
|||
Plants have a base or minimum temperature, optimal temperature, and maximum temperature, all based on daily average temperatures.
|
||||
|
||||
* Base temperature - this is the minimum daily average temperature needed for a plant to grow.
|
||||
* Optimum temperature - this is the best daily average temperature to get the most growth
|
||||
* Optimum temperature - this is the best daily average temperature to get the most growth.
|
||||
* Maximum temperature - The is the maximum temperature a plant can withstand. Above this the plant will shut down it's growth in an attempt to conserve water and stay alive.
|
||||
|
||||
> 💁 These are average temperatures, averaged over the daily and nightly temperatures. Plants also need different temperatures day and night to help them photosynthesize more efficiently and save energy at night.
|
||||
|
@ -149,7 +149,7 @@ Work through the relevant guide to publish temperature data over MQTT using your
|
|||
|
||||
### Task - capture and store the temperature information
|
||||
|
||||
Once the IoT device is publishing telemetry, the server code can be written to subscribe to this data and store it. Rather than save it to a database, the server code will save it to a Comma Separated Values (CSV) file. CSV files store data as rows of values as text with each value separated by a comma, and each record on a new line. They are a convenient, human-readable and well supported way to save data as a file.
|
||||
Once the IoT device is publishing telemetry, the server code can be written to subscribe to this data and store it. Rather than save it to a database, the server code will save it to a Comma Separated Values (CSV) file. CSV files store data as rows of values as text, with each value separated by a comma, and each record on a new line. They are a convenient, human-readable and well supported way to save data as a file.
|
||||
|
||||
The CSV file will have two columns - *date* and *temperature*. The *date* column is set as the current date and time that the message was received by the server, the *temperature* comes from the telemetry message.
|
||||
|
||||
|
@ -218,11 +218,14 @@ The CSV file will have two columns - *date* and *temperature*. The *date* column
|
|||
2021-04-19T17:41:36-07:00,25
|
||||
```
|
||||
|
||||
1. Run this code for a while to capture data. Ideally you should run this for a an entire day to gather enough data for GDD calculations.
|
||||
1. Run this code for a while to capture data. Ideally you should run this for an entire day to gather enough data for GDD calculations.
|
||||
|
||||
> 💁 If you are using Virtual IoT Device, select the random checkbox and set a range to avoid getting the same temperature everytime the temperature value is returned.
|
||||
![Select the random checkbox and set a range](../../../images/select-the-random-checkbox-and-set-a-range.png)
|
||||
|
||||
> 💁 If you want to run this for an entire day, then you need to make sure the computer your server code is running on won't go to sleep, either by changing your power settings, or running something like [this keep system active Python script](https://github.com/jaqsparow/keep-system-active).
|
||||
|
||||
> 💁 You can find this code in the [code-server/server](code-server/server) folder.
|
||||
|
||||
> 💁 You can find this code in the [code-server/temperature-sensor-server](code-server/temperature-sensor-server) folder.
|
||||
|
||||
### Task - calculate GDD using the stored data
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ In this lesson you gathered GDD data using an IoT sensor. To get good GDD data,
|
|||
|
||||
Start by gathering data for a few days. You will need to ensure your server code is running all the time your IoT device is running, either by adjusting your power management settings or running something like [this keep system active Python script](https://github.com/jaqsparow/keep-system-active).
|
||||
|
||||
Once you have temperature data, you can use a Jupyter Notebook in this repo to visualize it and calculate GDD. Jupyter notebooks mix code and instructions in blocks called *cells*, often code in Python. You can read the instructions, then run each block of code block by block. You can also edit the code. In this notebook for example, you can edit the base temperature used to calculate the GDD for your plant.
|
||||
Once you have temperature data, you can use the Jupyter Notebook in this repo to visualize it and calculate GDD. Jupyter notebooks mix code and instructions in blocks called *cells*, often code in Python. You can read the instructions, then run each block of code, block by block. You can also edit the code. In this notebook for example, you can edit the base temperature used to calculate the GDD for your plant.
|
||||
|
||||
1. Create a folder called `gdd-calculation`
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ def handle_telemetry(client, userdata, message):
|
|||
|
||||
with open(temperature_file_name, mode='a') as temperature_file:
|
||||
temperature_writer = csv.DictWriter(temperature_file, fieldnames=fieldnames)
|
||||
temperature_writer.writerow({'date' : datetime.date.today(), 'temperature' : payload['temperature']})
|
||||
temperature_writer.writerow({'date' : datetime.now().astimezone().replace(microsecond=0).isoformat(), 'temperature' : payload['temperature']})
|
||||
|
||||
mqtt_client.subscribe(client_telemetry_topic)
|
||||
mqtt_client.on_message = handle_telemetry
|
||||
|
|
|
@ -27,7 +27,7 @@ In this lesson we'll cover:
|
|||
|
||||
Plants require water to grow. They absorb water throughout the entire plant, with the majority being absorbed by the root system. Water is used by the plant for three things:
|
||||
|
||||
* [Photosynthesis](https://wikipedia.org/wiki/Photosynthesis) - plants react water with carbon dioxide and light to produce carbohydrates and oxygen
|
||||
* [Photosynthesis](https://wikipedia.org/wiki/Photosynthesis) - plants creates a chemical reaction with water, carbon dioxide and light to produce carbohydrates and oxygen.
|
||||
* [Transpiration](https://wikipedia.org/wiki/Transpiration) - plants use water for diffusion of carbon dioxide from the air into the plant via pores in the leaves. This process also carries nutrients around the plant, and cools the plant, similar to how humans sweat.
|
||||
* Structure - plants also need water to maintain their structure - they are 90% water (as opposed to humans at only 60%), and this water keeps the cells rigid. If a plant doesn't have enough water it will wilt and eventually die.
|
||||
|
||||
|
@ -55,7 +55,7 @@ There are a range of different types of sensor you can use to measure soil moist
|
|||
|
||||
![A capacitive soil moisture sensor](../../../images/grove-capacitive-soil-moisture-sensor.png)
|
||||
|
||||
These are both analog sensors, returning a voltage to indicate soil moisture. So how does this voltage get to your code? Before going any further with these sensors, lets look at how sensors and actuators communicate with IoT devices.
|
||||
These are both analog sensors, returning a voltage to indicate soil moisture. So how does this voltage get to your code? Before going any further with these sensors, let's look at how sensors and actuators communicate with IoT devices.
|
||||
|
||||
## How sensors communicate with IoT devices
|
||||
|
||||
|
@ -64,8 +64,8 @@ So far in these lessons you've learned about a number of sensors and actuators,
|
|||
To communicate with most sensors and actuators you need some hardware, and a communication protocol - that is a well defined way for data to be sent and received. Take for example a capacitive soil moisture sensor:
|
||||
|
||||
* How is this sensor connected to the IoT device?
|
||||
* If it measures a voltage that is an analog signal, it will need an ADC to create a digital representation of the value, and this value is sent as an alternating a voltage to send 0s and 1s - but how long is each bit sent for?
|
||||
* If the sensor returns a digital value, that will be a stream of 0s and 1, again how long is each bit sent for?
|
||||
* If it measures a voltage that is an analog signal, it will need an ADC to create a digital representation of the value, and this value is sent as an alternating voltage to send 0s and 1s - but how long is each bit sent for?
|
||||
* If the sensor returns a digital value, that will be a stream of 0s and 1s, again how long is each bit sent for?
|
||||
* If the voltage is high for 0.1s is that a single 1 bit, or 2 consecutive 1 bits, or 10?
|
||||
* At what point does the number start? Is `00001101` 25, or are the first 5 bits the end of the previous value?
|
||||
|
||||
|
@ -107,7 +107,7 @@ For example, on a 3.3V board, if the sensor returns 3.3V, the value returned wou
|
|||
|
||||
***A soil moisture sensor sent 3.3V and returning 1.65v, or a reading of 511. probe by Adnen Kadri / Microcontroller by Template - all from the [Noun Project](https://thenounproject.com)***
|
||||
|
||||
> 💁 Back in lesson 3, the light sensor returned a value from 0-1,023. If you are using a Wio Terminal, the sensor was connected to an analog pin. If you are using a Raspberry Pi, then the sensor was connected to an analog pin on the base hat that has an integrated ADC to communicate over the GPIO pins. The virtual device was set to send a value from 0-1,023 to simulate an analog pin.
|
||||
> 💁 Back in nightlight - lesson 3, the light sensor returned a value from 0-1,023. If you are using a Wio Terminal, the sensor was connected to an analog pin. If you are using a Raspberry Pi, then the sensor was connected to an analog pin on the base hat that has an integrated ADC to communicate over the GPIO pins. The virtual device was set to send a value from 0-1,023 to simulate an analog pin.
|
||||
|
||||
Soil moisture sensors rely on voltages, so will use analog pins and give values from 0-1,023.
|
||||
|
||||
|
@ -153,7 +153,7 @@ UART involves physical circuitry that allows two devices to communicate. Each de
|
|||
|
||||
UART devices have a [baud rate](https://wikipedia.org/wiki/Symbol_rate) (also known as Symbol rate), which is the speed that data will be sent and received in bits per second. A common baud rate is 9,600, meaning 9,600 bits (0s and 1s) of data are sent each second.
|
||||
|
||||
UART uses start and stop bits - that is is sends a start bit to indicate that it's about to send a byte (8 bits) of data, then a stop bit after it's sent the 8 bits.
|
||||
UART uses start and stop bits - that is it sends a start bit to indicate that it's about to send a byte (8 bits) of data, then a stop bit after it sends the 8 bits.
|
||||
|
||||
UART speed is dependent on hardware, but even the fastest implementations don't exceed 6.5 Mbps (megabits per second, or millions of bits, 0 or 1, sent per second).
|
||||
|
||||
|
@ -180,7 +180,7 @@ SPI controllers use 3 wires, along with 1 extra wire per peripheral. Peripherals
|
|||
|
||||
***SPI with on controller and two peripherals. chip by Astatine Lab - all from the [Noun Project](https://thenounproject.com)***
|
||||
|
||||
The CS wire is used to activate one peripheral at a time, communicating over the COPI and CIPO wires. When the controller needs to change peripheral, it deactivates the CS wire connected to currently active peripheral, then activates the wire connected to the peripheral it wants to communicate with next.
|
||||
The CS wire is used to activate one peripheral at a time, communicating over the COPI and CIPO wires. When the controller needs to change peripheral, it deactivates the CS wire connected to the currently active peripheral, then activates the wire connected to the peripheral it wants to communicate with next.
|
||||
|
||||
SPI is *full-duplex*, meaning the controller can send and receive data at the same time from the same peripheral using the COPI and CIPO wires. SPI uses a clock signal on the SCLK wire to keep the devices in sync, so unlike sending directly over UART it doesn't need start and stop bits.
|
||||
|
||||
|
@ -218,13 +218,13 @@ Work through the relevant guide to measure soil moisture using your IoT device:
|
|||
|
||||
Sensors rely on measuring electrical properties such as resistance or capacitance.
|
||||
|
||||
> 🎓 Resistance, measured in ohms (Ω) is how much opposition there is to the electric current travelling through something. When a voltage is applied to a material, the amount of current that passes through it is dependant on the resistance of the material. You can read more on the [electrical resistance page on Wikipedia](https://wikipedia.org/wiki/Electrical_resistance_and_conductance).
|
||||
> 🎓 Resistance, measured in ohms (Ω) is how much opposition there is to the electric current travelling through something. When a voltage is applied to a material, the amount of current that passes through it is dependant on the resistance of the material. You can read more on the [electrical resistance page on Wikipedia](https://wikipedia.org/wiki/Electrical_resistance_and_conductance).
|
||||
|
||||
> 🎓 Capacitance, measured in farads (F), is the ability of a component or circuit to collect and store electrical energy. You can read more on capacitance on the [capacitance page on Wikipedia](https://wikipedia.org/wiki/Capacitance).
|
||||
|
||||
These measurements are not always useful - imagine a temperature sensor that gave you a measurement of 22.5KΩ! Instead the value measured needs to be converted into a useful unit by being calibrated - that is matching the values measured to the quantity measured to allow new measurements to be converted to the right unit.
|
||||
These measurements are not always useful - imagine a temperature sensor that gave you a measurement of 22.5KΩ! Instead the value measured needs to be converted into a useful unit by being calibrated - that is matching the values measured to the quantity measured to allow new measurements to be converted to the right unit.
|
||||
|
||||
Some sensors come pre-calibrated. For example the temperature sensor you used in the last lesson was already calibrated so that it can return a temperature measurement in °C. In the factory the first sensor created would be exposed to a range of known temperatures and the resistance measured. This would then be used to build a calculation that can convert from the value measured in Ω (the unit of resistance) to °C.
|
||||
Some sensors come pre-calibrated. For example the temperature sensor you used in the last lesson was already calibrated so that it can return a temperature measurement in °C. In the factory the first sensor created would be exposed to a range of known temperatures and the resistance measured. This would then be used to build a calculation that can convert from the value measured in Ω (the unit of resistance) to °C.
|
||||
|
||||
> 💁 The formula to calculate resistance from temperature is called the [Steinhart–Hart equation](https://wikipedia.org/wiki/Steinhart–Hart_equation).
|
||||
|
||||
|
@ -241,7 +241,7 @@ Soil moisture sensors measure electrical resistance or capacitance - this not on
|
|||
|
||||
![A graph of voltage vs soil moisture content](../../../images/soil-moisture-to-voltage.png)
|
||||
|
||||
The graph above shows how to calibrate a sensor . The voltage is captured for a soil sample that is then measured in a lab by comparing the moist weight to the dry weight (by measuring the weight wet, then drying in an over and measuring dry). Once a few readings have been taken, this can be plotted on a graph and a line fitted to the points. This line can then be used to convert soil moisture sensor readings taken by an IoT device into actual soil moisture measurements.
|
||||
The graph above shows how to calibrate a sensor . The voltage is captured for a soil sample that is then measured in a lab by comparing the moist weight to the dry weight (by measuring the weight wet, then drying in an oven and measuring dry). Once a few readings have been taken, this can be plotted on a graph and a line fitted to the points. This line can then be used to convert soil moisture sensor readings taken by an IoT device into actual soil moisture measurements.
|
||||
|
||||
💁 For resistive soil moisture sensors, the voltage increases as soil moisture increases. For capacitive soil moisture sensors, the voltage decreases as soil moisture increases, so the graphs for these would slope downwards, not upwards.
|
||||
|
||||
|
|
|
@ -42,9 +42,9 @@ Program the device.
|
|||
|
||||
1. Launch VS Code, either directly on the Pi, or connect via the Remote SSH extension.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for setting up and launch VS Code in lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/pi.md).
|
||||
> ⚠️ You can refer to [the instructions for setting up and launch VS Code in nightlight - lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/pi.md).
|
||||
|
||||
1. From the terminal, create a new folder in the `pi` users home directory called `soil-moisture-sensor`. Create a file in this folder called `app.py`:
|
||||
1. From the terminal, create a new folder in the `pi` users home directory called `soil-moisture-sensor`. Create a file in this folder called `app.py`.
|
||||
|
||||
1. Open this folder in VS Code
|
||||
|
||||
|
|
|
@ -32,7 +32,7 @@ The solution to this is to have a pump connected to an external power supply, an
|
|||
|
||||
***A light switch turns power on to a light. switch by Chattapat / lightbulb by Maxim Kulikov - all from the [Noun Project](https://thenounproject.com)***
|
||||
|
||||
> 🎓 [Mains electricity](https://wikipedia.org/wiki/Mains_electricity) refers to the electricity delivered to homes an businesses through national infrastructure in may parts of the world.
|
||||
> 🎓 [Mains electricity](https://wikipedia.org/wiki/Mains_electricity) refers to the electricity delivered to homes and businesses through national infrastructure in many parts of the world.
|
||||
|
||||
✅ IoT devices can usually provide 3.3V or 5V, at less than 1 amp (1A) of current. Compare this to mains electricity which is most often at 230V (120V in North America and 100V in Japan), and can provide power for devices that draw 30A.
|
||||
|
||||
|
@ -102,27 +102,27 @@ So far your relay is controlled by the IoT device directly based off a single so
|
|||
|
||||
1. Add the relevant MQTT libraries/pip packages and code to your `soil-moisture-sensor` project to connect to MQTT. Name the client ID as `soilmoisturesensor_client` prefixed by your ID.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for for connecting to MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#connect-your-iot-device-to-mqtt).
|
||||
> ⚠️ You can refer to [the instructions for connecting to MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#connect-your-iot-device-to-mqtt).
|
||||
|
||||
1. Add the relevant device code to send telemetry with the soil moisture settings. For the telemetry message, name the property `soil_moisture`.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for for sending telemetry to MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#send-telemetry-from-your-iot-device).
|
||||
> ⚠️ You can refer to [the instructions for sending telemetry to MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#send-telemetry-from-your-iot-device).
|
||||
|
||||
1. Create some local server code to subscribe to telemetry and send a command to control the relay in a folder called `soil-moisture-sensor-server`. Name the property in the command message `relay_on`, and set the client ID as `soilmoisturesensor_server` prefixed by your ID. Keep the same structure as the server code you wrote for project 1, lesson 4 as you will be adding to this code later in this lesson.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for for sending telemetry to MQTT](../../../1-getting-started/lessons/4-connect-internet/README.md#write-the-server-code) and [sending commands over MQTT](../../../1-getting-started/lessons/4-connect-internet/README.md#send-commands-to-the-mqtt-broker) in project 1, lesson 4 if needed.
|
||||
> ⚠️ You can refer to [the instructions for sending telemetry to MQTT](../../../1-getting-started/lessons/4-connect-internet/README.md#write-the-server-code) and [sending commands over MQTT](../../../1-getting-started/lessons/4-connect-internet/README.md#send-commands-to-the-mqtt-broker) in project 1, lesson 4 if needed.
|
||||
|
||||
1. Add the relevant device code to control the relay from received commands, using the `relay_on` property from the message. Send true for `relay_on` if the `soil_moisture` is greater than 450, otherwise send false, the same as the logic you added for the IoT device earlier.
|
||||
|
||||
> ⚠️ You can refer to [the instructions for for responding to commands from MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#handle-commands-on-the-iot-device).
|
||||
> ⚠️ You can refer to [the instructions for responding to commands from MQTT in project 1, lesson 4 if needed](../../../1-getting-started/lessons/4-connect-internet/README.md#handle-commands-on-the-iot-device).
|
||||
|
||||
> 💁 You can find this code in the [`code-mqtt`](./code-mqtt) folder.
|
||||
> 💁 You can find this code in the [code-mqtt](./code-mqtt) folder.
|
||||
|
||||
Make sure the code is running on your device and local server, and test it out by changing soil moisture levels, either by changing the values sent by the virtual sensor, or by changing the moisture levels of the soil by adding water or removing the sensor from the soil.
|
||||
|
||||
## Sensor and actuator timing
|
||||
|
||||
Back in lesson 3 you built a nightlight - an LED that turned on as soon as a low level of light was detected by a light sensor. The light sensor detected a change in light levels instantly, and the device was able to respond quickly, only limited by the length of the delay in the `loop` function or `while True:` loop. As an IoT developer, you can't always rely on such a fast feedback loop.
|
||||
Back in lesson 3 you built a nightlight - an LED that turns on as soon as a low level of light was detected by a light sensor. The light sensor detected a change in light levels instantly, and the device was able to respond quickly, only limited by the length of the delay in the `loop` function or `while True:` loop. As an IoT developer, you can't always rely on such a fast feedback loop.
|
||||
|
||||
### Timing for soil moisture
|
||||
|
||||
|
@ -146,7 +146,7 @@ Imagine you have been tasked with building an irrigation system for a farm. Base
|
|||
|
||||
You could program the device in the same way as the nightlight - all the time the sensor reads above 450, turn on a relay to turn on a pump. The problem is that water takes a while to get from the pump, through the soil to the sensor. The sensor will stop the water when it detects a level of 450, but the water level will continue dropping as the pumped water keeps soaking through the soil. The end result is wasted water, and the risk of root damage.
|
||||
|
||||
✅ Remember - too mch water can be as bad for plants as too little, and wastes a precious resource.
|
||||
✅ Remember - too much water can be as bad for plants as too little, and wastes a precious resource.
|
||||
|
||||
The better solution is to understand that there is a delay between the actuator turning on and the property that the sensor reads changing. This means not only should the sensor wait for a while before measuring the value again, but the actuator needs to turn off for a while before the next sensor measurement is taken.
|
||||
|
||||
|
@ -156,7 +156,7 @@ How long should the relay be on each time? It's better to err on the side of cau
|
|||
|
||||
![A strawberry plant connected to water via a pump, with the pump connected to a relay. The relay and a soil moisture sensor in the plant are both connected to a Raspberry Pi](../../../images/strawberry-with-pump.png)
|
||||
|
||||
For example, I have a strawberry plant with a soil moisture sensor and a pump controlled by a relay.I've observed that when I add water it takes about 20 seconds for the soil moisture reading to stabilize. This means I need to turn the relay off and wait 20 seconds before checking the moisture levels. I'd rather have too little water than too much - I can always turn the pump on again, but I can't take water out of the plant.
|
||||
For example, I have a strawberry plant with a soil moisture sensor and a pump controlled by a relay. I've observed that when I add water it takes about 20 seconds for the soil moisture reading to stabilize. This means I need to turn the relay off and wait 20 seconds before checking the moisture levels. I'd rather have too little water than too much - I can always turn the pump on again, but I can't take water out of the plant.
|
||||
|
||||
![Step 1, take measurement. Step 2, add water. Step 3, wait for water to soak through the soil. Step 4, retake measurement](../../../images/soil-moisture-delay.png)
|
||||
|
||||
|
@ -181,7 +181,7 @@ The server code can be modified to add control around the timing of the watering
|
|||
|
||||
1. Telemetry message received
|
||||
1. Check the soil moisture level
|
||||
1. if it's ok, do nothing. If the reading is too high (meaning the soil moisture is too low) then:
|
||||
1. If it's ok, do nothing. If the reading is too high (meaning the soil moisture is too low) then:
|
||||
1. Send a command to turn the relay on
|
||||
1. Wait for 5 seconds
|
||||
1. Send a command to turn the relay off
|
||||
|
@ -206,6 +206,13 @@ Update your server code to run the relay for 5 seconds, then wait 20 seconds.
|
|||
|
||||
1. Open the `app.py` file
|
||||
|
||||
1. Add the following code to the `app.py` file below the existing imports:
|
||||
```python
|
||||
import threading
|
||||
```
|
||||
|
||||
This statement imports `threading` from Python libraries, threading allows python to execute other code while waiting.
|
||||
|
||||
1. Add the following code before the `handle_telemetry` function that handles telemetry messages received by the server code:
|
||||
|
||||
```python
|
||||
|
@ -271,6 +278,8 @@ Update your server code to run the relay for 5 seconds, then wait 20 seconds.
|
|||
```
|
||||
|
||||
A good way to test this in a simulated irrigation system is to use dry soil, then pour water in manually whilst the relay is on, stopping pouring when the relay turns off.
|
||||
|
||||
> 💁 You can find this code in the [code-timing](./code-timing) folder.
|
||||
|
||||
> 💁 If you want to use a pump to build a real irrigation system, then you can use a [6V water pump](https://www.seeedstudio.com/6V-Mini-Water-Pump-p-1945.html) with a [USB terminal power supply](https://www.adafruit.com/product/3628). Make sure the power to or from the pump is connected via the relay.
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ This lesson covered how to control a relay via sensor data, and that relay could
|
|||
|
||||
For this assignment you will calculate how long the pump should run for a particular rise in soil moisture.
|
||||
|
||||
> ⚠️ If you are using virtual IoT hardware, you can work through this process, but simulate the results by increasing the soil moisture reading manually by a fixed amount per second the relay us on.
|
||||
> ⚠️ If you are using virtual IoT hardware, you can work through this process, but simulate the results by increasing the soil moisture reading manually by a fixed amount per second the relay is on.
|
||||
|
||||
1. Start with dry soil. Measure the soil moisture.
|
||||
|
||||
|
@ -37,4 +37,4 @@ For this assignment you will calculate how long the pump should run for a partic
|
|||
| Criteria | Exemplary | Adequate | Needs Improvement |
|
||||
| -------- | --------- | -------- | ----------------- |
|
||||
| Capture soil moisture date | Is able to capture multiple readings after adding fixed quantities of water | Is able to capture some readings with fixed quantities of water | Can only capture one or two readings, or is unable to use fixed quantities of water |
|
||||
| Calibrate the server code | Is able to calculate an average decrease in soil moisture reading and update the serve code to use this | Is able to calculate an average decrease, but cannot update the server code, or is unable to correctly calculate an average, but uses this value to correctly update the server code | Is unable to calculate an average, or update the server code |
|
||||
| Calibrate the server code | Is able to calculate an average decrease in soil moisture reading and update the server code to use this | Is able to calculate an average decrease, but cannot update the server code, or is unable to correctly calculate an average, but uses this value to correctly update the server code | Is unable to calculate an average, or update the server code |
|
||||
|
|
|
@ -78,7 +78,7 @@ Now that the relay is working, it can be controlled in response to soil moisture
|
|||
|
||||
Control the relay.
|
||||
|
||||
1. Delete the 3 lines of code that you added to test the relay. Replace them with the following code in its place:
|
||||
1. Delete the 3 lines of code that you added to test the relay. Replace them with the following code:
|
||||
|
||||
```python
|
||||
if soil_moisture > 450:
|
||||
|
@ -89,11 +89,11 @@ Control the relay.
|
|||
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.
|
||||
This code checks the soil moisture level from the soil moisture sensor. If it is above 450, it turns on the relay, and turns it off when it goes below 450.
|
||||
|
||||
> 💁 Remember the capacitive soil moisture sensor reads lower the more moisture there is in the soil.
|
||||
> 💁 Remember the capacitive soil moisture sensor reads the lower the soil moisture level, the more moisture there is in the soil and vice versa.
|
||||
|
||||
1. Run the Python app. You will see the relay turn on or off depending on the soil moisture levels. Try in dry soil, then add water.
|
||||
1. Run the Python app. You will see the relay turn on or off depending on the soil moisture level. Try in dry soil, then add water.
|
||||
|
||||
```output
|
||||
Soil Moisture: 638
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Control a relay - Virtual IoT Hardware
|
||||
|
||||
In this part of the lesson, you will add a relay to your Raspberry Pi in addition to the soil moisture sensor, and control it based off the soil moisture level.
|
||||
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.
|
||||
|
||||
## Virtual Hardware
|
||||
|
||||
|
@ -93,9 +93,9 @@ Control the relay.
|
|||
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.
|
||||
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 lower the more moisture there is in the soil.
|
||||
> 💁 Remember the capacitive soil moisture sensor reads the lower the soil moisture level, the more moisture there is in the soil and vice versa.
|
||||
|
||||
1. 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.
|
||||
|
||||
|
|
|
@ -70,7 +70,7 @@ Now that the relay is working, it can be controlled in response to soil moisture
|
|||
|
||||
Control the relay.
|
||||
|
||||
1. Delete the 3 lines of code that you added to test the relay. Replace them with the following code in its place:
|
||||
1. Delete the 3 lines of code that you added to test the relay. Replace them with the following code:
|
||||
|
||||
```cpp
|
||||
if (soil_moisture > 450)
|
||||
|
@ -85,13 +85,13 @@ Control the relay.
|
|||
}
|
||||
```
|
||||
|
||||
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.
|
||||
This code checks the soil moisture level from the soil moisture sensor. If it is above 450, it turns on the relay, and turns it off when it goes below 450.
|
||||
|
||||
> 💁 Remember the capacitive soil moisture sensor reads lower the more moisture there is in the soil.
|
||||
> 💁 Remember the capacitive soil moisture sensor reads the lower the soil moisture level, the more moisture there is in the soil and vice versa.
|
||||
|
||||
1. Build and upload the code to the Wio Terminal.
|
||||
|
||||
1. Monitor the device via the serial monitor. You will see the relay turn on or off depending on the soil moisture levels. Try in dry soil, then add water.
|
||||
1. Monitor the device via the serial monitor. You will see the relay turn on or off depending on the soil moisture level. Try in dry soil, then add water.
|
||||
|
||||
```output
|
||||
Soil Moisture: 638
|
||||
|
|
Двоичные данные
images/counterfit-relay.png
Двоичные данные
images/counterfit-relay.png
Двоичный файл не отображается.
До Ширина: | Высота: | Размер: 26 KiB После Ширина: | Высота: | Размер: 25 KiB |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 61 KiB |
Загрузка…
Ссылка в новой задаче