IoT Device Control
IoT devices are becoming a part of the mainstream electronics culture and people are adopting smart devices into their homes faster than ever. It's estimated that there are up to 21 billion devices connected to the internet today. The future is happening now, and these devices are getting smarter every day through machine learning and artificial intelligence.
This improves cities' overall efficiency and saves the government money since everything can be remotely managed. Smart homes, thermostats, lighting systems, and coffee makers can all collect data on your habits and patterns of usage. All this data can be used to help facilitate machine learning.
PubNub makes connecting IoT devices easy. You don't need to set up your own servers and write custom code to communicate with your back end. With PubNub, all you need is the code below to publish information to any devices subscribed to the channel.
Features
Features that PubNub offers to support your IoT development:
- Monitor device activity
- Securely send signals to your devices
- View activity via realtime dashboards
- Store device shadows
These features are offered through our MQTT Gateway. It's super easy to integrate MQTT devices into PubNub. Simply put your pub/sub credentials into your device identifier, and PubNub takes care of the rest.
Publishing a message
This operation happens as regular publish API usage without any specific format. Pick a topic and send a message to it.
When using PubNub, you simply create channels (known as topics in the MQTT world) and publish messages to those channels with your pub/sub keys. You do the same with MQTT. The only two things you need to do to connect your device to the PubNub Network are:
- Use a broker address of
mqtt.pndsn.com
. Use the standard ports; PubNub supports port 1883 for unsecured connections and port 8883 for TLS secured connections. - Use a client ID composed as follows:
<publish_key>
/<subscribe_key>
/<actual device ID>
.
For example, here's how to set up a Python client and publish a message:
import paho.mqtt.client as mqtt
publish_key = "<your publish key>"
subscribe_key = "<your subscribe key>"
client_id = "<your unique client identifier>"
client = mqtt.Client(client_id=publish_key + "/" + subscribe_key + "/" + client_id)
client.connect("mqtt.pndsn.com", 1883, 60)
client.publish("<topic to publish>", json.dumps({ "hi": 10 }))
Subscribing to a channel
This operation happens as regular subscribe API usage without any specific format. Pick a topic and subscribe to receive messages from it. For example, subscribing with the Python MQTT client:
import paho.mqtt.client as mqtt
publish_key = "<your publish key>"
subscribe_key = "<your subscribe key>"
client_id = "<your unique client identifier>"
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client(client_id=publish_key + "/" + subscribe_key + "/" + client_id)
client.connect("mqtt.pndsn.com", 1883, 60)
client.on_message = on_message
client.subscribe("<topic to subscribe>")