Skip to main content

Logging Arduino serial port data to a MySQL database using Python involves reading data from the Arduino via serial communication and storing it in a MySQL database using Python. Below is a general outline of the process:

Hardware Components
Arduino Board: This is the hardware device that collects data and sends it over the serial port to the computer.
Software Components
Arduino Sketch: Write an Arduino sketch to collect data from sensors or other sources and send it over the serial port to the computer.

Python Script: Write a Python script to read data from the serial port, parse it, and store it in a MySQL database.

Steps to Implement
Install Required Libraries: Ensure you have the pyserial and mysql-connector-python libraries installed in your Python environment. You can install them using pip:

Copy code
pip install pyserial mysql-connector-python
Write Arduino Sketch: Write an Arduino sketch to read data from sensors or other sources and send it over the serial port to the computer. Here's a simple example:

arduino
Copy code
void setup() {
   Serial.begin(9600);
}

void loop() {
   // Read sensor data or other sources
   int sensorValue = analogRead(A0);
   
   // Send data over serial port
   Serial.println(sensorValue);
   
   delay(1000);  // Adjust delay as needed
}
Write Python Script: Write a Python script to read data from the serial port, parse it, and store it in a MySQL database. Here's an example:

python
Copy code
import serial
import mysql.connector

# Open serial port
ser = serial.Serial('/dev/ttyUSB0', 9600)  # Adjust port and baudrate as needed

# Connect to MySQL database
conn = mysql.connector.connect(
   host='localhost',
   user='username',
   password='password',
   database='database_name'
)
cursor = conn.cursor()

# Create table if not exists
cursor.execute('''CREATE TABLE IF NOT EXISTS sensor_data (id INT AUTO_INCREMENT PRIMARY KEY, value INT)''')

# Read data from serial port and store in database
while True:
   data = ser.readline().strip()
   if data:
       value = int(data.decode())
       cursor.execute('''INSERT INTO sensor_data (value) VALUES (%s)''', (value,))
       conn.commit()

# Close serial port and database connection
ser.close()
conn.close()
Run the Python Script: Run the Python script while the Arduino is connected to the computer. The script will continuously read data from the serial port, parse it, and store it in the MySQL database.

Important Considerations
Ensure that the port specified in the Python script (/dev/ttyUSB0 in the example) matches the port to which the Arduino is connected on your system.
Adjust the baud rate in both the Arduino sketch and the Python script to match the baud rate set in the Arduino sketch (9600 in the example).
Replace 'localhost', 'username', 'password', and 'database_name' with your MySQL server details.
Handle exceptions and errors in the Python script for robustness.
Consider adding timestamp information to each data entry in the database for better analysis and visualization.
By following these steps, you can create a system to log Arduino serial port data to a MySQL database using Python.