Merge branch 'microsoft:main' into 1.3-Text-fix

pull/59/head
Mohammad Iftekher Ebne Jalal 4 years ago
commit 430927b040

@ -1,20 +0,0 @@
from counterfit_shims_grove.counterfit_connection import CounterFitConnection
import time
from counterfit_shims_grove.grove_light_sensor_v1_2 import GroveLightSensor
from counterfit_shims_grove.grove_led import GroveLed
CounterFitConnection.init('127.0.0.1', 5000)
light_sensor = GroveLightSensor(0)
led = GroveLed(5)
while True:
light = light_sensor.light
print('Light level:', light)
if light < 200:
led.on()
else:
led.off()
time.sleep(1)

@ -12,7 +12,7 @@ If you are using a Raspberry Pi as your IoT hardware, you have two choices - you
Before you begin, you also need to connect the Grove Base Hat to your Pi. Before you begin, you also need to connect the Grove Base Hat to your Pi.
### Task ### Task - setup
Install the Grove base hat on your Pi and configure the Pi Install the Grove base hat on your Pi and configure the Pi
@ -29,7 +29,7 @@ Install the Grove base hat on your Pi and configure the Pi
If you want to work directly on your Pi, you can use the desktop version of Raspberry Pi OS and install all the tools you need. If you want to work directly on your Pi, you can use the desktop version of Raspberry Pi OS and install all the tools you need.
#### Task #### Task - work directly on your Pi
Set up your Pi for development. Set up your Pi for development.
@ -79,7 +79,7 @@ Rather than coding directly on the Pi, it can run 'headless', that is not connec
To code remotely, the Pi OS needs to be installed on an SD Card. To code remotely, the Pi OS needs to be installed on an SD Card.
##### Task ##### Task - set up the Pi OS
Set up the headless Pi OS. Set up the headless Pi OS.
@ -115,7 +115,7 @@ The OS will be written to the SD card, and once compete the card will be ejected
The next step is to remotely access the Pi. You can do this using `ssh`, which is available on macOS, Linux and recent versions of Windows. The next step is to remotely access the Pi. You can do this using `ssh`, which is available on macOS, Linux and recent versions of Windows.
##### Task ##### Task - connect to the Pi
Remotely access the Pi. Remotely access the Pi.
@ -147,7 +147,7 @@ Remotely access the Pi.
Once you are connected to the Pi, you need to ensure the OS is up to date, and install various libraries and tools that interact with the Grove hardware. Once you are connected to the Pi, you need to ensure the OS is up to date, and install various libraries and tools that interact with the Grove hardware.
##### Task ##### Task - configure software on the Pi
Configure the installed Pi software and install the Grove libraries. Configure the installed Pi software and install the Grove libraries.
@ -179,7 +179,7 @@ Configure the installed Pi software and install the Grove libraries.
Once the Pi is configured, you can connect to it using Visual Studio Code (VS Code) from your computer - this is a free developer text editor you will be using to write your device code in Python. Once the Pi is configured, you can connect to it using Visual Studio Code (VS Code) from your computer - this is a free developer text editor you will be using to write your device code in Python.
##### Task ##### Task - configure VS Code for remote access
Install the required software and connect remotely to your Pi. Install the required software and connect remotely to your Pi.
@ -199,7 +199,7 @@ The Hello World app for the Pi will ensure that you have Python and Visual Studi
This app will be in a folder called `nightlight`, and it will be re-used with different code in later parts of this assignment to build the nightlight application. This app will be in a folder called `nightlight`, and it will be re-used with different code in later parts of this assignment to build the nightlight application.
### Task ### Task - hello world
Create the Hello World app. Create the Hello World app.

