A really Toit GLAMP server
In this project, we will use Toit to push data to a classic LAMP stack. We will also add Grafana to the stack. In other words, we are building a Toit-powered GLAMP stack! Data will flow from a BME280 sensor connected to an ESP32 running Toit, and this will forward the data via HTTP to a Linux computer running the GLAMP stack.
The original LAMP stack is a classic web server setup. The acronym stands for “Linux, Apache, MySQL, and PHP” and this setup has been used by numerous well-known internet companies throughout the years. There are many tutorials available online (try this one, for example) covering how to set up such a system, so that part will not be covered in this post: I will assume you have an online computer running a LAMP stack. The same goes for Grafana: Downloading and installing Grafana on your computer is very straightforward and we’ll assume this step is also finished before starting.
Parts needed
- An ESP32 development kit (you can buy one here).
- The BME280 sensor module, e.g. from Sparkfun.
- A 1602 LCD. Check this one out.
- A Potentiometer, around 0–5 kOhm. This will do the trick.
On the sending side
On the data generation side (i.e. the ESP32 with peripherals) we need to set up our hardware, i.e. connect the ESP32 to the LCD and the BME280 sensors. We will also create a free account at toit.io to initialize (or “provision”) the ESP32 with Toit. Finally, we’ll download the required files from GitHub so that we can install the Toit application on the ESP32.
Hooking up the hardware
The BME280 sensor will be connected to the EPS32 using I²C, so we only need two wires for that: SDA and SCL. The LCD requires a few more wires since we talk to it in nibbles, i.e. chunks of four bits. It also requires a potentiometer for adjusting the LCD contrast, and two signal pins for RS (Register Select) and E (Enable). We will use the following GPIOs on the ESP32:
Below is a Fritzing diagram of the setup. Note that we need 5V for the LCD, and 3.3V for the BME280. Do not mix these wires up.
Setting up Toit
Getting Toit up and running on your ESP32 is done in a few minutes:
- Connect your ESP32 dev kit to your computer.
- Go to toit.io and set up your free Toit account. Once logged in, you will be guided through the Toit setup process.
- When the installation is complete after about a minute, the ESP32 will go online and show up in your list of devices in your Toit console.
- You can now interact with your ESP32 without having it connected to your computer. All communication goes via the Toit console.
- Install the Toit CLI and VSCode by following these instructions.
The Toit code
First of all, you need to install the Toit driver for the BME280 sensor, using the following command:
$toit pkg install github.com/toitware/bme280-driver
The Toit package registry contains a suitable driver for the LCD. Install it with
$toit pkg install github.com/nilwes/HD44780
The code for reading the BME280 sensor, writing to the LCD, and publishing data via HTTP can be downloaded from GitHub. The code should be pretty much self-explanatory. Just download it and save the repository to a folder of your choice. Open VSCode and open the folder where you saved the GitHub files.
In VSCode, if you have installed the Toit extension, you should have a Toit logo in the left menu. If you click this logo, your Toit devices will show up in the list next to the left-hand menu. You can run your code by pressing the “Play” symbol that appears next to the device name (in my case “Huzzah1”) if you hover your mouse over it.
When the code is executed, it should print temperature, humidity, and barometric pressure on the LCD:
Pro Tip: You can also run your code from your command line by typing:
>>toit device -d MyDeviceName run glamp.toit
In order to get repeated sensor readings, we will use the yaml-file glamp.yaml
(also included in the GitHub repository you downloaded) that will trigger the program to run at certain intervals. If we look in the yaml-file we’ll see that it is set up so that the Toit application will run (1) when the ESP32 reboots, (2) when we initially install the application, and (3) subsequently it will run every five minutes.
To deploy your application using your yaml-file, press the symbol with multiple “Play” symbols next to your Toit device:
Pro Tip: You can also deploy your app from your terminal by typing:
>>toit device -d MyDeviceName deploy glamp.yaml
You can leave this app running on your ESP32. In the next step we’ll set up your database that will catch all data it generates!
On the receiving side
On the server-side we need to set up the PHP script that will receive the data from the ESP32, the MySQL database that will store our data, and Grafana that will display our data. Let us start with the MySQL setup.
MySQL database setup
Log in to MySQL in a terminal. Then use the following MySQL commands to create a database called THPDatabase
that contains a table called THPData
:
MariaDB [(none)]> create database THPDatabase;MariaDB [(none)]> use THPDatabase;MariaDB [THPDatabase2]> CREATE TABLE THPData (Time TIMESTAMP, Temp VARCHAR(20), Hum VARCHAR(20), Press VARCHAR(20));
You now have an empty table with four columns: Time
, Temp
, Hum
, and Press
Now it is time to write the PHP script that will receive the data from your Toit ESP32 via HTTP, and insert it into this database table.
PHP script
Grab the file insert.php
that you fetched from GitHub, and move it into your HTML root folder (probably /var/www/html
). NOTE: You need to enter your MySQL database credentials into the PHP script:
Go back to your MySQL command prompt, make sure your database is selected, and print all rows in the table THPData
:
MariaDB [THPDatabase]> SELECT * FROM THPData;
This should present a few lines with data that has been sent from your Toit application on your ESP32 since you deployed it earlier in this tutorial:
+ — — — — — — — — — — -+ — — — + — — — + — — — — — +| Time | Temp | Hum | Press |+ — — — — — — — — — — -+ — — — + — — — + — — — — — +| 2021–10–28 19:11:28 | 26.4 | 36.6 | 101556.2 || 2021–10–28 19:11:58 | 26.4 | 36.6 | 101561.6 || 2021–10–28 19:12:29 | 26.4 | 36.6 | 101554.6 || 2021–10–28 19:12:59 | 26.3 | 36.6 | 101559.0 |+ — — — — — — — — — — -+ — — — + — — — + — — — — — +4 rows in set (0.001 sec)
Now it is time for the last step in the process. Displaying your data in a fancy way in Grafana!
Set up Grafana
Open Grafana in your browser by entering the IP to your computer followed by the port 3000, like so:
http://x.x.x.x:3000/
Log in and go to “Data Sources” under “Configuration”
Click “Add data source” and search for MySQL.
Click “Select” and enter the name of your database and your MySQL credentials.
Finally, at the bottom of the page, press “Save & Test”. Grafana now tries to connect to your database and gives an OK message if everything works fine. Your Grafana setup should now be complete.
To create a new dashboard, click the plus-sign in Grafana and click “Add an empty panel”.
In order to pull data from your database into Grafana you can enter MySQL queries by pressing the pencil button to the right:
In the SQL text field, paste the following MySQL query:
SELECT UNIX_TIMESTAMP(Time) as “time”, CONVERT(Temp, FLOAT) as Temperaturefrom THPData
When you click outside the SQL text field, the data should be immediately pulled from your database and presented in a graph. NOTE: You might have to change the time span (x-axis) of the graph to see your data:
Click apply and voilà, you have your first graph showing temperature readings from your ESP32 running Toit. To create graphs for humidity and barometric pressure you simply have to add two new panels and paste the following MySQL queries, just like you did before:
For humidity:
SELECT UNIX_TIMESTAMP(Time) as “time”, CONVERT(Hum, FLOAT)from THPData
For barometric pressure:
SELECT UNIX_TIMESTAMP(Time) as “time”, CONVERT(Press/100, FLOAT)from THPData
The result could look something like this:
Fine-tuning your Toit device
Currently, the device is set to be online all the time and send data every five minutes. But going online consumes lots of power! If you are powering your ESP32 from a battery, you can increase battery life dramatically by changing how often your ESP32 should go online. This is easily done from your Toit console by going to the “Configuration” tab of your Toit device, and changing the “Max offline” parameter from 0s
to, say, 30 minutes, i.e. 30m
. Your application will still collect sensor data every five minutes, but the ESP32 will only go online every 30 minutes to offload this data.
Pro Tip: Under the device Configuration tab you can also sett logging levels to further tweak the amount of data your device produces.
Being able to configure how often — or rather how seldom — a device should be online is one of the key strengths of the Toit platform, enabling devices to run on batteries for years. Instead of having to include connectivity as one task of many in a single program, it is handled by the platform, and configured with just a click in the console. The apps you develop focus only on the task at hand: Measuring data, triggering an actuator, or maybe sending an MQTT message.
Summary
The LAMP stack has been around for years, and with the combination of Toit and Grafana it becomes a fantastic system for easy set-up of an IoT application.
Want to keep going? Got an idea for an IoT project? Why not check the list of drivers that are included in our package registry, or reach out to our team directly at contact@toit.io to get help writing your own. We’re also always open for a chat at chat.toit.io!