Introduction: Window "opener" for Temperature Regulation
This article covers how I developed an embedded system which
measures the environment stimuli to “open” a window in your house. The temperature and rain outside is measured by a particle argon, which wirelessly sends the data to a raspberry pi, which is measuring the temperature inside. The raspberry pi then uses my logic to decide when to open or close the window, so temperature is regulated to a user defined temperature. Note that the hardware doesn’t actually open a window, rather the raspberry pi tells an arduino uno to flash an LED which represents the state of the window.
I built this device because I live in a house in which some rooms get too hot in summer and too cold in winter, and I’m sick of opening windows to regulate the temperature. This embedded system will do all the work for me, which could save me spending money on heating and cooling
Step 1: Equipment
The hardware I used was
· Raspberry pi 4 (3 will work too)
· Arduino uno
· Particle argon (with bread board and wifi adapater)
· 2 x DHT11 temperature sensors
· Soil moisture sensor (any sensor should work)
The software I used was
· Raspbian (OS for raspberry pi 4)
· Arduino app (installed on computer)
· Particle sight (website)
Step 2: The Design
Refereeing to the above diagram, my raspberry pi (which was also connected to a monitor, keyboard, and mouse) was the brain of the embedded system. Note, you can also connect it to your computer via ssh. My raspberry also had a temperature sensor on it, which records the inside temperature. The raspberry launches a GUI when the program begins, which gives the user the ability to change variables in the program.
The Arduino uno was connected to the raspberry via i2c, where the Arduino was the slave (using the wire library) and raspberry was the master (using smbus). The ardunio switched on and off a LED to represent the state of the window (I used the inbuilt LED on the arduino). For a extension of the project, the ardunio would have the motor to physically open and close the window.
The particle argon wasn’t connected physically to the raspberry, instead they communicated via mqtt. The particle would send the outside temperature and moisture readings to a particular mqtt server, which is then extracted via the raspberry pi. Note that the argon was connected a wifi connection using the wifi adapter, while the particle was connected wireless using it’s built in wifi capabilities.
Step 3: How the Code All Comes Together
All 3 devices had their own software on them, but the
raspberry pi is the brain behind it all. There were 6 major components on the pi, which were;
1. Running the GUI
2. Reading the inside temperature
3. Connecting to the Argon
4. Reading the outside temperature
5. Reading the outside moisture
6. Connecting to the Arduino
Step 4: Running the GUI
The code above shows the libraries used to make the GUI. I decided to use tkinter as it’s fairly easy to use and I’m familiar with it. For any help on how to build a GUI, watch this video from core electronics (https://www.youtube.com/watch?v=ap-ABFNCBoE). Note that you can exclude the code “from gpiozero import LED” , as I changed my mind and didn’t use an LED on the raspberry
Step 5: Reading the Inside Temperature
To record the temperature, I used the Adafruit library , with the input pin on GPIO 23.
Step 6: Reading Inside Variable Part 2
Notice how I’ve got a “dummy” variable, that’s to record the humidity. The read_retry method must record to two variables.
Step 7: Connecting to the Argon Part 1
As mentioned above, the argon and raspberry are indirectly connected using mqtt. The argon code is towards the bottom of this article, and the raspberry code is below this paragraph. Note that the code connects to the “test.mosquitto.org” server with port 1883 . This is used to measure the outside temperature and moisture
Step 8: Connecting to the Argon Part 2
To access the argon, the pi subscribes to tempOutside In this function, and MoistureReading in another function. It then calls the messageFunction
Step 9: Connecting to the Argon Part 3
above is the message function, with the most important parts
being the topic and the value (underlined in the photo). The topic is either tempOutside or MoistureReading, and the value is the message from the argon. For more help, read this link: https://www.digikey.com.au/en/maker/projects/how-...
Step 10: Connecting to the Arduino
above is the smbus library to connect to the arduino slave.The addr variable is the ardunio address (00000001). The arduino code is towards the end of the article
To write to the arduino, i use "arduino.write_byte(addr,0x1)" to send a 1, and "arduino.write_byte(addr,0x0)" to send a 0
Step 11: Bring It All Together (threading)
To get the program working together, I used threading. Using the threading library, I’m able to run four functions run simultaneously.
As you can see from the code above, threads 2,3 and 4 are
daemon threads, which means they finish when the last line of code in the program is executed. The “t1.join()” stops the program from executing until t1 (the GUI) finishes. Thread 1 is terminated when the user closes the gui window, which then allows the program to read the following lines. Without making t2,t3, and t4 daemon threads, they would run forever even if t1 finished.
Step 12: Now It's Time for the Particle Argon Code..
Step 13: Argon Code Part 1
The above code is for the Argon. The MQTT and Adafruit libraries are for the mqtt connection and temperature sensor respectively. The next 4 states define constants, note that the DHTTYPE is DHT11. On line 17 the argon creates an MQTT client by connecting to the server. The “1883” is the port number, while the callback is the procedure the argon switches to when it receives data. Since it doesn’t receive data, it’s left empty (it must still be included though). The setup() procedure has some basic pin mode adjustment. Note that the moisture sensor is being powered by D3, so that pin is set to output and given a HIGH digital write value. The “MoisturePin” is the input from the sensor, therefore it must be analog. I then connect the client to my two topics (tempOutside and MoistureReading), and use “dht.begin()” to start the temperature sensor.
Step 14: Argon Code Part 2
This code above is the main loop, which is self explanatory. Note that the delays are used to give the sensors time to get their readings. These aren’t always needed, I just noted that my sensor had some error readings without these delays