使用 PubSubclient 在 esp8266 和 arduino 之间进行 mqtt

2023-11-30

我正在使用 WiFiEsp 库将 ESP8266 与 arduino 结合使用。我想与 arduino 建立 MQTT 连接,所以我使用 PubSubclient 库。我收到错误:

正在尝试 MQTT 连接...失败,rc=-2 5 秒后重试

我的代码是:

#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);
  }
  }
  }

我正在使用 ESP8266 与 arduino 使用 WiFiEsp 库。我想与 arduino 建立 MQTT 连接,所以我使用 PubSubclient 库


我不认为 PubSubClient 支持 WiFiEsp 作为 arduino 的网络层。

虽然doc列出 ESP8266 我相信这是直接在 ESP8266 硬件上运行 PubSubClient。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 PubSubclient 在 esp8266 和 arduino 之间进行 mqtt 的相关文章

  • 最大 MQTT 连接数

    我需要创建一个服务器场 可以处理 5 100 万个连接 5 00000 个主题 每个客户端一个 每秒处理 300k 消息 我尝试了解各种消息代理的功能 因此我目前使用两个 RHEL EC2 实例 r3 4xlarge 来获取大量可用资源 所
  • 在 mqtt.js 中发布无法访问的主题时如何捕获“访问错误”?

    我正在使用带有 mqtt js 的 node js 和 mosquitto 代理创建一个项目 在 mosquitto 配置文件中 我设置了 pwfile aclfile 来控制哪个用户可以访问哪个主题 如果用户名 密码 发布主题和订阅主题正
  • ARDUINO 常数有什么用?以前是22,现在是100

    我正在使用热电偶并下载了 MAX6675 库 我想知道以下几行中 ARDUINO 常量的值是做什么用的 if ARDUINO gt 100 lcd write byte 0 else lcd print 0 BYTE endif lcd p
  • 如何将值从 Arduino 发送到 Python,然后使用该值

    我正在构建一个使用 Python 进行远程控制的机器人 通过简单的 GUI 通过互联网发送控制消息 我的部分代码 GUI 和控制系统 运行得很好 但我陷入了困境 我正在尝试使用视差平传感器来获取与物体的距离信息Arduino Mega ht
  • java.io.EOFException 与 paho

    我想对蚊子进行压力测试 所以我创建了一些代码 如下所示 for int i 0 i lt 800 i final int j i Thread t new Thread new Runnable Override public void r
  • AWS-IoT 核心代理的最大消息负载大小是多少?

    在文档中AWS 服务限制 https docs aws amazon com general latest gr aws service limits htmlIoT Core 服务的有效负载大小限制尚不清楚 既然我们可以将本地 mosqu
  • C++ Linux (Ubuntu) 正确写入串行(对于 Arduino)

    我想知道是否有一种标准方法可以与高效的串行设备进行通信 我应该使用标准库吗 如果有 是哪一个 现在我正在摆弄让 LED 根据输入的数字以给定的量亮起 Arduino 代码如下 只是练习一些东西 看我过于简单且低效的测试 include
  • Arduino从SD卡读取最后一行

    我对 Arduino 业务还很陌生 如何从 SD 卡读取最后一行 通过以下代码片段 我可以读取第一行 n 之前的所有字符 现在我想添加一个 向后 声明 或其他内容 到目前为止我的代码 include
  • 带/不带类的回调函数指针 C++

    我被困 我正在尝试形成一个函数 它将吃掉无类函数指针和对象中的函数指针 这是我当前的代码 希望能解释更多 它应该在 Arduino 上运行 所以我不能使用大型库 首先 我在 Arduino 上使用这个库 SimpleTimer A time
  • 如何M2MQTT自动重连

    我正在尝试使用此协议 并且已经有 2 个客户端 一个用于发布 另一个用于订阅 和一个正在工作的经纪人 我的问题是我想在订阅客户端中实现重新连接功能 因为 wifi 信号不稳定并且不想每次都手动重新启动客户端 我该如何实现这一点 您可以使用
  • C#读取Arduino

    我正在尝试制作一个从 Arduino 读取传出信号的应用程序 但我无法使其在 C 中工作Windows 窗体 http en wikipedia org wiki Windows Forms 仅在控制台中 我的 C Windows 窗体代码
  • 使用串口通过 Arduino 将多个值发送到 Raspberry

    我有一个关于 Arduino 和 Raspberry Pi 之间串行通信的问题 事实上 我想使用 Arduino 将 2 个变量发送到 Raspberry Pi 并以不同的方式使用它们 这是我的 Arduino 草图 int one 1 i
  • AVRDUDE Android Arduino ioctl 错误

    我之前的问题 Android 无法使用 AVRDUDE 与 Arduino 通信 https stackoverflow com questions 15596376 android cannot talk to arduino using
  • Arduino 上的 Serial 和 Stream 有什么区别,Serial.write 是如何实现的?

    我很难理解串行和流这两个术语之间的区别 串行不是流的一种吗 我对作业有一些我不明白的问题 计算机通过使用 流或串行 读取 彼此发送的数据来确定每个字节的含义 Also Serial write 我也很困惑 它返回一个字节的数据 对吗 一个字
  • ESP8266 I2C从机不确认数据

    我有一个 TM4C123 处理器作为 I2C 主处理器 一个 ESP8266 作为从处理器 对于 ESP 我使用的是 Arduino IDE 并在 2 5 2 版安装了 ESP8266 支持 它应该支持 I2C 从模式 但是 我无法让它工作
  • AWS-IOT Mqtt Broker 是否会记住跨客户端连接订阅的客户端和未传递的消息?

    AWS 文档在此 http docs aws amazon com iot latest developerguide iot message broker html http docs aws amazon com iot latest
  • 如何在 Windows 上获取 Arduino 草图的汇编语言列表?

    我希望能够看到我的 Arduino 草图的汇编语言列表 我怎样才能实现这个目标 Update 我正在 Windows 机器上运行 Arduino 软件 一种方法是使用avr objdump on the elf构建创建的文件 例如 在 OS
  • SIM900 GSM/GPRS 未获得正确的 AT+CREG?回答

    我使用的是带有 IComsat SIM900 GSM GPRS 扩展板的 Arduino UNO 使用以下教程 Arduino 实时 GPS 追踪器 http www samaria me uk 2011 12 arduino live g
  • 如何提高 PySerial 读取速度

    我目前正在构建一台使用 Arduino Mega2560 作为主控制器的机器 Arduino 通过串口连接 获取命令 执行它并每 1ms 吐出一堆测量数据 我有一个运行 Python 的 Raspberry Pi 为用户提供一个漂亮的 GU
  • 通过 MQTT 将 LoRa 测量结果发送到 Fiware IOT 代理

    我有 LoRa 传感器 它使用 Cayenne LPP 格式有效负载发送测量值 我的网关 LOrank8v1 捕获这些数据包并将其转换为 UDP 网关中有一个服务 lora gateway bridge 它将数据包从 UDP 转换为 TCP

随机推荐