Modifying functions and adding remote handler
This commit is contained in:
		
							parent
							
								
									d3fb04dc71
								
							
						
					
					
						commit
						ff914468d7
					
				
					 7 changed files with 35 additions and 123 deletions
				
			
		|  | @ -1,49 +0,0 @@ | |||
| import flask | ||||
| from RPi import GPIO | ||||
| import time | ||||
| import threading | ||||
| 
 | ||||
| app = flask.Flask(import_name=__name__) | ||||
| 
 | ||||
| motor_pin1 = 38 | ||||
| motor_pin2 = 40 | ||||
| 
 | ||||
| GPIO.setmode(GPIO.BOARD) | ||||
| GPIO.setup(motor_pin1, GPIO.OUT, initial=GPIO.LOW) | ||||
| GPIO.setup(motor_pin2, GPIO.OUT, initial=GPIO.LOW) | ||||
| 
 | ||||
| def switch_off(mini_rotation_counter=30): | ||||
|     GPIO.output(motor_pin1, GPIO.LOW) | ||||
|     GPIO.output(motor_pin2, GPIO.LOW) | ||||
|     for _ in range(mini_rotation_counter): | ||||
|         GPIO.output(motor_pin2, GPIO.HIGH) | ||||
|         time.sleep(0.002) | ||||
|         GPIO.output(motor_pin2, GPIO.LOW) | ||||
|         time.sleep(0.2) | ||||
| 
 | ||||
| def switch_on(mini_rotation_counter=30): | ||||
|     GPIO.output(motor_pin1, GPIO.LOW) | ||||
|     GPIO.output(motor_pin2, GPIO.LOW) | ||||
|     for _ in range(mini_rotation_counter): | ||||
|         GPIO.output(motor_pin1, GPIO.HIGH) | ||||
|         time.sleep(0.002) | ||||
|         GPIO.output(motor_pin1, GPIO.LOW) | ||||
|         time.sleep(0.2) | ||||
| 
 | ||||
| def into_thread(): | ||||
|     for _ in range(7): | ||||
|         switch_on() | ||||
|         switch_off() | ||||
|     switch_off() | ||||
| 
 | ||||
| @app.route("/open") | ||||
| def open_command_function(): | ||||
|     command_applier = threading.Thread(target=into_thread) | ||||
|     command_applier.start() | ||||
|     return "OK\n" | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| if __name__=="__main__": | ||||
|     switch_off() | ||||
|     app.run(host="0.0.0.0") | ||||
							
								
								
									
										46
									
								
								gsminit.py
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								gsminit.py
									
									
									
									
									
								
							|  | @ -1,46 +0,0 @@ | |||
| import serial | ||||
| import os | ||||
| import time | ||||
| import requests | ||||
| 
 | ||||
| port = serial.Serial("/dev/serial0", baudrate=9600, timeout=1) | ||||
| port.flush() | ||||
| 
 | ||||
| data="" | ||||
| print("Finding module") | ||||
| while 1: | ||||
|     data="";port.flush() | ||||
|     port.write(b'AT\n') | ||||
|     data=port.read(150) | ||||
|     print("received",data) | ||||
|     r=data.find(b'OK') | ||||
|     if r>=0: print("AT...good...");break | ||||
|     time.sleep(0.75) | ||||
| 
 | ||||
| while 1: | ||||
|     data="";port.flush() | ||||
|     port.write(b'AT+CLIP=1\n') | ||||
|     data=port.read(150) | ||||
|     print("received",data) | ||||
|     r=data.find(b'OK') | ||||
|     if r>=0: print("CLIP...good");break | ||||
|     time.sleep(0.75) | ||||
| 
 | ||||
| print("Finding network") | ||||
| while 1: | ||||
|     data="";port.flush() | ||||
|     port.write(b'AT+CPIN?\n') | ||||
|     data=port.read(150) | ||||
|     print("received",data) | ||||
|     r=data.find(b'READY') | ||||
|     if r>=0: print("READY...good");break | ||||
|     time.sleep(0.75) | ||||
| 
 | ||||
