The main goal of this project is to develop a weather station with Raspberry Pi and to be able to monitor all the sensors with ThingSpeak and Twitter. The sensors used without any problems are the DHT11 (humidity and temperature sensor) and the BMP085 (barometric pressure and temperature sensor).
Why not use the DHT22, BMP180, or the BMP280? The reasons are because the manufacturers are always going to come out with new versions of these sensors and I’d never finish this chapter and book. Don’t worry; the adjustments will be minimal between one sensor version and another.
A weather station is a facility, either on land or sea, with instruments and equipment for measuring atmospheric conditions to provide information for weather forecasts and to study the climate. The measurements taken include temperature, atmospheric pressure, humidity, wind speed, wind direction, and precipitation amounts. Wind measurements are taken with as few other obstructions as possible, while temperature and humidity measurements are kept free from direct solar radiation or insulation. Manual observations are taken at least once a day, while automated measurements are taken at least once an hour. In this chapter, you will limit yourself to measuring humidity, temperature, barometric pressure, and height above sea level.
Hardware
Make the electrical connections of the diagram in Figure 4-1.
The barometric pressure sensor BMP085 is used to measure the atmospheric pressure between 300 and 1100hPa. It can also measure between -500 and 9000 meters above sea level, making the corresponding calculations. Additionally, it measures the temperature from 0 to 65 degrees centigrade. You must activate the 12C interface on the Raspberry Pi Configuration screen and reboot the system for it to work correctly. You can download the library from the link provided at the end of this tutorial.
The DHT11 sensor measures the relative humidity between 20% and 90%; it also measures the temperature between 0 and 50 degrees Celsius. The digital output is through a simple signal bus.
When the communication between the application and the Raspberry Pi board is established, you can perform the following functions in an interactive way:
1.
The sensors begin to work and perform the tasks that correspond to them.
2.
The DHT11 sensor measures the relative humidity and temperature in degrees Celsius and sends the data to the Raspberry Pi board.
3.
The BMP085 sensor sends the barometric pressure data in hPa and the temperature in degrees Celsius to the Raspberry Pi board. Here you make the conversion to mmHg.
4.
You print the measured values on the screen.
5.
You send the following measured values to the ThingSpeak server: temperature measured by the DHT11 sensor, humidity measured by the DHT11 sensor, pressure measured in mmHg by the BMP085 sensor, and height calculated by the BMP085 sensor.
6.
The ThingSpeak server sends a ThingTweet when the temperature is above 25 degrees Celsius and another ThingTweet when the humidity is above 45%.
The following are the parts needed for this project:
f = urllib2.urlopen(baseURL + "&field1=%s&field2=%s&field3=%s&field4=%s" % (TW, RHW, Pressure, Altitude))
print f.read()
f.close()
sleep(int(myDelay))
except Exception as e:
print e
print 'exiting.'
break
# call main
if __name__ == '__main__':
main()
//******************************
Here is an example of what to do if you use another sensor. If you use the BMP180 sensor, download the library for this sensor, and change the following line:
import Adafruit_BMP.BMP180 as BMP180
Also, change the next line of code:
sensor = BMP180.BMP180 ()
Many of your doubts will be eased when you test the sensors. For example, I suggest you try the DHT humidity sensor with this example from the library:
AdafruitDHT.py
//******************************
import sys
import Adafruit_DHT
# Parse command line parameters.
sensor_args = { '11': Adafruit_DHT.DHT11,
'22': Adafruit_DHT.DHT22,
'2302': Adafruit_DHT.AM2302 }
if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
Now that you have assembled the hardware pieces and you’ve programmed your Raspberry Pi board, you may have some problems with the installation of the libraries or with the configuration of the IoT server. This is what the next section is about.
Procedure
In the following sections, you will correctly install the libraries and also learn to configure the IoT server. Remember that in the previous chapters you used analog and digital sensors, but now you are using sensors with the 12C protocol interface from different manufacturers.
Installing the DHT11 Sensor Library
The DHT11 moisture sensor library is attached at the end of this tutorial and is called Adafruit_Python_DHT-master.zip.
Unzip the package. To execute the installation, type the following in the command line:
>>> sudo python setup.py install
In the library you can find simple examples to test this sensor.
Installing the BMP085 Sensor Library
The BMP085 pressure sensor library is attached at the end of this tutorial and is called Adafruit_Python_BMP-master.zip.
Optionally, you can download the library from
https://github.com/adafruit/Adafruit_Python_BMP.
Unzip the package. To execute the installation, type the following in the command line:
>>> sudo python setup.py install
In the library you can find simple examples to test this sensor.
Enabling the 12C Interface
You must activate the 12C interface on the Raspberry Pi Configuration screen and reboot the system for it to work correctly.
Creating a Project in ThingSpeak
Use the account you created earlier and create a new channel. Enter the following data (Figure 4-6):
Name: WeatherStation
Description:
Field1: Temperature (C)
Field2: Humidity (%)
Field3: Pressure (hPa)
Field4: Altitude (m)
Save the project. The system will provide you with the channel number and the Write API key.
Copy the Write API key, which is 16 characters, and paste it into the Python code where it says
myAPI = "GET_YOUR_KEY"
Remember that in the free account in ThingSpeak you can only monitor a maximum of four channels simultaneously.
With this configuration, you can get the information shown in Figures 4-7 through 4-11.
Using Your Twitter Account in ThingSpeak
If you registered your Twitter account in Chapters 2 and 3, then it’s not necessary to repeat this step. If this is not the case, you should click Apps ➤ ThingTweet. You must link your Twitter account. The system will provide you with an API Key.
Sending an Alert to Your Twitter Account
Click Apps and React. Then fill out the new React as follows (Figure 4-12):
React Name: WeatherStation – temperature
Condition type: Numeric
Test Frequency: On data insertion
Condition: If channel WeatherStation temperature is greater than 25
Action: ThingTweet
Then tweet: “From WeatherStation – temperature is above 25 degrees Celsius”
Using Twitter account: “YOUR TWITTER LOGIN”
Options: Run action only the first time the condition is met.
Do the same with the humidity React data:
React Name: WeatherStation – humidity
Condition type: Numeric
Test Frequency: On data insertion
Condition: If channel WeatherStation humidity is greater than 45
Action: ThingTweet
Then tweet: “From WeatherStation – humidity is above 45 %”
Using Twitter account: “YOUR TWITTER LOGIN”
Options: Run action only the first time the condition is met.
Now you can get the next tweet alert (Figure 4-13).
Challenges
Use the next sensors: DHT22, BMP180, and BMP280.
Conclusion
In this chapter, you used the 12C and One Wire protocols to build a weather station with your Raspberry Pi. Additionally, you tested the ThingSpeak server to display four graphs of your IoT system.