changed 3 years ago
Published Linked with GitHub

A simple experiment using TIG-STACK for implementing a Portable Environment Analyser

Olof Magnusson, om22pq@lnu.se

Short project description

This project is built upon developing a Portable Environment Analyser for Plants with evaluating values such as: temperature, water, soil, humidity, carbon dioxide, volatile organic compounds for improving cultivation.

The idea of this experiment is to simulate a client to server communication by using a TIG-stack, where we use Docker for implementing containers and establish the connections, Telegraf as the data agent, InfluxDB the database and Grafana for displaying/monitoring the data using TTN as the middleman of the intercourse. Later on, we are going to be using machine-learning for analysing historical data for being able to analyse any interesting patterns.

This tutorial requires no prior knowledge of computers or programming, thus it is developed for individuals who want to be acquainted with IoT and MicroPython.

Objective

The reasoning behind developing this device represent the fact that I want to investigate the possibility to improve cultivation with using variously of sensors and analyse their cohesion. As such, the data gained from the experiment will be analysed to find patterns or signs of deviations. Which could serve the information about which condition is the optimal for the growing process of the plants.

As I did not possess any prior knowledge of electronics, this project did in turn take time to complete. Specially with learning the hardware essentials, soldering, connecting and developing the prototype. The amount of time for the project is estimated to be 10-15 hours/week converting that into approximately 40-50 hours for the whole project.

BEFORE USING ELECTRONICS!

  • Understand the datasheet, device and the essential wiring
  • Threat the device as they are live or energized.
  • Disconnect the power source when fixing the wiring or applying a new sensor
  • Use tools (multimeter, voltmeter) for testing current, voltage and power of the sensors
  • Make a circuit diagram to get a more comphrensive understanding of the wire connections on the device. I recommend Orcad
  • When using LoRa, connect the antenna to the 868 MHz frequency before running any software. There is a chance that it could potentially damage the device otherwise.
  • And the most important Have fun! :)

Getting started

Internet of Things

Maybe have you already asked yourself about what the buzzword Internet of Things actually means? It can be simplified by subdividing applications into three groups like Comfort, Health and Security. The first group Comfort is where the applications main goals are to automate daily tasks and decrease the burden for the user. Examples of these applications are Roomba, an automated vacuum cleaner, Automated indoor temperature controls and Voice-controlled virtual assistants like Amazon Alexa and Google Home.

The second group is Health where the application monitores various aspects of the user's health parameters. The sensors can measure heart-rate, sleep activity, body temperature along with many others and be remotely monitored by a health care responsible.

