- Google Cloud Functions used to send data using Python code to Google Cloud Pub/Sub where data can be published with a message
- Code from D-I-Ry with modifications - see 👏 Inspiration below
- Note: to open web links in a new window use: ctrl+click on link
- Pub/Sub used to pass messages between services and to trigger Cloud Functions to run workflow
- To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that, passing a callback function.
- Python code deployed from VS Code terminal
- Cloud Build API has to be enabled for deployment to work. Mine was already enabled from a similar previous project
- From Pub/Sub factor-sensors topic is triggered by message published to pubsub factory sensors topic
- Python v3 programming language
- Python Cloud Client Libraries
- JavaScript Object Notation - JSON lightweight data-interchange format
- Google Cloud Functions functions as a service (FaaS) to run Python code
- Google Cloud Pub/Sub a fully-managed real-time messaging service that allows you to send and receive messages between independent applications.
- Google Cloud FireStore fully managed, scalable, and serverless document database.
- Ref. tutorial
- When creating a Pub/Sub I found that the subscription was created automatically
gcloud functions deploy automation_readings --runtime python39 --trigger-http --allow-unauthenticated
to deploy to Cloud Functions. Python code can be viewed in Cloud Functionsautomation_readings
SOURCE menu. Message logs can be viewed under the LOGS menu. Logs can also be viewed in Firebase Console Logs menu.python sendMessage.py
to runsendMessage.py
. This will trigger the Cloud Function using a http trigger URL and will supply sensor readings payload data in JSON format to the Cloud Functions Topicautomation_readings
. A response is returned with a status code. Clicking on 'PULL' in Cloud Pubsub Topic Messages will display message
main.py
extract - to move data to Pubsub. Topic path copied from Cloud Pub/Sub Subscription Details/Topic name
topic_path = 'projects/python-pubsub-firestore/topics/factory-automation'
message_json = json.dumps({
'data': {'message': 'automation sensor readings'},
'readings': {
'sensorName': sensor_name,
'sensorReference': sensor_reference,
'temperature': temperature,
'humidity': humidity
}
})
message_bytes = message_json.encode('utf-8')
try:
publish_future = publisher.publish(topic_path, data=message_bytes)
publish_future.result() # verify that the publish succeeded
except Exception as e:
print(e)
return (e, 500)
return ('Message received and published to Pubsub', 200)
- Cloud Pub/Sub decoupling senders and receivers, allowing secure and highly available communications between independently written applications.
- Status: Working
- To-do: Nothing
- Repo created by ABateman, email: gomezbateman@yahoo.com