1

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:

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)  

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!

4
  • How do you start the programs?
    – Bex
    Commented Apr 17, 2016 at 9:42
  • At least one of the programs produces output when operating normally. Where do you want the output to go? The second program produces output at least on error. Where do you want that to go?
    – goldilocks
    Commented Apr 17, 2016 at 11:36
  • ObscureRobot, thank you but how to stop it? Commented Apr 4, 2017 at 16:03
  • If it's running, can I stop it with a script? Commented Apr 4, 2017 at 16:04

2 Answers 2

3

If you want to run multiple programs at once from the command line, you can use & to run a command in the background:

$ python foo.py > foo.log &
$ python bar.py > bar.log &

However, if your ssh session ends, or you log out, those processes will be terminated. If you would like to keep your programs running after logging out, use nohup:

$ nohup python foo.py > foo.log &
$ nohup python bar.py > bar.log &

If you want to run programs at specific times, look into crontab:

$ man 5 crontab

If you need your programs to run at boot time, look into the System V init system.

2
  1. How are you running the programs individually? Are you SSH-ing into your Pi? If so, then you can open two terminals and run them both separately. You can use the "python program-name.py" command.

  2. Another option, is to combine the two codes into one program and run that, so that you are publishing on a channel "A" for the temperature and humidity, and subscribe on another channel "B" to receive commands for the lights in your house. PubNub python publish and subscribe are asynchronous, so they don't block on each other.

  3. A 3rd option is to use Python-twisted, which handles threading, so you can have different threads for publish and subscribe.

1
  • 1
    For bullet 1. terminal multiplexers like tmux or screen might come in handy. There'll be a blog post any time soon (see here)
    – Ghanima
    Commented Apr 18, 2016 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.