More on sunlight sensor

pull/57/head
Jim Bennett 4 years ago
parent 6cf8e579b6
commit 72c5e149ad

@ -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)

@ -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.

@ -9,7 +9,7 @@ while True:
light = light_sensor.ReadVisible 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);
} }

@ -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
@ -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!
@ -105,7 +105,7 @@ Program the nightlight.
Light level: 290 Light level: 290
``` ```
1. Cover and uncover the sunlight 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.

@ -75,7 +75,7 @@ Program the device.
print('Light level:', light) print('Light level:', light)
``` ```
This will read the current light 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. 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.

@ -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)

@ -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

@ -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()

Loading…
Cancel
Save