Connecting a GrovePi to Eclipse Hono

This tutorial explains how to generate a simple Python application for you GrovePi that sends sensor data to Eclipse Hono using MQTT.

Prerequisites

1. Setup your device.

In this example Raspberry Pi is used but you can use any device which can run python.

2. Setup your development environment (on your development machine).

3. Generate application code using the Python generator.

  • Open the Distance Sensor in the Vorto Repository.
  • Click Eclipse Hono Generator and choose Python.
  • Click Generate
  • Download the ZIP file and extract the source code.
  • Get the certificate and copy and paste the content in a .crt file where you stored your source code

4. Registering to Eclipse Hono

Eclipse Hono provides a sandbox environment, which we can use in this tutorial to integrate the GrovePi with. So before we can send any data, we must register the device in Eclipse Hono. We use vortodemo as a Hono tenant and 1234 as a device ID:

Registering our Device

curl -X POST http://hono.eclipse.org:28080/registration/vortodemo
-i -H 'Content-Type: application/json'
-d '{"device-id": "1234"}'

Adding device credentials

PWD_HASH=$(echo -n "S3cr3t" | openssl dgst -binary -sha512 | base64 | tr -d '\n') 
curl -X POST http://hono.eclipse.org:28080/credentials/vortodemo
-i -H 'Content-Type: application/json'
-d 'JSON request payload' 

JSON request payload:

{
  "device-id": "1234",
  "auth-id": "1234",
  "type": "hashed-password",
  "secrets": [
    {
      "hash-function": "sha-512",
      "pwd-hash": "'$PWD_HASH'"
    }
  ]
}

5. Install GrovePi Python dependencies on the Raspberry Pi.

  • Install necessary dependencies for GrovePi on the Raspberry Pi. To do so, just execute the commands
sudo curl https://raw.githubusercontent.com/DexterInd/Raspbian_For_Robots/master
upd_script/fetch_grovepi.sh | bash
       
sudo reboot
  • You can find full instructions here.

6. Connect the sensor to the Raspberry Pi.

  • Select a sensor from the kit.
  • Connect the selected sensor to the Raspberry Pi. In this tutorial, ultrasonic sensor is used, which measures a non-contact distance. The ultrasonic sensor is connected to pin “3” on the GrovePi.

7. Update the application code.

The ZIP file contains the necessary libraries for your project and a sample Python main program. The main program is named after the thing type and stored in the main directory of the project. Open this file in your editor of choice to make the necessary adjustments.

Code sections which can be customized for your needs are marked with

        ### BEGIN SAMPLE CODE

and

    
        ### END SAMPLE CODE
  • Import the GrovePi dependencies:
      from grovepi import *
  • Update the Device Configuration values like:
      hono_tenant = "vortodemo"
      hono_password = "S3cr3t"
      hono_endpoint = "ssl://hono.eclipse.org:8883"
      hono_deviceId = "1234"
      hono_clientId = hono_deviceId
      hono_authId = hono_deviceId + "@" + hono_tenant
      hono_certificatePath = "PATH TO YOUR CERTIFICATE"
      ditto_namespace = "org.eclipse.vorto"
  • Define a constant and assign the pin number of the pin to which the sensor is physically connected:
      # constants
      ultrasonic_ranger = 3
  • The function periodicAction is called periodically every timePeriod seconds. It is meant to periodically read your sensor’s values and transmit them using the thing type as a data model layer and the Paho MQTT Client.

  • Read the data and assign it to the functional block properties:

      infomodel.distance.sensorValue = ultrasonicRead(ultrasonic_ranger)
      infomodel.distance.sensorUnits = "cm"
  • The variable timePeriod is set further down in the file. In this example, the variable timePeriod is set to 10.

8. Run the application on the device.

  • Copy the code to the device (How to copy files to Raspberry Pi?).
  • Open the Terminal and navigate to the source code folder.
  • Run the application by typing the following command:
      python GENERATED_MAIN_PYTHON_FILE.py

example output:

grovePi Screenshot

Installation of GrovePi required Modules

What’s next?