Showing posts with label Firebase. Show all posts
Showing posts with label Firebase. Show all posts

Tuesday, 2 September 2025

MQTT_Firebase bridge

In my previous post, I had shared a tutorial about posting data from ESP32 to Firebase using Arduino Firebase client.

But what if you want to use MQTT on ESP32 side and still post messages to Firebase. This made me to work upon an intermediate C program which receives data from ESP32 MQTT client and post the data to Firebase REST API.

 A MQTT_Firebase bridge couples an MQTT enabled IOT device to talk to Firebase without additional work in IOT end. With IOT device having access to Firebase database which serves as a backend for mobile and web applications, we are moving towards an unified backend for embedded, mobile and web applications. There are many advantages of having an unified backend which enables seamless integration of application across the domain. 

Here is a simple C program which you can execute on your Rasberry Pi that can execute C program.

Prerequisites to execute this C program

1. A linux OS on your Rasberry Pi

2. Install Paho MQTT client and libcurl using the command  sudo apt install libcurl4-openssl-dev libpaho-mqtt-dev

3. Run the Program using  gcc name_of_program.c -o name_of_program -lcurl -lpaho-mqtt3c

4. Execute the program using  ./name_of_program

To test the program

1. Run the C program

2. Open an Online Mqtt client and publish to the Mqtt broker & Topic configured in your C program. 

3. Once you publish the data to the topic, you should see the output of the C program listening to the topic and posting the topic data to the Firebase URL. On successful posting to the Firebase URL you will see an appropriate success message or the error code.

Once the program is tested, you can replace the online MQTT client with ESP32 MQTT client.

Here is the C Program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include "MQTTClient.h"

// MQTT Config
#define MQTT_ADDRESS   "tcp://broker.hivemq.com:1883" 
#define MQTT_CLIENTID  "MQTT_FIREBASE_BRIDGE"
#define MQTT_TOPIC     "test/int"
#define MQTT_QOS       1
#define MQTT_TIMEOUT   10000L

// Firebase Config
#define FIREBASE_URL "https://your-project-id.firebaseio.com/test_int.json" // Must end with .json

// POST to Firebase
void post_to_firebase(const char *value) {
    CURL *curl;
    CURLcode res;
    char json_data[256];

    snprintf(json_data, sizeof(json_data), "{\"value\": \"%s\"}", value);

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    if (curl) {
        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");

        curl_easy_setopt(curl, CURLOPT_URL, FIREBASE_URL);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data);

        printf("Sending to Firebase: %s\n", json_data);

        res = curl_easy_perform(curl);

        long response_code = 0;
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);

        if (res != CURLE_OK || response_code != 200) {
            fprintf(stderr, "POST failed. CURL error: %s, HTTP response: %ld\n",
                    curl_easy_strerror(res), response_code);
        } else {
            printf("POST successful. HTTP %ld\n", response_code);
        }

        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
}

// MQTT Message Callback
int message_arrived(void *context, char *topicName, int topicLen, MQTTClient_message *message) {
    char *payload = malloc(message->payloadlen + 1);
    memcpy(payload, message->payload, message->payloadlen);
    payload[message->payloadlen] = '\0';

    printf("MQTT Message received: %s\n", payload);
    post_to_firebase(payload);

    free(payload);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    return 1;
}

// Main Function
int main() {
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    int rc;

    MQTTClient_create(&client, MQTT_ADDRESS, MQTT_CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL);
    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    MQTTClient_setCallbacks(client, NULL, NULL, message_arrived, NULL);

    printf("Connecting to MQTT broker...\n");
    rc = MQTTClient_connect(client, &conn_opts);
    if (rc != MQTTCLIENT_SUCCESS) {
        fprintf(stderr, "MQTT connection failed: %d\n", rc);
        return EXIT_FAILURE;
    }

    printf("Subscribed to topic: %s\n", MQTT_TOPIC);
    MQTTClient_subscribe(client, MQTT_TOPIC, MQTT_QOS);

    // Keep running
    while (1) {
        sleep(1);
    }

    MQTTClient_disconnect(client, MQTT_TIMEOUT);
    MQTTClient_destroy(&client);
    return 0;
}
 

✅ Final Thoughts

This C-based bridge lets you decouple your ESP32 from directly interacting with Firebase, which is useful in systems that prefer lightweight MQTT communication. You can now process or filter data at the intermediary (Raspberry Pi) before sending it to Firebase.


 

 

Wednesday, 13 August 2025

Controlling ESP32 GPIO using Firebase

🚀 Ever tried connecting an embedded application to a cloud database?

If not, here's a fun weekend project: Connect your ESP32/ESP8266 to Google Firebase and explore the power of real-time IoT!

I recently worked on a project where I controlled the GPIOs of an ESP32 by reading data directly from a Firebase database using Arduino Firebase client—and it worked like a charm! 🔌✨

With this setup, you can:
✅ Update Firebase from your ESP32 (send sensor data, etc.)
✅ Control your ESP32 remotely by updating Firebase via a mobile or web app
✅ Build an IoT dashboard that reflects real-time data updates

Whether you're building smart home projects or learning about cloud-connected devices, Firebase + ESP32 is a powerful combo.

💡 It’s a great hobby project to understand how embedded systems and cloud platforms can work together.

Give it a try—and let me know if you’d like the link to the tutorial I followed!

Link to the tutorial is as below

https://bokfive.com/getting-started-with-esp32-and-firebase-realtime-database/?srsltid=AfmBOoqpRdJlXAHtQO-PQKYZjYaUst6EPDzp-l1Iz2KjHIjtEd0mYUh0 

#IoT #ESP32 #Firebase #EmbeddedSystems #CloudComputing #Makers #TechHobby #DIYProjects #LinkedInTech