[英]ESP32 - How to make a PubSubClient function in a utils file that connects to AWS IoT Core
I want to make a function that uses PubSubClient and WifiClientSecure to connect to AWS MQTT broker, and I want to define it in a separate file so I can reuse it, but the ESP32 is crashing every time the code runs.我想制作一个使用 PubSubClient 和 WifiClientSecure 连接到 AWS MQTT 代理的 function,我想在一个单独的文件中定义它以便我可以重用它,但是每次代码运行时 ESP32 都会崩溃。
I want to reuse this function I found in this tutorial in a iot_utils.cpp
file, instead of definining it in the main file.我想重用我在本教程中的
iot_utils.cpp
文件中找到的这个 function,而不是在主文件中定义它。 When i define it in the main file, it works fine, but thats not what i want to do.当我在主文件中定义它时,它工作正常,但这不是我想要做的。
This is the function in the tutorial :这是教程中的 function :
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
void connectAWS()
{
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.println("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Configure WiFiClientSecure to use the AWS IoT device credentials
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Connect to the MQTT broker on the AWS endpoint we defined earlier
client.setServer(AWS_IOT_ENDPOINT, 8883);
// Create a message handler
client.setCallback(messageHandler);
Serial.println("Connecting to AWS IOT");
while (!client.connect(THINGNAME)) {
Serial.print(".");
delay(100);
}
if (!client.connected()) {
Serial.println("AWS IoT Timeout!");
return;
}
// Subscribe to a topic
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC);
Serial.println("AWS IoT Connected!");
}
I dont want to use it in the main file, instead, i want to put it in a separate file.我不想在主文件中使用它,相反,我想将它放在一个单独的文件中。 To do this, i made the corresponding header and src file, and in the
iot_utils.cpp
file I defined the function like this:为此,我制作了相应的 header 和 src 文件,并在
iot_utils.cpp
文件中定义了 function,如下所示:
void connectAWS(WiFiClientSecure& net, PubSubClient& client, const char* topic) {
// the code here...
}
But when in my main file, I call it like this:但是在我的主文件中,我这样称呼它:
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
void setup() {
connectAWS(net, client, "esp32/pub");
}
The ESP32 crashes with the following information: ESP32 崩溃并显示以下信息:
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:13132
load:0x40080400,len:3036
entry 0x400805e4
Connecting to Wi-Fi
.Connecting to AWS IOT
Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x4015b50c PS : 0x00060f30 A0 : 0x800d3bba A1 : 0x3ffb26f0
A2 : 0x3ffc2c74 A3 : 0x3f4050f9 A4 : 0x00000015 A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x800d78a6 A9 : 0x3ffb2700
A10 : 0x000003e8 A11 : 0x3f4050f9 A12 : 0x00000002 A13 : 0x0000ff00
A14 : 0x00ff0000 A15 : 0xff000000 SAR : 0x00000017 EXCCAUSE: 0x0000001c
EXCVADDR: 0x000003e8 LBEG : 0x40089729 LEND : 0x40089739 LCOUNT : 0xfffffffa
Backtrace:0x4015b509:0x3ffb26f00x400d3bb7:0x3ffb2710 0x400d3de2:0x3ffb2750 0x400d3915:0x3ffb2780 0x400d3819:0x3ffb27a0 0x400d877e:0x3ffb2820
What am I doing wrong?我究竟做错了什么? I just want to reuse the connectAWS in other files, so i dont have to copy-paste it every time
我只想在其他文件中重用 connectAWS,所以我不必每次都复制粘贴它
I realized how to do it, and it is declaring net
and client
in the iot_utils.hpp
header file, and defining them in iot_utils.cpp
source file, just like this:我意识到该怎么做,它是在
iot_utils.hpp
header 文件中声明net
和client
,并在iot_utils.cpp
源文件中定义它们,就像这样:
iot_utils.hpp iot_utils.hpp
#ifndef iot_utils
#define iot_utils
#include "WiFi.h"
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
// The reason why net and client are both in the header and in the source: https://stackoverflow.com/questions/74729454/platformio-c-multiple-multiple-definition-of
extern WiFiClientSecure net;
extern PubSubClient client;
void connectAWS();
#endif
iot_utils.cpp iot_utils.cpp
#include "iot_utils.hpp"
#include "leds_secrets.h"
// The reason why net and client are both in the header and in the source: https://stackoverflow.com/questions/74729454/platformio-c-multiple-multiple-definition-of
WiFiClientSecure net = WiFiClientSecure();
PubSubClient client(net);
void connectAWS()
{
(...)
}
And then just including "iot_utils.hpp"
in my main file然后在我的主文件中包含
"iot_utils.hpp"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.