Skip to content

ETCE-LAB/FarmInsight-FPF-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FarmInsight-FPF-Backend

A Django-based sensor service that allows configuring sensors, collecting sensor data, and sending it to a remote system based on configurable intervals.

Table of Contents

The FarmInsight Project

Welcome to the FarmInsight Project by ETCE!

The FarmInsight platform brings together advanced monitoring of "Food Production Facilities" (FPF), enabling users to document, track, and optimize every stage of food production seamlessly.

All FarmInsight Repositories:

Link to our productive System: FarmInsight.etce.isse.tu-clausthal.de

Core vision

FarmInsight empowers users to manage food production facilities with precision and ease.

Key features include:

  • User Management: Set up organizations with role-based access control for secure and personalized use.
  • FPF Management: Configure and manage Food Production Facilities (FPFs), each equipped with sensors and cameras.
  • Sensor & Camera Integration: Collect sensor data and capture images or livestreams at configurable intervals, all accessible through the web application.
  • Harvest Documentation: Log and track harvests for each plant directly from the frontend interface.
  • Data Visualization: Visualize sensor data with intuitive graphs and charts.
  • Controllable Action: To control the FPF you can add controllable actions which can perform actions on hardware which is reachable via network.
  • Weather forecast: You can configure a location for your FPF for which a weather forecast will be gathered.
  • Media Display: View and manage captured images and livestreams for real-time monitoring.

Overview

The FPF (Food Production Facility) Backend application collects data from all configured sensors and sends measurements to the FarmInsights Dashboard-Backend based on user-configured intervals.

Built with

Python
Django
SQLite

Features

  • Manage sensor configurations remotely via API.
  • Schedule sensor data collection based on configurable intervals.
  • Send sensor measurements to a remote API.
  • API support for managing sensor configurations.

Development Setup

Prerequisites

  • Python 3.8 or higher
  • Django 3.x or higher
  • SQLite
  • pip (Python package manager)
  • virtualenv (recommended for isolated environments) You need to activate the virtual environment with
source .venv/bin/activate

Step-by-Step Guide

  1. After cloning the repository, move into the following folder
cd .\django_server\
  1. Install the required dependencies for the project using pip:
pip install -r requirements.txt

Setup .env files at:

  • django-server/.env.dev
  • django-server/.env.prod

Example of .env file:

MEASUREMENTS_BASE_URL=http://localhost:3001
GENERATE_MEASUREMENTS=True

RESOURCE_SERVER_INTROSPECTION_URL=<URL HERE>
DASHBOARD_BACKEND_USER_ID=<ID HERE>

MQTT_HOST=<IP of MQTT broker HERE>
MQTT_PORT=<Port of MQTT broker HERE (default is:1883)>

  1. Pull the git submodule:
git submodule init
git submodule update
  1. Run the server via the IDE or via:
python manage.py runserver

Running the Application

You can start the app with the following command. In development mode, there are predefined settings (e.g. a default port) in order for the app to work seamlessly with other FarmInsight projects. Start the app with:

python manage.py runserver

Otherwise, you can also specify the port yourself:

python manage.py runserver 8002

On server startup, the scheduler starts automatically.

Additional Hints

If you already set the FPF-Backend up before and want to start from scratch, please remove the existing config in the local db.sqlite as it contains an API Key for authorized communication with the Dashboard-Backend.

Option 1: Remove the entries from the database manually (e.g. with SQL queries)

Option 2: Delete the db.sqlite file and re-initialize it with

python manage.py makemigrations
python manage.py migrate

API Endpoints

Here are some of the key API endpoints :

  • GET /api/sensors/types - It will return all available sensor configs
  • POST /api/sensors - Create a new sensor
  • GET /api/sensors/{sensorId} - Get the config for given sensor
  • PUT /api/sensors/{sensorId} - Edit a sensor

Sensor scripts

In this repository, you can find PiPico or Arduino Nano IOT 33 code in the .documentation folder. There, you can also find a template script for HTTP and HTTP + MQTT communication.

Available sensor scripts in this repository:

Platform Sensor Type Script / Folder Name Notes
arduino-code Ultrasonic A02YYUW-ultrasonic-sensor A02YYUW sensor
arduino-code Atlas Kit atlas-kit-sensor General Atlas sensor kit
arduino-code Atlas Kit atlas-kit-sensor-calibration Calibration routine
arduino-code pH Sensor Haoshi101-ph-sensor Haoshi 101 pH
arduino-code Soil Sensor npk-soil-sensor NPK (nutrient) sensor
arduino-code Soil Moisture v2-soil-moisture-sensor Moisture sensor v2
arduino-code Temperature & Humidity sht-31-sensor SHT-31 sensor
arduino-code Light Sensor tsl2591-sensor TSL2591 sensor
arduino-code Template template-sensor Basic sensor template
arduino-code Template template-sensor-mqtt With MQTT integration
pi-pico-code CO Sensor co-sensor
pi-pico-code DHT Sensor dht-sensor DHT22

Sensor types supported by the FPF

For most cases you can use a standard http_sensor, mqtt_sensor or http_mqtt_sensor type but if you require special processing of the values you get from the microcontroller for example you can implement your own type as described below.

Follow the template or the existing classes in this sensors package.

πŸ”— Http Endpoint Response

HTTP/1.1 200 OK
Content-Type: application/json
{
  "value": 42.5
}

πŸ“¦ 2. MQTT Topic & Payload

measurements/sensor-id-123
{
  "value": 42.5,
  "timestamp": "2025-06-28T14:35:00Z"
}

or

{
  "value": 42.5
}

When no timestamp is provided, the FPF will insert the current time automatically.

Add new sensor support

To add a new sensor:

  1. First create a new file in fpf_sensor_service/sensors/ <sensor_model>_sensors.py.
  2. Use the boilerplate code from the typed_sensor.template file to setup the basic class structure, name the class accordingly.
  3. Fill out the SensorDescription in get_description() so the frontend can correctly display it as a hardware configuration, for more details on all the types and how to fill it there is further documentation in the sensor_description.py.
  4. Implement the get_measurement() method and init_additional_information() if needed.
  5. Import the sensor class to the __init__.py file so it gets loaded with the rest of the sensor module and the TypedSensorFactory can pick up on it.

If you want to add MQTT functionalities, make sure to pass 'payload' to the get_measurement function, just like in all other MQTT classes. This will be the payload of the sensor.

MQTT support

To enable MQTT, a MQTT broker must be running in the network, so the FPF can connect to it. A common setup is to have a mosquitto broker running on the same raspberry PI. A guide to set it up can be found in the MOSQUITTO.md.

πŸ‘‰ MOSQUITTO.md β€” Full Setup Guide

Once it is set up, you need to configure the MQTT setting in the env.dev file e.g. MQTT_HOST=192.168.178.54 MQTT_PORT=1883 Optionally add username and password in case you configured the broker with it. The FPF will connect to the broker on startup and listens for incoming messages of the sensors which are communicating via MQTT.

πŸ”„ Contribute to FarmInsight

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch: git checkout -b feature/your-feature
  3. Make your changes and commit them: git commit -m 'Add new feature'
  4. Push the branch: git push origin feature/your-feature
  5. Create a pull request.

Past/Present Contributors

This project was developed as part of the Digitalisierungsprojekt at DigitalTechnologies WS24/25 by:

  • Tom Luca Heering
  • Theo Lesser
  • Mattes Knigge
  • Julian SchΓΆpe
  • Marius Peter

Project supervision:

  • Johannes Mayer
  • Benjamin Leiding

License

This project is licensed under the AGPL-3.0 license.


For more information or questions, please contact the ETCE-Lab team.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5