diff --git a/2-farm/lessons/2-detect-soil-moisture/assignment.md b/2-farm/lessons/2-detect-soil-moisture/assignment.md deleted file mode 100644 index b7959d5a..00000000 --- a/2-farm/lessons/2-detect-soil-moisture/assignment.md +++ /dev/null @@ -1,47 +0,0 @@ -# Calibrate your sensor - -## Instructions - -In this lesson you gathered soil moisture sensor readings, measured as values from 0-1023. To convert these into actual soil moisture readings, you need to calibrate your sensor. You can do this by taking readings from soil samples, then calculating the gravimetric soil moisture content from these samples. - -You will need to repeat these steps multiple times to get the readings needed, with different wetness of soil each time. - -1. Take a soil moisture reading using the soil moisture sensor. Write down this reading. - -1. Take a sample of the soil, and weigh it. Write down this weight. - -1. Dry the soil - a warm oven at 110°C (230°F) for a few hours is the best way, you can do this in sunlight, or place it in a warm, dry place until the soil is completely dry. It should be powdery and loose. - - > 💁 In a lab for the most accurate results you would dry in an oven for 48-72 hours. If you have drying ovens at your school, see if you can use these to dry for longer. The longer, the more dry the sample and the more accurate the results. - -1. Weigh the soil again. - - > 🔥 If you dried it in an oven make sure it has cooled down first! - -The gravimetric soil moisture is calculated as: - -![soil moisture % is weight wet minus weight dry, divided by weight dry, times 100](../../../images/gsm-calculation.png) - -* Wwet - the weight of the wet soil -* Wdry - the weight of the dry soil - -For example, say you have a soil sample that weights 212g wet, and 197g dry. - -![The calculation filled in](../../../images/gsm-calculation-example.png) - -* Wwet = 212g -* Wdry = 197g -* 212 - 197 = 15 -* 15 / 197 = 0.076 -* 0.076 * 100 = 7.6% - -In this example, the soil has a gravimetric soil moisture of 7.6%. - -Once you have the readings for at least 3 samples, plot a graph of soil moisture % to soil moisture sensor reading and add line to best fit the points. You can then use this to calculate the gravimetric soil moisture content for a given sensor reading by reading the value from the line. - -## Rubric - -| Criteria | Exemplary | Adequate | Needs Improvement | -| -------- | --------- | -------- | ----------------- | -| Gather calibration data | Capture at least 3 calibration samples | Capture at least 2 calibration samples |Capture at least 1 calibration sample | -| Make a calibrated reading | Successfully plot the calibration graph and make a reading from the sensor, and convert it to gravimetric soil moisture content | Successfully plot the calibration graph | Not able to plot the graph | diff --git a/2-farm/lessons/2-detect-soil-moisture/virtual-device-soil-moisture.md b/2-farm/lessons/2-detect-soil-moisture/virtual-device-soil-moisture.md deleted file mode 100644 index ddceaead..00000000 --- a/2-farm/lessons/2-detect-soil-moisture/virtual-device-soil-moisture.md +++ /dev/null @@ -1,109 +0,0 @@ -# Measure soil moisture - Virtual IoT Hardware - -In this part of the lesson, you will add a capacitive soil moisture sensor to your virtual IoT device, and read values from it. - -## Virtual Hardware - -The virtual IoT device will use a simulated Grove capacitive soil moisture sensor. This keeps this lab the same as using a Raspberry Pi with a physical Grove capacitive soil moisture sensor. - -In a physical IoT device, the soil moisture sensor would be a capacitive sensor that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases. - -This is an analog sensor, so uses a simulated 10-bit ADC to report a value from 1-1,023. - -### Add the soil moisture sensor to CounterFit - -To use a virtual soil moisture sensor, you need to add it to the CounterFit app - -#### Task - dd the soil moisture sensor to CounterFit - -Add the soil moisture sensor to the CounterFit app. - -1. Create a new Python app on your computer in a folder called `soil-moisture-sensor` with a single file called `app.py` and a Python virtual environment, and add the CounterFit pip packages. - - > ⚠️ You can refer to [the instructions for creating and setting up a CounterFit Python project in lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/virtual-device.md). - -1. Make sure the CounterFit web app is running - -1. Create a soil moisture sensor: - - 1. In the *Create sensor* box in the *Sensors* pane, drop down the *Sensor type* box and select *Soil Moisture*. - - 1. Leave the *Units* set to *NoUnits* - - 1. Ensure the *Pin* is set to *0* - - 1. Select the **Add** button to create the humidity sensor on Pin 0 - - ![The soil moisture sensor settings](../../../images/counterfit-create-soil-moisture-sensor.png) - - The soil moisture sensor will be created and appear in the sensors list. - - ![The soil moisture sensor created](../../../images/counterfit-soil-moisture-sensor.png) - -## Program the soil moisture sensor app - -The soil moisture sensor app can now be programmed using the CounterFit sensors. - -### Task - program the soil moisture sensor app - -Program the soil moisture sensor app. - -1. Make sure the `soil-moisture-sensor` app is open in VS Code - -1. Open the `app.py` file - -1. Add the following code to the top of `app.py` to connect the app to CounterFit: - - ```python - from counterfit_connection import CounterFitConnection - CounterFitConnection.init('127.0.0.1', 5000) - ``` - -1. Add the following code to the `app.py` file to import some required libraries: - - ```python - import time - from counterfit_shims_grove.adc import ADC - ``` - - The `import time` statement imports the `time` module that will be used later in this assignment. - - The `from counterfit_shims_grove.adc import ADC` statement imports the `ADC` class to interact with a virtual analog to digital converter that can connect to a CounterFit sensor. - -1. Add the following code below this to create an instance of the `ADC` class: - - ```python - adc = ADC() - ``` - -1. Add an infinite loop that reads from this ADC on pin 0 and write the result to the console. This loop can then sleep for 10 seconds between reads. - - ```python - while True: - soil_moisture = adc.read(0) - print("Soil moisture:", soil_moisture) - - time.sleep(10) - ``` - -1. From the CounterFit app, change the value of the soil moisture sensor that will be read by the app. You can do this in one of two ways: - - * Enter a number in the *Value* box for the soil moisture sensor, then select the **Set** button. The number you enter will be the value returned by the sensor. - - * Check the *Random* checkbox, and enter a *Min* and *Max* value, then select the **Set** button. Every time the sensor reads a value, it will read a random number between *Min* and *Max*. - -1. Run the Python app. You will see the soil moisture measurements written to the console. Change the *Value* or the *Random* settings to see the value change. - - ```output - (.venv) ➜ soil-moisture-sensor $ python app.py - Soil moisture: 615 - Soil moisture: 612 - Soil moisture: 498 - Soil moisture: 493 - Soil moisture: 490 - Soil Moisture: 388 - ``` - -> 💁 You can find this code in the [code/virtual-device](code/virtual-device) folder. - -😀 Your soil moisture sensor program was a success! diff --git a/2-farm/lessons/2-detect-soil-moisture/wio-terminal-soil-moisture.md b/2-farm/lessons/2-detect-soil-moisture/wio-terminal-soil-moisture.md deleted file mode 100644 index 6697d316..00000000 --- a/2-farm/lessons/2-detect-soil-moisture/wio-terminal-soil-moisture.md +++ /dev/null @@ -1,103 +0,0 @@ -# Measure soil moisture - Wio Terminal - -In this part of the lesson, you will add a capacitive soil moisture sensor to your Wio Terminal, and read values from it. - -## Hardware - -The Wio Terminal needs a capacitive soil moisture sensor. - -The sensor you'll use is a [Capacitive Soil Moisture Sensor](https://www.seeedstudio.com/Grove-Capacitive-Moisture-Sensor-Corrosion-Resistant.html), that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases. - -This is an analog sensor, so connects to analog pins on the Wio Terminal, using an onboard ADC to create a value from 0-1,023. - -### Connect the soil moisture sensor - -The Grove soil moisture sensor can be connected to the Wio Terminals configurable analog/digital port. - -#### Task - connect the soil moisture sensor - -Connect the soil moisture sensor. - -![A grove soil moisture sensor](../../../images/grove-capacitive-soil-moisture-sensor.png) - -1. Insert one end of a Grove cable into the socket on the soil moisture sensor. It will only go in one way round. - -1. With the Wio Terminal disconnected from your computer or other power supply, connect the other end of the Grove cable to the right-hand side Grove socket on the Wio Terminal as you look at the screen. This is the socket farthest away from the power button. - -![The grove soil moisture sensor connected to the right hand socket](../../../images/wio-soil-moisture-sensor.png) - -1. Insert the soil moisture sensor into soil. It has a 'highest position line' - a white line across the sensor. Insert the sensor up to but not past this line. - -![The grove soil moisture sensor in soil](../../../images/soil-moisture-sensor-in-soil.png) - -1. You can now connect the Wio Terminal to your computer. - -## Program the soil moisture sensor - -The Wio Terminal can now be programmed to use the attached soil moisture sensor. - -### Task - program the soil moisture sensor - -Program the device. - -1. Create a brand new Wio Terminal project using PlatformIO. Call this project `soil-moisture-sensor`. Add code in the `setup` function to configure the serial port. - - > ⚠️ You can refer to [the instructions for creating a PlatformIO project in project 1, lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/wio-terminal.md#create-a-platformio-project). - -1. There isn't a library for this sensor, instead you can read from the analog pin using the built in Arduino [`analogRead`](https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) function. Start by configuring the analog pin for input so values can be read from it by adding the following to the `setup` function. - - ```cpp - pinMode(A0, INPUT); - ``` - - This sets the `A0` pin, the combined analog/digital pin, as an input pin that voltage can be read from. - -1. Add the following to the `loop` function to read the voltage from this pin: - - ```cpp - int soil_moisture = analogRead(A0); - ``` - -1. Below this code, add the following code to print the value to the serial port: - - ```cpp - Serial.print("Soil Moisture: "); - Serial.println(soil_moisture); - ``` - -1. Finally add a delay at the end of 10 seconds: - - ```cpp - delay(10000); - ``` - -1. Build and upload the code to the Wio Terminal. - - > ⚠️ You can refer to [the instructions for creating a PlatformIO project in project 1, lesson 1 if needed](../../../1-getting-started/lessons/1-introduction-to-iot/wio-terminal.md#write-the-hello-world-app). - -1. Once uploaded, you can monitor the soil moisture using the serial monitor. Add some water to the soil, or remove the sensor from the soil, and see the value change. - - ```output - > Executing task: platformio device monitor < - - --- Available filters and text transformations: colorize, debug, default, direct, hexlify, log2file, nocontrol, printable, send_on_enter, time - --- More details at http://bit.ly/pio-monitor-filters - --- Miniterm on /dev/cu.usbmodem1201 9600,8,N,1 --- - --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- - Soil Moisture: 526 - Soil Moisture: 529 - Soil Moisture: 521 - Soil Moisture: 494 - Soil Moisture: 454 - Soil Moisture: 456 - Soil Moisture: 395 - Soil Moisture: 388 - Soil Moisture: 394 - Soil Moisture: 391 - ``` - - In the example output above, you can see the voltage drop as water is added. - -> 💁 You can find this code in the [code/wio-terminal](code/wio-terminal) folder. - -😀 Your soil moisture sensor program was a success! diff --git a/4-manufacturing/lessons/3-run-fruit-detector-edge/README.md b/4-manufacturing/lessons/3-run-fruit-detector-edge/README.md index 120c1d18..9c463298 100644 --- a/4-manufacturing/lessons/3-run-fruit-detector-edge/README.md +++ b/4-manufacturing/lessons/3-run-fruit-detector-edge/README.md @@ -12,16 +12,194 @@ This video gives an overview of running image classifiers on IoT devices, the to ## Introduction -In this lesson you will learn about +In this lesson you will learn about how to run machine learning (ML) models on the edge. We'll cover the benefits and drawbacks of edge computing versus cloud computing + In this lesson we'll cover: -* [Thing 1](#thing-1) +* [Comparison of Edge and Cloud Computing](#Comparison-of-Edge-and-Cloud-Computing) +* [How to register an edge device in Azure IoT Edge](#Azure-IoT-Edge) +* [How to configure a Custom Vision classifier for the edge](#Task----Create-a-CV-classifier-that-can-run-on-the-edge) +* [How to run the CV classifier on the edge via IoT Edge](#Run-your-classifier-on-the-edge) + +## Comparison of Edge and Cloud Computing +Like most technologies, there are pros and cons to both edge computing and cloud computing. + +Edge computing may be preferred over cloud computing for four primary reasons: +1. **Speed**: edge computing is ideal for time-sensitive data as actions are done on the device, enabling higher speeds and requiring less network traffic; +2. **Remote accessibility**: local compute is preferred in areas with limited or no connectivity to a centralized location; +3. **Lower costs**: Performing data collection, storage, analysis, and triggering actions on the device reduces usage of cloud services which can reduce the overall cost of an IoT system; and +3. **Privacy and security**: with edge compute, data stays on the device and is not uploaded to the cloud. This is often preferred for sensitive and personally identifiable information, especially because data does not need to be saved on the device, which greatly reduces the risk of data leaks. + +However, there are also situations in which cloud computing may be preferred. The main reasons include: +1. Scale and flexibility: Cloud computing can adjust to network and data needs in real-time by adding or reducing servers and other resources; +1. Reliability and resiliency: Cloud computing leverages multiple servers often in multiple locations for redundancy and disaster recovery; and +1. Maintenance: Cloud service providers provide system maintenance and updates. -## Thing 1 +For IoT systems, you'll often want a blend of cloud and edge computing, leveraging each service based on the needs of the system, its customers, and its maintainers. --- +## Azure IoT Edge + +![The Azure IoT Edge logo](../../../images/azure-iot-edge-logo.png) + +Azure IoT Edge can make your IoT solution more efficient by moving workloads out of the cloud and to the edge. This capability lends itself well to services that process a lot of data, like computer vision models. + +IoT Edge runs code from containers. You can use Custom Vision to build custom image classifiers and deploy them to devices as containers. Together, these two services enable you to find insights from images or video streams without having to transfer all of the data to the cloud. + +## Register an IoT Edge device +![The Azure IoT Edge Diagram: Step 1 - Register an IoT Edge Device](../../images/register-device.png) + +To use an IoT Edge device, it needs to be registered in IoT Hub. + +### Task - register an IoT Edge device + +1. Create an IoT Hub in the `fruit-quality-detector` resource group. Give it a unique name based around `fruit-quality-detector`. + +1. Register an IoT Edge device called `fruit-quality-detector-edge` in your IoT Hub. The command to do this is similar to the one used to register a non-edge device, except you pass the `--edge-enabled` flag. + + ```sh + az iot hub device-identity create --edge-enabled \ + --device-id fruit-quality-detector-edge \ + --hub-name + ``` + + Replace `` with the name of your IoT Hub. + +1. Get the connection string for your device using the following command: + + ```sh + az iot hub device-identity connection-string show --device-id fruit-quality-detector-edge \ + --output table \ + --hub-name + ``` + + Replace `` with the name of your IoT Hub. + + Take a copy of the connection string that is shown in the output. + +## Set up an IoT Edge device + +### Task - Install and start the IoT Edge Runtime +![The Azure IoT Edge Diagram: Step 2 - Install and start the IoT Edge Runtime](/../../images/start-runtime.png) + +**The IoT Edge runtime only runs Linux containers.** It can be run on Linux, or on Windows using Linux Virtual Machines. + +* If you are using a Raspberry Pi as your IoT device, then this runs a supported version of Linux and can host the IoT Edge runtime. Follow the [Install Azure IoT Edge for Linux guide on Microsoft docs](https://docs.microsoft.com/azure/iot-edge/how-to-install-iot-edge?WT.mc_id=academic-17441-jabenn) to install IoT Edge and set the connection string. + + > 💁 Remember, Raspberry Pi OS is a variant of Debian Linux. + +* If you are not using a Raspberry Pi, but have a Linux computer, you can run the IoT Edge runtime. Follow the [Install Azure IoT Edge for Linux guide on Microsoft docs](https://docs.microsoft.com/azure/iot-edge/how-to-install-iot-edge?WT.mc_id=academic-17441-jabenn) to install IoT Edge and set the connection string. + +* If you are using Windows, you can install the IoT Edge runtime in a Linux Virtual Machine by following the [Install and start the IoT Edge runtime section of the Deploy your first IoT Edge module to a Windows device quickstart on Microsoft docs](https://docs.microsoft.com/azure/iot-edge/quickstart?WT.mc_id=academic-17441-jabenn#install-and-start-the-iot-edge-runtime). You can stop when you reach the *Deploy a module* section. + +* If you are using macOS, you can create a virtual machine (VM) in the cloud to use for your IoT Edge device. These are computers you can create in the cloud and access over the internet. You can create a Linux VM that has IoT Edge installed. Follow the [Create a virtual machine running IoT Edge guide](vm-iotedge.md) for instructions on how to do this. + +#### Troubleshooting tips: +##### Virtual Machine: +1. Uninstall and try again + + ```sh + . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; + Uninstall-IoTEdge + ``` +1. More info: [Install Azure IoT Edge for Windows](https://docs.microsoft.com/en-us/azure/iot-edge/how-to-install-iot-edge-windows-on-windows?view=iotedge-2018-06#uninstall-iot-edge) + +##### Raspberry Pi: +1. Uninstall and try again: + + ```sh + sudo apt-get remove aziot-edge + ``` + When the IoT Edge runtime is removed, any containers that it created are stopped but still exist on your device. View all containers to see which ones remain: + + ```sh + sudo docker ps -a + ``` + + Delete the containers from your device, including the two runtime containers: + + ```sh + sudo docker rm -f + ``` + + Finally, remove the container runtime from your device. + + ```sh + sudo apt-get remove --purge moby-cli + sudo apt-get remove --purge moby-engine + ``` +2. More info: [Install Azure IoT Edge for Windows](https://docs.microsoft.com/en-us/azure/iot-edge/how-to-install-iot-edge?WT.mc_id=academic-17441-jabenn&view=iotedge-2020-11#uninstall-iot-edge) + +## Task -- Create a CV classifier that can run on the edge +Since edge devices tend to have smaller amounts of storage space, we'll need to create a new Custom Vision image classification model that is compact. + +1. Login to your Custom Vision account +1. Open your Fruit Classifier project. +1. Click on 'Settings' (the ⚙ icon) +1. Change the Domain type to General (compact) domain + + *Note: be sure to select the 'General (compact)' domain and **NOT** the 'General (compact) [S1]' domain* + +1. Under 'Export Capabilities', select 'Basic platforms (Tensorflow, CoreML, ONNX, ...)' + +1. At the bottom of the Settings page, click 'Save Changes'. +1. Nagivate back to the Perfomance tab on the Custom Vision site and retrain your model using the Quick Training option. +1. click the 'Export' button at the top. +1. Choose the export option that matches your edge device: + * For a Virtual Machine, select 'Dockerfile' and 'Linux' platform. + * For a Raspberry Pi, select 'Dockerfile' and the 'ARM (Raspberry Pi 3)' platform. +1. Save the files to your computer, then unzip the folder. + +## Run your classifier on the edge +Next, we need to load the CV image classifier onto our edge device. +### Task - deploy your classifier using IoT Edge +To deploy our classifier, we'll need a service called [Docker](https://www.docker.com/). + +* [Download Docker for the Raspberry Pi here](https://docs.docker.com/engine/install/debian/) +* [Download Docker for Linux here](https://docs.docker.com/engine/install/) + +#### Raspberry Pi Instructions +⚠ **Important:** Be sure to use 'sudo' each time you run a docker command, otherwise you'll get a 'permission denied' error. + +1. On your Pi, create a folder in your home directory called ```iot-edge-docker``` +1. Transfer your CV export files from your PC into the Raspberry Pi ```iot-edge-docker``` folder. +1. In the Raspberry Pi terminal, navigate to the ```iot-edge-docker``` folder. B +1. Build the docker image with the following command, replacing with ```fruit-detector-classifier```: + + ```sh + sudo docker build -t . + ``` +1. Run the container using the following command: + + ```sh + sudo docker run -p 127.0.0.1:80:80 -d fruit-detector-classifier + ``` + +1. Check the images Docker created: + + ```sh + sudo docker images + ``` +1. Test the container with one of the test images: + + ```sh + curl -X POST http://127.0.0.1/url -d '{ "url": "https://raw.githubusercontent.com/microsoft/IoT-For-Beginners/main/4-manufacturing/lessons/1-train-fruit-detector/images/testing/ripe/banana-ripe-1.png?token=AB7YU53D4PFAEZDJHKXKXLDA5FAOE"}' + ``` +1. You should see an output like the following: + + ```sh + {"created":"2021-07-03T06:41:19.706346","id":"","iteration":"","predictions":[{"boundingBox":null,"probability":1.0,"tagId":"","tagName":"ripe"}],"project":""} + ``` + 💁‍♀️```tagName``` is the prediction for the image we provided. ```probability``` is the model prediction estimate of its accuracy. + +🎉 Congratulations! You've successfully deployed your Custom Vison model to your Raspberry Pi edge device! + +#### Linux Virtual Machine Instructions + +### Task - Use the edge classifier from your IoT device + ## 🚀 Challenge ## Post-lecture quiz