KnalexAlex
Posts: 10
Joined: Wed Mar 16, 2016 7:13 pm

Running Various Programs at once

Sun Apr 17, 2016 9:30 am

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!

pcmanbob
Posts: 6646
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Running Various Programs at once

Sun Apr 17, 2016 11:15 am

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/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: Running Various Programs at once

Sun Apr 17, 2016 2:49 pm

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.

Return to “Automation, sensing and robotics”