After the successful Bolt Kickstarter campaign, Makers and developers from all around the world are building innovative IoT projects using Bolt. They are able to visualize their sensor data with just a few clicks on Bolt Cloud. But some developers had asked me is there a way to plot the graphs on their own server after fetching the data from Bolt Cloud API.
So this project is all about plotting the graph on sensor data on your own server.
We will write a cron job that will fetch data from Bolt Cloud using Bolt Cloud API and store the data in MySQL database and there will be a Flask server that will fetch the data from the MySQL and will plot the graph. Follow the below instructions.
1. First login to your Ubuntu 16.04 server and install python3 (Ubuntu 16.04 ships with Python3 pre-installed.)
2. Update the Ubuntu package lists by typing the below command.
sudo apt-get update
3. Install the MySQL server and then install the dependencies to build packages. Note down the root password while installing the MySQL server.
sudo apt-get install mysql-server sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
4. To manage software packages for Python install the pip.
sudo apt-get install -y python3-pip
5. Type below commands to install virtual environment packages. Virtualenv is a very simple tool for a kind of virtualising the Python environment
apt-get install python3-venv
6. and now will will create and activate the virtual environment. I named my virtual environment.bolt_graph
python3 -m venv .bolt_graph source .bolt_graph/bin/activate
5. Now install mysqlclient for interfacing Python with MySQL.
python3 -m pip install mysqlclient
6. And then install flask and requests.
python3 -m pip install flask python3 -m pip install requests
7. Login to MySQL and type the password after pressing enter.
mysql -u root -p
8. and then import the database schema.
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; CREATE DATABASE IF NOT EXISTS bolt_graph; USE bolt_graph; -- Table structure for table `sensor_data` CREATE TABLE `sensor_data` ( `sr` int(10) NOT NULL, `temperature` varchar(5) NOT NULL, `humidity` varchar(5) NOT NULL, `time_stamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ALTER TABLE `sensor_data` ADD PRIMARY KEY (`sr`); ALTER TABLE `sensor_data` MODIFY `sr` int(10) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
10. Now go inside the Bolt-Visualization folder and run the below command to run python script.
and you will see the below message terminal.
and if you visit your server IP on port 5000, you will output something similar to this.
Graph is blank because there is not data points exist in database.
Refer this project https://www.hackster.io/vinayak-shantaram-joshi/humidity-and-temperature-monitoring-system-38a12b for setting up your Humidity and Temperature monitoring system using Bolt.
But in this project, you will fetch the data using Bolt Cloud API. You can refer Bolt cloud documentation https://cloud.boltiot.com/docs#uart
Cron is a standard Linux feature that allows you to schedule tasks, to run unattended at a specified frequency. We will write a python script (cron_script.py ) that will fetch the data from Bolt Cloud API and store that data inside MySQL. You can read about fetching the UART data over Bolt cloud here https://cloud.boltiot.com/docs#uart.
We will run the cron_script.py every minute using Cronjob.
To run the cron script first find the absolute path of your virtual environment and absolute path of cron_script.py.
and then open crontab by typing the below command.
Go to the end of file and add the below line and then save the file.
* * * * * /home/ubuntu/.bolt_graph/bin/activate /home/ubuntu/cron_script.py
After storing some data points you can visit you server IP on port 5000 and you can see the graph with data points.