Python Forum
please help me in loop i am trying to run this program in raspberry pi by sending msg
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please help me in loop i am trying to run this program in raspberry pi by sending msg
#1
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()
Reply
#2
what problem do you have? post the full traceback (if any) - in error tags
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,319 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Try-except in while loop: error with closing program Lupin_III 7 2,792 Jul-03-2020, 10:57 AM
Last Post: Lupin_III
  Port my python program to Raspberry pi seamlessly Hassibayub 1 1,897 Jun-29-2020, 12:58 PM
Last Post: snippsat
  While loop won't restart program :( TheDovah77 4 4,153 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  How to rerun the program in a loop with different input? bharaths 3 3,714 Nov-23-2018, 09:36 AM
Last Post: bharaths
  Nested while loop in pyramid program. raj55 2 5,706 May-04-2018, 10:05 AM
Last Post: ThiefOfTime
  WHILE LOOP in my program kurokszxcky 1 3,286 Oct-02-2017, 03:45 PM
Last Post: ichabod801
  sending data from my raspberry pi to my website mohitsangavikar 2 17,738 Sep-05-2017, 06:55 PM
Last Post: wrybread

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020