ESP32 and MQTT
This guide demonstrates how to integrate CupCarbon with ESP32 microcontrollers using the MQTT protocol for real-world IoT projects.
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.
#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
}
}
}
Update the following variables with your WiFi network credentials:
ssid - Your WiFi network namepassword - Your WiFi passwordConfigure the MQTT broker and topic:
mqtt_server - MQTT broker address (default: broker.hivemq.com)mqtt_topic - Topic name for publishing GPS data (default: cupcarbon/gps)Important: Make sure the MQTT topic in your ESP32 code matches the topic you configured in CupCarbon.
The ESP32 device follows this workflow:
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.
To test your setup: