Marc-Antoine Doyon
Published © CC BY-NC

Connect Sensors to IBM Watson IoT Platform

Collect data from various sensors using little programming with Node-RED, some basic services in IBM Cloud and a Raspberry Pi.

BeginnerFull instructions provided4 hours2,210
Connect Sensors to IBM Watson IoT Platform

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Watson
IBM Watson
Node-RED
Node-RED

Story

Read more

Schematics

DHT11 - Wiring

Code

DHT11

Python
Get the temperature and the humidity in a JSON format
#!/usr/bin/python
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys
import time
import Adafruit_DHT
import json

sensor = Adafruit_DHT.DHT11

pin = 19 # GPIO Raspberry Pi

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Un-comment the line below to convert the temperature to Fahrenheit.
# temperature = temperature * 9/5.0 + 32
# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!

if humidity is not None and temperature is not None:
data = {"Temperature":temperature, "Humidity":humidity}
dataJSON = json.dumps(data)

print(dataJSON)

else:
print('Failed to get reading. Try again!')
sys.exit(1)

Credits

Marc-Antoine Doyon

Marc-Antoine Doyon

4 projects • 6 followers
Maker / Electrical Engineer / Technology lover

Comments