Skip to content

Instantly share code, notes, and snippets.

@imakin
Last active August 29, 2015 14:21
Show Gist options
  • Save imakin/862b677f47957a7db801 to your computer and use it in GitHub Desktop.
Save imakin/862b677f47957a7db801 to your computer and use it in GitHub Desktop.
Agus Raspi
#!/usr/bin/env python
#-- pip install pyserial
#-- pip install PyMySQL
from serial import Serial #-- untuk serial
import pymysql.cursors #-- MySQL
import time #-- delay
DBHOST = "192.168.1.52"
DATABASE_NAME = "raspberrypi"
DATABASE_USERNAME = "root"
DATABASE_PASSWORD = "password"
# konek ke laptop
connection = pymysql.connect(host=DBHOST, user=DATABASE_USERNAME, passwd=DATABASE_PASSWORD, db=DATABASE_NAME, cursorclass=pymysql.cursors.DictCursor)
#-- serial dari arduino
arduinoserial = Serial("/dev/ttyACM0",9600,timeout=1)
while (1):
data = arduinoserial.readline()
with connection.cursor as cursor:
sql = "INSERT INTO settings (value_string) VALUES ('" +data+ "')"
cursor.execute(sql) #-- tambah ke database
#-- simpan perubahan(penambahan) ke database
connection.commit()
time.sleep(1) #-- delay 1 detik di tiap loopnya
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment