CupCarbon Klines

ESP32 and MQTT

Table of Contents

1. Introduction

This guide demonstrates how to integrate CupCarbon with ESP32 microcontrollers using the MQTT protocol for real-world IoT projects.

2. Setup Instructions

In CupCarbon, create a subscriber on the topic you want:

Example: abcd/gps (change abcd by your preferred topic)

Then write the Arduino code of the ESP32 which will send the GPS coordinate via the same topic. Add the instruction move to move the device to the received coords.

3. Arduino Code for ESP32

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "*******";           // The ssid of your wifi
const char* password = "*****";         // The password
const char* mqtt_server = "broker.hivemq.com";  // can be modified
const char* mqtt_topic = "cupcarbon/gps";       // modify it

WiFiClient espClient;
PubSubClient client(espClient);

int pled = 2;
bool ready = false;
int i = 0;
long lastMsg = 0;

char* gpsLocs[] = {
    "50.150486 26.31439",
    "50.150502 26.31444775",
    "50.150518 26.3145055",
    "50.150534 26.31456325",
    "50.15055 26.314621",
    "50.15055525 26.3146875",
    "50.1505605 26.314754",
    "50.15056575 26.3148205",
    "50.150571 26.314887",
    "50.15056375 26.31496475",
    "50.1505565 26.3150425",
    "50.15054925 26.31512025",
    "50.150542 26.315198",
    "50.15052875 26.31527825",
    "50.1505155 26.3153565",
    "50.15050225 26.31543475",
    "50.150489 26.315513",
    "50.15048 26.31559325",
    "50.150471 26.3156735",
    "50.15046 26.31575375",
    "50.150449 26.315834"
};

int numLocs = sizeof(gpsLocs) / sizeof(gpsLocs[0]);

void setup_wifi() {
    delay(10);
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
    
    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    
    for (int i = 0; i < length; i++) {
        Serial.print((char)payload[i]);
    }
    Serial.println();
}

void reconnect() {
    while (!client.connected()) {
        Serial.print("Attempting MQTT connection...");
        
        if (client.connect("ESP32Client")) {
            Serial.println("connected");
            client.subscribe(mqtt_topic);
        } else {
            Serial.print("failed, rc=");
            Serial.print(client.state());
            Serial.println(" try again in 5 seconds");
            delay(5000);
        }
    }
}

void setup() {
    pinMode(pled, OUTPUT);
    Serial.begin(115200);
    setup_wifi();
    client.setServer(mqtt_server, 1883);
    client.setCallback(callback);
}

void loop() {
    if (!client.connected()) {
        reconnect();
    }
    client.loop();
    
    long now = millis();
    if (now - lastMsg > 2000) {
        lastMsg = now;
        
        if (i < numLocs) {
            Serial.print("Publishing GPS: ");
            Serial.println(gpsLocs[i]);
            client.publish(mqtt_topic, gpsLocs[i]);
            i++;
        } else {
            i = 0;  // Reset to loop through coordinates again
        }
    }
}

4. Configuration Parameters

4.1 WiFi Settings

Update the following variables with your WiFi network credentials:

4.2 MQTT Settings

Configure the MQTT broker and topic:

Important: Make sure the MQTT topic in your ESP32 code matches the topic you configured in CupCarbon.

5. How It Works

The ESP32 device follows this workflow:

  1. Connects to your WiFi network
  2. Establishes a connection to the MQTT broker
  3. Publishes GPS coordinates to the specified MQTT topic every 2 seconds
  4. CupCarbon subscribers receive these coordinates in real-time
  5. The device position updates based on the received GPS data

The GPS coordinates are stored in the gpsLocs[] array and are published sequentially. Once all coordinates have been sent, the sequence loops back to the beginning.

6. Testing the Integration

To test your setup:

  1. Upload the code to your ESP32 board
  2. Open the Serial Monitor (115200 baud rate)
  3. Verify WiFi connection and MQTT broker connection
  4. Check CupCarbon to see if the device is receiving GPS updates
  5. Observe the device movement on the CupCarbon map