0

I have the following code in python for sending data to a mysql database

import time
import datetime
import MySQLdb
from time import strftime
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
PIR_PIN = 21
GPIO.setup(PIR_PIN, GPIO.IN)

# Variables for MySQL
db = MySQLdb.connect(host="*******",    user="root",passwd="*****", db="sensor1")
cur = db.cursor()

while True:

i = GPIO.input(PIR_PIN)
print i

datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
print datetimeWrite

sql = ("""INSERT INTO templog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,i))
try:
    print "Writing to database..."
    # Execute the SQL command
    cur.execute(*sql)
    # Commit your changes in the database
    db.commit()
    print "Write Complete"

except:
    # Rollback in case there is any error
    db.rollback()
    print "Failed writing to database"

cur.close()
db.close()
break

My problem is that my XAMPP server is installed in pc where I want to view the the data from raspberry pi in mysql database. So for getting the connection established what should I write in host= "?"

sam
  • 33
  • 1
  • 8

2 Answers2

0

The connection-string should be like this:

db = MySQLdb.connect(host="192.168.0.xxx",    user="root",passwd="*****", db="sensor1")

See this Question

Community
  • 1
  • 1
Brolantor
  • 31
  • 5
  • I had done that. But there is error: can't connect to MySQL server on 192.168.0.xxx – sam Apr 06 '17 at 12:57
  • Have you checked your firewall settings? – Brolantor Apr 06 '17 at 13:03
  • Try on your Pi following command: `telnet 192.168.0.xxx 3306` 3306 is according to the documentation the standard port of the mysql python connector. If it exits with a `connection refused` or similar, then your firewall (maybe your router setting or Windows-Firewall on the DB-Server) blocks the connection. – Brolantor Apr 07 '17 at 07:53
  • It is showing unable to connect to remote host: connection timed out. – sam Apr 08 '17 at 06:04
  • Hmm, there is no standard answer for this, it depends on many things: - Network Configuration: make sure that both machines are in the same subnet - MySQL configuration: make sure that the user (root) is allowed to access from network location. As far as I know the standard configuration is access only to 127.0.0.1 - Windows Settings: Make sure Windows allows connections from local network – Brolantor Apr 09 '17 at 19:10
0

Host will be IP address of your system on which XAMPP is installed e.g. 192.168.x.x

  • I did what you said but still, it is not connecting. Cannot connect to mysql server on 192.168.x.x – sam Apr 06 '17 at 13:18