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.


 

 

Living a Decision

The Difference Between Taking a Decision and Living a Decision

We often think of decision-making as a single moment — the instant we choose one path over another. But the truth is, there’s a profound difference between taking a decision and living a decision. Understanding this difference can transform how we approach life, growth, and even success.

Taking a Decision: The Starting Point

Taking a decision is the moment of choice. It’s mental, strategic, and often influenced by logic, pressure, or external expectations. For example, you might decide to change careers, move to a new city, or start a new relationship. This decision is important, but it’s just the beginning.

At this point, the decision is mostly abstract. It exists in your mind, your plans, or your conversations. It can be exciting, scary, or hopeful. But crucially, taking a decision is a single event—something that happens once.

Living a Decision: The Real Challenge

Living a decision is entirely different. It’s the ongoing process of bringing your decision into reality. It means showing up every day, navigating uncertainty, overcoming obstacles, and committing emotionally and physically to the choice you made.

If you decided to change careers, living that decision means actually stepping into a new work environment, learning new skills, and facing challenges you couldn’t predict. It means resilience, persistence, and sometimes sacrifice.

Living a decision transforms who you are. It’s where growth happens, where intentions become habits, and where dreams meet reality.

Why Living the Decision Matters

Many people take decisions but never fully live them. This is why so many goals remain dreams and why so many plans fall apart. A decision that’s only made but not lived can feel hollow, uncertain, or fleeting.

On the other hand, a decision that is truly lived—despite hardships or doubts—never fails. It shapes your identity, teaches invaluable lessons, and ultimately leads to transformation.

The Takeaway

Taking a decision is necessary but not sufficient.

Living a decision is the real work—and the true path to change.

The difference lies in commitment, action, and endurance over time.

As the saying goes, “A decision not lived is just a wish. A decision lived is a path.” So next time you make a choice, remember that the real journey begins after you say yes.


Monday, 1 September 2025

Art of Disruption

The Charge Cycle and the Curve: Breaking Patterns to Awaken Progress

In every society, in every community, there exists a powerful force — a force that shapes how people think, believe, and act. I call it the “charge”: the collective emotional, social, and ideological energy that binds a group together. It’s the invisible current of shared identity, myths, and beliefs that creates belonging, but also builds walls.

This charge is not inherently bad. It gives us roots, culture, and meaning. But when the charge becomes rigid, sacred, and unquestionable, it locks communities into a repeating loop — the charge cycle — that resists change, punishes dissent, and fights the truth.

And that cycle has a profound impact on what I call “the curve” — the arc of progress, awakening, and collective evolution.

What is the Charge Cycle?

The charge cycle is the repeating pattern where:

A belief or myth becomes central to a group’s identity.

Questioning or opposing this myth is seen as a threat.

The challenger is framed as an enemy — an outsider, a rebel, a traitor.

The group defends the myth with renewed vigor.

The cycle repeats, often for generations.

This cycle is the backbone of social cohesion, but also the prison of social stagnation.

Why Does the Charge Cycle Persist?

Because the charge provides:

Safety — belonging to a group feels safer than standing alone.

Comfort — familiar myths simplify the world.

Identity — the charge gives people a sense of who they are.

Breaking the cycle means risking isolation, conflict, and uncertainty. Most people naturally avoid this.

The Curve: Progress and Its Adversary

The curve represents the growth of:

Truth

Awareness

Justice

Compassion

Freedom

It’s the slow but steady rise toward something better — both within individuals and societies.

But the charge cycle works against this curve:

It flattens progress by punishing innovation and dissent.

It warps progress by distorting values to fit old myths.

In extreme cases, it breaks the curve altogether, collapsing communities into chaos or authoritarianism.

Disrupting the Charge Cycle

To awaken the curve — to foster genuine progress — we must disrupt the charge cycle.

This doesn’t mean chaos or rebellion for its own sake. It means:

Exposing patterns without blame.

Living and embodying truth, even when misunderstood.

Using art, story, and metaphor to reach beyond defense.

Protecting the flame of inner clarity over external approval.

Finding or creating communities of fellow seekers.

Why This Matters Now

In a world rife with polarization, misinformation, and tribalism, the charge cycle is more powerful than ever. But so is the hunger for truth, connection, and meaning.

Breaking the cycle is hard. It’s lonely. It’s risky. But it is also the only path to real awakening — for individuals, communities, and humanity itself.

Closing Reflection

The curve is meant to rise — not in perfection, but in awakening. The charge cycle weighs it down. Only when we release the charge can the curve remember its path.

If this resonates with you, share it. Start conversations. Be the disruption the world needs.