or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
A simple experiment using TIG-STACK for implementing a Portable Environment Analyser
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!
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.
- 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.
- 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.
- 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 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
– 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.
- 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
- 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
- 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.
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.
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.
- 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 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
- 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.
- 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 →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:
- 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 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 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 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 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 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
- 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:
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.
- 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).
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:
Which give us:
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?
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
– 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:
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):
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
- 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
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.
Secondly, we need to install Microsoft Visual Studio Code.
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
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
Register an account at Thethingsnetwork Just follow the steps!
Create an application (if you do not have a gateway)
Add application
Adding information to your application

Great job! We have created our first application!
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
- 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 →We are in turn going to use OTAA authentication parameters in LoRa.py
Use
.gitignore
for the keys.py when distributing the configuration.- 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.
– 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:
Copy influxdb/influxdb.env.examle to influxdb/influxdb.env to configure influxdb
- 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 →Copy telegraf/telegraf.conf.example to telegraf/telegraf.conf.example then configure telegraf
- 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
.gitignore
for influxdb/data/* influxdb/influxdb.env telegraf/telegraf.conf when distributing the docker configuration.Run
docker-compose up -d
to start containers and setup network.Run
docker-compose ps
to check the state of the containers- 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 →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
Download the Discord Application Discord
Create your first server
- 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 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 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 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 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 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.
- 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.
- 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)
- 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
- 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 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:
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:
The payload function:
- 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 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 →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
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.
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.
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
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.
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.
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.
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:
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):
- 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:
- 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 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 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
- 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 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 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 →