Page 193 - MDP2022-2
P. 193
}
else if (strcmp(topic, "iot/music") == 0)
{
if (strcmp(to_arduino_msg, "on") == 0)
{
Serial.println("g");
memset(to_arduino_msg, 0, MSG_BUFFER_SIZE);
}
else if (strcmp(to_arduino_msg, "off") == 0)
{
Serial.println("G");
memset(to_arduino_msg, 0, MSG_BUFFER_SIZE);
}
}
else if (strcmp(topic, "device/connect") == 0)
{
Serial.println("P");
memset(to_arduino_msg, 0, MSG_BUFFER_SIZE);
}
if ((char)payload[0] == '1')
{
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level)
}
else
{
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect()
{
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
client.publish("device/connected", "STM32 & ESP8266 ON");