Page 1 of 1

Running Various Programs at once

Posted: Sun Apr 17, 2016 9:30 am
by KnalexAlex
How can I run various programs at once. I have a DHT22 sensor sending info to Pubnub, and the LED's program running so I can control the form Pubnub. How can I run both programs at the same time?

the code for the DHT22 sensor is:

Code: Select all

    import time
    import sys
    from pubnub import Pubnub
    import Adafruit_DHT as dht

    pubnub = Pubnub(publish_key='demo', subscribe_key='demo')
    channel = 'pi-house'

    def callback(message):
    print(message)

    #published in this fashion to comply with Eon
    while True:
        h,t = dht.read_retry(dht.DHT22, 4)
        temp='{0:0.1f}'.format(t)
        hum='{0:0.1f}'.format(h)
        message = {'temperature': temp, 'humidity': hum}
        print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(t, h)
        pubnub.publish(channel=channel, message=message, callback=callback, error=callback)  

Code: Select all

the code for the led's is:
    
    import RPi.GPIO as GPIO
    import time
    import sys
    from pubnub import Pubnub

    GPIO.setmode(GPIO.BCM)

    PIN_LIVING = 22
    PIN_PORCH = 17
    PIN_FIREPLACE = 27

    GPIO.setup(PIN_LIVING,GPIO.OUT)
    GPIO.setup(PIN_PORCH,GPIO.OUT)
    GPIO.setup(PIN_FIREPLACE,GPIO.OUT)

    FREQ = 100 # frequency in Hz
    FIRE_FREQ = 30 #  flickering effect

    # Duty Cycle (0 <= dc <=100)

    living = GPIO.PWM(PIN_LIVING, FREQ)
    living.start(0)

    porch = GPIO.PWM(PIN_PORCH, FREQ)
    porch.start(0)

    fire = GPIO.PWM(PIN_FIREPLACE, FIRE_FREQ)
    fire.start(0)

    # PubNub

    pubnub = Pubnub(publish_key='demo', subscribe_key='demo')

    channel = 'pi-house'

    def _callback(m, channel):
        print(m)

        dc = m['brightness'] *10

        if m['item'] == 'light-living':
            living.ChangeDutyCycle(dc)

        elif m['item'] == 'light-porch':
            porch.ChangeDutyCycle(dc)

        elif m['item'] == 'fireplace':
            fire.ChangeDutyCycle(dc)

    def _error(m):
      print(m)

    pubnub.subscribe(channels='pi-house', callback=_callback, error=_error)

    try:
        while 1:
            pass
    except KeyboardInterrupt:
        GPIO.cleanup()
        sys.exit(1)

    


Thanks!

Re: Running Various Programs at once

Posted: Sun Apr 17, 2016 11:15 am
by pcmanbob
Hi.

This depends on how and when you want to run your programs, if you are just running them from the terminal window or via ssh then you could run them both like this :-
python test1.py & python test2.py &

both programs will run but if you close the terminal window then they will of course stop.

If you want them both to run at system boot and continue to run then I would suggest using cron to run both programs at boot
using a line like this for each program :- @reboot python /home/pi/test1.py &

this web site gives a good explanation on how to use cron. :- http://www.raspberrypi-spy.co.uk/2013/0 ... sing-cron/

Re: Running Various Programs at once

Posted: Sun Apr 17, 2016 2:49 pm
by experix
Another way is to run them in separate terminals or x-terms. That way their outputs don't get mixed up and it's less confusing to run them than when you use "command1 & command2" in one terminal.