Code a Temperature Controlled Desk Fan, with XinaBox and Zerynth
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.
|1||×||XinaBox IP01 - USB Programming Interface Module for FT232R|
|1||×||XinaBox CW02 - WI-FI & BLUETOOTH CORE (ESP32)|
|1||×||XinaBox OC03 - Relay Out for PCA9554A Module|
|1||×||XInaBox SW01 - Advanced Weather Sensor (BME280)|
|1||×||XinaBox xBus Connectors, Pack of 10|
note: all except the bell wire come in our XK12 IoT Starter Kit, powered by Zerynth!
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.
Connect 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”.
Figure 1: xChips assembled Figure 2: xChips and fan
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.
If 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
Programming the Device
# 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
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)
pinMode(D26,OUTPUT) # defining CW01 LED output
temp_threshold = 25 # setting threshold temperature for fan operation
# loop forever
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 the voltage HIGH
print("Temperature sensor shows " + str(tempC) + ", which is above the threshold of " + str(temp_threshold))
sleep(2000) # wait for 2 secs
OC03.writePin(False) # turn off OC03 relay
digitalWrite(D26, LOW) # turn the CW01 LED OFF by setting the voltage LOW
print("Temperature sensor shows " + str(tempC) + ", which is below the threshold of " + str(temp_threshold))
sleep(2000) # wait for 2 secs
Figure 3: screenshot of Zerynth Studio
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.
Figure 4: Serial monitor output
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