Skip to content

Instantly share code, notes, and snippets.

@apherio
Created November 16, 2018 08:04
Show Gist options
  • Save apherio/31f8ff515e1f9382c4875e90d8dc7bc8 to your computer and use it in GitHub Desktop.
Save apherio/31f8ff515e1f9382c4875e90d8dc7bc8 to your computer and use it in GitHub Desktop.
Gateway broadcast for Pubnub
# importing pubnub libraries
from pubnub.pubnub import PubNub, SubscribeListener, SubscribeCallback, PNStatusCategory
from pubnub.pnconfiguration import PNConfiguration
from pubnub.exceptions import PubNubException
import pubnub
import serial
import json
import csv
import simplejson
import time
pnconf = PNConfiguration() # pubnub_configuration_object
pnconf.publish_key = 'XXXXXXXXXXX' # pubnub publish_key
pnconf.subscribe_key = 'XXXXXXXXXX' # pubnub subscibe_key
pnconf.uuid = 'Gateway-device'
ser = serial.Serial(
port= '/dev/ttyACM0',
baudrate=115200,
timeout=2.1
)
ser.flushInput()
ser.write(b'\r\r')
time.sleep(0.5)
ser.write(b'\r\r')
ser.write(b'\r\r')
time.sleep(0.5)
ser.write(b'\r\r')
res=ser.read(10)
time.sleep(0.5)
ser.write(b'lep\r')
pubnub = PubNub(pnconf) # pubnub_object using pubnub_configuration_object
channel='Railway' # pubnub channel_name
my_listener = SubscribeListener() # listner_object to read the msg from the Broker/Server
pubnub.add_listener(my_listener) # listner_object to pubnub_object to subscribe it
pubnub.subscribe().channels(channel).execute() # subscribe the channel (Runs in background)
my_listener.wait_for_connect() # wait for the listner_obj to connect to the Broker.Channel
print('connected') # print confirmation msg
#dumps_lines(objs = ser.realines())
data = ser.readlines() # data to be published
while True:
pubnub.publish().channel(channel).message(data).sync()
result = my_listener.wait_for_message_on(channel)
print(result.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment