Setting up and running LF-Edge's Fledge (from source) on a Raspberry Pi 4
Tags: iot, raspberrypi4, iiot, edgecomputing
Published: 2021-07-30 10:03:35

## Fledge

> Fledge is an open source framework and community for the industrial edge focused on critical operations, predictive maintenance, situational awareness and safety. Fledge is architected to integrate Industrial Internet of Things (IIoT), sensors and modern machines with the cloud and existing “brown field” systems like historians, DCS (Distributed Control Systems), PLC (Program Logic Controllers) and SCADA (Supervisory Control and Data Acquisition). All sharing a common set of administration and application APIs.

I wanted to give [Fledge](https://github.com/fledge-iot) a try to see how one can bring an open-source IoT/IIoT Stack to a quick realization.

## Getting Hands Dirty already!

### Hardware

__Device__: __Raspberry Pi 4 Model B__
__Spec__: 2GB RAM, 32GB Micro SD Card
__Operating System__: __Ubuntu LTS 20.04.2__

### Setup

I am going to just clone stuff in my home directory

1. Clone the [fledge Repository](https://github.com/fledge-iot/fledge)

        $ git clone https://github.com/fledge-iot/fledge.git && cd fledge/

2. Install the Dependencies (in the root directory of fledge)

         $ sudo ./requirements.sh

3. Let's install fledge on the Pi

        $ sudo make install

    grab something to munch on till the installation is complete

4. The installation will add the files and binaries in to the following directory: `/usr/local/fledge/`. You can export this path:

        $ export FLEDGE=/usr/local/fledge

5. start Fledge and check the status using the following:

        $ $FLEDGE/bin/fledge start
        $ $FLEDGE/bin/fledge status

6. Checkout the Fledge UI by hitting the Pi's IP Address: `http://<IP_ADDRESS_of_PI>`

At this point of time, Fledge is very _bare-metal_ which implies there are lot of plugins that are missing. We begin by installing a very basic _simulated_ plugin.

### South-Plugin Installation

#### Sinusoid 

Since we are doing things by source, we need to clone the plugin repository. We start with a very basic `sinusoidal` South Plugin which will generate sine waves to the Fledge Core.

1. clone the [`fledge-south-sinusoid` plugin repository](https://github.com/fledge-iot/fledge-south-sinusoid)

        $ cd 
        $ git clone https://github.com/fledge-iot/fledge-south-sinusoid.git && cd fledge-south-sinusoid/

2. we just need to copy the source code of the sine-wave generator to the directory path of plugins for Fledge

        $ # we are currently in the root directory of fledge-south-sinusoid
        $ sudo cp -r python/fledge/plugins/south/sinusoid $FLEDGE/python/fledge/plugins/south

You can refresh the UI and click on the __South__ Menu and you will be able to see `sinusoid` as a __South Plugin__

#### CoAP

Let's get one more Plugin into Fledge: CoAP plugin

1. clone the [`fledge-south-coap` plugin repository](https://github.com/fledge-iot/fledge-south-coap)

        $ cd 
        $ git clone https://github.com/fledge-iot/fledge-south-coap.git && cd fledge-south-coap/

2. Let's copy the plugin's code to Fledge as we did previously

        $ # we are currently in the root directory of fledge-south-coap
        $ sudo cp -r python/fledge/plugins/south/coap $FLEDGE/python/fledge/plugins/south

3. Copy the python dependencies that are necessary to execute the plugin and install them:

        $ sudo cp -r python/requirements-coap.txt $FLEDGE/python/
        $ sudo pip3 - r $FLEDGE/python/requirements-coap.txt

4. Since the code is in the `/usr/local/` directory we need to provide root privileges to the coap plugin for execution by the Fledge Core

        $ sudo chown -R root:root $FLEDGE/python/fledge/plugins/south/coap

That's it ! Now you have we two South Plugins in our Fledge Application that can provide us some simulated data!

Your UI __South__ Menu should look like the following:

![Sinusoid & CoAP Plugins in the Fledge UI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm1f7kwbt9ohedh87hdc.png)

## Setup / Data Generation for Sinusoid Plugin

1. Let's setup the Sine-Wave Generator by naming it `test-sine-south` and click __Next__

    ![Sinusoid Plugin Setup in Fledge UI. Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4oc0bxiksywblr1mxf4s.png)

2. Name the __Asset Name__. We name it `sine-wave-asset`

    ![Name the Asset in Fledge UI. Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57fcmuvxxoi25h4xtbts.png)

3. Make sure to enable the plugin by checking the __Enabled__ checkbox and finally click __Done__

On the UI you now see the Sinusoid Plugin Enabled with active readings being fed into the Fledge Core. You can click on the link `test-sine-south` to change the configuration of the plugin / delete it or export readings to a file.

You can click on __Assets & Readings__ Menu and click on the Graph Icon to get some visualization delete it or export readings to a file.

![Assets & Readings of Sinusoid Wave](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uou30gkzoi27qev2s8sa.png)

And there you have visualization of the data coming in from your South Plugin, a lovely sine wave. Not practical but you get to understand how the Fledge Architecture would work!

![Data Visualization of Sine Wave Asset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e9867eqjv08k8cq8infz.png)

You can click on the __Summary__ tab to get some basic statistics of the information

## Setup / Data Generation for CoAP Plugin

1. Let's setup the CoAP South Plugin similarly to Sinusoid by clicking on the __+ Add__ button on the top-right of the Panel under __South__ Page

    ![Setting up CoAP South Plugin: Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/54qloscll9hnh1vgb63o.png)

2. Call the service `test-coap-south` and click __Next__

    ![Naming CoAP Service: Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq2938t0y4171q0qnmp0.png)

3. In the next step keep the configuration as it is for now i.e. Port as `5683` and the URI as `sensor-values` and click __Next__ and enable the plugin on the last setup.

This should enable the CoAP Plugin for Fledge and it's time to generated some CoAP data from a simulated client to a CoAP Server running on the Pi.

Fledge does provide some testing information for CoAP in the core directory under the the CLI called `fogbench`

Let's generate some CoAP data on the Pi.

```bash
$FLEDGE/bin/fogbench -t $FLEDGE/data/extras/fogbench/fogbench_sensor_coap.template.json -I 100
```
here `-t` flag is for mentioning a template and we use a coap template provided by Fledge mentioned above. `-I` is the time interval

Upon executing the command on the Pi, you can check the UI which will generate the following output for the CoAP Plugin.

![Assets for Fogbench CoAP Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xia5fphgrxxkftlm8oep.png)

You can explore the Assets and Visualization by going to __Assets & Readings__ and clicking on any of the asset you wish to observe. 

> NOTE: In order to visualize live-data you can run the `fogbench` command above a couple of more times.

![Statistics for Luxometer Asset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1m30qkuk7v290t6xfnqj.png)

## Filter Plugins in Fledge

The sine wave isn't that cool to look at to so let's get some Frequency Domain Analysis on it with a __Fast Fourier Transform__ on it using Fledge's FFT filter plugin

1. Let's clone the plugin in the home directory

        $ cd 
        $ git clone https://github.com/fledge-iot/fledge-filter-fft.git && cd fledge-filter-fft/

2. This plugin is not python based so we need to do some tasks for installing it. We export a `FLEDGE_ROOT` variable and create a `build` directory in the root of the repo

        $ # we export the directory where we built Fledge for the filter plugin to use
        $ export FLEDGE_ROOT=~/fledge/
        $ # in the root of the `fledge-filter-fft` repo
        $ mkdir build && cd build/

3. compile the plugin

        $ cmake ..

4. install the plugin

        $ sudo make install

Now you have the FFT plugin ready to be applied in Fledge

### Applying FFT on the our Sine-Wave Asset

1. Let's head back to the __South__ Plugins in the UI and click on the `test-sine-south` service

2. Click on the __Applications__ menu for the Service 

    ![UI for test-sine-south Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnkcabc64w9tmspti7ul.png)

3. Select `fft` from the available plugins and name the service `test-fft-sine`

    ![Name the FFT filter Plugin Service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aon94xjylbxsosal3btk.png)

4.  Setup the Service to conduct filtering on the asset called `sine-wave-asset` (See steps to setup Sinusoid Plugin) and other parameters as shown below (no fine tuning done here). Don't forget to click the __Enabled__ checkbox and finally __Done__

    ![Configuring FFT Filter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yleb6l3yo1ln7vxv7gru.png)

The Fast Fourier Transform might take some time to show up but you can head to __Assets & Readings__ and click on the `sine-wave-asset FFT`'s Graph Icon to see some visualizations. This is what mine look like

![FFT of the sine-wave-asset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dncghkughxwtg5or1ytr.png)


Great stuff. Imagine having vibration sensors and you need to conduct FFT analyses on it! Fledge makes it nifty!!