The Python code of this repository is for a Raspberry Pi. It enables the Raspberry Pi to connect to the Datapool web application.
It connects to the web application via the Datapool processor SourcePot\Datapool\Processing\RemoteClient and it can be used within any data app.
The typical application is a motion sensor triggered camera. Here are some examples:
The class SourcePot\Datapool\Foundation\ClientAccess, method request receives requests from e.g. multiple Raspberry Pis. These requests are facilitated by regularly calling up the website ../resource.php. The further processing of these requests require the client to be registered with a Datapool user account. If the registered scope is SourcePot\Datapool\Processing\RemoteClient and the method is set to clientCall, the class RemoteClient will receive the requests containing data (status information, sensory data) and/or files (images, videos) e.g. of a Raspberry Pi. The web application's answer to each request contains the settings for the respective Raspberry Pi.
To setup the Raspberry Pi client you need to create a folder on your Raspberry Pi and copy the following three python files into this folder:
- __init__.py,
- datapoolclient.py and
- sentinel.py
You will need to adjust the url in datapoolclient.py to the web address of your Datapool web application:
Alternatively, the address of the website can be specified in the file
client.json mentioned later.
When sentinel.py runs for the first time, datapoolclient.py will create a set of sub-directories. You will need to adjust the client.json file in the newly created settings sub-directory.
Important
The client.json file contains the access details, such as the Datapool web application url, the client_id and client_secret.
These details must match the client registration within the Datapool web application.
You will need to register the new client with one of the Datapool user accounts. To do this, go to 'Admin' → 'Account' and expand 'App credentials' (Lock symbol).
Remember the registered client will have the same privileges as the user of this account but limited to the selected scope. The scope for the Raspberry Pi client must be class SourcePot\Datapool\Processing\RemoteClient and the method clientCall. The method clientCall of class SourcePot\Datapool\Processing\RemoteClient will handle the client requests.
The following screenshot shows the registration of the client in row 0001:
Multiple Raspberry Pis can run as clients with the same client_id. The Raspberry Pis are are distinguished by their location and the location of a Raspberry Pi is set by entry={'Group':'Town','Folder':'Address','Name':'Location'} in sentinel.py. Just replace Town, Address and Location with the correct values.
When a Raspberry Pi connects for the very first time to the Datapool host a database entry is created based on Town, Address and Location in the Datapool database table remoteclient. The entry's EntryId is {baseEntryId}_definition and contains the html widget template definitions for status and settings. These are provided by the Raspberry Pi, i.e. the Raspberry Pi's Python script defines the user interface within the Datapool web application. The settings, that control the Raspberry Pi, will be stored in the same table as entry with an EntryId="..._setting". The latest data provided by the Raspberry Pi is saved as entry in the same table with the EntryId={baseEntryId}_lastentry.
Initially the client needs to request a new access token. Aften having received the access token, the client can send client (status) data such as measurements, senosor data and optionally media files such as images or movies (POST request). The Datapool host answers the host request with new settings data for the client.
The RemoteClient processor provides an user interface which is actually defined by the Python code of the remote client itself. Within the sentinel.py file the Python dictionary entry, key Content||Settings||... defines the control elements and entry key Content||Status||... the status elements (display). This allows each remote client to provide different functionalities and to specify its' specific control elements (buttons, drop-down menus, slider etc.) for the Datapool web application.
In this example, the operating system of the Raspberry Pi is Debian Trixie. The directory containing the Python files of the Pi client is /home/carsten/Pi/.
First, a new service for systemd is created, which executes the file sentinel.py every time the system boots, which then runs in an infinite loop.
sudo -i
cd /etc/systemd/system/
nano pi.service
The last command opens a new file pi.service in the editor, to which the following content is now added:
[Unit]
Description=This service starts the RaspberryPi Sentinel
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/carsten/Pi/sentinel.py
WorkingDirectory=/home/carsten/Pi/
Restart=always
User=carsten
[Install]
WantedBy=multi-user.target
The following commands are used to adjust the access rights:
sudo chmod 744 /home/carsten/Pi/sentinel.py
sudo chmod 664 /etc/systemd/system/pi.service
Finally, the new service pi.service must be activated:
sudo systemctl daemon-reload
sudo systemctl enable pi.service
The next time the system starts up, pi.service will be executed and this will start the Python script sentinel.py. To test whether the service was executed correctly or whether there were any errors, the status can be checked as follows:
sudo systemctl status pi.service