Apologies, I gave you the wrong link.. This is the one I ended up using:
Making an old roomba smarter using and ESP01. Contribute to thehookup/MQTT-Roomba-ESP01 development by creating an account on GitHub.
The project video is here, which helps explain the MQTT side:
Go through the notes with the project and in the youtube text to add the various libraries.
In that code, it's this section that you customise for each device:
C:
//USER CONFIGURED SECTION START//
const char* ssid = "your_wifi_name";
const char* password = "and_password";
const char* mqtt_server = "192.168.0.52";
const int mqtt_port = 1883;
const char *mqtt_user = "";
const char *mqtt_pass = "";
const char *mqtt_client_name = "Roomba880"; // Client connections can't have the same connection name
//USER CONFIGURED SECTION END//
In the original, every publish has the device name hardcoded, like
client.publish("roomba/status", "Cleaning");
With that, you would need to change every publish line to a different name...
I added a bit of code in the end of that user config section to make it more versatile:
C:
// The name to use for this specific machine -
// Change it if you have more than one roomba / scooba / dirt dog
// etc. so each has a unique set of topic data to identify it.
// Ensure the string ends with a single forward slash/
const char *mqtt_topic = "roomba/";
Plus a couple of extra functions:
C:
void buildTopic(char *topic) {
// Result is in topicBuf global, no need to return anything
strcpy(topicBuf, mqtt_topic);
strcat(topicBuf, topic);
}
void rPublish(char* topic, char* payload) {
buildTopic(topic);
client.publish(topicBuf, payload);
}
All the publish lines then only need the sub-topic and payload, and call the rPublish routine rather than publish directly. rPublish builds the topic string with the global device name.
Also add the character buffers at the end of the variables list:
char topicBuf[50];
char commandBuf[50];
It also need the callback routine matching against the device name, but I got sidetracked by the comms and hardware variations between all the different robots - no two are identical & a couple need major work just to access the internal serial ports..
You can use the buildtopic function to create another character string with the "device_name/commands"
that's what the commandBuf was for, so it would be possible to match the callback topic string.
You could use a #define for the name everywhere instead..