#!/usr/bin/python3 from ev3dev.ev3 import * from time import sleep from subprocess import call from PIL import Image import zbarlight from socket import * def getCode(file_path): "This function gets text from QR code" with open(file_path,'rb') as image_file: image = Image.open(image_file) image.load() code = zbarlight.scan_codes('qrcode', image) return code def initMailbox(): "This function creates mailbox and opens socket" mbx = socket(AF_INET, SOCK_DGRAM) mbx.bind(('',12345)) return mbx def sendMessage(message): s=socket(AF_INET, SOCK_DGRAM) s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1) s.sendto(message.encode('utf-8'),('255.255.255.255',12345)) mB = MediumMotor('outB') mC = MediumMotor('outC') mA = LargeMotor('outA') encB = mB.position btn = Button() ir = InfraredSensor('in1') ir.mode = 'IR-PROX' mC.reset() mB.reset() mC.run_to_rel_pos(position_sp=-360, speed_sp=200) mC.stop(stop_action='hold') mailbox = initMailbox() while not btn.backspace: sendMessage('test!') m = mailbox.recv(1024).decode('utf-8') print(m) while not btn.backspace: print(ir.value()) #print(mC.position) #mB.run_forever(speed_sp=-200) distance = 0.7 * ir.value() if distance < 10: mB.stop(stop_action="hold") sleep(3) call(['fswebcam','/home/robot/qr-code.gif']) s = getCode('/home/robot/qr-code.gif') print(s) mB.stop(stop_action='hold') mC.run_to_rel_pos(position_sp=360, speed_sp=200) sleep(2) mC.stop(stop_action='hold')