PubSubClient publish method not working

Hi there.

I'm trying to incorporate the mosquitto pub/sub server setup for my arduino. I made a game that just involved boxes colliding with each other, and I wanted to publish a message to the "Game/CurrentScore" topic every time the player hit a box.

That code looks like this:

void boxCollisionCheck() {
if ( (charPosX == boxPosX) && (charPosY == boxPosY) ) {
if (character == npc) {
// Publush to Game/CurrentScore
snprintf (msg, 75, "Current Score: %ld", currentScore);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish(currentScoreTopic, msg);
/////////////////////////////////////////////
screenColor(175, 0, 0); // Screen flashes green
placeNpc();
screenColor(255, 255, 255);

} else {
screenColor(0, 175, 0); //Screen flashes red
placeNpc();
screenColor(255, 255, 255);
higestScore = currentScore; //Save higest score
currentScore = 0; //Reset current score
}
}
}

I have a serial port open that's is subscribed to the Game/CurrentScore topic. However if the player hits the correct box, nothing gets published. I'm not really sure what I'm doing wrong here. Could I be doing something in the wrong order?

I've attached a file that contains the code for the entire project if you'd like to look at that. I'm not really sure what code would be relevant to add to this question aside from the one above.

Thanks for your time.

assign1.ino (6.61 KB)

char msg[50];
        snprintf (msg, 75, "Current Score: %ld", currentScore);

Why did you lie to snprintf()? It certainly is NOT OK for it to write 75 characters to the msg array.

What do your Serial.print()s tell you is happening?

The serial prints tell me that this:

Attempting MQTT connection...connected
Publish message: Current Score: 1
Publish message: Current Score: 2

This happens every time the score is updated. I'm trying to publish that to the "Game/CurrentScore" topic every time the score updates but it's not working.

Also, I changed the value in the sprintf. I can't believe I didn't see that.

I'm trying to publish that to the "Game/CurrentScore" topic every time the score updates but it's not working.

Based on what evidence? It appears as though the Arduino thinks it is happening.

You should probably add Serial.print() statements to the PubSubClient library, to see if the message is actually being published.

From what I read on the documentation for the PubSub library, the publish method returns either true or false depending on if the publish succeeded or failed.

Here's an edit I made to the code(this is just for the high score one instead, but the same problem applies):

snprintf (msg, 50, "New High Score: %d", higestScoreNum);
Serial.print("Publish message: ");
Serial.println(msg);
int i = client.publish(higestScoreTopic, msg);
Serial.println(i);

This is what I get back:

Publish message: New High Score: 5
1
Publish message: New High Score: 6
1

I don't know why it says it returns True or False when the return type is int...

I don't know if 1 means True or False. I'm guessing it means True but I have no idea...

0 means false. Any other value means true.

If I were troubleshooting, I'd add some Serial.print() statements to the library, to see what is actually be sent out. If the subscriber is not seeing anything, there could be any number of reasons why not.