PYTHON CODE

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.0 kB
5
Indexable
Never
import serial
import os
import time
from time import sleep 
ser = serial.Serial(
port='/dev/ttyACM0',
 
baudrate = 9600,
timeout=0.1,
writeTimeout=1,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=False,
rtscts=False,
dsrdtr=False
)
counter=0
ser.close()
ser.open()
ser.flushInput()
ser.flushOutput()
while True:
           serdata  =str(ser.readline())
        
           data1=serdata.rfind('!',0,7)
           print(serdata)
           if data1>-1:
              sensor=open("/var/www/html/type.txt","w")
              sensor.write('ALUMINUM')
              sensor.close()
              
              sensor=open("/var/www/html/status.txt","w")
              sensor.write('WAITING')
              sensor.close()
              
              
              ser.flushInput()
              ser.flushOutput()
           data1=serdata.rfind('@',0,7)
           print(serdata)
           if data1>-1:
              sensor=open("/var/www/html/type.txt","w")
              sensor.write('PLASTIC')
              sensor.close()
              
              sensor=open("/var/www/html/status.txt","w")
              sensor.write('WAITING')
              sensor.close()
              
              ser.flushInput()
              ser.flushOutput()
           data1=serdata.rfind('#',0,7)
           print(serdata)
           if data1>-1:
              sensor=open("/var/www/html/type.txt","w")
              sensor.write('REJECT')
              sensor.close()
              
              sensor=open("/var/www/html/status.txt","w")
              sensor.write('WAITING')
              sensor.close()
              
              ser.flushInput()
              ser.flushOutput()


           data1=serdata.rfind('%',0,7)
           print(serdata)
           if data1>-1:
              sensor=open("/var/www/html/type.txt","w")
              sensor.write('')
              sensor.close()
              
              sensor=open("/var/www/html/status.txt","w")
              sensor.write('SENDING SMS')
              sensor.close()
              ser.write("C".encode())
              
              ser.flushInput()
              ser.flushOutput()

           data1=serdata.rfind('&',0,7)
           print(serdata)
           if data1>-1:
              
              sensor=open("/var/www/html/status.txt","w")
              sensor.write('')
              sensor.close()
              ser.flushInput()
              ser.flushOutput()

              
              
              
              
           data1=serdata.rfind('R',0,12)
           print(serdata)
           if data1>-1:
              # serial    pl  al alb plb time    date
              # 970587358R0S0T0U0V15W29X10:12amY12/16/2022 
              sensor=open("/var/www/html/datafromarduino.txt","w")
              sensor.write(serdata)
              sensor.close()
              ser.flushInput()
              ser.flushOutput()
            

   
           cmdre=open("/var/www/html/datatoarduino.txt")
           cmde=cmdre.readline()
           data1=cmde.rfind('T',0,2);
           if data1>-1:
              sensor=open("/var/www/html/datatoarduino.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write(cmde.encode())
              print(cmde)

           
           
           cmdre=open("/var/www/html/cmd.txt")
           cmde=cmdre.readline()
           data1=cmde.rfind('z',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              os.system("sudo shutdown -h now")
              print("shutdown")

           cmdre=open("/var/www/html/cmd.txt")
           cmde=cmdre.readline()
           data1=cmde.rfind('a',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write("a".encode())
              print("med1")
    
    
           cmdre=open("/var/www/html/cmd.txt")
           cmde=cmdre.readline()
           data1=cmde.rfind('B',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write("B".encode())
              print("OPEN")
    
           cmdre=open("/var/www/html/cmd.txt")
           cmde=cmdre.readline()
           data1=cmde.rfind('A',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write("A".encode())
              print("OPEN")
    
    
    
           data1=cmde.rfind('b',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write("b".encode())
              print("med1")
           data1=cmde.rfind('c',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('x')
              sensor.close()
              ser.write("c".encode())
              print("med3")
           data1=cmde.rfind('y',0,2);
           if data1>-1:
              sensor=open("/var/www/html/cmd.txt","w")
              sensor.write('a')
              sensor.close()
              cmdre=open("/var/www/html/st.txt")
              cmde=cmdre.readline()
              cmde=cmde.replace('/',' ')
              cmde="sudo date --set=\""+ cmde+"\""

              sensor=open("/var/www/html/st.txt","w")
              sensor.write('')
              sensor.close()
              
              os.system(cmde)
                                                                                
              time.sleep(2.5)
              
              os.system("sudo hwclock -w")