The third group is 'Security' where the appliances' goal is to increase the physical home security. Devices like surveillance cameras, smart door locks, pressure sensors on floors, magnetic contact sensors installed in windows, IR sensors and alarm sirens. The appliances in this category can be monitored, accessed or controlled locally and remotely via Internet.

  • Video explaining the Internet of Things (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Python

The Pycom device is built upon using MicroPython and therefore I recommend to go through the Python fundamentals before implementing any code on the device. However, if you have background in Java/C/C++/C# there will be no difficulties to get going.

It is possible you already asked yourself why we are using MicroPython instead of C. There is no lie that C provides better performance like speed, memory and battery life. The Chip in Pycom have to run Python as an interpreter instead of the code, which makes iterations much slower and utilize more power.

However, the primary reason using MicroPython is its simplicity compared than C with implementing arrays/loops and deploying data structures. The focus is not about the time complexity of algorithms, and it is rather to learn about electronics and building applications to gain insight about the data using different platforms.

  • Python fundamentals Francis Palma, PHD Linnaeus university (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

List of material

Lop4 + expansion board (3.0 - 3.1 Development Board)

The LoPy4 constitute the base material for all the different components with LoRa, Sigfox, Wifi, Bluetooth capabilities. It is an ideal IoT platform for the connected sensors as a brief overview of the essential information can be shown in the illustration below. Thanks to Pycom for the pictures.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
For more information regarding the datasheet ➜ Datasheet

  • Hardware setup(English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

DHT11 Ref Electrokit

  • Combined sensor that measures both temperature and humidity.
  • The sensor is calibrated and needs only an external pullup resistance of ~ ~5kohm.
  • Technical specifications:
    Operating voltage: 3 - 5VDC
    Power consumption: 0.5 - 2.5mA
    Sleep current: 100 - 150uA
    Measurable range: 20 - 80% RH
    Working temperature: 0 to + 50 ° C
    Accuracy (RH): ± 5%
    Accuracy (temp): ± 2 ° C

The reasoning behind using DHT11 instead of DHT22 represent the fact that the sensor operates under exceptionally reduced cost. Note, however, that DHT22 could be considered more accurate for a more extended range.

  • DHT 11 Set-up (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Technical comparsion of DHT 11/22 using AdaFruit:

DHT 11
  • Ultra low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 4 pins with 0.1" spacing
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
DHT 22
  • Low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 0-100% humidity readings with 2-5% accuracy
  • Good for -40 to 80°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1" spacing
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Passive Buzzer Module (Ref Electrokit)

The Passive Buzzer Module is a way of generating tones between the 1.5 to 2.5 kHz by switching different frequencies. Which serves a excellent purpose with playing Super Mario tone when a connection to LoRa is successful.

  • Operating Voltage 1.5 - 15V
  • Frequency range 1.5 – 2.5kHz
  • Dimensions 18.5mm x 15mm [0.728in x 0.591in]

Vellman VMA303 - ARDUINO® COMPATIBLE SOIL MOISTURE SENSOR + WATER LEVEL SENSOR MODULE X3

The sensor measures the resistance for both the soil and liquid's level, as the voltage increase the value depending of the depth of immersion. Since we do not use Arduino we determine when soil and the liquid level by analysing its resistance. Where low resistance indicates the plants are wet — high volt, wherein high resistance indicates that the plants are dry — low volt. Can sense up to 4 cm of water. One interesting note is that the voltage is unconstant which constitutes a problem with the device.

  • Returns a numeric range of 0-4095 of all ADC inputs that is 0-1v
  • Maximum range when using the 11db attenuation is 0-3.3V.
  • Supply voltage: 3.3 - 5V
  • Output: 0 – 4.2V (@ 5V)
  • Power consumption: 35mA

Note: The guidelines tell "Indoor use only" which I do not really apply for the sensor. I have been testing this sensor for two weeks now, and it looks like that, the device is working functionally both inside and outside.

CSS811

The CSS811 is a digital gas sensor that sense a broad range of Total Volatile Organic Compounds (TVOC), carbon dioxide (eC02) and metal oxide (MOX) levels. Credits to Adafruits for the pictures.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Technical specifications Ref Electrokit

  • Total Volatile Organic Compound (TVOC) sensing from 0 to - 1,187 parts per billion
  • eCO2 sensing from 400 to 8,192 parts per million
  • Five Operating Modes
  • Integrated MCU
  • Onboard Processing
  • Standard I2C Digital Interface
  • Optimized Low-Power Modes
  • Optional NTC Thermistor Pins
  • Power Consumption 30 mA
  • CSS811 Set-up (English)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Photoresistor

Module with a light-sensitive resistance, used as a single light sensor determining the amount of light output. Depending on the intensity of the light, its resistance changes with the current in the circuit.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
  • Returns a numeric range of 0-4095 of all ADC inputs that is 0-1v
  • Maximun range when using the 11db attenuation is 0-3V
  • Supply voltage: 3.3 - 5V
  • Output: 0 – 4.2V @ 5V
  • Power consumption: 35mA

Building the prototype for the Pycom Device

The design of the case is created by Alexander Pennerup, and provides a way of developing the prototype for the Pycom device depending on your specification. It is required to make some changes if you would like a breadboard with sensors together with the Pycom device (the sensors do not fit with the provided build, unable to close). Everything is the same but the height of the box is now 53 mm instead of 33 mm, which can be found at the github repository. Here is some results:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
For more 3D-printing-models ➜ applied-iot-20

  • Video explaining 3D printing (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Video explaining 3D printing (Swedish - a must-watch)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Test-setup/Electronic calculations

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

How do we perform the test?

Before performing the test using voltimeter, it is important to know the Analog to Digital Conversion (ADC) reference voltage. Which defines the ceiling regarding what value ADC actually can convert. Measuring the ADC can be done in the following way:

import machine adc = machine.ADC() # create an ADC object apin = adc.channel(pin='P16') # create an analog pin on P16 val = apin() # read an analog value measure = adc.vref(vref) # to compare values print(measure)

It is significant to know that the range of ADC's pin is 0-1.1V. The maximun value of the ADC could be accomplished using the highest attenuation of 11dB. Do not, what so ever, exceed that maximun of 3.3V, as this could potentailly damage your device.

A example testing the voltage of the sensor Photoresistor, which is a good way to confirm that the devices are operating in a correct way. Current can be measured by taking one wire to the VCC on the breadboard and one wire to the plus on the sensor. Voltage can be measured by taking one wire to the ground and the other one to the plus on the sensor.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

The current through the control circuit itself has not been measured, instead the current through the sensors has been measured to give an indicator of its consumption. However, the main reason why we are getting a bit loss from 5V (ADC-sensors), is the fact that ADC has a non-linear response with the input-referred noise.

For an example: C02 is calculated, converting from microamphere, (0.29 * 10^-3) = 0.00029 * 3.24 = 0.00147).

Devices Current(mA) Voltage(V) Power(mW)
Photoresistor 0.00035 4,2 0.00147
Co2 0,00029 3,24 0.0009396
DHT 11 0,12 3,24 0.3888
VMA303 PLANT 1(water) 0.000026 4,13 0.00010738
VMA303 PLANT 1(moist) 0.000032 4,28 0.00013696
VMA303 PLANT 2 (water) 0.00004 4,2 0.000168
VMA303 PLANT 2 (moist) 0.000058 4,2 0.0002436
VMA303 PLANT 3 (water) 0.00004 4,39 0.0001756
VMA303 PLANT 3 (moist) 0.00002 4,05 0.000081

Well, good result from cheap sensors. Due to the fact that the device is needed to be portable, we have to calculate the Battery Life of the attached PowerBank. This can be accomplished by:

Device mHa
Powerbank 24000mHa
VMA303 * 6 210mA
DHT11 2.5mA
CCS811 30mA
Photoresistor 35mA
PWM 30mA
LoRa transmit 54mA
Result 361mA
Formula: Battery Life = Battery Capacity in mAh / Load Current in mA.

Which give us:

Battery Life = (24000mHa / 361) = 66 Hrs 28 Min

Note: This is just an estimated output, and it is important to understand that this is a cheap PowerBank, so maybe it is needed to upgrade that particular device for the longer term.

Where you bought them and how much they cost?

Devices COST Company Link
Lop4 + Expansion board 799SEK Electrokit Electrokit
DHT 11 43SEK Electrokit Electrokit
Piezohögtalare aktiv 39SEK Electrokit Electrokit
CSS811 Co2/Voc 329SEK Electrokit Electrokit
Vellman VMA 303 - 3x 420SEK Kjell och Company Electrokit
Total = 1630 SEK
Equipment for Wiring COST Company Link
Clutch wires for clutch tires 36SEK Electrokit Electrokit
Laboratory cable 20 pin 15cm female / male 29SEK Electrokit Electrokit
Laboratory cable 20 pin 15cm male / male 29SEK Electrokit Electrokit
Breadboard 400 connections 59SEK Electrokit Electrokit
BreadBoard 840 connections (useful for testing) 69 SEK Electrokit Electrokit
Total = 222SEK

Computer Setup

Describe all steps from flashing the firmware, installing plugins in your favorite editor. How flashing is done on MicroPython.

Hardware (taken from Offical Pycom)

Before connecting the hardware to the system, it is important that the Expansion Board 3.0/3.1 is updated. This process varies depending on which operating system is used.

In Windows

  • Software
    DFU-util v0.9 for uploading the firmware to the Pytrack/Pysense
    Zadig – Installer tool for the Pytrack/Pysense board DFU Firmware
    To uploaded the latest DFU firmware to the Pytrack/Pysense, first install the DFU drivers to the host computer. Open Zadig and select libusbK as the driver. Note: that the Pytrack/Pysense needs to be in DFU-mode.

To install the drivers, the Pytrack/Pysense board must be in DFU-mode:

  1. Disconnect the USB cable
  2. Hold down the button on the shield
  3. Connect the USB cable
  4. Keep the button pressed for at least one second
  5. Release the button. When the board is connected in DFU-mode, it will be in this state for 7 seconds.
  6. Click the Install Driver button immediately. If the driver was unsuccessful, repeat from step 1.
  7. Here the USB ID has to be the DFU-bootloader one (0xF014for Pytrack or 0xF011 for Pysense).
  8. This is a successful DFU driver installation for Pytrack

Open the command prompt and navigate to the directory where the DFU-util and the firmware was downloaded (must be in same directory). Repeat the procedure to get the board in DFU-mode and run the command below but replace X.X.X with the firmware version and replace Pysense with Pytrack if it is the Pytrack that is to be updated (e.g: pytrack_0.0.8.dfu):

dfu-util-static.exe -D pysense_X.X.X.dfu

If the update was successful, a message,“Done!” should appear in the bottom of the command prompt.

Double-check Serial USB (CDC) driver is installed in Application mode: if, by mistake, the libusbk driver was installed while the USB ID is the Application mode (0xF013 for Pytrack or 0xF012 for Pysense), then the Serial USB (CDC) driver has to be installed for application mode. This will allow Windows to allocate a COM port, which is required for REPL console.

In Linux

  • Flashing the expansion board Pycom - Mac/Linux (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Chosen IDE

  • Chosen IDE
  • I have been using Visual Studio Code for Web Application Development which was a natural choice for me. There are many, in my opinion, advantages with using this integrated development environment. One of them is the simplicity of using extensions as: Docker, Git, Python for Visual Code and Javascript as some of them can be visualised below. The installation process is simple, just search for the extension, add it and you are ready to go!

  • How the code is uploaded

The code is uploaded using the Pymakr Console, which is a very spot on tool to use with embedded feature Upload project to your board.

  • Steps that you needed to do for your computer. Installation of Node.js, extra drivers, etc.
  1. First we need to install Nodjs for the interaction of Pymakr. Install Node js

Secondly, we need to install Microsoft Visual Studio Code.

  1. Install the lastest Microsoft Visual Studio Code IDE Install Microsoft Visual Code
  2. Open Microft Visual Code
  3. Go to the extensions
  4. Add the Pymakr plugin
  5. Add the Pymakr.conf, for uploading the code to the Device and to etablish a connection.
{ "address": "192.168.4.1", "username": "micro", "password": "python", "sync_folder": "", "open_on_start": true, "safe_boot_on_upload": false, "py_ignore": [ "pymakr.conf", ".vscode", ".gitignore", ".git", "project.pymakr", "env", "venv" ], "fast_upload": false }

Putting everything together

The circuit diagram shows how the sensors are connected to the Pycom device. This is built upon Orcad with the intension to create a real circuit and at the same time to analyse if it is possible to have this setup in the future. Could it been use in production? Well it is pretty hard to say. Wireless sensors would be a more ideal use, as this setup could be end up in fuzzy wiring.

Platform

  • Describe platform in terms of functionality
  • Explain and elaborate what made you choose this platform (advanced)

The Things Network (TTN)

Before we can setup docker and the TIG-stack, we need to setup the username and password on our application at the plattform TTN. Which is a way of building a open LoRaWAN network for building the application on low cost and at the same time providing sufficent security features acting as middleman for the distribution of information.

Setup

  1. Register an account at Thethingsnetwork Just follow the steps!

  2. Create an application (if you do not have a gateway)

  3. Add application

  4. Adding information to your application

Great job! We have created our first application!

  1. Register a device by clicking on Get started by registering one

  1. Final step of registering the device
  • Device EUI can be identified by using:
    print("DevEUI: " + ubinascii.hexlify(lora.mac()).decode('utf-8').upper())

When succesfull, it is time for applying some code. Make sure, before connecting to LoRa to attach the antenna to the right socket.

Apply the information into the Device EUI. The App key will be autogenerated. Place the keys into keys.py

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Code for LoRa ➜ LoRa.config

APP_EUI = 'your EUI key' 
APP_KEY = 'your APP-key'

We are in turn going to use OTAA authentication parameters in LoRa.py

app_eui = ubinascii.unhexlify(keys.APP_EUI)
app_key = ubinascii.unhexlify(keys.APP_KEY)

Use .gitignore for the keys.py when distributing the configuration.

  • Please feel free to follow the series below LoRa 1 - Setup for more information regarding TTN (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Telegraf, InfluxDB, Grafana (TIG-Stack)

The TIG (Telegraf, InfluxDB, Grafana) are open-source platforms which provides the possibility to collect, store and graphing on time series data. The reasoning about using Docker for storing data in influxdb and telegraf for the transportation (using MQTT) and fetch data from the thethings network.

  • Installation
    Docker (Required)
    Kitematic (Run containers through a simple powerful graphical user interface)

Time for some code! The services configuration is specifing that we are going to use Grafana, InfluxDB, Telegraf with the keyword services and the image (docker hub image) that we are going to use. As well as which ports the applications should listen to. Moreover, the container-name defines the alias of the container. Restart: unless-stopped starting the containers automatically when starting the computer or if the application closes with the exeception of manually stopped. The containers will find each other by using docker bridge network tig-net.

Setting up enviroment for SMTP server for sending e-mail through grafana could be done in severals way. Here is a neat way of doing it is by setting the enviroment variables in the Grafana configuration:

environment: SERVICE_NAME: app SERVICE_TAGS: dev
  • Creating services and grafana
services: grafana: image: grafana/grafana:latest container_name: grafana restart: unless-stopped tty: true ports: - "3000:3000" networks: - tig-net;
  • Creating InfluxDB - Adding environment, volumes

Copy influxdb/influxdb.env.examle to influxdb/influxdb.env to configure influxdb

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
InfluxDB, configuring environment and config ➜ Infuxdb.conf.env

influx: image: influxdb:latest container_name: influxdb restart: unless-stopped ports: - "8086:8086" env_file: - "./influxdb/influxdb.env" environment: INFLUXDB_HTTP_AUTH_ENABLED: "true" INFLUXDB_DB: "iot" volumes: - "./influxdb/data:/var/lib/influxdb" networks: - tig-net
  • Creating Telegraf - Adding volume and dependencies

Copy telegraf/telegraf.conf.example to telegraf/telegraf.conf.example then configure telegraf

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Telegraf, configuring environment and config ➜ Telegraf.conf.env

telegraf: image: telegraf:latest container_name: telegraf restart: unless-stopped tty: true depends_on: - "influx" volumes: - "./telegraf/telegraf.conf:/etc/telegraf/telegraf.conf:ro"
  • Defining the way how the containers are going to communicate, and in this case through the network tig-net.
networks: tig-net: driver: bridge

Use .gitignore for influxdb/data/* influxdb/influxdb.env telegraf/telegraf.conf when distributing the docker configuration.

  • Start-up
    Run docker-compose up -d to start containers and setup network.
    Run docker-compose ps to check the state of the containers

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
For more commands ➜ Docker cheat sheet

Check that the tig-stack is running by open Docker desktop.

You should be able to see something like this when typing localhost:3000 in the browser:

This project will setting up an Grafana bot using the Discord Application, making it possible for sending triggers to multiple users to get the current state of the sensors.

Grafana setup

  1. Download the Discord Application Discord

  2. Create your first server

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Create a new panel in Grafana.

  • From : is where the data will be loaded from
  • Select : selecting the data to be used
  • Group by: grouping the data into intervals
  • Format AS: How the data is going to be displayed
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Use Create Alert for creating a Alert.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Specifying the conditions of the alert(s)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Adding a text message for the trigger

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Video explaining the TIG-stack and why it is useful (English)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

For the experiment, I choosed the TIG-stack - using Docker with Telegraf, Grafana, InfluxDB with TTN for the simplicity and to increase the flexibility in the applications. However, I tried out different platforms like Amazon, Microsoft Azure but I wanted to have a local installation for developing and testing. As the Telegraf is acting as the data agent which handles the MQTT transportation, InfluxDB the database and the Grafana is used for monitoring the sensors.

This was the best solution for the parameters security and privacy. As I didn't see the point of publishing the data online. This project will be continued, I'm planning on building a Water Control System and Node-Red could be an alternative when the project expands.

The code

Since it is not possible, yet not efficent to distribute the overall code here, it is only intended to show the most interesting one.

How can we compress our data with focus on optimization? As shown in the documentation, we know that the values for our ADC is from between the interval 0-4095. We can then, demonstrate, with trivial mathematics to prove a better resolution for our data by dividing \(4095/16 = 256\) which is our limitation for the ADC. Use this in a more practically context we get:

def waterPlantOne():
    plantOne_val = apin()
    print("WaterOne %d" % plantOne_val) #Used for comparing
    return (plantOne_val/16)

Further, we use that value for packing the data. Thus, taking the value from the ADC multiplying by (256/256) to reduce the resolution of the data. As on the other side (the payload) we implement multiplication for maintaining the data into the same original resolution:

package = (struct.pack('>H',int(co2) ) + struct.pack('>H',int(voc) ) + struct.pack('>B',int( (photoRes) * (256/256) )) + struct.pack('>B',int( (soilMoisturePlantOne) * (256/256) )) + struct.pack('>B',int( (soilMoisturePlantTwo) * (256/256) )) + struct.pack('>B',int( (soilMoisturePlantThree) * (256/256) )) + struct.pack('>B',int( (waterLevelPlantOne) * (256/256) )) + struct.pack('>B',int( (waterLevelPlantTwo) * (256/256) )) + struct.pack('>B',int( (waterlevelPlantThree) * (256/256) )) + struct.pack('>B',int( (dth_T+40) * (256/125) )) + struct.pack('>B',int( (dth_RH) * (256/100) )) )

The payload function:

return {
        co2: co2,
        voc: voc,
        photo_res: (photo_res * 16),
        soilMoisturePlantOne: (soilMoisturePlantOne * 16),
        soilMoisturePlantTwo: (soilMoisturePlantTwo * 16),
        soilMoisturePlantThree: (soilMoisturePlantThree * 16),
        waterLevelPlantOne: (waterLevelPlantOne * 16),
        waterLevelPlantTwo: (waterLevelPlantTwo * 16),
        waterLevelPlantThree: (waterLevelPlantThree * 16),
        dht_temp: (dht_temp) / (256 / 125) - 40,
        dht_RH: dht_RH / (256 / 100),
    };
}

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Code for the main application ➜ Main application

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Code for Docker compose (TIG-stack) ➜ Docker

So how exactly are we reading the data? The readings that are from ADC connected sensors does not have such great precision that we need specific reading so we can reduce the byte required to send the reading. Instead, we divide the ADC reading by 16, so it can fit the data into one byte. Which provides efficency and less battery usage by reduces the total LoRa payload with almost half of the current size.

In the TTN payload format, we multiply with 16 to keep the value range the same as the ADC reading range. The picture below shows a illustration of the reading (cyan) compared to the decoded TTN value (green).

Transmitting the data / connectivity

  • How often is the data sent?

For data mining, the data was sent every 5 minutes to understand the pattern (how does the data work?) and to predict the data. Sending data too fast resulted in outliers, which serves the indicator that the sensor was unable to read a correct value. However, when the system is running normal, it is good enough to send the data every 30 minutes. This saves computing memory and battery within the system.

  • Which wireless protocols did you use (WiFi, LoRa) / Elaborate on the design choices regarding data transmission and wireless protocols. That is how your choices affect the device range and battery consumption?

The project is using LoRa for enabling data communication over a longer range due to the fact that this prototype is implemented to be portable and to not consume much power, which is important for embedded devices. The problem with using WIFI/BLE based networks is that these portocol requires higher bandwidth and not so reliable for longer range. Another problem is how frequent traffic is required to be sent to keep a connection. After some initial data collection, the rate of the readings transfered could be reduced when the outcome becomes more predicable with the help of machine learning models.

  • Which transport protocols were used (MQTT, webhook)?

MQTT are implemented in Telegraf for connecting to the thethingsnetwork, which could be considered more effecient for embedded information exchange with its Pub/Sub methology. The Webhook was used in Grafana for sending different alerts depending on the state of the sensors.

Presenting the data

  • Provide visual examples on how the dashboard looks. Pictures needed.

The data is presented for two days, where it is possible to withdraw the different state from the senors such as Temperature, Humidity, Water-level, Soil Moisture and Air Quality for two days. Some conclusion have been made, as when there is much water the sensor react with higher voltage around 2.500-3.000mV which increase the negativity to have more pests in the plants. Around 1.500-2.100mV seems to be the optional way of growing plants in the most efficent way. Atleast for this setup.

  • How often is data saved in the database.

When a message is recieved from the TTN networks MQTT Broker and in this case every 5/30 minutes depending on the which type of test is performed.

  • Explain your choice of database.

I choosed to have a Time series database (InfluxDB) to analyse historical data to make good predicitions. The database provides in turn a good way of integrating other platforms such as TTN.

  • Automation/triggers of the data.

The automation and triggers are implemented using WebHook, which provides the possibility for sending alerts through Grafana to a Discord server, where many individuals can monitor the alerts at the same time.

Finalizing the design

The picture below shows the final result of the project, where the Grafana dashboard can be visualised monitoring different values such as Temperature, Humidity, Water-level, Soil Moisture, Air Quality and Light with alarms attached. This provides the essential information regarding the enviroment to optimize cultivation.

Using machine learning model ZeroR trying to predict the current moist_temperature:

=== Run information ===

Scheme:       weka.classifiers.rules.ZeroR 
Relation:     Soil Moisture-data-as-join by time-2020-07-07 17_01_33
Instances:    474
Attributes:   4
              Time
              Soil_Moisture_Plant_One
              Soil_Moisture_Plant_Two
              Soil_Moisture_Plant_Three
Test mode:    10-fold cross-validation

=== Classifier model (full training set) ===

ZeroR predicts class value: 2000.0261478689542

Time taken to build model: 0 seconds

=== Cross-validation ===
=== Summary ===

Correlation coefficient                 -0.1813
Mean absolute error                    293.3361
Root mean squared error                389.583 
Relative absolute error                100      %
Root relative squared error            100      %
Total Number of Instances              474 

However, it is important to understand that it is need to analyse this further with more data and that the sensors are cheap and may not be give value with military precision. ZeroR is also a very simple classifier.

Evaluating the model (not on my mother-in-law's flowers, too risky):

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

How good is the ADC in the 0-4095 spectrum?

Looking into the picture below, there is a possibility to withdraw some conclusions. One of them is that the ADC-sensor are very inconsistent when applying new water in the plants which is logically correct. However, looking into a longer spectrum of the data, the result could vary and give inconsistent results. This tells us that when the soil is active-low (L), it gives consistent values. This information provides that the ADC is better with lower compared to higher values.

The project could be done in another way with purchasing a cheap display monitor (mounted on the wall) instead of using a laptop. There is a need of a applied application for developing a Water Control System where its purpose is to gain the intelligence from the Enivorment Analyser to make efficent decisions. However, this is something that could be implemented in the future. One solution to the ADC problem with buying more accurate sensors, but this project for me was more intended to learn the fundamentals of electronics and understand the different platforms.

Final product:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Operating!

However, I did not have time to fix the wires to the plants which I planned to do. This shows a normal scenario when monitoring the plants and the enviroment. I would recommend to use wireless soil moistures sensors instead of building with wires. These can be shown below. However, it was fun to play around with wires

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Wireless soil moisture sensors ➜ Neue Xiaomi Mi Flora Monitor Digital

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Select a repo