Python Forum

Full Version: please help me in loop i am trying to run this program in raspberry pi by sending msg
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
import paho.mqtt.client as mqttClient
import time, random, RPi.GPIO as GPIO

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)


 
def on_connect(client, userdata, flags, rc):
 
    if rc == 0:
 
        print("Connected to broker")
 
        global Connected                
        Connected = True                 
 
    else:
 
        print("Connection failed")
 
Connected = False

def setupgpio():
  GPIO.setup(LED_RED, GPIO.OUT)
  GPIO.setup(LED_yellow, GPIO.OUT)
  GPIO.setup(LED_GREEN, GPIO.OUT)
        
def alloff (val):
    GPIO.output (LED_RED, LEDOFF)
    GPIO.output (LED_yellow, LEDOFF)
    GPIO.output (LED_GREEN, LEDOFF)
    time.sleep(val)
    
def allon (val):
    GPIO.output (LED_RED, LEDON)
    GPIO.output (LED_yellow, LEDON)
    GPIO.output (LED_GREEN, LEDON)
    time.sleep(val)

def red (val):
    GPIO.output (LED_RED, LEDON)
    GPIO.output (LED_yellow, LEDOFF)
    GPIO.output (LED_GREEN, LEDOFF)
    time.sleep(val)

def redamber (val):
    GPIO.output (LED_RED, LEDON)
    GPIO.output (LED_yellow, LEDON)
    GPIO.output (LED_GREEN, LEDOFF)
    time.sleep(val)
    
def yellow (val):
    GPIO.output (LED_RED, LEDOFF)
    GPIO.output (LED_yellow, LEDOFF)
    GPIO.output (LED_GREEN, LEDON)
    time.sleep(val)


def green (val):
    GPIO.output (LED_RED, LEDOFF)
    GPIO.output (LED_yellow, LEDOFF)
    GPIO.output (LED_GREEN, LEDON)
    time.sleep(val)
    




 
broker_address= "broker name"
port = 1883
user = "user name"
password = "pwd"

LED_RED = 7
LED_yellow = 11
LED_GREEN = 13

LEDOFF = 0
LEDON = 1
 
client = mqttClient.Client()                       
client.username_pw_set(user, password=password)    
client.on_connect= on_connect                      
client.connect(broker_address, port=port)

 
client.loop_start()       
 
while Connected != True:    
    time.sleep(0)

def basicSequence (val):
    count=0
    while count <val:
       
       
       
        red (3)
        print("sig: " +str(count))
        client.publish("/topic/signals", count)
            
        time.sleep(0)


def basicSequence (val):
    collect= 0
    while collect <val:
        
    
        
        redamber (1)
        print("sign: " +str(collect))
        client.publish("/topic/signals", collect)
            
        time.sleep(0)
        
def basicSequence (val):
    conn= 0        
    while conn <val:
        
       
        
        green (5)
        print("signa: " +str(conn))
        client.publish("/topic/signals", conn)

        
        time.sleep(0)

def basicSequence (val):
    coy= 0        
    while coy <val:
        
       
        
        yellow (3)
        print("signal: " +str(coy))
        client.publish("/topic/signals", coy)

        
        time.sleep(0)

        


            

        
 
try:   
  while True:
    setupgpio() 
    alloff(0)
    basicSequence (5)

    

    
   

except KeyboardInterrupt:
  GPIO.cleanup()
  exit()
what problem do you have? post the full traceback (if any) - in error tags
actually i am not a programmer, thats why i am stuck in this thing. i make this program for traffic signals where it runs periodically red,yellow and green. now i want this to run with mqtt on real time while the signal is red it send messages to subscribers and when its yellow or green it send messages when it flashes green or yellow. so i am struggling in my loop i could't manage to run with mqtt.