Weather Station
This project will show you how to collect temperature and humidity data from a DHT22 sensor and send it to IBM’s Watson IoT Platform, where you can view it on the web!
Overview
Skill Level: Intermediate
Time Required: 40 minutes
We’ll be using the Arduino Dock to read the temperature and humidity data from the DHT22 sensor since the on-board microcontroller will make this a breeze. On the Omega side, we’ll use the Python pySerial module to periodically send commands to the Arduino Dock and trigger a response with the sensor data. The Omega will then read that data back and send it to IBM Watson, where you can visualize the data in real time! We will then automate the project to run automatically when the Omega is turned on.
The device code can be found in Onion’s iot-weather-station
repo on GitHub, while the Arduino Dock sketch can be found in the Examples of the Onion Arduino Library.
Ingredients
- Onion Omega2+
- or Omega2 standard booting from a USB storage device with at least 16 MB of free memory; see Booting From External Storage
- For convenience, this tutorial will assume you are using an Omega2+.
- Onion Arduino Dock 2
- Breadboard
- DHT22 temperature + humidity sensor
- 10kΩ resistor
- M-M jumper wires
- (Optional) M-F jumper wires
Step-by-Step
Follow these instructions to setup the Weather Station project on your very own Omega!
1. Prepare
You’ll have to have an Omega2+ ready to go, complete the First Time Setup Guide to connect your Omega to WiFi and update to the latest firmware.
Note 1: The Arduino Dock does not have a USB to serial converter chip, so all connections to the Omega’s command line must be done over SSH.
Note 2: The Omega’s firmware along with this project and its dependencies will require approximately 18 MB of storage space. If you wish to install apps on the Console such as the Editor, we recommend booting the filesystem from an external SD card or USB drive.
After completing the first time setup, follow the steps in the Arduino Dock guide to prepare it for flashing Arduino sketches.
2. Wire Up the Sensor
We will treat the side of the DHT sensor with the holes as the front.
- Insert the DHT sensor into the breadboard with the front facing the middle gap.
- The pins will be numbered 1-4 from left to right.
- Connect Vdd (pin 1) to the Arduino Dock’s 5V pin.
- Connect DATA (pin 2) to the Arduino Dock’s digital pin 2.
- Connect GND (pin 4) to one of the Arduino’s GND pins.
- This is not a typo. Pin 3 is unused on this DHT sensor!
- Connect the 10kΩ resistor across Vdd and DATA.
Your setup should look something like this:
In the above setup, two 5.1kΩ resistors were used in series to achieve the 10kΩ pullup.
Optional - Remove the DHT sensor from the breadboard, and use the M-F jumper wires to connect the pins back to the breadboard. This is so you can easily move the sensor around!
3. Download the Project Code on the Omega
The code for this project can be found in Onion’s iot-weather-station repo on GitHub. We’ll first need to connect to the Omega’s command line and install the packages we need for git
:
opkg update
opkg install git git-http ca-bundle
Then we’ll use git
to download the project code to your Omega:
cd /root
git clone https://github.com/OnionIoT/iot-weather-station.git
Now enter the repo directory and run install.sh
to install the required packages and dependencies:
cd iot-weather-station
sh install.sh
This may take several minutes, go grab a drink or a quick snack!
The
install.sh
script will install Python and the additional modules needed to communicate with the Watson IoT platform. Specifically, it will install Python and PIP, the Python package manager. Then, it will use PIP to install IBM’sibmiotf
module and all of it’s dependencies.
4. Find your Omega’s MAC Address
Then get the Omega’s MAC address for use with Watson by running the watsonHelper.py
script:
python watsonHelper.py
You will see output that looks something like this:
Copy or write down the MAC address underneath “Device ID” for later.
5. Arduino IDE Setup
If you don’t already have it, install the Arduino IDE on your computer. Once you’re in the Arduino IDE, install the Adafruit Unified Sensor
and DHT sensor library
libraries from the Library Manager by following this guide guide on the Arduino website.
Then you’ll need to install the Onion Arduino Library by doing the following:
- In your web browser, download the Onion Arduino Library ZIP file.
- Install the ZIP library by following the instructions in the Arduino Library Installation guide.
- Restart your Arduino IDE to reload the library.
Finally, follow our Arduino Dock setup instructions to setup the Arduino IDE to wirelessly flash the Arduino Dock 2.
6. Flash the Arduino Dock’s Microcontroller
Flash the weather station sketch to the Arduino Dock by doing the following:
- Click on
File > Examples > Onion > weatherStation
to open the weather station sketch. - Flash it to the Arduino Dock by following the instructions in the Arduino Dock guide.
This sketch will read the temperature and humidity measurements from the DHT22 sensor and will transmit the value via serial of the correct command is received from the other end. So the Omega will have to issue a command through UART1, in this case, the command is just the r
character, and it will then receive a JSON-formatted string of the sensor data as a response.
7. Setup the Omega on the IBM Watson IoT Platform
We will be using IBM’s guide on registering devices in Watson as a reference for this section. Open the link in your web browser and refer to the additional information that we have provided for each step below.
If you’re having difficulties in this section, follow these two developer recipes to become familiar with the Watson web interface:
- Connect an Onion Omega2 to IBM Watson IoT Platform
- How to Register Devices in IBM Watson IoT Platform
Step 1 - Introduction
- You can register for an IBM Bluemix account here.
Step 2 - Create IBM Watson IoT Platform Organization
- You can call the service “Onion IoT”, “Watson IoT”, or whatever you like.
Step 3 - Create Device Type
- Call the device type
omega
.- The description can be something like “Onion Omega IoT board”.
In the Define Template substep on the Watson website, check off only the Model attribute.
- In Submit Information, enter
omega2
for the Model.- New devices by default will have this value for their Model attribute unless you specify something else.
You can leave Metadata blank.
Step 4 - Add Device in IBM Watson IoT Platform
- When adding a device, choose the
omega
device type we just created. - In the Device Info substep, enter the Device ID that we got from the
watsonHelper.py
helper script a few steps back. - You can leave the Model field blank and it will automatically fill it in with
omega2
. - You can leave the Metadata field blank.
- In Security, we recommend letting Watson automatically generate an authentication token for you. Click on Next without entering anything.
- In the Summary substep, review that your information is correct, then click Add.
You should see a card containing your Organization ID, Device ID, and more. Don’t close this card until you’ve recorded the token somewhere, because there’s no way to view the authentication token for this device again! Take a look at the sample card below:
On the Omega, open the device.cfg
file for editing and replace the placeholders in ALLCAPS with the information in the fields above like so:
Watson Website | device.cfg |
---|---|
Organization ID | YOURORG |
Device ID | YOURDEVICEID |
Authentication Token | YOURTOKEN |
Remaining steps
“Step 5 - Generate API Keys” and onwards are not necessary for this project.
8. Set Up Visualization Boards and Cards on Watson
Follow the steps in IBM’s guide to configuring cards in Watson with the additional information we have provided below:
Step 2 - Overview to Boards & Cards
- You can make your own board to show the collected data from the sensor. In this example, we’ve called it
Weather Station
.
Step 3 - Realtime Data Visualization
- First create a line chart card according to the guide.
- When connecting data sets, set
weather
as the Event. - Create a data set for temperature following the example below:
- Repeat for humidity by replacing all instances of
temperature
withhumidity
. - We recommend using an XL line chart size to be able to see enough data over time.
- Set the title to “History”
You can also add a Value card to clearly display the most recent measurement values.
- When adding the card, click on “Value” as the type.
- Use a M size chart to display both temperature and humidity at the same time.
- You can set the title of this card to “Current”
Now you’ve got visualization set up on Watson!
9. Running the Weather Station Project
On the Omega, navigate to the iot-weather-station
directory and run the main.py
file:
python main.py
You should see messages being published from the command line, and new data points in your Watson dashboard! Try placing the sensor near sources of cold or hot air, or try breathing over it to change the relative humidity and see what happens on the dashboard.
10. Run the Program on Boot
We can automate this project to run when the Omega is turned on, and we can also make it run in the background so you can use the Omega for other things while it’s running! To do this, we’ll place a script in /etc/init.d
.
In the repo folder, make the weather-station
file executable, copy it to /etc/init.d
, then enable it to run on boot:
chmod +x init.d/weather-station
cp init.d/weather-station /etc/init.d
/etc/init.d/weather-station enable
Reboot the Omega, and you will see the dashboard automatically being populated with data while the command line is available for you to use!
The
/etc/init.d/weather-station
script registers the Weather Station Python script as a service withprocd
, the process management daemon of the system.procd
then ensures that the process gets started at boot and continues to run until the service is disabled.
Code Highlight
This project makes use of two main interfaces: the Arduino Dock and the IBM Watson IoT platform.
The Arduino Dock sketch is set to read data from the DHT sensor only when an r
character is sent over the serial bus.
if (Serial.available() > 0) {
// read the input
int inByte = Serial.read();
delay(500); // small delay before responding
// respond only if correct command is received
if ((char)inByte == 'r') {
responder();
delay(delayMS);
}
}
It then sends a response in JSON.
void responder() {
// read the weather sensor
if (getWeather()) {
String temperature = String(sensorResponse.temperature);
String humidity = String(sensorResponse.humidity);
// encode output to JSON
String response = "{\"temperature\":" + temperature + ", \"humidity\":" + humidity + "}";
Serial.println(response);
}
else {
// send false
Serial.println("false");
}
}
JSON was chosen as it is a widely used data serialization format, and JSON parsers exist in most programming languages. You can use this example to create your own Arduino Dock apps that can collect data and send it back to the Omega, which can then run any programming language you like!
The IBM Watson IoT Python library is also useful for sending data to Watson from the Omega. You can reuse the device.cfg
and watsonHelper.py
files in your next IoT projects!