The PubSubEasy library provides a simplified way to publish messages to Google Cloud Pub/Sub topics from Arduino-based devices. It handles secure connections, JWT authentication, and message publication with optional attributes, making it easier to integrate IoT devices with Google Cloud Pub/Sub.
- Easy publishing of JSON messages to Google Cloud Pub/Sub.
- Support for adding custom message attributes.
- Integrated JWT authentication handling.
- Secure communication via HTTPS.
- An Arduino board with networking capabilities (e.g., ESP8266, ESP32).
- A Google Cloud Platform account and a Pub/Sub topic created.
- A service account with Pub/Sub Publisher role and its JSON key file.
-
Download the Library: Click on the "Code" button on this GitHub page and select "Download ZIP".
-
Install in Arduino IDE:
- Open the Arduino IDE.
- Go to
Sketch>Include Library>Add .ZIP Library.... - Choose the downloaded ZIP file and click "Open" to install.
-
Using PlatformIO:
- Add the library to your
platformio.inidependencies. - or copy the library files to your project's
libdirectory. - or make a
symlinkto the project'slibdirectory:
- Add the library to your
ln -s /path/to/YourLibraryName /path/to/projects/lib/YourLibraryName
-
Prepare Your Google Cloud Setup:
- Ensure you have a Pub/Sub topic created.
- Download the JSON key file for a service account with permissions to publish to the topic.
-
Configure Your Device:
- Store the service account JSON key in your device's file system (e.g., using SPIFFS or LittleFS).
-
Basic Usage:
#include <PubSubEasy.h>
// Replace these with your actual configuration values
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* projectID = "your-google-cloud-project-id";
const char* topicName = "your-pubsub-topic-name";
const char* serviceAccountKeyPath = "/path/to/service/account/key.json";
PubSubEasy pubSub(projectID, topicName, serviceAccountKeyPath);
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Initialize PubSubEasy
pubSub.init();
// Publish a test message
StaticJsonDocument<512> doc;
// Add sensor data dynamically
doc["temperature"] = 37.5; doc["humidity"] = 85.2;
// Serialize JSON object to String
String message; serializeJson(doc, message);
// Attributes
PubSubEasy::Attribute attributes[] = {
{"sensorId", "sensor_001"},
{"location", "greenhouse"}
};
pubSub.publish(message, attributes, 2);
}
void loop() {
// Your loop code
}- WiFi Setup: Ensure your device is connected to the internet via WiFi.
- Service Account and Key File: Place your Google Cloud service account key JSON file in the device's file system and provide the path to
PubSubEasy.
For detailed documentation on all available methods and configurations, please refer to the comments in the PubSubEasy.h and PubSubEasy.cpp files.
For support, please open an issue in the GitHub repository. Contributions are welcome! If you'd like to contribute, please fork the repository and submit a pull request.