Embedotronics Technologies
Published © GPL3+

Sending DHT11 Data to MySQL Server Using Python with Arduino

In this project, we are sending data of DHT11 which is humidity and temperature to phpmyadmin database using python

IntermediateFull instructions provided3 hours2,803
Sending DHT11 Data to MySQL Server Using Python with Arduino

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Schematics

circuit_diagram

Code

Python_code

Python
import serial 
import MySQLdb
import time

dbConn = MySQLdb.connect("localhost","root","","dbserial") or die ("could not connect to database")
#open a cursor to the database
cursor = dbConn.cursor()

device = 'COM18' #this will have to be changed to the serial port you are using
try:
  print "Trying...",device 
  arduino = serial.Serial(device, 9600) 
except: 
  print "Failed to connect on",device    

while True:
    try:
      time.sleep(2)
      data = arduino.readline()  #read the data from the arduino
      print data
      pieces = data.split(" ")  #split the data by the tab
      #Here we are going to insert the data into the Database
      try:
        cursor.execute("INSERT INTO dht11serial (humidity,temperature) VALUES (%s,%s)", (pieces[0],pieces[1]))
        dbConn.commit() #commit the insert
        cursor.close()  #close the cursor
      except MySQLdb.IntegrityError:
        print "failed to insert data"
#      finally:
#        cursor.close()  #close just incase it failed
    except:
      print "Failed to get data from Arduino!"

            
            


  

Credits

Embedotronics Technologies

Embedotronics Technologies

31 projects • 30 followers
We are a team of passionate engineers who works on embedded system and IoT.

Comments