Advertisement
rozman50

sending and recieving data- ARDUINO

Dec 27th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. #LED - LED
  2. #TEMPERATURE - TMP
  3.  
  4. from socket import *
  5. from random import randint
  6. import time
  7. import random
  8. import MySQLdb
  9. import sys
  10.  
  11. from pympler import asizeof # for measuring memory behaviour | asizeof.asizeof(variable)
  12.  
  13. address = ('192.168.2.100', 5000) #define who we are talking to
  14. #address = ('109.182.33.136', 45510) #define who we are talking to
  15.  
  16. client_socket = socket(AF_INET, SOCK_DGRAM) #set up the socket
  17. client_socket.settimeout(0.5) #only wait 1 second for a response
  18.  
  19. try:
  20.     db = MySQLdb.connect(host="127.0.0.1",    # your host, usually localhost
  21.                         user="root",         # your username
  22.                         passwd="Kalisovec5",  # your password
  23.                         db="arduino")        # name of the data base
  24. except Exception as e:
  25.     sys.exit("We can't get into the database")
  26.  
  27. primerjava = ""
  28. counter = 0
  29.  
  30. while(1):
  31.  
  32.     cur = db.cursor()
  33.     cur.execute("SELECT * from soba1")
  34.     result = cur.fetchall()
  35.     db.commit();
  36.     # print(result)
  37.  
  38.     # checks if there is result from database
  39.     if result:
  40.  
  41.         # counter is set only for the very first time, script is opened up
  42.         if(counter != 0):
  43.             # only sends sends command, if anything changed in database
  44.             if(primerjava != result):
  45.                 # outputs column names, but leaves out id
  46.                 field_names = [i[0] for i in cur.description]
  47.                 # print(field_names[3])
  48.                 for i in range(1, len(field_names)) :
  49.                     # checks column by column if anything changed, and if changed
  50.                     # it will only send out that specific command
  51.                     if(primerjava != "") and (result[0][i] != primerjava[0][i]) :
  52.                         # concatenates comman for arduino
  53.                         # ex. led01 (from database) + led state (1/0)
  54.                         izbira = str(field_names[i]) + str(result[0][i])
  55.                         data = izbira
  56.  
  57.                         # converts data to binary, so that data can be sent over UDP
  58.                         byte_data = data.encode()
  59.                         client_socket.sendto(byte_data, address) #send command to arduino
  60.                         try:
  61.                             rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
  62.                         except:
  63.                             pass
  64.  
  65.                 primerjava = result
  66.         else:
  67.             field_names = [i[0] for i in cur.description]
  68.             for i in range(1, len(field_names)):
  69.                 print("lala")
  70.  
  71.                 izbira = str(field_names[i]) + str(result[0][i])
  72.                 print(izbira)
  73.                 data = izbira
  74.                 byte_data = data.encode()
  75.                 client_socket.sendto(byte_data, address) #send command to arduino
  76.                 try:
  77.                     rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
  78.  
  79.                 except:
  80.                     pass
  81.             counter += 1
  82.  
  83. #db.commit();
  84. db.close()
  85.  
  86.  
  87. #time.sleep(2)
  88.  
  89. '''
  90.  
  91. # print(data)
  92. #data = izbira
  93. byte_data = data.encode()
  94. client_socket.sendto(byte_data, address) #send command to arduino
  95. try:
  96.    rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
  97.    # print(rec_data)
  98.  
  99. except:
  100.    pass
  101.  
  102. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement