void mqttCheckConnect()
{
while (!client.connected())
{
Serial.println("Connecting to MQTT Server ...");
if (client.connect(CLIENT_ID, MQTT_USRNAME, MQTT_PASSWD))
{
digitalWrite(MCU_LED,HIGH);
client.subscribe(mqtt_topic_sub);//连接成功后,订阅主题
}
else
{
digitalWrite(MCU_LED,LOW);
Serial.print("MQTT Connect err:");
Serial.println(client.state());
delay(2000);
}
}
}
通过MQTT协议进行联网
三个参数全部被传上去
成功则订阅主题
失败则向串口显示没有成功,然后等两秒再试试