[问题]esp8266连MQTT

楼主: only0715 (jack)   2019-01-09 19:35:06
是这样的,目前利用nano+esp8266去连MQTT,但一直连
不上去,会出现
1547033662: New connection from 192.168.31.203 on port 1883.
1547033666: Socket error on client <unknown>, disconnecting.
想请教版上的各位有没有解?以下是我的程式码
#include <PubSubClient.h>
#include <WiFiEsp.h>
char LOG;
#define WIFI_SSID "!!!!!!" //填入WiFi帐号
#define WIFI_PASSWORD "!!!!!!!" //填入WiFi密码
int WiFi_Status = WL_IDLE_STATUS; //WiFi状态
const char* mqttServer = "192.168.31.72"; //填入MQTT Broker的IP或Domain
const int mqttPort = 1883; //MQTT Broker的Port
const char* clientID = "arduinoClient2"; //填入自订ID
const char* topic = "QOO"; //填入自订主题名称(阶层式命名)
#define N_SERVOS 6
WiFiEspClient espClient; //设定WiFiEspClient物件
PubSubClient client(espClient); //设定PubSubClient物件(带入espClient)
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
//wifi设定
wifi_Setting();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
if (client.connect(clientID)) {
Serial.println("MQTT 已连接!");
} else {
Serial.print("失败!, rc=");
Serial.print(client.state());
Serial.println("三秒之后重试...");
client.state();
delay(3000);
}
}
client.subscribe("QOO");
}
void callback(char*topic, byte* payload, unsigned int length) {
//Serial.print("Messagearrived in topic: ");
//Serial.println(topic);
//Serial.print("Message:");
LOG="";
for (int i = 0; i< length; i++) {
LOG=(char)payload[i];
Serial.print((char)payload[i]);
}
//Serial.print(LOG);
//
Serial.println();
Serial.println("

Links booklink

Contact Us: admin [ a t ] ucptt.com