-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.py
37 lines (30 loc) · 1.15 KB
/
edge.py
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
import serial # PySerial library for Serial communication
import mysql.connector # MySQL library to connect to the database
import json # To deserialize the data sent by the Arduino
from datetime import datetime
device = '/dev/cu.usbmodem1201'
arduino = serial.Serial(device, 9600)
mydb = mysql.connector.connect(
host="localhost",
user="tuandoan",
db="smart_parking_db"
)
mycursor = mydb.cursor()
while True:
serialData = arduino.readline()
decodedData = serialData.decode("utf-8")
entry = json.loads(decodedData)
password = entry["password"]
if password == "Zz38dDtS3tXwveW":
arduino.write(b"open")
firstName, lastName = entry["name"].split()
phone = entry["phone"]
model = entry["model"]
plate = entry["plate"]
time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
sql = f"INSERT INTO parking (id, firstName, lastName, phone, model, plate, time) VALUES (NULL,'{firstName}','{lastName}','{phone}','{model}','{plate}','{time}')"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
else:
print("not authenticated")