Creating a database for IoT devices using dweet.io

Create a database for IoT using dweet.io (Tutorial)

We sometimes use affiliate links in our content. This won't cost you anything, but it helps us to offset the costs of paying our writing team. You can support us directly on BuyMeACoffee. Thank you!

In the last tutorial, we fetched data from connected devices published on dweet.io. Now, we’re going to create a Python script to grab device data for a database.

If you haven’t read the previous tutorial, I recommend completing that before attempting this one. This tutorial is most suitable for those who are familiar with SQL databases and have written programs using Python. I will not be reviewing these topics in detail, so if you aren’t familiar with them, I recommend checking out Python for Everybody, first.

Materials You’ll Need

Again, you’ll need to use a computer for this project. A tablet, like an iPad, will not work.

Install Python on your computer. You’ll also need a text editor such as Atom.io or Sublime Text. I’m a huge fan of Atom.io and will be using it throughout this tutorial.

Download & configure the dweepy repository. The README explains all of the steps for configuration in detail.

Install SQLite Browser (where we’ll create our databases).

Refresher from Fetching Data

If you’ve already read my tutorial on fetching data from dweet.io using Python, then go ahead and skip to the next section. If not, I’m going to briefly recap what we did to obtain the data from dweet.io.

We created a retrieve.py script using the dweepy library. Next, we picked a “thing” from the Discover page of the dweet.io website. Furthermore, we used the dweepy.get_latest_dweet_for(thing) command to obtain the URL feed in JSON format. Lastly, we parsed the JSON feed and printed out key metrics (date, time, temperature, and humidity).

Now it’s time to move on to adding this data to a database.

Creating a database for IoT devices using dweet.io

We are going to import dweepy, sqlite3, and time. Then prompt the user to identify the “thing” name on dweet.io.

Next, create a connection to the sqlite3 database (db). Configure the database cursor and execute a new table.

Resume data logging at anytime

The next section of code is where the real “magic” happens. We want to be able to resume data collection at anytime without losing data. Therefore, we create a section of code that searches for the “maximum id” in the Database table. If there aren’t any rows in the table, we set our start to zero. Otherwise, we resume at the next row.

Retrieve data from dweet.io

We’ll ask the user how many readings to add to the database, then run some code to fetch these readings.

The try and except statements provide a way to exit the code without a traceback. This is helpful if you want to stop fetching data by pressing a key on the keyboard or if there’s an error retrieving data from the provided thing name.

You’ll notice we also parsed the JSON format from the thing’s URL in the try statement to obtain our data readings.

Committing data to the database

Lastly, we’ll commit the readings to our database. I’ve included a user option to have the most recent readings print out in the Terminal window.

And that’s pretty much it! (Not too shabby!) You can download a copy of the source code below and give this project a try!

Viewing the database

Open up the DB Browser for SqLite. Navigate to the folder where your retrieve_store.py script is stored. This is where the DB file (with .sqlite extension) is created and saved. Use DB Browser to open up this file. The script creates a database for each thing in the format “thing.sqlite.”

Creating a database for IoT devices using dweet.io
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

Then, click on the Browse Data tab to view the collected data for your thing.

Creating a database for IoT devices using dweet.io
  • Facebook
  • Twitter
  • Pinterest
  • Buffer
  • reddit
  • LinkedIn

If everything worked correctly, you should see a listing of all of the collected readings. If not, then try clicking the refresh button next to the Data table dropdown.

Next Steps & Applications

Once you know how to obtain data and store it into your database, you have the freedom to do whatever data analysis you want to with your data!

I think it would be pretty cool to use the Python script to retrieve readings every hour. The nice thing is once you have an understanding of what you’re trying to analyze, you can create another Python script to manipulate the database. Rather than leave the script running on your laptop, you could move it over to a single-board computer like the Raspberry Pi.


Also, if you’re more of an Excel person, you could export your data table to a .CSV, and use Excel to perform graphing. (Or use something like D3.js to create graphics using Python.) The possibilities are endless!

In the last few tutorials, we’ve been using public things on dweet.io. In the next tutorial, I’m going to show you how to set up the hardware for your IoT device and publish your own sensor data to dweet.io.

Share this Article

If this article helped you, then please share it on Social Media! This helps spread the word about my mission! Thanks for the support! 🙂

You May Also Like

Have a question? Need Help?

Thanks for dropping by! Comments are no longer moderated daily.
Before posting your comment, please consider buying me a coffee. If you need immediate support, you can schedule an appointment. Providing customized information for your individual scenario takes a lot of my time. I’m happy to help, but I can no longer provide individualized advice pro bono.
Thank you for understanding.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Structured Learning

Control your robotics journey

Industry Innovation

Learn alongside engineering professionals

Make Advancements

Designed for all backgrounds

Learn Robotics Botly Favicon

Become a Learn Robotics Insider

 

Be the first to know when we launch new robotics content. Plus gain FREE robotics tips to your inbox.

Awesome! Check your inbox for a surprise!

Remote Robotics Courses for High School, Middle School, or Pre-College Training

Join the Powered by Learn Robotics Program

Teach robotics anywhere with our turnkey robotics program!

Download our free program brochure!

Success! Check your email for the download!

Pin It on Pinterest