我正在尝试为我的arduino整合mosquitto pub / sub服务器设置。我做了一个只涉及盒子相互碰撞的游戏,我想在每次玩家点击一个盒子时向“Game / CurrentScore”主题发布消息。
该代码如下所示:
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
}
}
}
我有一个打开的串口,它订阅了Game / CurrentScore主题。但是,如果玩家击中了正确的方框,则不会发布任何内容。我不确定我在这里做错了什么。我能以错误的顺序做某事吗?
感谢您的时间。
编辑:
这是在本地主机上设置的。
//PUB SUB
/////////////////////////////////////////////////
char* higestScoreTopic = "Game/HigestScore";
char* currentScoreTopic = "Game/CurrentScore";
char* ssid = ".......";
char* password = "......";
char* mqtt_server = "localhost";
char msg[50];
WiFiClient espClient;
PubSubClient client(espClient);
/////////////////////////////////////////////////
//PUB SUB
// CONNECTION SETUP
Serial.begin(115200);
setp_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// CONNECTION SETUP