This quick start tutorial walks through sending a message from a Raspberry Pi to the Hologram Cloud via our Python SDK.
This tutorial also assumes that the Python SDK doesn't handle the network interfaces. This is automatically done by your operating system. We will go more in depth on how to send messages by establishing a cellular connection in a separate tutorial.
Setup InstructionsSetting up the Python SDKWe wrote and tested this tutorial by installing and using the Hologram SDK on Raspbian Stretch. 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 send-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:
In order to receive messages from the Hologram cloud, we need to establish a cellular network.
hologram = HologramCloud(dict(), network='cellular')
Connect to cellular networkLet’s connect to the cell network.
result = hologram.network.connect()
if result == False:
print ' Failed to connect to cell network'
Sending a cloud messageresponse_code = hologram.sendMessage("hello, world!")
print hologram.getResultString(response_code) # Prints 'Message sent successfully'.
Go ahead and run python send-hologram-message.py
. You should receive a logged message that says: Message sent successfully
.
Go back to your dashboard and click on the Logs section and you should be able to see the hello, world!
message that you just sent.
Once we’re done, let’s disconnect from the cellular network.
hologram.network.disconnect()
SummaryHurray, you’re now sending data messages from your Raspberry Pi to the Hologram Cloud! 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'
response_code = hologram.sendMessage("hello, world!")
print hologram.getResultString(response_code) # Prints 'Message sent successfully'.
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