2.3. SORACOM Beam Pubnub Bidirectional Messaging - soracom/handson GitHub Wiki
You can learn how to control your device remotely and safely by using SORACOM Beam and MQTT.
- RPi connected with SORACOM Air
- Recommend to complete the previous scenario 2.2
PubNub’s realtime network provides the infrastructure and APIs for low-latency communication across IoT deployments at any size, anywhere in the world. The PubNub Data Stream Network (DSN) lets developers serve dynamic content to and between devices and even leverage pre-built integrations with popular IoT APIs. For IoT developers, this means you can connect even the smallest, most constrained devices using the IoT-optimized MQTT protocol.
SORACOM Beam (“Beam”) is a service that enables you to offload the high-load processes related to device encryptions into the cloud. It supports generic protocols like HTTP/MQTT/RAW TCP socket/RAW UDP packet.
To use PubNub MQTT interface, a device will need to build the ClientID string by concatting the publish key and subscribe key and client UUID with “/”.
publish_key + "/" + subscribe_key + "/" + client_uuid
This would usually require hard-coding your credential in the application code running on the device. This is very secure, but not very flexible. Imagine if you already deployed your devices all over the world and need to change that credential?
In a case like this, Beam comes to the rescue. Devices should only know the Beam entry point address (beam.soracom.io) and authentication is properly handled behind the curtain. You just update the credential in web based user console and it is applied to new connections right away.To use Beam, devices first need to connect using SORACOM’s IoT-optimized SIM. First log-in to SORACOM user console.
https://console.soracom.io/#/sam_login
*Ctrl/Cmd+Click
to open a new browser tab, so that you can keep this page open
Please enter the following information to log in:
- Operator ID:
OP0062924430
- Username:
workshop
- Passworrd:
X4HqiGF6
At SIM management screen:
Please find the sticker on the back of 3G USB Dongle you received. Enter the name to the search window.
- Select the SIM you are using, and click "Actions" then select "Change Group".
- Select "Beam_Pubnub" group for your SIM.
# How to send/receive data?
First, you need to login to Pubnub Website using Workshop account.
*Ctrl/Cmd+Click
to open a new browser tab, so that you can keep this page open
Please enter the following information to log in:
- Email:
workshop
- Passworrd:
X4HqiGF6!
Navigate to click "Demo Project":
Navigate to click "Demo Key Set":
Navigate to click "Debug Console":
Type your 'Default Channel" Name, and click 'Add Client':
- For example: "beam_ws001"
Now it is ready to send data to Pubnub. When your device is connected using the Soracom SIM, you can just publish a message to the Beam endpoint without authentication like below.
$ mosquitto_pub -h beam.soracom.io -t beam_ws001 -m 'Hello PubNub from Beam!'
- please change "beam_ws001" to what you setup on Pubnub Debug Console.
You check the PubNub debug console if the data is arrived:
You can also send data from Pubnub debug console to devices by subscribe to a topic ("beam_ws001" in this case):
$ mosquitto_sub -h beam.soracom.io -t beam_ws001 {"text":"Hello Beam from PubNub!"}
Then, type in on Pubnub Debug Console:
You should see the console for the message arriving:
$ mosquitto_sub -h beam.soracom.io -t beam_ws001 {"text":"Hello Beam from PubNub!"}
{"text": "Hello From Pubnub"}
It is very cool to integrate your web app or mobile app with embedded device. Please let us know if you make something cool!
Once you have connected your device , you can configure your Beam setting by modifying the SIM configuration group (If you did not create a group for your SIM upon registering, you can create a new group and tie it to the SIM).
Open the group and open Beam setting, select Type in Destination as “PubNub” and click on + in “Credentials Set” section.
You will be asked to provide Publish Key and Subscribe Key for your PubNub key. Click on “Register” once you filled all required fields and click on “Save” in the MQTT configuration window.