In this article I will describe the process I used to create a new endpoint for my Raspberry Pi weather station, and how I set it up to to use Python and MariaDB for storage.
If you’ve been following my Raspberry Pi Weather Station tutorials, I set the first one up to use Firebase as a data store. I found it somewhat clunky to work with and I wasn’t very happy with my options. I’m not sure this is the type of data best suited for Firebase. So in the second iteration I used a .Net Web API on a Windows Azure website and had it store to a MS-SQL database. This was a really good solution.
I started getting a flood of emails with people asking for a more open source endpoint. Granted ASP.Net is open source, but some complained about the cost of hosting it. If you wanted to keep the database in house you’d have to pay for windows license and a SQL server for instance. Another issue was some people were developing in Linux or Mac, which makes interfacing with ASP.Net difficult (though they are working on that). Long story short I decided to create a Linux friendly solution and decided upon Python/Flask with MariaDB.
Why Python? Why MariaDB?
I chose Python because I’ve always loved the language from my first line of code. It’s simple, elegant and does a lot of things very well. I had never written a Web API with it, so I thought I’d give it a shot.
MariaDB is a fork of MySQL that is maintained by the founder, and superior to the original in many ways. Since it’s a drop in replacement I can still use the MySQL tools I’m familiar with. It seemed like a good fit.
Get your Linux server ready. For this task I chose to use a Digital Ocean Server running Debian. If you must use Ubuntu, put “sudo” in front of everything.
The first thing you’ll want to do is update everything:
Next we’ll get the database set up.
1. Install MariaDB
As I mentioned, MariaDB is a drop in replacement (upgrade really) of MySQL. You’ll need to add the repository:
Then add it to your sources:
And add the following to it:
1 2 3
save and close the file
Now run the following:
You will see a screen like this that will ask to set your root password:
To add some additional security settings, run
After that you can log into your MariaDB server:
To make it accessible from outside the server, you’ll need to make a change to my.cnf:
Look for “bind-address” and change it:
Save the file and close it.
Now restart the server:
You should now be able to connect to your server from the outside.
2. PYTHON / SQL Connection
In order to connect to MariaDB you’ll need to install a couple things:
You’ll also need to download the mysql connector.
1 2 3
This will install it the proper files to use the MySQL connector module.
3. Set up Python Flask
Next, we’re going to use Python Flask for the REST interface. Flask is written in Python and a very fast, powerful and stable way to connect.
1 2 3 4 5 6 7 8 9 10 11 12
This will set up Flask on your machine. Now let’s build a sample API to test it out.
4. Set up a test endpoint
We will want to add a web user (or whatever you want to name it) to set up this application.
I would like to give a shout to Miguel Grinberg for all the work that he’s done setting this up! I learned a lot from his tutorials.
1 2 3 4 5 6 7 8 9
Now we want to create a file called app.py and make it look like this:
1 2 3 4 5 6 7 8 9 10 11 12
Next, we’ll want to set it to be executable, then execute it.
Now create a new SSH session and type the following:
You will see a hello world message:
Next, change it to have some actions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Notice at the bottom I put in
This will allow your API to be accessed from the outside.
Now, get POSTMAN or something similar and point it at your URL. Send it a GET:
and it should look like this:
Congrats, you have Flask working. For security you should install and implement httpauth later down the road.
5. Set up MariaDB for the Weather Station
Now we will set up a database for the weather station endpoint.
1 2 3 4
Log in as that web user:
You will be created with an SQL prompt. Type the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
This creates your MariaDB database for storing items.
6. Set up Flask for the Weather Station
For this rather than dump the code on this page, just check out the endpoint from my github repo:
You’ll need to change the values in datastore.py to match your credentials.
start it up as you did before:
Note, if you want to run as a service in the background:
()I’ve had mine up for about 2 weeks now without crashing)
Now, you can add a new reading like this:
But how do we add this as an endpoint for the Rasperry Pi Weather Station project? Easy! Just point it to your Linux endpoint instead of the Azure/.Net one in readings.py:
And you’re set! All your results will be stored by the minute in a MariaDB database:
While this is specific to an endpoint I created for my project it’s a good outline for building a REST API with Python in Linux. It’s a very stable and performant way to collect data. The work on Flask has made it incredibly easy to get an API up and going quickly. I hope to do more cool stuff with it in the future and I hope you do too.