Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #LED - LED
- #TEMPERATURE - TMP
- from socket import *
- from random import randint
- import time
- import random
- import MySQLdb
- import sys
- from pympler import asizeof # for measuring memory behaviour | asizeof.asizeof(variable)
- address = ('192.168.2.100', 5000) #define who we are talking to
- #address = ('109.182.33.136', 45510) #define who we are talking to
- client_socket = socket(AF_INET, SOCK_DGRAM) #set up the socket
- client_socket.settimeout(0.5) #only wait 1 second for a response
- try:
- db = MySQLdb.connect(host="127.0.0.1", # your host, usually localhost
- user="root", # your username
- passwd="Kalisovec5", # your password
- db="arduino") # name of the data base
- except Exception as e:
- sys.exit("We can't get into the database")
- primerjava = ""
- counter = 0
- while(1):
- cur = db.cursor()
- cur.execute("SELECT * from soba1")
- result = cur.fetchall()
- db.commit();
- # print(result)
- # checks if there is result from database
- if result:
- # counter is set only for the very first time, script is opened up
- if(counter != 0):
- # only sends sends command, if anything changed in database
- if(primerjava != result):
- # outputs column names, but leaves out id
- field_names = [i[0] for i in cur.description]
- # print(field_names[3])
- for i in range(1, len(field_names)) :
- # checks column by column if anything changed, and if changed
- # it will only send out that specific command
- if(primerjava != "") and (result[0][i] != primerjava[0][i]) :
- # concatenates comman for arduino
- # ex. led01 (from database) + led state (1/0)
- izbira = str(field_names[i]) + str(result[0][i])
- data = izbira
- # converts data to binary, so that data can be sent over UDP
- byte_data = data.encode()
- client_socket.sendto(byte_data, address) #send command to arduino
- try:
- rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
- except:
- pass
- primerjava = result
- else:
- field_names = [i[0] for i in cur.description]
- for i in range(1, len(field_names)):
- print("lala")
- izbira = str(field_names[i]) + str(result[0][i])
- print(izbira)
- data = izbira
- byte_data = data.encode()
- client_socket.sendto(byte_data, address) #send command to arduino
- try:
- rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
- except:
- pass
- counter += 1
- #db.commit();
- db.close()
- #time.sleep(2)
- '''
- # print(data)
- #data = izbira
- byte_data = data.encode()
- client_socket.sendto(byte_data, address) #send command to arduino
- try:
- rec_data, addr = client_socket.recvfrom(2048) #reading response from arduino
- # print(rec_data)
- except:
- pass
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement