带有 PubSubclient 的 esp8266 和 arduino 之间的 mqtt [英] mqtt between esp8266 and arduino with PubSubclient
问题描述
我在使用 WiFiEsp 库的 arduino 中使用 ESP8266.我想与 arduino 建立 MQTT 连接,所以我使用 PubSubclient 库.我得到错误:
<块引用>正在尝试 MQTT 连接...失败,rc=-2 5 秒后重试
我的代码是:
#include #include #include #include "SoftwareSerial.h"#include IP地址服务器(212、72、74、21);字符 ssid[] = "atmel";//您的网络 SSID(名称)char pass[] = "bets56789";//你的网络密码int 状态 = WL_IDLE_STATUS;//Wifi 无线电的状态//初始化以太网客户端对象WiFiEspClient espClient;PubSubClient 客户端(espClient);软件串行软(2,3);//接收,发送无效设置(){//初始化串口以进行调试Serial.begin(9600);//为 ESP 模块初始化串口软开始(115200);//初始化ESP模块WiFi.init(&soft);//检查屏蔽是否存在如果(WiFi.status()== WL_NO_SHIELD){Serial.println("WiFi 屏蔽不存在");//不要继续同时(真);}//尝试连接到 WiFi 网络而(状态!= WL_CONNECTED){Serial.print("正在尝试连接到 WPA SSID:");Serial.println(ssid);//连接到 WPA/WPA2 网络status = WiFi.begin(ssid, pass);}//你现在已经连接上了,所以打印出数据Serial.println("您已连接到网络");//延迟(2000);//连接到MQTT服务器客户端设置服务器(服务器,1883);client.setCallback(回调);}//打印收到的订阅主题的任何消息无效回调(字符*主题,字节*有效载荷,无符号整数长度){Serial.print("消息到达[");Serial.print(主题);Serial.print("] ");for (int i=0;i
我使用 WiFiEsp 库将 ESP8266 与 arduino 一起使用.我想与 arduino 建立 MQTT 连接,所以我使用 PubSubclient 库
我不认为 PubSubClient 支持 WiFiEsp 作为来自 arduino 的网络层.
虽然 doc 列出了 ESP8266,但我相信这是直接在 ESP8266 硬件上运行 PubSubClient.>
I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library.i got error:
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
my code is:
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include "SoftwareSerial.h"
#include <PubSubClient.h>
IPAddress server(212, 72, 74, 21);
char ssid[] = "atmel"; // your network SSID (name)
char pass[] = "bets56789"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(espClient);
SoftwareSerial soft(2,3); // RX, TX
void setup() {
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
soft.begin(115200);
// initialize ESP module
WiFi.init(&soft);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
//delay(2000);
//connect to MQTT server
client.setServer(server, 1883);
client.setCallback(callback);
}
//print any message received for subscribed topic
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void loop() {
// put your main code here, to run repeatedly:
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect, just a name to identify the client
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outSHADAB","hello world");
// ... and resubscribe
client.subscribe("inShadab");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
I am using ESP8266 with arduino using WiFiEsp library.I want to make MQTT connection with arduino so I use PubSubclient library
I don't think the PubSubClient supports the WiFiEsp as a network layer from an arduino.
While the doc list the ESP8266 I believe this is running the PubSubClient directly on the ESP8266 hardware.
这篇关于带有 PubSubclient 的 esp8266 和 arduino 之间的 mqtt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!