This project is a web app to control an AC (Air Conditioner) in my room. Raspberry pi is used to host the web app and control the AC using an IR blaster. The web app is built using a Flask framework and MQTT protocol is used to publish data to multiple users. Celery is used to schedule tasks and run asynchronous tasks.
- View current temperature of the room
- View temperature history of the room.
- Automatically acquire temperature data from the temperature sensor after every 10 mins.
- Control the AC by switching it ON/OFF, controlling the air temperature and fan speed.
- Sleep mode
Following is an overview of the code and libraries used for various features of the app. Please refer the repository for the complete code.
When a button on the web app interface is pressed, (for eg. OFF) a HTTP POST request with the current state is sent to the Flask app. The app processes the data, sends the required IR signal to the AC and publishes the changed state to the MQTT broker to the topic "remote/update". The MQTT broker publishes data to all the clients subscribed to the topic "remote/update". Therefore all the clients using the web app will have the updated current state.
Temperature data acquired from DHT22 sensor is published over MQTT to the topic “temperature/sensor01” after every 10 mins to all the clients subscribed to this topic. The periodic publishing of the data is done using the Celery Beat Scheduler.
The web app can shutdown the AC automatically at a particular time in the future. When the client sends a request to the app to shut the AC off at a particular time the app executes an asynchronous celery task which calculates the time difference in seconds between the time the sleep mode is set and the time you want the AC to turn off. The code execution will be delayed using the time.sleep() method after which the IR signal to turn off the AC is sent.
- Raspberry Pi connected to the internet.
- DHT22/11 temeprature and humidity sensor.
- IR blaster circuit
The IR codes to control the AC are decoded using the LIRC. Refer this blog to decode a the IR signal for a particular remote and setup the Raspberry PI for IR signal transmission through the IR blaster circuit.
The decoded IR codes are stored in a file called lircd.conf. Following is a sample lircd.conf file:-
# contributed by : Chaitanya M
#
# brand: LG
# model no. of remote control: 6711A20109B
# devices being controlled by this remote:AIR CONDITIONER
begin remote
name LG
bits 28
flags CONST_LENGTH
eps 30
aeps 100
header 8400 4200
one 651 1515
zero 651 459
ptrail 651
gap 103993
begin codes
AC16_LOW_TURNON 0x8800101
AC16_MED_TURNON 0x8800123
AC16_HI_TURNON 0x8800145
end codes
end remote
The IR signals can be sent using python as follows:-
import os
os.system("irsend SEND_ONCE LG AC16_LOW_TURNON")
The web app can be started on the Raspberry Pi using the following steps:
- Install and configure Mosquitto Broker for websockets
- Install and run redis message broker.
- Execute the following command in linux CLI to start celery worker:
$ celery -A main.celery worker --loglevel=INFO
- Execute the following command in linux CLI to start celery beat scheduler:
$ celery -A main.celery beat --loglevel=INFO
- Execute the following command in linux CLI to start flask app:
$ python main.py
- Enter the IP adrress of the Raspberry pi with port number 8080 in the browser of the device connected to the same network to which the Raspberry pi is connected. Eg "192.169.1.101:8080"
Note: To access the web app over the internet a server like NGINX and UWSGI will have to be installed on the Raspberry pi. Please find the configuration files for UWSGI and NGINX in the repository for furthur reference.
I prensented my project at a meetup called PYTHON PUNE on June 2019. You can find the PPT at the following link:
https://docs.google.com/presentation/d/1q0qw2O6a-XGQaC2bJTWJJGZb-Y2YTlUi307I75EcJnY/edit?usp=sharing