Child pages
  • Tutorial N°1: Air Monitoring Using Raspberry Pi
Skip to end of metadata
Go to start of metadata

In this tutorial we will provide you with a step by step guide to enable and connect to Labeeb IoT platform an Air Monitoring sensor using Raspberry Pi.

 


Before starting

Required:

Assumptions:

  • We assume you have already been granted an account on Labeeb IoT platform
  • We assume your Raspberry Pi will be connected to the Internet

 


Step by step Guide

Step 1: Configure your device and related device and date models on Labeeb IoT platform


First you have to define a Data Type for your sensor: navigate to "Data Type" sub-menu under "Platform Management" and click on + to add all the needed Data types as shown below.

First, you have to create a Data Model for your sensor: navigate to "Data Models" sub-menu under "Platform Management" and click on + to add all the needed Data Models as shown below.

Second, attach the data types  to your Data Model, then set the accuracy profile. For example, if you set the accuracy level to 90, the time period to 1 second and the action Mark Data Model as Malfunctioning then the received data belonging to this Data Model should pass all the filters for 90% of the packets over a period of 1 second. Otherwise, the Data Model will be marked as Malfunctioning.

Finally, click on "Save" to make sure your new data model is created.

Once the Data Model is created, you have to define a Device Template: navigate to "Device Template" sub-menu under "Platform Management" and click on + to add the template as shown below:
Second, set the Device Heartbeat profile by defining the number of expected packets over the defined Time Period.
Third, you will have to define the password you will use to authorize a device to send the data to your account. This password will be used with the generated login
Fourth, add the data model that you created in previous step.

Finally, click on "Save" to make sure your new device template is created.

Now, once the new device template has been created with data model, you just need to create a new device: navigate to "Device" menu and click on + to add the new device as shown below:

Define the name of your new device and Under "Device Template" you have toe select the previously created device template:


Finally, click on "Save" to make sure your new device is created. Now your new device is now successfully created and listed in my list of devices.

Important Note

You will have to use the generated MQTT topic and the login password you have set when creating the Device Template in order to push data from this device over MQTT.


You can check the device status and the data statistics under "My Account" menu as shown below:

 

Step 2: Prepare the Raspberry Pi and Connect it to Labeeb IoT


  1. Let us start by downloading and installing a fresh Raspberry PI Operating System (OS).
    For this tutorial I used the distribution "RASPBIAN JESSIE LITE" which is available for download at:
    https://www.raspberrypi.org/downloads/raspbian/
     
  2. To install the downloaded OS image by writing it on the SD card you can follow the online guide at:
    https://www.raspberrypi.org/documentation/installation/installing-images/README.md
    Once the OS is installed and the Raspberry Pi is turned on, you can login following the information available at: https://www.raspberrypi.org/help/quick-start-guide/\
     
  3. It is now time to connect the temperature-humidity sensor to Raspberry. An excellent tutorial on how to wire and read the sensor's data is available at: https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/overview
    In this tutorial I used the DHT22 model (https://www.adafruit.com/products/393)
     
  4. To read the data from the DHT22 (AM2302) sensor I opted for the python library by Adafruit (https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/software-install-updated).
    To download and install the library:

    git clone https://github.com/adafruit/Adafruit_Python_DHT.git 
    sudo apt-get update
    sudo apt-get install build-essential python-dev python-openssl --fix-missing
    cd Adafruit_Python_DHT
    sudo python setup.py install
  5. Now I should be able to receive the data from the sensor. To make a test I run the example that comes with the Adafruit library as follow:

    cd examples
    sudo ./AdafruitDHT.py 2302 4

    I can see the temperature and humidity values printed out on my terminal as follow:

     

I modified the above example from Adafruit (AdafruitDHT.py) by adding few code lines to connect to Labeeb and publish the sensed data (temperature and humidity).

  1. To enable the communication with Labeeb IoT I used the paho mqtt library for python (https://www.eclipse.org/paho/clients/python/).

    sudo git clone https://github.com/eclipse/paho.mqtt.python.git
    sudo cd org.eclipse.paho.mqtt.python.git
    sudo python setup.py install
  2. First I created a copy of the above python example to which I add few lines to connect with Labeeb IoT and publish the data:

    cd /home/pi/Adafruit_Python_DHT/
    sudo cp AdafruitDHT.py monitor.py
  3. I added the few lines below:

    import paho.mqtt.publish as publish
    import paho.mqtt.client as mqtt
    import time 
    ...
    myHostname = "mea.labeeb-iot.com"
    myUsername = "demo_device_template_airMonDevTemp"
    myPassword = "temppwd"
    myMqttTopic = "/demo/airMonDevTemp/AirMonDevice/dataCSV"
    myPayload = str(int(time.time()))+",,air monitoring,humidity:"+str(humidity)+",temperature:"+str(temperature)
    publish.single(myMqttTopic, payload=myPayload, qos=0, retain=False, hostname=myHostname, port=1883, client_id="", keepalive=60, will=None, auth={'username':myUsername, 'password':myPassword}, tls=None, protocol=mqtt.MQTTv31)

    The full version of the new python file (monitor.py) is shown below:
     

    #!/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 Adafruit_DHT
    import paho.mqtt.publish as publish
    import paho.mqtt.client as mqtt
    import time
    # 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:
            sensor = sensor_args[sys.argv[1]]
            pin = sys.argv[2]
    else:
            print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
            print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
            sys.exit(1)
    # 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:
            print 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)
            myHostname = "mea.labeeb-iot.com"
            myUsername = "demo_device_template_airMonDevTemp"
            myPassword = "temppwd"
            myMqttTopic = "/demo/airMonDevTemp/AirMonDevice/dataCSV"
            myPayload = str(int(time.time()))+",,air monitoring,humidity:"+str(humidity)+",temperature:"+str(temperature)
            publish.single(myMqttTopic, payload=myPayload, qos=0, retain=False, hostname=myHostname, port=1883, client_id="", keepalive=60, will=None, auth={'username':myUsername, 'password':myPassword}, tls=None, protocol=mqtt.MQTTv31)
    else:
            print 'Failed to get reading. Try again!'
            sys.exit(1)
    
    
  4. Now I execute the program and the temperature and humidity data should be transmitted to Labeeb IoT platform.

    sudo ./monitor.py 2302 4


    To double check that the date has been transmitted to Labeeb IoT I go back to my account on Labeeb IoT portal and I click "Dashboard", and I could see that some data has been received (4 Data as I executed the program twice).

    And if I navigate to Data/Search Data on the top menu and then I click on "Go" in the "Search Data" page I could see the received data as shown below: