Advertisement
Tavi33

IAQ

May 23rd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import smbus
  2. import time
  3. import MySQLdb
  4. import bluetooth
  5.  
  6.  
  7. DEVICE_ADDRESS = 0x5a
  8.  
  9. bus = smbus.SMBus(1)    # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
  10.  
  11. # Bluetooth stuff
  12. server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  13. port = 1
  14. server_sock.bind(("",port))
  15. server_sock.listen(1)
  16.  
  17. data = bytearray([0x00, 0x00, 0x00])
  18.  
  19. print "Se asteapta conexiunea de la Android..."
  20. client_sock, address = server_sock.accept()
  21. print "S-a facut conexiunea cu ", address
  22.  
  23.  
  24. # MySQL stuff
  25. db = MySQLdb.connect(host="46.101.121.75", user="ocp", passwd="####", db="ocp")
  26.  
  27.  
  28. cs = db.cursor()
  29. # 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;")
  30.  
  31. try:
  32.     while True:
  33.         client_sock.send("SD")
  34.         gps = client_sock.recv(1024)
  35.         gpsc = gps.split(";")
  36.         print "GPS [%f, %f]" % (float(gpsc[0]),float(gpsc[1]))
  37.         data[0] = bus.read_byte(DEVICE_ADDRESS)
  38.         data[1] = bus.read_byte(DEVICE_ADDRESS)
  39.         data[2] = bus.read_byte(DEVICE_ADDRESS)
  40.  
  41.         # N-am idee de ce asa ????
  42.         sensor_data = (int(data[0]) << 8) + int(data[2])
  43.    
  44.         print "Senzor [%d]" % sensor_data
  45.         client_sock.send(str(sensor_data))
  46.  
  47.         if sensor_data > 450 and sensor_data < 30000:
  48.             query = "INSERT INTO sensordata(lat,lng,time,value) VALUES (%f, %f, CURRENT_TIMESTAMP, %d);" % (float(gpsc[0]), float(gpsc[1]), sensor_data)
  49.             cs.execute(query)
  50.            
  51.         time.sleep(1)
  52.        
  53. except KeyboardInterrupt:
  54.     db.close()
  55.     client_sock.close()
  56.     server_sock.close()
  57.     print "End"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement