Web-Based Raspberry Pi IOT Air-Conditioner Control via AWS
Conventional building management system doesn’t provide feedback from more than one occupant. Generally, a single thermostat is the only feedback mechanism to determine thermal comfort. Also, thermostats are influenced by occupant indoor behaviour and do not reflect the occupant comfort. This project aims to directly interact with the building, it’s heating and cooling equipment based on the comfort levels of people, which will eventually reduce the energy consumption of building resulting from the HVAC system.
This framework was implemented in the prototype using Raspberry Pi which measures the occupant indoor behaviour via sensors and sends the sensor information to the computation algorithm hosted in AWS cloud services. Then based on the climatic location of user HVAC device and indoor micro-climate, the optimum internal temperature is computed and send back to the user local-server (Raspberry Pi integrating with Temperature and Humidity sensors). AWS Server will send optimum temperature calculation, Infrared (IR) device selection, IR databases and occupant mode selection. Based on the user selection modes and optimum temperature calculation, the IR sensors integrated with Raspberry Pi will send IR pulse to the air-conditioner and automatically changes the mode and desired set-point temperature to maintain thermal comfort. Implementation of this project is done on Python, Flask, SQLAlchemy and AWS. Overall, the architecture implementation of this project is shown below:
Let’s say that we want to control the air-conditioner in the home or industry without using the infrared remote without knowing inside temperature automatically. I will describe how we can achieve to control air-conditioner/HVAC from the internet using Raspberry Pi IOT device.
Steps:
1. Configuration of Raspberry Pi
Install any version of Linux or raspi-config in your Raspberry Pi. You can find more details on installation on Raspberry Pi. I have used Ubuntu Linux OS.
2. Setup Hardware Configuration
Purchase the following hardware requirements:
· Raspberry Pi <any versions>
· DHT22 Sensors <Temperature and Humidity Sensors>
· Infrared Emitter/Receiver <IR Sensors>
· Breadboard
· Connecting Wires
Setup the hardware with the following configurations:
3. Installation of Adafruit library package to give sensors output < Indoor Temperature and Humdity>
You can find more details about Adafruit python packages for digital input and output on Adafruit Installation. Use the following command to install adafruit packages so that DHT22 Temperature and Humdity sensors will give digital output.
pip install adrafruit-io
4. Installation and Configuration of LIRC
Linux Infrared Remote Control (LIRC) is a Linux package to encode and decode infrared signals used in the remote control. You can read more details on LIRC.
First, install LIRC on your Raspberry Pi
sudo apt-get install lirc
Then, add the following configuration to /etc/modules file using the following commands
sudo nano /etc/modules
lirc_dev
lirc_rpi gpio_in_pin = 23 gpio_out_pin =22
Then, edit your /boot/config.txt
sudo nano /boot/config.txt
dtoverlay=lirc-rpi, gpio_in_pin =23, gpio_out_pin =22
And, change your /etc/lirc/hardware.conf file to followings:
sudo nano /etc/lirc/hardware.conf
Now, restart lircd so that changes are reflected
sudo /etc/init.d/lircd stop
sudo /etc/init.d/lircd start
5. Testing the IR Receiver < Let’s choose the case of Kogan Air-Conditioner>
Run the following two commands to stop lircd and start outputting raw data from the IR receiver:
sudo /etc/init.d/lircd stop
mode2 –d /dev/lirc0
Then point a remote control at your IR receiver and press some buttons. You should see something like this:
space 16300
pulse 95
space 28794
pulse 80
If you see something like above, you can check whether you can control your device or not with Raspberry Pi. Run the following code in your terminal
sudo /etc/init.d/lircd stop
Then you can use the following commands to record and write the lircd.conf file with your remote.
irrecord –f –d /dev/lircd0 ~/lircd.conf
Then you can put the name of your IR remote
name of remote: kogan
Then copy over your new configuration files
sudo cp ~/kogan.lircd.conf /etc/lirc/lircd.conf
Then restart the lirc
sudo /etc/init.d/lircd restart
Then send the IR command to your kogan device <testing purposes>
# List all of the commands that LIRC knows for ‘Kogan’
irsend LIST kogan “ “
0000000000000001 KEY_1
0000000000000002 KEY_2
0000000000000003 KEY_3
0000000000000004 KEY_4
Then send the power on command
irsend SEND_ONCE kogan KEY_1
You can find the list of LIRC database extracted from reverse engineering from GitHub. If everything works, then we can move forward to the web part on implementation.
Web Implementation in AWS using Python Flask
The web is implemented in python based flask micro-framework and contains app.py file in GitHub and installation packages are shown in requirements.txt file in GitHub. The main application file is app.py.
Let’s assume, we have one user only i.e. 1 air-conditioner to control. Our air-conditioner automation is achieved with the help of ambient climatic conditions and indoor temperature. We can achieve the ambient weather based on your location and country using Weather API. You need to sign up weather API with a free account.
We can also initialize the user activity behaviour lets suppose the user can study, sleep, walk. You can find many articles in Science Direct regarding human comfort behaviour and their activity factors. Similarly, we can also initialize the user mode, threshold heating and cooling temperature. Then we initialize the AWS MySQL database.
Then initialize the default user data which contains information of the country, location, user activity, user mode and set-point temperature selection. All this will handle via the web interface. Get the behaviour of the climatic condition <Wind speed, Temperature, Humidity, Pressure> from Weather API using the following functions.
You can calculate the optimal temperature required to control your HVAC based on user activity, user-mode <Heating or Cooling>, Set-point Temperature and Ambient Temperature. You can find or you can research which algorithm suits you best to calculate the optimal temperature of a room based on different factors like occupant activity, occupant mode, set-point temperature and ambient climatic conditions. I have only provided some references based on some search on the internet in the main file app.py in GitHub.
You can get the occupant requirements from a web browser for each username. You can also see some graphical representation of climatic conditions of each user illustrated in app.py file in GitHub.
Let’s assume that IP address to send the optimal temperature calculation and occupant behaviour information from AWS to the local server i.e. Raspberry Pi is http://52.62.179.199
Then, we can program in Raspberry Pi<GitHub link contains : ir_call_fxn.py, ir_send_dev.sh and Rasbp_v3.py>
Assuming that we have received data of the country, location, occupant activity, occupant mode and setpoint temperature, we will initialize sensor of Adafruit DHT22 for particular users. Assuming that our URL hosted in AWS knows (let’s suppose http://52.62.179.199), let’s define the URL of the device to cloud and from cloud to device i.e. Raspberry Pi.
Let’s read the data from the sensors from following Raspb_v3.py file.
Then let’s call the IR call functions from file ir_call_fxn.py stored in Raspberry Pi.
Then based on the lircd.conf file stored in our local Raspberry Pi server, IR pulse is sent to the Air-conditioner via ir_send_dev.sh file so that we can achieve our desired temperature and configuration.
I hope you can successfully build web based control of air conditioner using Raspberry Pi hosted either in local server or from cloud server. Refer to GitHub link if required.
Thank’s for reading this article! If you have any questions, feel free to leave a comment!
Happy Coding !
References:
- Instructables. 2020. Zero To Air Conditioner Controller With Raspberry Pi. [online] Available at: <https://www.instructables.com/Zero-to-Air-Conditioner-Controller-With-Raspberry-/>.