| while 1: | ||||
|     rcv = port.read(150) | ||||
|     if b"SM" in rcv: | ||||
|         port.write(b'AT+CMGF=1\n') | ||||
|         msg = port.write(b'AT+CMGR=1\n') | ||||
|         if b'please' in msg: print("got it !!!");port.write(b'AT+CMGDA=DEL ALL\n')#requests.get("http://192.168.0.103:/open"); port.write(b'AT+CMGDA=DEL ALL\n') | ||||
|     #print("received", rcv) | ||||
|     time.sleep(0.5) | ||||
							
								
								
									
										35
									
								
								myapp.py
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								myapp.py
									
									
									
									
									
								
							|  | @ -1,5 +1,8 @@ | |||
| import os | ||||
| import flask | ||||
| import evdev | ||||
| import requests | ||||
| import time | ||||
| import threading | ||||
| 
 | ||||
| app = flask.Flask(import_name=__name__) | ||||
|  | @ -11,6 +14,34 @@ def opener(lck): | |||
|         for _ in range(5): | ||||
|             requests.get("http://192.168.0.103/open") | ||||
| 
 | ||||
| def remote_handler(lck): | ||||
|     n_attempts = 0 | ||||
|     remote = None | ||||
|     while remote is None: | ||||
|         #---search for remote--- | ||||
|         input_devices = os.listdir("/dev/input") | ||||
|         for device in input_devices: | ||||
|             try: | ||||
|                 dev = evdev.InputDevice("/dev/input/"+device) | ||||
|                 if "Wireless Present" in dev.name and "Keyboard" in dev.name and "usb" in dev.phys: | ||||
|                     remote = dev | ||||
|                     print("Remote found") | ||||
|                     break | ||||
|             except: | ||||
|                 pass | ||||
|         #---start retrieving events if remote detected--- | ||||
|         if remote is not None: | ||||
|             try: | ||||
|                 for event in remote.read_loop(): | ||||
|                     if event.type==evdev.ecodes.EV_KEY and event.code==evdev.ecodes.KEY_B and event.value==1: | ||||
|                         print("Command received from remote") | ||||
|                         opener(lck) | ||||
|             except: | ||||
|                 remote = None | ||||
|         n_attempts += 1 | ||||
|         print("Attempt "+str(n_attempts)+" fails in finding remote") | ||||
|         time.sleep(3) | ||||
| 
 | ||||
| @app.route(rule="/") | ||||
| def index(): | ||||
|     return "200 OK (working)\n" | ||||
|  | @ -55,4 +86,6 @@ def receiver(): | |||
| 
 | ||||
| 
 | ||||
| if __name__=="__main__": | ||||
|     app.run(host="0.0.0.0", debug=True) | ||||
|     remote_handler_thread = threading.Thread(target=remote_handler, args=(lock,)) | ||||
|     remote_handler_thread.start() | ||||
|     app.run(host="0.0.0.0") | ||||
|  | @ -1,11 +0,0 @@ | |||
| import flask | ||||
| 
 | ||||
| app = flask.Flask(__name__) | ||||
| 
 | ||||
| @app.route("/") | ||||
| def index(): | ||||
|     return "Okay, it's working guy" | ||||
| 
 | ||||
| if __name__=="__main__": | ||||
|     app.run(host="0.0.0.0") | ||||
| 
 | ||||
|  | @ -52,7 +52,7 @@ class SIM800L: | |||
| 
 | ||||
|     def command(self, cmdstr, lines=1, waitfor=500, msgtext=None): | ||||
|         while self.ser.in_waiting: | ||||
|             self.ser.readline() | ||||
|             self.ser.readline() #or self.ser.read(150) | ||||
|         self.ser.write(cmdstr.encode()) | ||||
|         if msgtext: | ||||
|             self.ser.write(msgtext.encode()) | ||||
|  |  | |||
|  | @ -1,15 +0,0 @@ | |||
| import serial | ||||
| import os | ||||
| import time | ||||
| #import RPi.GPIO as GPIO | ||||
| 
 | ||||
| #GPIO.setmode(GPIO.BOARD) | ||||
| port = serial.Serial("/dev/serial0", baudrate=9600, timeout=1) | ||||
| port.flush() | ||||
| 
 | ||||
| port.write(b'AT\r') | ||||
| 
 | ||||
| rcv = port.read(100) | ||||
| 
 | ||||
| print("received", rcv) | ||||
| time.sleep(1) | ||||
		Loading…
	
		Reference in a new issue