Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smbus
- import time
- import MySQLdb
- import bluetooth
- DEVICE_ADDRESS = 0x5a
- bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
- # Bluetooth stuff
- server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
- port = 1
- server_sock.bind(("",port))
- server_sock.listen(1)
- data = bytearray([0x00, 0x00, 0x00])
- print "Se asteapta conexiunea de la Android..."
- client_sock, address = server_sock.accept()
- print "S-a facut conexiunea cu ", address
- # MySQL stuff
- db = MySQLdb.connect(host="46.101.121.75", user="ocp", passwd="####", db="ocp")
- cs = db.cursor()
- # cs.execute("CREATE TABLE IF NOT EXISTS `sensordata` ( `id` int(11) NOT NULL AUTO_INCREMENT, `lat` float(10,6) NOT NULL, `lng` float(10,6) NOT NULL, `time` timestamp NOT NULL, `value` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;")
- try:
- while True:
- client_sock.send("SD")
- gps = client_sock.recv(1024)
- gpsc = gps.split(";")
- print "GPS [%f, %f]" % (float(gpsc[0]),float(gpsc[1]))
- data[0] = bus.read_byte(DEVICE_ADDRESS)
- data[1] = bus.read_byte(DEVICE_ADDRESS)
- data[2] = bus.read_byte(DEVICE_ADDRESS)
- # N-am idee de ce asa ????
- sensor_data = (int(data[0]) << 8) + int(data[2])
- print "Senzor [%d]" % sensor_data
- client_sock.send(str(sensor_data))
- if sensor_data > 450 and sensor_data < 30000:
- query = "INSERT INTO sensordata(lat,lng,time,value) VALUES (%f, %f, CURRENT_TIMESTAMP, %d);" % (float(gpsc[0]), float(gpsc[1]), sensor_data)
- cs.execute(query)
- time.sleep(1)
- except KeyboardInterrupt:
- db.close()
- client_sock.close()
- server_sock.close()
- print "End"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement