Introduction: Fanair: a Weather Station for Your Room
There are countless ways to find out the current weather, but then you only know the weather outside. What if you want to know the weather inside your house, inside a specific room? That's what I attempt to resolve with this project.
Fanair uses multiple sensors to sense:
- temperature
- humidity
- light
- certain gasses
- air pressure
It is very compact and makes use of the Raspberry Pi 3 B, for getting the values, saving them in a MySQL database and then making them easily available on a website. Included in the code and website is the ability to get actual weather data for your location so that the website can be used to check outside weather as well.
Step 1: Get All the Required Items
First of all you're going to need basic electronics like wires / jumper cables. If you plan on soldering everything together as a permanent project then obviously you will also need a soldering iron, solder, and for your own comfort a PCB to solder everything to.
For more specific items I made this Bill of Materials.
- Raspberry Pi 3 B
- MPC3008 - 8-Channel 10-Bit ADC
- AM2301 Thermometer Temperature and Humidity Sensor
- WR Rademacher WR-tyoe 930-1 PCB
- ABS Case for prototyping (black)
- LDR
- MQ135 gass sensor
- 2x BC517 Darlington Transistor
- RGB LED
- Resistor Pack
- Buzzer
- Wires
- BMP280 Barometer
- Quiet USB fan (5V)
All together this should cost about € 110. Keep in mind that the total price is an approximation. Also, my project has an LCD screen in the photo but its not connected since there wasn't enough space in my case, so it's not included in the instructable.
Attachments
Step 2: Get Your Raspberry Pi Ready
Get your Raspian Jessie with Pixel operating system from the official Raspberry Pi site and once its downloaded unzip it. You'll get an image file. To get it on the Pi you'll have to write it to the SD-Card using Win32 Disk Imager. Once it's installed select the image file and the drive in which you're SD-Card resides. Then press write and wait until it's finished.
When it's finished click on your SD-Card in File Explorer and open the file called 'cmdline.txt' go the end of the text file just before rootwait and write 'ip=254.169.10.2' with spaces around of course. After you save it you should right click somewhere inside the window and create a new file called 'ssh'. Notice that this file does not have a file extension. To make sure it doesn't have an extension click on 'view' in file explorer and check 'hidden files'. If it does rename it and delete the extension so it just reads 'ssh'.
After that is done you can finally put your SD-Card in the Raspberry Pi. Download an ssh client (I recommend MobaXterm). And connect using ssh with the IP you set earlier '254.169.10.2'. The username is 'pi', the password 'raspberry'. If you want you can change the password by typing the command 'sudo passwd' in the terminal and following the instructions. Since we're going to use a good chunk of the SD-Card for Fanair you should extend the available storage by typing 'sudo rapi-config' choosing 'Advanced Options' and then 'Expand File System'. If prompted restart.
We'll be making use of the 1-wire, SPI, and I2C interface. To be able use them you first need to set them up!
Step 3: Connect Everything Together
Its time to make a circuit! First time you should probably try it out on a breadboard, but if you don't have time for that you could try to solder it together.
The circuit you'll need looks like the above, for convenience a breadboard version is also included.
Step 4: Create Your Database
To efficiently save all the data you'll receive we'll make use of a TSQL MySQL database. Before installing MySQL let's make sure everything is up to date by typing a few commands in the terminal:
- 'sudo apt-get update'
- 'sudo apt-get upgrade'
- 'sudo apt-get dist-upgrade'
Accept every prompt by typing 'y' and enter.
To install MySQL type:
- 'sudo apt-get install mysql-server'
- 'sudo apt-get install mysql-client'
When prompted create a root password of your choice.
After it's done installing login by using:
- 'mysql -uroot -p'
And typing your password.
Get the Fanair code from My Github! Click on 'Clone or download' and then on 'Download ZIP'. Unzip the code in a folder of your choice. The query to create the fully operational database is in the database folder. Open the file copy the code and paste it in the terminal with MySQL open. Then click enter and the database is done!
Step 5: Time for the Code!
To make use of all the sensors you obviously need to run a program. Luckily the code is already done (for the most part), and we already downloaded it when getting the database SQL query.
Like I said there are a few things that still need to be changed / added for the program to work properly. First of all the program uses the Darksky API to get the weather data based on longitude and latitude. To make use of it you need to sign up. The first 1000 calls are free and afterwards it costs $ 0.0001 per call. I've heard if you don't pay your account gets blocked. To be fair $ 0.0001 per call is quite cheap. Fanair calls the most recent data every hour which means that in a year if run full time it will only cost you $ 0.876. You also get 41 days free if Fanair runs full time.
Once signed up look for your API key. Look for your location's longitude and latitude on google maps or try to google it. Create a new text file the root directory of Fanair (with classes, main.py ... ). Name the file 'key_location.txt'. Open the text file and on the first line paste your Darksky API key. On the second line paste your longitude and on the third line paste your latitude (both in decimal format). When you're done save the file.
In the same directory create another text file called 'database_dsn.txt'. The first line should say 'localhost'. The second line is 'root'. The in the third line you should type your password that you made for the database earlier, and the fourth and last line you should type 'fanair'.
Right click the main.py file and select edit. The 'fanair = Fanair(5, 26, 17, 27, 22, 4, "AM2301", 1, 0, 0, 0, "key_location.txt", "database_dsn.txt")' needs to be changed. If you followed the picture the first 5 values should be changed from '5, 26, 17, 27, 22, 4' to '20, 21, 26, 19, 13'. '/home/muhsin/Applications/Fanair' should also be changed to your Fanair root path (where main.py is). Make sure to put a slash in front of 'home' but not after 'Fanair'.
Now this is all great but the code needs to get in the Pi. Luckily for us the sftp protocol exists. To put the files on the Pi use an sftp client like FileZilla or if you decided to use MobaXterm you can upload the files easily in the sftp interface left of the terminal.
In my experience I don't always get the right date and time in the Raspberry Pi. To make sure you'll receive the correct datetime I suggest to follow this guide: Raspberry Pi sync date and time.
And last but not least a few libraries need to be installed and a few interfaces need to be activated:
- 'sudo apt-get install build-essential python-dev'
Adafruit DHT:
- 'pip3 install adafruit_python_dht'
1-Wire:
- 'sudo raspi-config'
- 'Interfacing Options'
- '1 Wire'
- Enable
- 'sudo nano /boot/config.txt'
- Add this line in the file: 'dtoverlay=w1-gpio'
- 'sudo reboot'
SPI:
- 'sudo raspi-config'
- 'Interfacing Options'
- 'SPI'
- Enable
- 'sudo reboot'
- 'sudo nano /boot/config.txt'
- Search for 'dtparam=spi=on' and uncomment it by removing the hashtag.
- 'sudo apt-get install python3-dev
Smbus:
- 'sudo raspi-config''
- 'Interfacing Options'
- 'I2C'
- Enable
- 'sudo reboot'
- 'sudo apt-get install -y python-smbus'
- 'sudo apt-get install -y i2c-tools'
- 'sudo nano /boot/config.txt'
- Search for dtparam=i2c_arm=on and uncomment it by removing the hashtag.
flask
- pip3 install Flask
mysql connector
- 'pip3 install -Iv mysql-connector==2.1.4'
spidev
- 'pip3 install py-spidev'
darksky
- 'pip3 install darkskylib'
Step 6: Try It Out!
If you've managed to make it this far tap yourself on the back. Now everything is done to make Fanair work properly. So to test it out type 'cd "<file_location>"'. File location is "/home/pi" for example if you just pasted in your home directory. Then type 'python3 main.py' and let it rest for 15 minutes so it will have data in the database when done. After that press 'ctrl + c' stop stop the program and type 'python3 Flask.py'. To see if your website works you need to surf to your Pi's IP address you made with ':5000' after it (like this: 169.254.10.1:5000').
To make it run automatically after boot:
'sudo nano /etc/rc.local'
At the end of the file BUT BEFORE exit 0:
'sudo python3 "<file_location>/main.py" &'
'sudo python3 "<file_location>/Flask.py" &'
Congratulations now you're script should run from startup and YOU'RE DONE! If you plan on keeping the project running and haven't already I'd suggest you solder everything together, just to make sure everything stays put. Although if you don't play to move it around much a breadboard also works of course.