0
0 Комментариев

Хочу сделать скрипт в автозапуск, что б он ждал 2 переменных, потом открыл реле(GPIO малины) сделал GEТ, и опять стал ждать переменные:

Питон 2.7, операционка raspberry os(debian)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import serial
import time
import RPi.GPIO as GPIO
import requests
import sys
import socket
GPIO.setmode(GPIO.BOARD)
GPIO.setup(33,GPIO.OUT)
GPIO.setup(35,GPIO.OUT)
GPIO.setup(37,GPIO.OUT)
GPIO.output(33,GPIO.HIGH)
GPIO.output(35,GPIO.HIGH)
GPIO.output(37,GPIO.HIGH)
ser = serial.Serial('/dev/ttyUSB1',9600)
ch = ser.readline()
mag = int(ch.encode('hex'),16)/100000-30
sock = socket.socket()
sock.bind(('', 9090))
sock.listen(1)
conn, addr = sock.accept()
 print 'connected:', addr
while True:
        data = conn.recv(1024)
        if not data:
            break
        conn.send(data.upper())
        opcl, prop = data.split(" ", 2)
        print(opcl)
        print(prop)
        z = int(opcl)+1
        print(z)
        print(data.upper())
        if int(opcl) < 8:
            if (mag == 6):
                ex = 4
            elif (mag == 4):
                ex = 3
            else:
                ex = 6    
            var = 0
            while var < 30:
                GPIO.output(33,GPIO.LOW)
                GPIO.output(35,GPIO.LOW)
                ch = ser.readline()
                mag = int(ch.encode('HEX'),16)/100000-30
                print(mag)
                if (ex == mag):
                    GPIO.output(33,GPIO.HIGH)
                    GPIO.output(35,GPIO.HIGH)                    
                    link = "http://localhost/vd.php?pr="+prop
                    f = requests.get(link)
                    print(f.text)
                    break;
                    exit()
                var = var + 1
            else:
                GPIO.output(33,GPIO.HIGH)
                GPIO.output(35,GPIO.HIGH)
                GPIO.output(37,GPIO.HIGH)                
        else:
            if (mag < 4):
                ex = 4
            elif (mag > 5):
                ex = 3
            else:
                ex = 6    
            var = 0
            while var < 30:
                GPIO.output(33,GPIO.LOW)
                GPIO.output(37,GPIO.LOW)
                ch = ser.readline()
                mag = int(ch.encode('HEX'),16)/100000-30
                print(mag)
                if (ex == mag):
                    GPIO.output(33,GPIO.HIGH)
                    GPIO.output(37,GPIO.HIGH)                    
                    link2 = "http://localhost/vihod.php?pr="+prop
                    f2 = requests.get(link2)
                    print(f2.text)
                    break;
                    exit()
                var = var + 1
            else:
                GPIO.output(33,GPIO.HIGH)
                GPIO.output(35,GPIO.HIGH)
                GPIO.output(37,GPIO.HIGH)                
conn.close()


Добавить комментарий