Advertisement
rozman50

Untitled

Apr 11th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. from socket import *
  2. import sys
  3. import os
  4. import MySQLdb
  5.  
  6. # NI POMEMBNO
  7. # IP address and port of Arduino W5100
  8. # address = ('192.168.2.100', 5000)
  9. # client_socket = socket(AF_INET, SOCK_DGRAM)
  10. # client_socket.settimeout(0.5)
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. # database connection
  18. # TIM
  19. # tle se probaš connectat na podatkovno bazo
  20. try:
  21.     db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="", db="arduino")
  22. except Exception as e:
  23.     sys.exit("Connection was not successfull!")
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. # NI POMEMBNO
  31. # counter = 0
  32. # dataBefore = []
  33. # dataAfter = []
  34.  
  35. while(1):
  36.  
  37.  
  38.  
  39.  
  40.     # v pythonu rabiš za database vse delat s cursorjem, notr shranš trenutno delo, k ga opravljaš
  41.     cur = db.cursor()
  42.  
  43.     # translation (SLO - ENG): prostor - room
  44.     # izvedeš stavek
  45.     cur.execute("SELECT i.item, i.state, p.id_arduino from items as i, prostor as p where i.id_prostor = p.id_prostor")
  46.  
  47.     # rezultate shraniš v spremenljivko result
  48.     result = cur.fetchall()
  49.  
  50.     # vsako stvar, k jo izvedeš, morš commitat, da zaključiš
  51.     # mene je tle neki jebalo, k se je prejšnja koda izvajala al neki takga
  52.     # preber si, kaj nardi commit
  53.     db.commit();
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.     try:
  61.         # NI POMEMBNO
  62.         # reading data from Arduino, sent over UDP on 192.168.2.100:5000
  63.         # data, server = client_socket.recvfrom(4096);
  64.         # string received over UDP is in format b'string', that's why I decode it and get first 3 letters (type of element)
  65.         # element = data[0:3].decode('UTF-8')
  66.         # Arduino sends temperature data in format TMP=22.50Mastr
  67.         # (TMP=    - type of element)
  68.         # (22.50   - temperature in celsius)
  69.         # (Mastr   - 5 digit room ID) (in this project also called node)
  70.         # if (element == "TMP"):
  71.  
  72.             # value = data[4:9].decode('UTF-8')
  73.             # node = data[9:14].decode('UTF-8')
  74.  
  75.             # updates temperatura based on a room ID (node)
  76.             # translation (SLO - ENG) - temperatura - temperature, prostor - room, id_prostor - id_room
  77.  
  78.  
  79.  
  80.  
  81.             # tle sm naredu querry za update, """ je v pythonu multiline string
  82.             # concatenatov sm parametre, jih shranu v spremenljivko query, in potem izvedu s cur.execute(query)
  83.             query = """UPDATE temperatura set temperatura = """ + value + """
  84.            WHERE id_prostor = (SELECT id_prostor from prostor where id_arduino = \'""" + str(node) + "\')"
  85.             print(query)
  86.             cur.execute(query)
  87.  
  88.  
  89.  
  90.  
  91.     except Exception as e:
  92.         pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement