Here’s how I connected my Hologram Nova to the Google Cloud Platform (Firebase).
Project Background:
I have a Raspberry Pi with physical sensors plugged in. A Python 3 script reads the sensors and is responsible for sending the data to my Firebase database. In this tutorial, I’ll be hardcoding the sensor values for simplicity (fill in your sensor functions here).
Solution Overview:
- we’ll create a Firebase database
- then we’ll get the credentials to write to Firebase (either with a REST call or with Firebase’s Python library)
- next, it’s time to code, and we’ll set up a Point-to-Point Protocol connection
- finally, we’ll write from our Nova directly to Firebase
Firebase Setup:
- firebase.google.com > “go to console” > “add project”
- enter a project name > (unselect A/B testing) > “create project” > “continue”
- click the “Web” icon next to the phrase “Add an app to get started”
- enter an app name (any name will do; you’ll never reference this again) > “register”
- copy the firebaseConfig variable for later use > “continue to console”
- in the left column, under Build > Firestore database > “create database” > “start in test mode” > “next”
- choose a physical location for your database under “Cloud Firestore location” (reference: Regiones y zonas | Documentación de Compute Engine | Google Cloud) > “enable”
You should now be in the Build > Firestore database > Data tab, which is where you’ll be able to see your data stream in!
Authentication Setup:
- There are 2 ways to authenticate:
- with a web API key for REST calls
- integrating the firebaseConfig variable that you copied above
For this tutorial, I’m going to show the REST approach because it’s generic, and because the firebaseConfig way needs to be integrated into your specific platform, and there are many tutorials for that on YouTube already.
To generate a web API key, navigate to Google Cloud Platform > (hamburger menu) > APIs & Services > Credentials > Create Credentials > API key > (copy the string)
PPP Setup:
Here’s an example Python script to enable PPP. Manually turn Wi-Fi off to verify
import netifaces # sudo pip3 install netifaces
import requests
ppp_exists = False
try:
netifaces.ifaddresses('ppp0')
ppp_exists = True
except:
ppp_exists = False
if ppp_exists == True:
try:
r = requests.get('https://google.com', timeout=10) # timeout's necessary in case we can't access the internet
print(f'status code = {r.status_code}')
except:
print('no connection')
print(f'PPP status: {ppp_exists}')
Write Directly from a Nova to the Google Cloud Platform:
import requests
import json
import datetime
project_id = 'MY_FIREBASE_PROJECT___SEE_THE_FIREBASECONFIG_VARIABLE_FROM_EARLIER'
web_api_key = 'MY_WEB_API_KEY'
collection_name = 'MySensors'
url = f'https://firestore.googleapis.com/v1/projects/{project_id}/databases/(default)/documents/{collection_name}/?key={web_api_key}'
d = datetime.datetime.utcnow()
time = d.strftime("%X") # hours:minutes:seconds
payload = {
'fields': {
'name': { 'stringValue': 'sensor123' },
'temperature': { 'doubleValue': 75 }, # NOTE: shorten these names to save on data throughput, ie: 't': {'doubleValue': 75}
'time': { 'stringValue': time }
}
}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
# print(f'response = {response.content}')
response_dict = json.loads(response.content) # print the response in a readable format
for i in response_dict:
print(f'{i}: {response_dict[i]}')
Thank you, @Reuben for helping me with the PPP interface!
And thanks for reading,
Ryan