]> vault307.fbx.one Git - garage_door_sensor.git/commitdiff
garageStatus.py now correctly modifies html code of index.html based on
authorjimmy <jimipunk88@gmail.com>
Thu, 25 Jul 2024 23:56:46 +0000 (18:56 -0500)
committerjimmy <jimipunk88@gmail.com>
Thu, 25 Jul 2024 23:56:46 +0000 (18:56 -0500)
serial data. will set as service to run garage host. this accurately
reflects the sensor readings and updates webpage to display. scrape this
page from client to activate LED based on light on/off door open/closed

garageStatus.py
index.html [new file with mode: 0644]

index 6fe6ac773c9e905ca8c2a55d65af014dc0cbeefc..265a49fc0e0b5e1d8ff58548a8c773ea34e60cc7 100755 (executable)
@@ -1,21 +1,57 @@
 #!/usr/bin/python3
 #!/usr/bin/python3
-import serial, time
+import serial,time
+from bs4 import BeautifulSoup
 
 ser=serial.Serial('/dev/ttyACM0',9600,timeout=.5)
 
 ser=serial.Serial('/dev/ttyACM0',9600,timeout=.5)
-
-l='' #lightsensor
-g='' #garagedoor  sensor
+global d
+global l
+d=''
+l=''
+global dstate
+global lstate
+dstate=''
+lstate=''
 
 def readLine():
 
 def readLine():
-       global l
-       global g
-       data=str(ser.readline())
-       l=data[2]
-       g=data[4]
-       return l
-       return g
+    global d
+    global l
+    data=str(ser.readline())
+    d=data[4]
+    l=data[2]
+    return d,l
+
+def check():
+    global d
+    global l
+    global dstate
+    global lstate
+    with open('/var/www/html/index.html') as fp:
+        soup=BeautifulSoup((fp),features='html.parser')
+    dstate=soup.find(id='dstate')
+    lstate=soup.find(id='lstate')
+    fp.close()
+    if int(l)==0:
+        lstate.string='OFF'
+    if int(l)==1:
+        lstate.string='ON'
+    if int(d)==1:
+        dstate=soup.find(id='dstate')
+        dstate.string='OPEN'
+        with open('/var/www/html/index.html','w') as writer:
+            writer.write(str(soup))
+        fp.close()
+        writer.close()
+        return dstate
+    elif int(d)==0:
+        dstate.string='CLOSED'
+        with open('/var/www/html/index.html','w') as writer:
+            writer.write(str(soup))
+        fp.close()
+        writer.close()
+        return dstate
 
 while True:
 
 while True:
-       readLine()
-       print(l,g)
-       time.sleep(1)
+    readLine()
+    check()
+    print(d,dstate)
+    time.sleep(.5)
diff --git a/index.html b/index.html
new file mode 100644 (file)
index 0000000..b217957
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE hmtl>
+<html lang="en">
+<html>
+<head>
+<meta charset="UTF-8" />
+<meta content="Garage Door/Light sensor server" name="description"/>
+<meta name="viewport" content="width=device-width, initial-scale=1.0" />
+<title>Garage Status</title>
+</head>
+<body>
+<h1>GARAGE</h1>
+<p id="l">Light is <span id="lstate">OFF</span></p>
+<p id="d">Door is <span id="dstate">CLOSED</span></p>
+</body>
+</html>