ditto-examples

Demo with a Raspberry Pi and a GrovePi+

This project enables you to use a Raspberry Pi with GrovePi+ as a Thing that is managed by Eclipse Ditto and presents information in a WebUI. The Thing will provide information about temperature, humidity and illumination. Other way round the Thing can accept messages to enable or disable a Buzzer. To be able to change the sampling rate, in which sensor values will be retrieved and sent to Ditto, the Thing is listening to property change events. Those property changes can be initiated by the WebUI and will have an effect on how often the values are updated. Go on reading, if you want to know more about the setup and how to get it running.

Raspberry Pi with GrovePi+ used gor Grove Control example

Hardware Setup

Prerequisites

Machine

Raspberry Pi

Setup

Setting up your machine

First, let’s start by cloning this project onto the machine:

~ $ git clone <project-path>
~ $ cd ditto-examples/grove-ctrl

For all following shell commands, we assume you are inside ditto-examples/grove-control folder on your machine.

To be able to move on, we need Eclipse Ditto installed and running on the machine. Please follow the guidelines at the Eclipse Ditto project to start Eclipse Ditto.

When you have the Eclipse Ditto services up and running, we can move on and create the Thing that we will use for playing around with our Raspberry. The structure of the Thing can be found inside resources/thing-model.json. To be able to use it, we will use the REST API of Eclipse Ditto to create the Thing.

curl -u ditto:ditto -X PUT -d '@resources/thing-model.json' 'http://localhost:8080/api/1/things/org.eclipse.ditto.example:raspberry'

This command will create the Thing in our running Eclipse Ditto instance. We used the default user/password ditto:ditto to create our Thing. When having a look at the json file we can see another user in the ACL (active control list) of the Thing: raspberry. This is the user we will be using to connect to Eclipse Ditto from the Raspberry. But first of all we need to create the user. Have a look at the README in the docker-folder of the Ditto project. It describes how to create a new password. We will use the password raspberry for our user raspberry. If you want to use a different user/password, have a look at the python/README.MD.

The user should automatically be available after changing the contents of the nginx.httpasswd file. We can check it by using curl to retrieve the Thing with the raspberry user:

curl -u raspberry:raspberry -X GET 'http://localhost:8080/api/1/things/org.eclipse.ditto.example:raspberry'

This should return JSON equal to the thing-model. If it returns a 403 error, you should restart your Eclipse Ditto containers (docker-compose restart).

After creating our Thing, we can commence by opening the WebUI for the first time. @Todo: Philipp

What we should be left with:

Setting up the Raspberry Pi

Before plugging in your Raspberry Pi, connect the GrovePi+ to your Raspberry Pi and connect the sensors as follows:

After that, we can boot the Raspberry Pi and continue by cloning the project and moving into the python folder:

~ $ git clone <project-path>
~ $ cd ditto-examples/grove-ctrl/python

This folder contains several python files that are used to represent the Raspberry as our Thing:

Before we are able to start, we need to set the correct address and port of the running Eclipse Ditto instance in ditto_grove_demo.py:

DITTO_IP = "192.168.1.100"
DITTO_PORT = "8080"

Make sure the Raspberry Pi and your machine are part of the same network and the port DITTO_PORT of your machine is available to other machines on the same network. Then we can start the python script:

python3 ditto_grove_demo.py

The script may take some seconds and then print ### Websocket opened ###. If everything is working as expecte, you should see changes in the WebUI on your machine. The sensor values should be updated once a second.

Playing around with the WebUI

The WebUI uses NPM for dependency management. Therefore you have to run npm install from the webapp root (/webapp) to fetch the WebUI dependencies before you start.

To monitor the sensor values and interact with the Raspberry Pi, view the WebUI in your Browser. Simply open /webapp/index.html. This works without running a webserver.

Running the WebUI from a different domain than your Ditto instance might cause cross origin issues and prevent the site from loading data. You can workaround this by starting your browser without security (e.g. running Chrome with --disabe-web-security).

You should see the following view:

WebUI Main

The top section shows all attributes of our thing rendered in a description list. This info is updated each page reload. Below are three cards, each modelling a feature of our Raspberry/Grove device. Feel free to

The monitor values can retrieved via active polling or server side events (SSE) (configurable). Based on your setup, SSE might not be available as this standard does not provide an option to send auth headers.

You can configure the webapp over the right Configure button in the navbar. In the opened modal you can set your Ditto instance, auth, the displayed thing and the UIs update strategy. Apply your changes by choosing the Save changes action.

WebUI Config

Troubleshooting

Timestamps are updated but sensor values stay the same

There seems to be a problem that causes the connection to the sensors to not work properly. Doing a reboot helps.

Why are the sensors slower than suggested by the sampling rate?

It may be that the sensors aren’t able to respond as fast as the sampling rate suggests them to. The python scripts running on the Raspberry Pi will catch the faulty values and not send them to Eclipse Ditto.

Temperature sensor doesn’t work at all

If the sensor is white, you need to change a parameter in the python script found at python/grove_temp_sensor.py:

# 0 = blue sensor, 1 = white sensor
module_type = 1

If that doesn’t help or apply to you, you should check if the Pi gets enough power from the power supply. You can try to plug-off unused hardware. Most times doing a simple reboot also helps.