This tutorial walks through receiving a message from the Hologram Cloud via our Python SDK. You will be using a modem to establish a cellular connection to receive inbound messages from the Hologram Cloud.
Before starting, make sure you have the following items:
- Active device (SIM) listed on your dashboard.
- A supported cellular modem (Hologram Nova or Huawei E303/MS2131 modem).
- Raspberry Pi 3 running Raspbian Jessie.
The SIM card is available from the Hologram store.
Setup InstructionsActivate Your SIMIf you haven’t already, create a Hologram account to manage your devices and billing.
From the Hologram Dashboard, click the Activate SIM button in the top-right corner.
Every SIM card has an 18-22 digit unique ID printed on it. Enter your SIM’s ID in the activation form.
Then choose your data plan and zone according to your needs. If you aren’t sure which plan is right for you, start with pay-as-you-go. See our pricing page for more details on plans and zones.
Once your device is active, you’re ready to start sending messages to the cloud.
Setting up the Python SDKWe wrote and tested this tutorial by installing and using the Hologram SDK on Raspbian Jessie. Please ensure that you have Python 2.7 installed on your Raspberry Pi. You’ll need to install the Python SDK. Please refer to this doc for SDK installation instructions. After installing the Python SDK, let’s start by creating an empty Python script named receive-hologram-message.py
.
You’ll then need to import the appropriate modules. In this example, let’s go ahead and import HologramCloud
like this:
from Hologram.HologramCloud import HologramCloud
Instantiating a HologramCloudAfter that, we create a HologramCloud
instance:
hologram = HologramCloud(dict(), network='cellular')
Connect and initialize the receive socketLet’s connect to the cell network and initialize the receive socket.
result = hologram.network.connect()
if result == False:
print ' Failed to connect to cell network'
hologram.openReceiveSocket()
This will listen to inbound data asynchronously. You might want to stick in a delay here to keep listening for inbound connections. This is how I would do it:
time.sleep(20) # sleep for 20 seconds
Receiving a cloud messageWe are now ready to start sending messages to the Pi running the Python SDK. Navigate to your dashboard and select the device that you’re using in the cellular modem.
Under Messaging, type a couple words into the input field (I typed “Hi there!”), set the port to 4010
and set the protocol to TCP
. Hit Send data message
.
You should be able to see your received message logged shortly after you send it. We also give you an option to manipulate the received message itself by calling .popReceivedMessage()
.
# Sending "first message"
# Sending "second message"
# Sending "third message"
receivedMsg = hologram.popReceivedMessage() # pops/returns "first message"
The Hologram receive buffer acts like a queue of received messages, and you can pop the earliest message sent.
Closing the receive socket and disconnect from cellular networkOnce we’re done, let’s close the receive socket and disconnect from the cellular network.
hologram.closeReceiveSocket()
hologram.network.disconnect()
SummaryHurray, you’re now receiving inbound data messages to your Pi! Your final Python script should look something like this:
from Hologram.HologramCloud import HologramCloud
hologram = HologramCloud(dict(), network='cellular')
result = hologram.network.connect()
if result == False:
print ' Failed to connect to cell network'
hologram.openReceiveSocket()
time.sleep(20) # sleep for 20 seconds
hologram.closeReceiveSocket()
recv = hologram.popReceivedMessage()
print 'Receive buffer: ' + str(recv)
hologram.network.disconnect()
Want to learn about more Hologram Python SDK features? Stay tuned for more tutorials! In the meantime, check out our official documentation, and feel free to post your questions in our forum.
Comments