@ -120,7 +120,7 @@ Create a Python application to print `"Hello World"` to the console.
code . code .
``` ```
> 💁 If your terminal returns `command not found` on macOS it means VS Code has not been added to PATH, you can add VS Code to PATH by following the instructions in the [Launching from the command line section of the VS Code documentation](https://code.visualstudio.com/docs/setup/mac?WT.mc_id=academic-17441-jabenn#_launching-from-the-command-line) and run the command afterwards. VS Code is installed to PATH by default on Windows and Linux. > 💁 If your terminal returns `command not found` on macOS it means VS Code has not been added to your PATH. You can add VS Code to yout PATH by following the instructions in the [Launching from the command line section of the VS Code documentation](https://code.visualstudio.com/docs/setup/mac?WT.mc_id=academic-17441-jabenn#_launching-from-the-command-line) and run the command afterwards. VS Code is installed to your PATH by default on Windows and Linux.
1. When VS Code launches, it will activate the Python virtual environment. You will see this in the bottom status bar: 1. When VS Code launches, it will activate the Python virtual environment. You will see this in the bottom status bar:

@ -8,7 +8,7 @@ The [Wio Terminal from Seeed Studios](https://www.seeedstudio.com/Wio-Terminal-p
To use your Wio Terminal, you will need to install some free software on your computer. You will also need to update the Wio Terminal firmware before you can connect it to WiFi. To use your Wio Terminal, you will need to install some free software on your computer. You will also need to update the Wio Terminal firmware before you can connect it to WiFi.
### Task ### Task - setup
Install the required software and update the firmware. Install the required software and update the firmware.
@ -32,7 +32,7 @@ The Hello World app for the Wio Terminal will ensure that you have Visual Studio
The first step is to create a new project using PlatformIO configured for the Wio Terminal. The first step is to create a new project using PlatformIO configured for the Wio Terminal.
#### Task #### Task - create a PlatformIO project
Create the PlatformIO project. Create the PlatformIO project.
@ -122,7 +122,7 @@ The VS Code explorer will show a number of files and folders created by the Plat
You're now ready to write the Hello World app. You're now ready to write the Hello World app.
#### Task #### Task - write the Hello World app
Write the Hello World app. Write the Hello World app.

@ -1,15 +1,15 @@
import time import time
from grove.grove_light_sensor_v1_2 import GroveLightSensor import seeed_si114x
from grove.grove_led import GroveLed from grove.grove_led import GroveLed
light_sensor = GroveLightSensor(0) light_sensor = seeed_si114x.grove_si114x()
led = GroveLed(5) led = GroveLed(5)
while True: while True:
light = light_sensor.light light = light_sensor.ReadVisible
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -12,7 +12,7 @@ while True:
light = light_sensor.light light = light_sensor.light
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -12,7 +12,7 @@ while True:
light = light_sensor.light light = light_sensor.light
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -19,7 +19,7 @@ void loop()
Serial.print("Light value: "); Serial.print("Light value: ");
Serial.println(light); Serial.println(light);
if (light < 200) if (light < 300)
{ {
digitalWrite(D0, HIGH); digitalWrite(D0, HIGH);
} }

@ -1,10 +1,9 @@
import time import time
from grove.grove_light_sensor_v1_2 import GroveLightSensor import seeed_si114x
light_sensor = GroveLightSensor(0) light_sensor = seeed_si114x.grove_si114x()
while True: while True:
light = light_sensor.light light = light_sensor.ReadVisible
print('Light level:', light) print('Light level:', light)
time.sleep(1) time.sleep(1)

@ -12,7 +12,7 @@ while True:
light = light_sensor.light light = light_sensor.light
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -12,7 +12,7 @@ The nightlight logic in pseudo-code is:
```output ```output
Check the light level. Check the light level.
If the light is less than 200 If the light is less than 300
Turn the LED on Turn the LED on
Otherwise Otherwise
Turn the LED off Turn the LED off
@ -22,7 +22,7 @@ Otherwise
The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color. The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color.
#### Task #### Task - connect the LED
Connect the LED. Connect the LED.
@ -44,9 +44,9 @@ Connect the LED.
## Program the nightlight ## Program the nightlight
The nightlight can now be programmed using the Grove light sensor and the Grove LED. The nightlight can now be programmed using the Grove sunlight sensor and the Grove LED.
### Task ### Task - program the nightlight
Program the nightlight. Program the nightlight.
@ -70,16 +70,18 @@ Program the nightlight.
The line `led = GroveLed(5)` creates an instance of the `GroveLed` class connecting to pin **D5** - the digital Grove pin that the LED is connected to. The line `led = GroveLed(5)` creates an instance of the `GroveLed` class connecting to pin **D5** - the digital Grove pin that the LED is connected to.
> 💁 All the sockets have unique pin numbers. Pins 0, 2, 4, and 6 are analog pins, pins 5, 16, 18, 22, 24, and 26 are digital pins.
1. Add a check inside the `while` loop, and before the `time.sleep` to check the light levels and turn the LED on or off: 1. Add a check inside the `while` loop, and before the `time.sleep` to check the light levels and turn the LED on or off:
```python ```python
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()
``` ```
This code checks the `light` value. If this is less than 200 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 200 it calls the `off` method, sending a digital value of 0 to the LED, turning it off. This code checks the `light` value. If this is less than 300 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 300 it calls the `off` method, sending a digital value of 0 to the LED, turning it off.
> 💁 This code should be indented to the same level as the `print('Light level:', light)` line to be inside the while loop! > 💁 This code should be indented to the same level as the `print('Light level:', light)` line to be inside the while loop!
@ -103,7 +105,7 @@ Program the nightlight.
Light level: 290 Light level: 290
``` ```
1. Cover and uncover the light sensor. Notice how the LED will light up if the light level is 200 or less, and turn off when the light level is greater than 200. 1. Cover and uncover the sunlight sensor. Notice how the LED will light up if the light level is 300 or less, and turn off when the light level is greater than 300.
> 💁 If the LED doesn't turn on, make sure it is connected the right way round, and the spin button is set to full on. > 💁 If the LED doesn't turn on, make sure it is connected the right way round, and the spin button is set to full on.

@ -4,31 +4,33 @@ In this part of the lesson, you will add a light sensor to your Raspberry Pi.
## Hardware ## Hardware
The sensor for this lesson is a **light sensor** that uses a [photodiode](https://wikipedia.org/wiki/Photodiode) to convert light to an electrical signal. This is an analog sensor that sends an integer value from 0 to 1,023 indicating a relative amount of light that doesn't map to any standard unit of measurement such as [lux](https://wikipedia.org/wiki/Lux). The sensor for this lesson is a **sunlight sensor** that uses [photodiodes](https://wikipedia.org/wiki/Photodiode) to convert visible and infrared light to an electrical signal. This is an analog sensor that sends an integer value from 0 to 1,023 indicating a relative amount of light, but this can be used to calculate exact values in [lux](https://wikipedia.org/wiki/Lux) by taking data from the separate infrared and visible light sensors.
The light sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi. The sunlight sensor is an eternal Grove sensor and needs to be connected to the Grove Base hat on the Raspberry Pi.
### Connect the light sensor ### Connect the sunlight sensor
The Grove light sensor that is used to detect the light levels needs to be connected to the Raspberry Pi. The Grove sunlight sensor that is used to detect the light levels needs to be connected to the Raspberry Pi.
#### Task #### Task - connect the sunlight sensor
Connect the light sensor Connect the sunlight sensor
![A grove light sensor](../../../images/grove-light-sensor.png) ![A grove sunlight sensor](../../../images/grove-sunlight-sensor.png)
1. Insert one end of a Grove cable into the socket on the light sensor module. It will only go in one way round. 1. Insert one end of a Grove cable into the socket on the sunlight sensor module. It will only go in one way round.
1. With the Raspberry Pi powered off, connect the other end of the Grove cable to the analog socket marked **A0** on the Grove Base hat attached to the Pi. This socket is the second from the right, on the row of sockets next to the GPIO pins. 1. With the Raspberry Pi powered off, connect the other end of the Grove cable to one of the three the I<sup>2</sup>C sockets marked **I2C** on the Grove Base hat attached to the Pi. This socket is the second from the right, on the row of sockets next to the GPIO pins.
![The grove light sensor connected to socket A0](../../../images/pi-light-sensor.png) > 💁 I<sup>2</sup>C is a way sensors and actuators can communicate with an IoT device. It will be covered in more detail in a later lesson.
## Program the light sensor ![The grove sunlight sensor connected to socket A0](../../../images/pi-sunlight-sensor.png)
The device can now be programmed using the Grove light sensor. ## Program the sunlight sensor
### Task The device can now be programmed using the Grove sunlight sensor.
### Task - program the sunlight sensor
Program the device. Program the device.
@ -36,38 +38,44 @@ Program the device.
1. Open the nightlight project in VS Code that you created in the previous part of this assignment, either running directly on the Pi or connected using the Remote SSH extension. 1. Open the nightlight project in VS Code that you created in the previous part of this assignment, either running directly on the Pi or connected using the Remote SSH extension.
1. Run the following command to install a pip package for working with the sunlight sensor:
```sh
pip3 install seeed-python-si114x
```
Not all the libraries for the Grove Sensors are installed with the Grove install script you used in an earlier lesson. Some need additional packages.
1. Open the `app.py` file and remove all code from it 1. Open the `app.py` file and remove all code from it
1. Add the following code to the `app.py` file to import some required libraries: 1. Add the following code to the `app.py` file to import some required libraries:
```python ```python
import time import time
from grove.grove_light_sensor_v1_2 import GroveLightSensor import seeed_si114x
``` ```
The `import time` statement imports the `time` module that will be used later in this assignment. The `import time` statement imports the `time` module that will be used later in this assignment.
The `from grove.grove_light_sensor_v1_2 import GroveLightSensor` statement imports the `GroveLightSensor` from the Grove Python libraries. This library has code to interact with a Grove light sensor, and was installed globally during the Pi setup. The `import seeed_si114x` statement imports the `seeed_si114x` module that has code to interact with the Grove sunlight sensor.
1. Add the following code after the code above to create an instance of the class that manages the light sensor: 1. Add the following code after the code above to create an instance of the class that manages the light sensor:
```python ```python
light_sensor = GroveLightSensor(0) light_sensor = seeed_si114x.grove_si114x()
``` ```
The line `light_sensor = GroveLightSensor(0)` creates an instance of the `GroveLightSensor` class connecting to pin **A0** - the analog Grove pin that the light sensor is connected to. The line `light_sensor = seeed_si114x.grove_si114x()` creates an instance of the `grove_si114x` sunlight sensor class.
> 💁 All the sockets have unique pin numbers. Pins 0, 2, 4, and 6 are analog pins, pins 5, 16, 18, 22, 24, and 26 are digital pins.
1. Add an infinite loop after the code above to poll the light sensor value and print it to the console: 1. Add an infinite loop after the code above to poll the light sensor value and print it to the console:
```python ```python
while True: while True:
light = light_sensor.light light = light_sensor.ReadVisible
print('Light level:', light) print('Light level:', light)
``` ```
This will read the current light level on a scale of 0-1,023 using the `light` property of the `GroveLightSensor` class. This property reads the analog value from the pin. This value is then printed to the console. This will read the current sunlight level on a scale of 0-1,023 using the `ReadVisible` property of the `grove_si114x` class. This value is then printed to the console.
1. Add a small sleep of one second at the end of the `loop` as the light levels don't need to be checked continuously. A sleep reduces the power consumption of the device. 1. Add a small sleep of one second at the end of the `loop` as the light levels don't need to be checked continuously. A sleep reduces the power consumption of the device.
@ -81,16 +89,16 @@ Program the device.
python3 app.py python3 app.py
``` ```
You should see light values being output to the console. Cover and uncover the light sensor to see the values change: You should see sunlight values being output to the console. Cover and uncover the sunlight sensor to see the values change:
```output ```output
pi@raspberrypi:~/nightlight $ python3 app.py pi@raspberrypi:~/nightlight $ python3 app.py
Light level: 634 Light level: 259
Light level: 634 Light level: 265
Light level: 634 Light level: 265
Light level: 230 Light level: 584
Light level: 104 Light level: 550
Light level: 290 Light level: 497
``` ```
> 💁 You can find this code in the [code-sensor/pi](code-sensor/pi) folder. > 💁 You can find this code in the [code-sensor/pi](code-sensor/pi) folder.

@ -12,17 +12,17 @@ The nightlight logic in pseudo-code is:
```output ```output
Check the light level. Check the light level.
If the light is less than 200 If the light is less than 300
Turn the LED on Turn the LED on
Otherwise Otherwise
Turn the LED off Turn the LED off
``` ```
### Add the sensors to CounterFit ### Add the actuator to CounterFit
To use a virtual LED, you need to add it to the CounterFit app To use a virtual LED, you need to add it to the CounterFit app
#### Task #### Task - add the actuator to CounterFit
Add the LED to the CounterFit app. Add the LED to the CounterFit app.
@ -48,7 +48,7 @@ Add the LED to the CounterFit app.
The nightlight can now be programmed using the CounterFit light sensor and LED. The nightlight can now be programmed using the CounterFit light sensor and LED.
#### Task #### Task - program the nightlight
Program the nightlight. Program the nightlight.
@ -75,13 +75,13 @@ Program the nightlight.
1. Add a check inside the `while` loop, and before the `time.sleep` to check the light levels and turn the LED on or off: 1. Add a check inside the `while` loop, and before the `time.sleep` to check the light levels and turn the LED on or off:
```python ```python
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()
``` ```
This code checks the `light` value. If this is less than 200 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 200 it calls the `off` method, sending a digital value of 0 to the LED, turning it off. This code checks the `light` value. If this is less than 300 it calls the `on` method of the `GroveLed` class which sends a digital value of 1 to the LED, turning it on. If the light value is greater than or equal to 300 it calls the `off` method, sending a digital value of 0 to the LED, turning it off.
> 💁 This code should be indented to the same level as the `print('Light level:', light)` line to be inside the while loop! > 💁 This code should be indented to the same level as the `print('Light level:', light)` line to be inside the while loop!
@ -101,7 +101,7 @@ Program the nightlight.
Light level: 253 Light level: 253
``` ```
1. Change the *Value* or the *Random* settings to vary the light level above and below 200. You will see the LED turn on and off. 1. Change the *Value* or the *Random* settings to vary the light level above and below 300. You will see the LED turn on and off.
![The LED in the CounterFit app turning on and off as the light level changes](../../../images/virtual-device-running-assignment-1-1.gif) ![The LED in the CounterFit app turning on and off as the light level changes](../../../images/virtual-device-running-assignment-1-1.gif)

@ -12,7 +12,7 @@ The sensor is a **light sensor**. In a physical IoT device, it would be a [photo
To use a virtual light sensor, you need to add it to the CounterFit app To use a virtual light sensor, you need to add it to the CounterFit app
#### Task #### Task - add the sensors to CounterFit
Add the light sensor to the CounterFit app. Add the light sensor to the CounterFit app.
@ -38,11 +38,10 @@ Add the light sensor to the CounterFit app.
The device can now be programmed to use the built in light sensor. The device can now be programmed to use the built in light sensor.
### Task ### Task - program the light sensor
Program the device. Program the device.
1. Open the nightlight project in VS Code that you created in the previous part of this assignment. Kill and re-create the terminal to ensure it is running using the virtual environment if necessary. 1. Open the nightlight project in VS Code that you created in the previous part of this assignment. Kill and re-create the terminal to ensure it is running using the virtual environment if necessary.
1. Open the `app.py` file 1. Open the `app.py` file

@ -12,7 +12,7 @@ The nightlight logic in pseudo-code is:
```output ```output
Check the light level. Check the light level.
If the light is less than 200 If the light is less than 300
Turn the LED on Turn the LED on
Otherwise Otherwise
Turn the LED off Turn the LED off
@ -22,7 +22,7 @@ Otherwise
The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color. The Grove LED comes as a module with a selection of LEDs, allowing you to chose the color.
#### Task #### Task - connect the LED
Connect the LED. Connect the LED.
@ -48,7 +48,7 @@ Connect the LED.
The nightlight can now be programmed using the built in light sensor and the Grove LED. The nightlight can now be programmed using the built in light sensor and the Grove LED.
### Task ### Task - program the nightlight
Program the nightlight. Program the nightlight.
@ -67,7 +67,7 @@ Program the nightlight.
1. Add the following code immediately before the `delay` in the loop function: 1. Add the following code immediately before the `delay` in the loop function:
```cpp ```cpp
if (light < 200) if (light < 300)
{ {
digitalWrite(D0, HIGH); digitalWrite(D0, HIGH);
} }
@ -77,7 +77,7 @@ Program the nightlight.
} }
``` ```
This code checks the `light` value. If this is less than 200 it sends a `HIGH` value to the `D0` digital pin. This `HIGH` is a value of 1, turning on the LED. If the light is greater than or equal to 200, a `LOW` value of 0 is sent to the pin, turning the LED off. This code checks the `light` value. If this is less than 300 it sends a `HIGH` value to the `D0` digital pin. This `HIGH` is a value of 1, turning on the LED. If the light is greater than or equal to 300, a `LOW` value of 0 is sent to the pin, turning the LED off.
> 💁 When sending digital values to actuators, a LOW value is 0v, and a HIGH value is the max voltage for the device. For the Wio Terminal, the HIGH voltage is 3.3V. > 💁 When sending digital values to actuators, a LOW value is 0v, and a HIGH value is the max voltage for the device. For the Wio Terminal, the HIGH voltage is 3.3V.
@ -101,7 +101,7 @@ Program the nightlight.
Light value: 344 Light value: 344
``` ```
1. Cover and uncover the light sensor. Notice how the LED will light up if the light level is 200 or less, and turn off when the light level is greater than 200. 1. Cover and uncover the light sensor. Notice how the LED will light up if the light level is 300 or less, and turn off when the light level is greater than 300.
![The LED connected to the WIO turning on and off as the light level changes](../../../images/wio-running-assignment-1-1.gif) ![The LED connected to the WIO turning on and off as the light level changes](../../../images/wio-running-assignment-1-1.gif)

@ -27,7 +27,7 @@ In this lesson we'll cover:
## Communication protocols ## Communication protocols
There are a number of popular communication protocols used by IoT devices to communicate with the Internet. The most popular are based around publish/subscribe messaging via some kind of broker. The IoT devices connect to the broker and publish telemetry and subscribe to commands, the cloud services also connect to the broker and subscribe to all the telemetry messages and publishes commands either to specific devices, or to groups of devices. There are a number of popular communication protocols used by IoT devices to communicate with the Internet. The most popular are based around publish/subscribe messaging via some kind of broker. The IoT devices connect to the broker and publish telemetry and subscribe to commands, the cloud services also connect to the broker and subscribe to all the telemetry messages and publish commands either to specific devices, or to groups of devices.
![IoT devices connect to a broker and publish telemetry and subscribe to commands. Cloud services connect to the broker and subscribe to all telemetry and send commands to specific devices.](../../../images/pub-sub.png) ![IoT devices connect to a broker and publish telemetry and subscribe to commands. Cloud services connect to the broker and subscribe to all telemetry and send commands to specific devices.](../../../images/pub-sub.png)
@ -39,7 +39,7 @@ MQTT is the most popular, and is covered in this lesson. Others include AMQP and
[MQTT](http://mqtt.org) is a lightweight, open standard messaging protocol that can send messages between devices. It was designed in 1999 to monitor oil pipelines, before being released as an open standard 15 years later by IBM. [MQTT](http://mqtt.org) is a lightweight, open standard messaging protocol that can send messages between devices. It was designed in 1999 to monitor oil pipelines, before being released as an open standard 15 years later by IBM.
MQTT has a single broker and multiple clients. All clients connect to the broker, and the broker routes messages to the relevant clients. Messages are routed using named topics, rather than being sent direct to an individual client. A client can publish to a topic, and any clients that subscribe to that topic will receive the message. MQTT has a single broker and multiple clients. All clients connect to the broker, and the broker routes messages to the relevant clients. Messages are routed using named topics, rather than being sent directly to an individual client. A client can publish to a topic, and any clients that subscribe to that topic will receive the message.
![IoT device publishing telemetry on the /telemetry topic, and the cloud service subscribing to that topic](../../../images/mqtt.png) ![IoT device publishing telemetry on the /telemetry topic, and the cloud service subscribing to that topic](../../../images/mqtt.png)
@ -49,19 +49,19 @@ MQTT has a single broker and multiple clients. All clients connect to the broker
### Connect your IoT device to MQTT ### Connect your IoT device to MQTT
The first part in adding Internet control to your nightlight is connecting it to an MQTT broker. The first part of adding Internet control to your nightlight is connecting it to an MQTT broker.
#### Task #### Task
Connect your device to an MQTT broker. Connect your device to an MQTT broker.
In this part of the lesson, you will connect your IoT nightlight to the Internet to allow it to be remotely controlled. Later in this lesson your IoT device will send a telemetry message over MQTT to a public MQTT broker with the light level, where it will be picked up by some server code that you will write. This code will check the light level and send a command message back to the device telling it to turn the LED on or off. In this part of the lesson, you will connect your IoT nightlight to the Internet to allow it to be remotely controlled. Later in this lesson, your IoT device will send a telemetry message over MQTT to a public MQTT broker with the light level, where it will be picked up by some server code that you will write. This code will check the light level and send a command message back to the device telling it to turn the LED on or off.
The real-world use case for such a setup could be to gather data from multiple light sensors before deciding to turn on lights, in a location that has a lot of lights, such as a stadium. This could stop the lights being turned on if only one sensor was covered by cloud or a bird, but the other sensors detected enough light. The real-world use case for such a setup could be to gather data from multiple light sensors before deciding to turn on lights, in a location that has a lot of lights, such as a stadium. This could stop the lights from being turned on if only one sensor was covered by cloud or a bird, but the other sensors detected enough light.
✅ What other situations would require data from multiple sensors to be evaluated before sending commands? ✅ What other situations would require data from multiple sensors to be evaluated before sending commands?
Rather than dealing with the complexities of setting up an MQTT broker as part of this assignment, you can use a public test server that runs [Eclipse Mosquitto](https://www.mosquitto.org), an open-source MQTT broker. This test broker is publicly available at [test.mosquitto.org](https://test.mosquitto.org), and doesn't require an accounts to be set up, making it a great tool for testing MQTT clients and servers. Rather than dealing with the complexities of setting up an MQTT broker as part of this assignment, you can use a public test server that runs [Eclipse Mosquitto](https://www.mosquitto.org), an open-source MQTT broker. This test broker is publicly available at [test.mosquitto.org](https://test.mosquitto.org), and doesn't require an account to be set up, making it a great tool for testing MQTT clients and servers.
> 💁 This test broker is public and not secure. Anyone could be listening to what you publish, so should not be used with any data that needs to be kept private > 💁 This test broker is public and not secure. Anyone could be listening to what you publish, so should not be used with any data that needs to be kept private
@ -76,7 +76,7 @@ Follow the relevant step below to connect your device to the MQTT broker:
### A deeper dive into MQTT ### A deeper dive into MQTT
Topics can have a hierarchy, and clients can subscribe to different levels of the hierarchy using wildcards. For example you can send temperature telemetry messages to `/telemetry/temperature` and humidity messages to `/telemetry/humidity`, then in your cloud app subscribe to `/telemetry/*` to receive both the temperature and humidity telemetry messages. Topics can have a hierarchy, and clients can subscribe to different levels of the hierarchy using wildcards. For example, you can send temperature telemetry messages to `/telemetry/temperature` and humidity messages to `/telemetry/humidity`, then in your cloud app subscribe to `/telemetry/*` to receive both the temperature and humidity telemetry messages.
Messages can be sent with a quality of service (QoS), which determines the guarantees of the message being received. Messages can be sent with a quality of service (QoS), which determines the guarantees of the message being received.
@ -327,9 +327,9 @@ Write the server code.
One important consideration with telemetry is how often to measure and send the data? The answer is - it depends. If you measure often you can respond faster to changes in measurements, but you use more power, more bandwidth, generate more data and need more cloud resources to process. You need to measure often enough, but not too often. One important consideration with telemetry is how often to measure and send the data? The answer is - it depends. If you measure often you can respond faster to changes in measurements, but you use more power, more bandwidth, generate more data and need more cloud resources to process. You need to measure often enough, but not too often.
For a thermostat, measuring every few minutes is probably more than enough as temperatures don't change that often. If you only measure once a day then you could end up heating your house for nighttime temperatures in the middle of a sunny day, whereas if you measure every second you will have thousands of unnecessary duplicated temperature measurements that will eat into the users Internet speed and bandwidth (a problem for people with limited bandwidth plans), use more power which can be a problem for battery powered devices like remote sensors, and increase the cost of the providers cloud computing resources processing and storing them. For a thermostat, measuring every few minutes is probably more than enough as temperatures don't change that often. If you only measure once a day then you could end up heating your house for nighttime temperatures in the middle of a sunny day, whereas if you measure every second you will have thousands of unnecessarily duplicated temperature measurements that will eat into the users' Internet speed and bandwidth (a problem for people with limited bandwidth plans), use more power which can be a problem for battery powered devices like remote sensors, and increase the cost of the providers cloud computing resources processing and storing them.
If you are monitoring a data around a piece of machinery in a factory that if it fails could cause catastrophic damage and millions of dollars in lost revenue, thn measuring multiple times a second might be necessary. It's better to waste bandwidth than miss telemetry that indicates that a machine needs to be stopped and fixed before it breaks. If you are monitoring data around a piece of machinery in a factory that if it fails could cause catastrophic damage and millions of dollars in lost revenue, then measuring multiple times a second might be necessary. It's better to waste bandwidth than miss telemetry that indicates that a machine needs to be stopped and fixed before it breaks.
> 💁 In this situation, you might consider having an edge device to process the telemetry first to reduce reliance on the Internet. > 💁 In this situation, you might consider having an edge device to process the telemetry first to reduce reliance on the Internet.
@ -339,7 +339,7 @@ Internet connections can be unreliable, with outages common. What should an IoT
For a thermostat the data can probably be lost as soon as a new temperature measurement has been taken. The heating system doesn't care that 20 minutes ago it was 20.5°C if the temperature is now 19°C, it's the temperature now that determines if the heating should be on or off. For a thermostat the data can probably be lost as soon as a new temperature measurement has been taken. The heating system doesn't care that 20 minutes ago it was 20.5°C if the temperature is now 19°C, it's the temperature now that determines if the heating should be on or off.
For machinery you might want to keep the data, especially if it is used to look for trends. There are machine learning models that can detect anomalies in streams of data by looking over data from defined period of time (such as the last hour) and spotting anomalous data. This is often used for predictive maintenance, looking for indications that something might break soon so you can repair or replace before it happens. You might want every bit of telemetry for a machine sent so it can be processed for anomaly detection, so once the IoT device can reconnect it will send all the telemetry generated during the Internet outage. For machinery you might want to keep the data, especially if it is used to look for trends. There are machine learning models that can detect anomalies in streams of data by looking over data from defined period of time (such as the last hour) and spotting anomalous data. This is often used for predictive maintenance, looking for indications that something might break soon so you can repair or replace it before that happens. You might want every bit of telemetry for a machine sent so it can be processed for anomaly detection, so once the IoT device can reconnect it will send all the telemetry generated during the Internet outage.
IoT device designers should also consider if the IoT device can be used during an Internet outage or loss of signal caused by location. A smart thermostat should be able to make some limited decisions to control heating if it can't send telemetry to the cloud due to an outage. IoT device designers should also consider if the IoT device can be used during an Internet outage or loss of signal caused by location. A smart thermostat should be able to make some limited decisions to control heating if it can't send telemetry to the cloud due to an outage.
@ -372,13 +372,13 @@ The next step for our Internet controlled nightlight is for the server code to s
1. Add the following code to the end of the `handle_telemetry` function: 1. Add the following code to the end of the `handle_telemetry` function:
```python ```python
command = { 'led_on' : payload['light'] < 200 } command = { 'led_on' : payload['light'] < 300 }
print("Sending message:", command) print("Sending message:", command)
client.publish(server_command_topic, json.dumps(command)) client.publish(server_command_topic, json.dumps(command))
``` ```
This sends a JSON message to the command topic with the value of `led_on` set to true or false depending on if the light is less than 200 or not. If the light is less than 200, true is sent to instruct the device to turn the LED on. This sends a JSON message to the command topic with the value of `led_on` set to true or false depending on if the light is less than 300 or not. If the light is less than 300, true is sent to instruct the device to turn the LED on.
1. Run the code as before 1. Run the code as before
@ -421,7 +421,7 @@ If the commands need to be processed in sequence, such as move a robot arm up, t
## 🚀 Challenge ## 🚀 Challenge
The challenge in the last three lessons was to list as many IoT devices as you can that are in your home, school or workplace and decide if they are built around microcontrollers or single-board computers, or even a mixture of both, and thing about what sensors and actuators they are using. The challenge in the last three lessons was to list as many IoT devices as you can that are in your home, school or workplace and decide if they are built around microcontrollers or single-board computers, or even a mixture of both, and think about what sensors and actuators they are using.
For these devices, think about what messages they might be sending or receiving. What telemetry do they send? What messages or commands might they receive? Do you think they are secure? For these devices, think about what messages they might be sending or receiving. What telemetry do they send? What messages or commands might they receive? Do you think they are secure?

@ -18,7 +18,7 @@ def handle_telemetry(client, userdata, message):
payload = json.loads(message.payload.decode()) payload = json.loads(message.payload.decode())
print("Message received:", payload) print("Message received:", payload)
command = { 'led_on' : payload['light'] < 200 } command = { 'led_on' : payload['light'] < 300 }
print("Sending message:", command) print("Sending message:", command)
client.publish(server_command_topic, json.dumps(command)) client.publish(server_command_topic, json.dumps(command))

@ -21,7 +21,7 @@ while True:
light = light_sensor.light light = light_sensor.light
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -24,7 +24,7 @@ while True:
light = light_sensor.light light = light_sensor.light
print('Light level:', light) print('Light level:', light)
if light < 200: if light < 300:
led.on() led.on()
else: else:
led.off() led.off()

@ -23,7 +23,7 @@ All the device code for Arduino is in C++. To complete all the assignments you w
These are specific to using the Wio terminal Arduino device, and are not relevant to using the Raspberry Pi. These are specific to using the Wio terminal Arduino device, and are not relevant to using the Raspberry Pi.
* [Grove camera kit](https://www.seeedstudio.com/Grove-Serial-Camera-Kit.html) * [ArduCam Mini 2MP Plus - OV2640](https://www.arducam.com/product/arducam-2mp-spi-camera-b0067-arduino/)
* [Grove speaker plus](https://www.seeedstudio.com/Grove-Speaker-Plus-p-4592.html) * [Grove speaker plus](https://www.seeedstudio.com/Grove-Speaker-Plus-p-4592.html)
## Raspberry Pi ## Raspberry Pi
@ -48,7 +48,7 @@ These are specific to using the Raspberry Pi, and are not relevant to using the
* Any USB speaker, or speaker with a 3.5mm cable * Any USB speaker, or speaker with a 3.5mm cable
or or
* [USB Speakerphone](https://www.amazon.com/USB-Speakerphone-Conference-Business-Microphones/dp/B07Q3D7F8S/ref=sr_1_1?dchild=1&keywords=m0&qid=1614647389&sr=8-1) * [USB Speakerphone](https://www.amazon.com/USB-Speakerphone-Conference-Business-Microphones/dp/B07Q3D7F8S/ref=sr_1_1?dchild=1&keywords=m0&qid=1614647389&sr=8-1)
* [Grove Light sensor](https://www.seeedstudio.com/Grove-Light-Sensor-v1-2-LS06-S-phototransistor.html) * [Grove Sunlight sensor](https://www.seeedstudio.com/Grove-Sunlight-Sensor.html)
## Sensors and actuators ## Sensors and actuators
@ -58,8 +58,6 @@ Most of the sensors and actuators needed are used by both the Arduino and Raspbe
* [Grove humidity and temperature sensor](https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-DHT11.html) * [Grove humidity and temperature sensor](https://www.seeedstudio.com/Grove-Temperature-Humidity-Sensor-DHT11.html)
* [Grove capacitive soil moisture sensor](https://www.seeedstudio.com/Grove-Capacitive-Moisture-Sensor-Corrosion-Resistant.html) * [Grove capacitive soil moisture sensor](https://www.seeedstudio.com/Grove-Capacitive-Moisture-Sensor-Corrosion-Resistant.html)
* [Grove relay](https://www.seeedstudio.com/Grove-Relay.html) * [Grove relay](https://www.seeedstudio.com/Grove-Relay.html)
* [Grove 125KHz RFID reader](https://www.seeedstudio.com/Grove-125KHz-RFID-Reader.html)
* [RFID tags (125KHz)](https://www.seeedstudio.com/RFID-tag-combo-125khz-5-pcs-p-700.html)
* [Grove GPS (Air530)](https://www.seeedstudio.com/Grove-GPS-Air530-p-4584.html) * [Grove GPS (Air530)](https://www.seeedstudio.com/Grove-GPS-Air530-p-4584.html)
* [Grove - Ultrasonic Distance Sensor](https://www.seeedstudio.com/Grove-Ultrasonic-Distance-Sensor.html) * [Grove - Ultrasonic Distance Sensor](https://www.seeedstudio.com/Grove-Ultrasonic-Distance-Sensor.html)

Binary file not shown.

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 496 KiB

Loading…
Cancel
Save