def disconnect(message): print("DISCONNECTED") pubnub.subscribe(channel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect) dataCollectionInterval = 15 time.sleep(dataCollectionInterval) pubnub.unsubscribe(channel=channel) print "Raw data:" print rawData chartDataX = [] chartDataY = [] for time, item in enumerate(rawData): chartDataX.append((time, item[0])) chartDataY.append((time, item[1])) print "chart data: " print chartDataX print chartDataY
class PubnubDataConnection(threading.Thread): "A connection to a PubNub data hub running on its own thread." def __init__(self, callback, credentials): """ Opens a PubNub connection with a callback and a credentials dict. :param callback: A callable to be called with two arguments: message_content and channel_name. :type callback: A function or object implementing the ``__call__`` method. :param credentials: A set of key/value pairs to be passed to PubNub. :type credentials: dict """ super(PubnubDataConnection, self).__init__() self._stop_event = threading.Event() self.callback = callback self.credentials = credentials self.channel = credentials['channel'] self.hub = Pubnub(publish_key=credentials.get('publishKey', None), subscribe_key=credentials.get('subscribeKey', None), cipher_key=credentials.get('cipherKey', None), auth_key=credentials.get('authKey', None), secret_key=None, ssl_on=True) if PY2 and "daemon" in Pubnub.__init__.func_code.co_varnames: self.hub.daemon = True elif PY3 and "daemon" in Pubnub.__init__.__code__.co_varnames: self.hub.daemon = True self.setDaemon(True) def run(self): """Thread method, called implicitly after starting the thread.""" self.subscribe(self.channel, self.callback) while not self._stop_event.is_set(): time.sleep(1) def stop(self): """Mark the connection/thread for being stopped.""" self._stop_event.set() self.unsubscribe(self.channel) def subscribe(self, channel_name, callback): """ Subscribes a callable to a channel with the given name. :param channel_name: The channel name. :type channel_name: string :param callback: The callable to be called with two arguments: message_content and channel_name. :type callback: A function or object implementing the ``__call__`` method. """ self.hub.subscribe(channel_name, callback) def unsubscribe(self, channel_name): """ Unsubscribes from a channel with given name. :param channel_name: the channel name :type channel_name: string """ self.hub.unsubscribe(channel_name)
try: pubnub.subscribe(channels=subchannel, callback=_kill, error=_error) ## Change to callback while True: previous_state = current_state current_state = GPIO.input(sensor) if current_state != previous_state: new_state = "HIGH" if current_state else "LOW" if current_state: # Motion is Detected lock.acquire() cam.start_preview() # Comment in future cam.preview_fullscreen = False cam.preview_window = (10,10, 320,240) print('Motion Detected') for i in range(imgCount): curTime = (time.strftime("%I:%M:%S")) + ".jpg" cam.capture(curTime, resize=(320,240)) t = threading.Thread(target=processImage, args = (curTime,)) t.daemon = True t.start() time.sleep(frameSleep) cam.stop_preview() lock.release() time.sleep(camSleep) except KeyboardInterrupt: cam.stop_preview() pubnub.unsubscribe(subchannel) sys.exit(0)
t = cal_temp(v) if t > 50 and is_warning == False: message = 'The room temperature is ' + str(int(t)) + ' C' pubnub.publish("my_channel", message, callback=_callback, error=_error) runMotor() is_warning = True if t < 40 and is_warning == True: message = 'Now it is fine. T: ' + str(int(t)) + ' C' pubnub.publish("my_channel", message, callback=_callback, error=_error) print('Temperature: ', "{0:.2f}".format(t), ' C') stop() is_warning = False display(adc_value, t) sleep(0.05) except KeyboardInterrupt: reset_ports() GPIO.cleanup() pubnub.unsubscribe(channel="text") GPIO.cleanup()
def reconnect(message): print("RECONNECTED") def disconnect(message): print("DISCONNECTED") pubnub.subscribe(channel, callback=callback, error=callback, connect=connect, reconnect=reconnect, disconnect=disconnect) dataCollectionInterval = 15 time.sleep(dataCollectionInterval) pubnub.unsubscribe(channel=channel) print "Raw data:" print rawData chartDataX = [] chartDataY = [] for time, item in enumerate(rawData): chartDataX.append((time,item[0])) chartDataY.append((time,item[1])) print "chart data: " print chartDataX print chartDataY