1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| # coding: utf-8
import socket
import RPi.GPIO as GPIO
import time
import MySQLdb
import sys
import serial
serial = serial.Serial('/dev/ttyUSB0', 115200)
paramMysql = {
'host' : '172.17.18.42',
'user' : 'raspberryGachesAfficheur',
'passwd' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'db' : 'velo'
}
gache = [17,27,22,0,0,0,0,0,0,0,0,0]
def ouvrirGache(numGache):
print "called OuvrirGache"
if (numGache > 0): ouvrirGache(0)
GPIO.output(gache[numGache],True)
time.sleep(2)
GPIO.output(gache[numGache],False)
host = '' # Toutes les interfaces réseau
port = 15555 # Port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
GPIO.setmode(GPIO.BCM)
for i in range(0,12):
GPIO.setup(gache[i],GPIO.OUT) # Gaches
GPIO.setup(12,GPIO.OUT) # Eclairage
GPIO.setup(16,GPIO.IN) # Intrusion
print host , port
while True:
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
try:
received_idcasier = conn.recv(1024)
if not received_idcasier: break
if (int(received_idcasier) < 42):
received_idcasier = int(received_idcasier)
ouvrirGache(received_idcasier)
try:
connexion = MySQLdb.connect(**paramMysql)
curseur = conn.cursor(MySQLdb.cursors.DictCursor)
requete_sql = """SELECT prenom, soldepoints FROM Cyclistes WHERE idcasier = %s"""
curseur.execute(requete_sql, (received_idcasier, ))
rows = curseur.fetchall()
for row in rows:
prenom = row['prenom']
soldepoints = row['soldepoints']
print(prenom)
print(soldepoints)
string = "Bienvenue "+prenom+" Bravo tu as "+str(soldepoints)+" points."
string_encode = string.encode()
serial.write(string_encode) |
Partager