2017-08-08 1 views
1

J'ai un problème en Python pour un programme RaspberryPi. Dans cette boucle continue, il détectera un changement dans l'une des broches et ouvrira alors une autre broche + demandera une URL.Détection d'une modification de la boucle Python pour RaspberryPi

Cela fonctionne mais l'URL est déclenchée tout le temps. Donc, je veux optimiser la boucle pour avoir la demande d'URL seulement sur le changement.

Si trouvé Dectecting a value change in Python loop mais je ne pouvais pas le faire fonctionner correctement.

Pouvez-vous m'aider avec ceci? Merci d'avance! Votre aide est très appréciée :)

P.S. Si le code urllib peut être optimisé, faites le moi savoir. Je fais connaissance avec Python plus beginner jour par jour;)

#!/usr/bin/env python 

import time 
import urllib2 
import RPi.GPIO as GPIO 

GPIO.setmode(GPIO.BCM) 
GPIO.setup(26, GPIO.IN, GPIO.PUD_UP) 
GPIO.setup(24, GPIO.OUT) 

GPIO.output(24, GPIO.LOW) 

username = "some" 
password = "one" 

while True: 
    if GPIO.input(26): 
     GPIO.output(24, GPIO.HIGH) 
     password_mgr1 = urllib2.HTTPPasswordMgrWithDefaultRealm() 
     top_level_url1 = "http://192.168.x.x/dev/sps/io/VI7/On" 
     password_mgr1.add_password(None, top_level_url1, username, password) 
     handler1 = urllib2.HTTPBasicAuthHandler(password_mgr1) 
     opener1 = urllib2.build_opener(handler1) 
     opener1.open(top_level_url1) 

    else: 
     GPIO.output(24, GPIO.LOW) 
     password_mgr2 = urllib2.HTTPPasswordMgrWithDefaultRealm() 
     top_level_url2 = "http://192.168.x.x/dev/sps/io/VI7/Off" 
     password_mgr2.add_password(None, top_level_url2, username, password) 
     handler2 = urllib2.HTTPBasicAuthHandler(password_mgr2) 
     opener2 = urllib2.build_opener(handler2) 
     opener2.open(top_level_url2) 
     time.sleep(5) 

GPIO.cleanup() 

Répondre

0

j'écrire presque jamais code python mais qu'en ce qui suit (il peut y avoir des erreurs de syntaxe, mais vous obtenez l'essentiel)

#!/usr/bin/env python 

import time 
import urllib2 
import RPi.GPIO as GPIO 

GPIO.setmode(GPIO.BCM) 
GPIO.setup(26, GPIO.IN, GPIO.PUD_UP) 
GPIO.setup(24, GPIO.OUT) 

GPIO.output(24, GPIO.LOW) 

username = "some" 
password = "one" 
lastState = False 
while True: 
    state = GPIO.input(26) 
    if (state != lastState): 
     lastState = state 
     if state: 
      GPIO.output(24, GPIO.HIGH) 
      password_mgr1 = urllib2.HTTPPasswordMgrWithDefaultRealm() 
      top_level_url1 = "http://192.168.x.x/dev/sps/io/VI7/On" 
      password_mgr1.add_password(None, top_level_url1, username, password) 
      handler1 = urllib2.HTTPBasicAuthHandler(password_mgr1) 
      opener1 = urllib2.build_opener(handler1) 
      opener1.open(top_level_url1) 

     else: 
      GPIO.output(24, GPIO.LOW) 
      password_mgr2 = urllib2.HTTPPasswordMgrWithDefaultRealm() 
      top_level_url2 = "http://192.168.x.x/dev/sps/io/VI7/Off" 
      password_mgr2.add_password(None, top_level_url2, username, password) 
      handler2 = urllib2.HTTPBasicAuthHandler(password_mgr2) 
      opener2 = urllib2.build_opener(handler2) 
      opener2.open(top_level_url2) 
      time.sleep(5) 

GPIO.cleanup() 
+0

fonctionne comme un charme! Merci beaucoup. – Racso

+0

@Racso pas de problème heureux d'aider! – Peter

0

ajoutez juste une variable qui stocke si la valeur a changé:

# [...] 

haschanged = False 

while True: 
    if GPIO.input(26): 
     if haschanged: 
      continue 
     haschanged = True 
     # [...] 
    else: 
     haschanged = False 
     # [...] 
# [...]