How to Use Arduino and Python to Save Sensor Data to a Database System

How to Use Arduino and Python to Save Sensor Data to a Database System

Source: intorobotics

Produced by Smart Workshop

Contributors: Lotus

Free source code tutorials are always readily available, so it is often useful to think about how to creatively apply source code for free. Whether you are looking for professional protocols or engineering solutions, both lines of thought can be greatly beneficial, allowing you to apply them anytime and anywhere without wasting effort or duplicating labor.

Big data and the Internet of Things are two sides of the same coin. We send thousands of emails every minute, upload hundreds of photos on social networks, and tweet constantly. By 2021, these numbers will continue to rise as more devices connect to the internet. Consequently, DIY devices are essential for us to learn how to use source code online to send, receive, and store data effectively.

To enhance data storage efficiency in the era of the Internet of Things and big data, it’s easy to think of the free tools and software available in the DIY community. Utilizing microcontrollers, sensors, and database systems is the easiest and least expensive method, practical for both beginners and experts.

In this tutorial, author Surendhar Reddy shows us how to use source code and programming languages Arduino and Python to save sensor data to a database system.

Components and Software

  • Arduino Uno microcontroller

  • Sensors (for example, the TMP36 temperature sensor)

  • Arduino IDE (Integrated Development Environment)

  • Python programming language

phpMyAdmin, a MySQL database management tool based on PHP, is used to create databases and tables.

Using source code and programming languages Arduino and Python to save sensor data to a database system (http://surendharreddy.me/logging-arduino-sensor-data-into-mysql-table-using-python/)

As the Internet of Things and big data become more prevalent, people expect to see innovative ways to save energy and improve efficiency. However, for those who are already accustomed to these issues, like manufacturers, how do we continue to work and live intertwined with the Internet of Things and big data? To address this, I have developed a small module that will run the programming language to save data when your source code data sensor logs into the database system.

Let’s start by understanding the required components and software:

For components, you need:

  • Arduino Uno.

  • Any sensor.

  • A few connecting wires.

For software, you need:

  • Arduino IDE (I prefer version 1.6.5)

  • Python (I prefer 2.7) with serial and MySQLdb libraries installed.

  • Code editor.

phpMyAdmin, a MySQL database management tool based on PHP, is used to create databases and tables.

Next, you need to understand the requirements for logging data. Download the code and start running it: https://github.com/surendharreddy/Arduino-MySQL download link.

Here are the steps to complete the process:

1. Connect the source code provided online to your sensor (I used the TMP36, a temperature sensor with only voltage readings);

2. Next, from the downloaded code, copy the file Arduino MySQL.ino and paste it into your integrated development environment;

3. Check if the code is running by opening the dynamic monitor to confirm;

4. Then, create a database and table in your local server using phpMyAdmin;

5. If you are not using phpMyAdmin, you can use the following script to create a table: (CREATE TABLE IF NOT EXISTS `tempdata` ( `ID` int(11) NOT NULL, `Date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, `voltage` varchar(4) NOT NULL ));

6. Get ArduinoMySQL.py from the downloaded code and paste it into any code editor you prefer;

7. Replace the database credentials in the fifth line, which includes your database username, password, and database name;

8. Finally, save the code, run the programming language script, and check your table; you should see the data.

Possible Errors

You may encounter errors similar to the image below:

How to Use Arduino and Python to Save Sensor Data to a Database System

At this point, you need to update your programming language database using the installation seed — update MySQL-python==1.2.4, recommended to update the database.

All done, and now you can record the data!

© This article is originally compiled by Smart Workshop. If you wish to reprint, please contact Smart Workshop for authorization.

  • Smart Workshop follows industry norms, and any reprinted articles will clearly indicate the author and source;

  • For original articles by Smart Workshop, if you wish to reprint, please contact the official account for authorization. When reprinting, please be sure to indicate the author and “Source: Smart Workshop”. Failure to respect originality may result in Smart Workshop pursuing responsibility;

  • Author submissions may be edited or supplemented by Smart Workshop editors.

How to Use Arduino and Python to Save Sensor Data to a Database System

Leave a Comment

×