Air Quality Telegram Bot

 

This project will try to accurately describe the process of the Telegram bot creation powered by the Mixtile Edge 2 Kit.

 

Things used in this project


Hardware components            Mixtile Edge 2 Kit ×1

Software apps and online services         Snappy Ubuntu Core    &   Telegram

 

Story


Problem

Sarajevo, Bosnia and Herzegovina is one of the most polluted cities in the world during the winter period. Currently, there isn’t any good data provider for the air quality. We have official websites that are hard to navigate and load very slowly. The need for good scalable solutions is something the people of Sarajevo need.

ハードウェア

The use of the Mixtile came to my mind trying to overcome the problem. But what is Mixtile?

Mixtile is a professional IoT hardware solution provider specializing in Linux and Android-based embedded systems.

Mixtile Edge 2 Kit is a high-performance ARM single-board computer. It comes in variants of 2GB of LPDDR4 DRAM and 16GB eMMC Flash storage, or 4GB of LPDDR4 DRAM and 32GB eMMC Flash storage. This single-board computer comes with preinstalled Android 11, and it runs the Ubuntu Linux operating system in an Android container. It has large connectivity options (Bluetooth, 4G/5G Cellular, GPS, Lora, Zigbee, and Z-Wave). You will need a module for those, but it comes with default onboard Wi-Fi connectivity, Gigabit Ethernet Port (RJ45), and aSerial Port (RS-485). Because it comes with an RS-485 port, which is industrial standard, and it comes within a strong metal case, it seems to me that it can be used in industrial projects. I used the official Raspberry Pi 5 power supply to power up my Mixtile Edge 2 Kit.

Mixtile Edge2 Kit

Solution

Telegram is a messaging app that is in the top 10 messaging apps in the world. widely used in the world. They offer an easy and free API that can be utilized in occasions like this one.

My idea is to build a small Python script that will scrape data from the local weather station and store them in the SQLite3 database which is on the the core standard of the Mixtile Edge 2 Kit.

Working Telegram Bot

In the following steps, I will present you with the necessary steps to activate the full power of the Mixtile Edge 2 Kit and release the energy of your bot.

1. Prepare your Mixtile Edge 2 Kit

To simplify control, I connected a USB mouse to one of the three USB3 ports on the Mixtile Edge 2 Kit. For this project, I used Ubuntu Linux. By default, Ubuntu is installed in a container within the Android system of the Mixtile Edge 2 Kit. When you boot the Mixtile Edge 2 Kit, it runs Android OS. To access the Edge 2 Kit remotely and achieve easier control, I installed the droidVNC server from this link.

It is an Android VNC server using Android 5+ APIs. It does not require root access.

I started the VNC server, connected with VNC Viewer and I got the following Android 11 screen:

Next, I installed SimpleSSHD from this link.

SimpleSSHD is an SSH server Android app based on Dropbear. It allows user access (user ssh) or full root access (by setting the login shell to /system/xbin/su) if theroot is permitted.

After installing the SSH server, I connected to it using the PuTTY SSH terminal with the username and password set to root/root.

The com.hubware.ubuntu is Ubuntu running in a container, and we are connected to it immediately.

Now, we will install the required software.

First, upgrade Ubuntu by typing the command:

apt-get upgrade

Second, install Python by typing:

apt-get install python

2. Setting the Telegram Bot

First, you need to open the Telegram account by downloading the app from your favorite store and activating your account with the phone number. Next you can use the computer app for easier navigation. In the search bar type @botfather and click on it.

Bot Father search

And of course click START

Bot Father

After you will be given many options but the first one is /newbot

Give your bot a name and later username and the next message is this:

Bot Father message

The key that will be given bellow the red part is your TOKEN and do not share it with anyone!

Now you can play with your bot and give him different options but we want to set the command that will be the same as in the code and that we can use. Run /setcommands and follow the prompt. You should end up with something like this:

/setcommands

Note that sarajevo25 is coded in my python script. If you choose something different it must resemble in the code and in the Telegram.

3. Preparing the Python environment

Open your favorite code editor and create the following documents:

Once the necessary files have been created (and all the code posted from the CODE section) you should end up with something like this:

Project folder structure

Then open your terminal, and navigate to the folder where you have saved your files. Once you are there run the following commands:

#create the virtual environment

python3 -m venv .venv

#activate the environment

source .venv/bin/activate

#install the necessary packages

pip install -r requirements.txt

The last part is to setup the.env file. In this file we will give the websites that we want to scrape, and the all API keys we need for our project. My.env file looks like this:

TOKEN = '******************************'
BOT_USERNAME = '@aqbih_bot'
FHMZBIH = 'https://www.fhmzbih.gov.ba/latinica/ZRAK/value24h.php'

Never ever push your.env file to the Github or any other repository. Once you make your.env file and it to the.gitignore file to be sure others will not be able to see it!

4. Running the bot

Open two terminals, and make sure that both have.venv activated. Once you are sure, in the first terminal run

python database.py

This will create an SQLite database in your system, and it will start scraping the desired data. Make sure in the line schedule.every().minute.do(store_data) set minutes to thedesired time you want to scrape your data.

In the second terminal run

python main.py

Which will start your Telegram bot.

Bot is working…

And you are done. Now you have data on the tip of your fingers.

Result

Last, but not less important: If you want to safely turn off your Mixtile Edge 2 Kit, I recommend you to install Shutdown (no Root) application: https://play.google.com/store/apps/details?id=com.samiadom.Shutdown&hl=en.

Conclusion

After testing, I discovered that the Mixtile Edge 2 Kit is designed for a wide range of applications, including industrial uses, IoT devices, smart home automation, and advanced AI and edge detection. It’s a low-power device with extensive built-in connectivity options. The RS-485 port allows connecting a variety of industrial sensors that can notify the user using Telegram’s API. The possibilities are endless and the only limit is your imagination.

I would like to thank the incredible team at Mixtile for creating such an impressive piece of hardware and especially for sending me the Mixtile Edge 2 Kit. Mixtile also promotes open-source values and software, and I believe more individuals and companies will engage in projects using this board.

In summary, I highly recommend this board for the types of projects I’ve outlined here.


 

Code


  • database.oy Python
  • main.py Python
  • scraper.py Python
  • requirements.txt Python
  • Credits


    Emir Hanic

    I am mechanical engineer with the focus on the Air Quality monitoring and modeling. Some of my previous projects were the CFD modeling of the polution dispersion in Sarajevo valley.