Python and pub nub iot cloud by anshu pandey

Post on 21-Jan-2018

75 views 0 download

Transcript of Python and pub nub iot cloud by anshu pandey

PubNub IoT PlatformPython

By ANSHU PANDEY

by ANSHU PANDEY | anshuspandey@gmail.com

Usecases for pubnub

• IoT Secure Messaging

• IoT Device Control

• Message Push Notification

• Home Automation Signalling

• Check this for more details https://stackoverflow.com/questions/20843632/what-is-advantage-and-disadvantage-of-using-pubnub-over-amazon-simple-notificati

by ANSHU PANDEY | anshuspandey@gmail.com

Navigate to pubnub.com>> signup for an account or login with google

by ANSHU PANDEY | anshuspandey@gmail.com

Click on Create NEW APP+

by ANSHU PANDEY | anshuspandey@gmail.com

Create App

by ANSHU PANDEY | anshuspandey@gmail.com

After Creating app, scroll down to dashboard

by ANSHU PANDEY | anshuspandey@gmail.com

Click on your app, you just created, you might see a default keyset, which can be used to connect to PubNub and you can also create you own keyset.

by ANSHU PANDEY | anshuspandey@gmail.com

Click on New keyset to create a keyset

by ANSHU PANDEY | anshuspandey@gmail.com

You should be able to get three keys which can be used to connect pubnubAllow pubnub Blocks as shown in below snapshot

by ANSHU PANDEY | anshuspandey@gmail.com

Navigate to Blocks option from Left Menu & create a block

by ANSHU PANDEY | anshuspandey@gmail.com

Click on create to create event handlers

by ANSHU PANDEY | anshuspandey@gmail.com

Name you handler, select after presence from second option, provide topic in third option and remember the topic

by ANSHU PANDEY | anshuspandey@gmail.com

The block is currently stopped, click on start block to start the block

by ANSHU PANDEY | anshuspandey@gmail.com

Once the block is running, now you are all set to communicate via pubnub

by ANSHU PANDEY | anshuspandey@gmail.com

Navigate back to keyinfo to get keys

by ANSHU PANDEY | anshuspandey@gmail.com

Install python package for pubnub

pip install pubnub

by ANSHU PANDEY | anshuspandey@gmail.com

Python code for Configuring PubNub

from pubnub.pnconfiguration import PNConfiguration

from pubnub.pubnub import PubNub, SubscribeListener

from pubnub.enums import PNStatusCategory

pnconfig = PNConfiguration()

pnconfig.publish_key = "pub-c-097ac4e7-23cf-45ee-980f-1775137fdf4e"

pnconfig.subscribe_key = "sub-c-a3cd0700-2bcd-11e7-97de-0619f8945a4f"

pnconfig.secret_key="sec-c-NzJhOGJlZmItNzQ2OC00MmM2LTg3ZjAtNWUxOWE2MjY4NTgx"

pnconfig.uuid="test"

pubnub = PubNub(pnconfig)

by ANSHU PANDEY | anshuspandey@gmail.com

Connecting to broker

my_listener = SubscribeListener()

pubnub.add_listener(my_listener)

pubnub.subscribe().channels('mytopic/new').execute()

my_listener.wait_for_connect()

print('connected')

by ANSHU PANDEY | anshuspandey@gmail.com

pubnub.publish().channel('mytopic/new').message({'fieldA': 'awesome', 'fieldB': 10}).sync()

result = my_listener.wait_for_message_on('mytopic/new')

print(result.message)

by ANSHU PANDEY | anshuspandey@gmail.com

Navigate to debug console from left bottom option

by ANSHU PANDEY | anshuspandey@gmail.com

Enter topic name, UUID as test because in program UUID taken is test, dnt put any authorization key

by ANSHU PANDEY | anshuspandey@gmail.com

Now send data from pubnub, run your python code, analyse the messages

by ANSHU PANDEY | anshuspandey@gmail.com

Explore PubNub

by ANSHU PANDEY | anshuspandey@gmail.com

I am Happy to see you on this slide,Now its your turn to do something exciting with this.

With more Expectation Anshu Pandeyhttps://www.linkedin.com/in/anshupandey/

by ANSHU PANDEY | anshuspandey@gmail.com