This tutorial explains how to use the Bosch BME280 weather sensor (xChip SW01), and specifically, temperature data, to control a desk fan, via a solid state relay (xChip OC03). We programmed in Python using Zerynth Studio - a partner of XinaBox.
Introduction
XinaBox’s modular and easy-to-code hardware allows users to assemble an electronic circuit in minutes, without soldering, wiring or hardware knowledge.
This tutorial explains how to use the Bosch BME280 weather sensor (xChip SW01), and specifically, temperature data, to control a desk fan, via a solid state relay (xChip OC03). In a future post, we will cover how to make this control more sophisticated using the Zerynth app to allow wireless live control.
Hardware SetupConnect the xChips according to the photograph in Figure 1, making sure that the xChip name and XinaBox logo are all visible from the same side.
Ensure that the switches on the IP01 xChip are in position “B” and “DCE”.
Connect bell wire to the OC03’s terminals. As shown in Figure 2, cut either of the Fan’s USB power wires and connect the two ends to the bell wire, creating a switch.
Zerynth SetupIf you are already familiar with Zerynth, virtualize your device and jump to Connecting the Device section.
For those of you who are using Zerynth for the first time, follow these steps:
- Download and install the Zerynth Studio using the Installation Guide for additional assistance. Note that the Zerynth Studio is also available as “DesignSpark Zerynth Studio” version over the portal on the DesignSpark engineering community, enabling easy access for customers to download the tools and buy more licenses.
- Continue with the Getting Started guide paying close attention to the Connect, Register and Virtualise your Device section
- Once your device is properly virtualized in Zerynth Studio as a XinaBox CW02 (ESP32) board, you can continue to the next step
Create a project and copy the following code into Zerynth Studio, in the main.py tab of your project and uplink it to the board:
# Temperature Controlled Desk Fan
# Created at 2018-10-03 by Daniel Berman, XinaBox
# Turn on fan via OC03 when SW01 temp > a set temperature (here 25 degrees Celsius)
# import xChips and settings
import streams
streams.serial() # opens serial monitor
from bosch.bme280 import bme280
SW01 = bme280.BME280(I2C0, 0x76, 100000)
from xinabox.oc03 import oc03
OC03 = oc03.OC03(I2C0)
OC03.start()
OC03.init()
pinMode(D26,OUTPUT) # defining CW01 LED output
temp_threshold = 25 # setting threshold temperature for fan operation
# loop forever
while True:
tempC = SW01.get_temp()
if tempC > temp_threshold:
OC03.writePin(True) # turn on OC03 relay
digitalWrite(D26, HIGH) # turn the CW01 LED ON by setting voltage HIGH
print("Temperature sensor shows " + str(tempC) + ", which is above the threshold of " + str(temp_threshold))
sleep(2000) # wait for 2 secs
else:
OC03.writePin(False) # turn off OC03 relay
digitalWrite(D26, LOW) # turn the CW01 LED OFF by setting voltage LOW
print("Temperature sensor shows " + str(tempC) + ", which is below the threshold of " + str(temp_threshold))
sleep(2000) # wait for 2 secs
On running, the serial monitor shows the results of one of the print statements (see Figure 4), depending on whether the temperature is above or below the set threshold. This is useful to ensure our IF statement is working properly, and also to see the temperature data, so we know the sensor information is being properly received by the CW01 and being received correctly by Zerynth.
If the temperature is above the threshold, the CW01 LED will light, and the OC03 completes the circuit, switching on the fan.
Please see below video showing the fan operation; initially, the fan switches off again quickly, as the fan's airflow lowers the temperature sensor reading. When the fan is repositioned not to blow on the sensor, the temperature sensor stays above 25 degrees Celsius, and the fan (and LED) stays on.
Need more info? Espressif Systems has published an article on their blog about Zerynth tutorials for XinaBox devices. A neat and useful overview that everyone needs to check out, no matter their skill level.
Don't forget to check out the XK12 IoT Starter Kit, that enables you to build this and lots of other devices.
Comments