Global declaration of object of "ESPAsyncWebServer" causing conflict #5
-
Hello @khoih-prog, I have been trying to develop multi cpp project in platformio and whenever I define main.cpp:
defines.h:
Test.h:
Test.cpp:
Error:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @otl1 Thanks for your trying the best to use my libraries. But the real-life scenario must govern what we have to do, and going with too easy and extreme way won't work. By changing a little bit in your code, you can eliminate the error of your over-complex code. But you still have to verify if it's still working and then have a better knowledge of the libraries / cores.
I'll explain a little bit in the end to help you somehow. But I don't think I'll spend any more time on your issues. Sorry I wish I'd have all the time in the world to do everything. Code changedmain.cpp#include "defines.h"
#include <AsyncMQTT_ESP32.h>
//#include <WebServer_ESP32_W5500.h>
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
/* ESPAsyncWebServer */
WiFiClient clientWiFi;
AsyncWebServer serverAsyncWeb(80);
AsyncWebSocket WebSocketAsync("/ws");
/* Processors */
String prc(const String &var)
{
return String();
}
/* Processors End */
void allAsyncWebServer()
{
serverAsyncWeb.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->send(SPIFFS, "/index.htm", String(), false, prc); }); /// log
}
/* allAsyncWebServer End */
/* WebSocketAsync */
void AsyncWebSocketEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len)
{
switch (type)
{
case WS_EVT_CONNECT:
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
break;
case WS_EVT_DISCONNECT:
Serial.printf("WebSocket client #%u disconnected\n", client->id());
break;
case WS_EVT_DATA:
// handleWebSocketMessage(arg, data, len);
break;
case WS_EVT_PONG:
case WS_EVT_ERROR:
break;
}
}
byte mac[][NUMBER_OF_MAC] =
{
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
};
// Select the IP address according to your local network
IPAddress myIP(192, 168, 2, 232);
IPAddress myGW(192, 168, 2, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
extern "C"
{
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
const char *PubTopic = "async-mqtt/ESP32_W5500_Pub"; // Topic to publish
// ESP32_W5500 ETH;
void connectToWifi()
{
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void connectToMqtt()
{
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void WiFiEvent(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_WIFI_READY:
Serial.println("WiFi ready");
break;
case ARDUINO_EVENT_WIFI_STA_START:
Serial.println("WiFi STA starting");
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("WiFi STA connected");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
Serial.println("WiFi lost IP");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
xTimerStart(wifiReconnectTimer, 0);
break;
#endif
default:
break;
}
}
void ETH_event(WiFiEvent_t event)
{
switch (event)
{
#if USING_CORE_ESP32_CORE_V200_PLUS
case ARDUINO_EVENT_ETH_START:
Serial.println("ETH starting");
break;
case ARDUINO_EVENT_ETH_CONNECTED:
Serial.println("ETH connected");
break;
case ARDUINO_EVENT_ETH_GOT_IP:
Serial.println("ETH got IP");
Serial.print("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case ARDUINO_EVENT_ETH_DISCONNECTED:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
case ARDUINO_EVENT_ETH_STOP:
Serial.println("ETH stops");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
#else
case SYSTEM_EVENT_ETH_CONNECTED:
Serial.println(F("ETH Connected"));
break;
case SYSTEM_EVENT_ETH_GOT_IP:
Serial.println("ETH connected");
Serial.println("IP address: ");
Serial.println(ETH.localIP());
connectToMqtt();
break;
case SYSTEM_EVENT_ETH_DISCONNECTED:
case SYSTEM_EVENT_ETH_STOP:
Serial.println("ETH lost connection");
// ensure we don't reconnect to MQTT when no ETH
xTimerStop(mqttReconnectTimer, 0);
break;
#endif
default:
break;
}
}
void printSeparationLine()
{
Serial.println("************************************************");
}
void onMqttConnect(bool sessionPresent)
{
Serial.print("Connected to MQTT broker: ");
Serial.print(MQTT_HOST);
Serial.print(", port: ");
Serial.println(MQTT_PORT);
Serial.print("PubTopic: ");
Serial.println(PubTopic);
printSeparationLine();
Serial.print("Session present: ");
Serial.println(sessionPresent);
uint16_t packetIdSub = mqttClient.subscribe(PubTopic, 2);
Serial.print("Subscribing at QoS 2, packetId: ");
Serial.println(packetIdSub);
mqttClient.publish(PubTopic, 0, true, "ESP32_W5500 Test");
Serial.println("Publishing at QoS 0");
uint16_t packetIdPub1 = mqttClient.publish(PubTopic, 1, true, "test 2");
Serial.print("Publishing at QoS 1, packetId: ");
Serial.println(packetIdPub1);
uint16_t packetIdPub2 = mqttClient.publish(PubTopic, 2, true, "test 3");
Serial.print("Publishing at QoS 2, packetId: ");
Serial.println(packetIdPub2);
printSeparationLine();
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
{
(void)reason;
Serial.println("Disconnected from MQTT.");
if (ESP32_W5500_isConnected())
{
xTimerStart(mqttReconnectTimer, 0);
}
}
void onMqttSubscribe(const uint16_t &packetId, const uint8_t &qos)
{
Serial.println("Subscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
Serial.print(" qos: ");
Serial.println(qos);
}
void onMqttUnsubscribe(const uint16_t &packetId)
{
Serial.println("Unsubscribe acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void onMqttMessage(char *topic, char *payload, const AsyncMqttClientMessageProperties &properties,
const size_t &len, const size_t &index, const size_t &total)
{
(void)payload;
Serial.println("Publish received.");
Serial.print(" topic: ");
Serial.println(topic);
Serial.print(" qos: ");
Serial.println(properties.qos);
Serial.print(" dup: ");
Serial.println(properties.dup);
Serial.print(" retain: ");
Serial.println(properties.retain);
Serial.print(" len: ");
Serial.println(len);
Serial.print(" index: ");
Serial.println(index);
Serial.print(" total: ");
Serial.println(total);
}
void onMqttPublish(const uint16_t &packetId)
{
Serial.println("Publish acknowledged.");
Serial.print(" packetId: ");
Serial.println(packetId);
}
void setup()
{
#define FORMAT_SPIFFS_IF_FAILED false
Serial.begin(115200);
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED))
{
Serial.println(F("W0")); // W0: SPIFFS Mount Failed
return;
}
while (!Serial && millis() < 5000);
Serial.print("\nStarting FullyFeature_ESP32_W5500 on ");
Serial.print(ARDUINO_BOARD);
Serial.println(" with " + String(SHIELD_TYPE));
//Serial.println(WEBSERVER_ESP32_W5500_VERSION);
Serial.println(ASYNC_MQTT_ESP32_VERSION);
AMQTT_LOGWARN(F("Default SPI pinout:"));
AMQTT_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
AMQTT_LOGWARN1(F("MOSI:"), MOSI_GPIO);
AMQTT_LOGWARN1(F("MISO:"), MISO_GPIO);
AMQTT_LOGWARN1(F("SCK:"), SCK_GPIO);
AMQTT_LOGWARN1(F("CS:"), CS_GPIO);
AMQTT_LOGWARN1(F("INT:"), INT_GPIO);
AMQTT_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
AMQTT_LOGWARN(F("========================="));
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void *)0,
reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.onSubscribe(onMqttSubscribe);
mqttClient.onUnsubscribe(onMqttUnsubscribe);
mqttClient.onMessage(onMqttMessage);
mqttClient.onPublish(onMqttPublish);
// mqttClient.setServer(MQTT_HOST, MQTT_PORT);
///////////////////////////////////
// To be called before ETH.begin()
WiFi.onEvent(ETH_event);
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
// ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
// ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[index] );
// byte esp32Mac[] = WiFi.macAddress();
Serial.print("WiFi.macAddress(): ");
Serial.println(WiFi.macAddress());
uint8_t baseMac[6];
esp_read_mac(baseMac, ESP_MAC_WIFI_STA);
// ETH.begin(MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, baseMac);
test();
Serial.print("index: ");
Serial.println(index);
Serial.print("ETH.macAddress(): ");
Serial.println(ETH.macAddress());
// Static IP, leave without this line to get IP via DHCP
// bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
// ETH.config(myIP, myGW, mySN, myDNS);
ESP32_W5500_waitForConnect();
connectToWifi();
allAsyncWebServer(); // Enable ESP Asynchronous Servers
serverAsyncWeb.begin(); // Start ESP Asynchronous Server
/* Initiallize AsyncWebSocket */
WebSocketAsync.onEvent(AsyncWebSocketEvent);
serverAsyncWeb.addHandler(&WebSocketAsync);
///////////////////////////////////
}
void loop()
{
} defines.h/****************************************************************************************************************************
defines.h
AsyncMqttClient_Generic is a library for ESP32, ESP8266, Protenta_H7, STM32F7, etc. with current AsyncTCP support
Based on and modified from :
1) async-mqtt-client (https://github.com/marvinroger/async-mqtt-client)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncMqttClient_Generic
***************************************************************************************************************************************/
#ifndef defines_h
#define defines_h
#include <Arduino.h>
#include <WiFi.h>
//#include <ESPAsyncWebServer.h>
#include <AsyncWebServer_ESP32_W5500.h>
#include <AsyncTCP.h>
#include <Update.h>
#include <ESPmDNS.h>
#include <SPIFFS.h>
#include "Test.h"
#include <AsyncMQTT_ESP32.hpp>
//#include <AsyncMqttClient.h>
//#include <WebServer_ESP32_W5500.hpp>
//#include "w5500/esp32_w5500.h"
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
#define _ASYNC_MQTT_LOGLEVEL_ 1
//////////////////////////////////////////////////////////
// Optional values to override default settings
// Don't change unless you know what you're doing
//#define ETH_SPI_HOST SPI3_HOST
//#define SPI_CLOCK_MHZ 25
// Must connect INT to GPIOxx or not working
// #define INT_GPIO 21 //4
// #define MISO_GPIO 19
// #define MOSI_GPIO 23
// #define SCK_GPIO 18
// #define CS_GPIO 22 //5
//////////////////////////////////////////////////////////
// #include <WebServer_ESP32_W5500.h>
/////////////////////////////////////////////
// Enter a MAC address and IP address for your controller below.
extern AsyncMqttClient mqttClient;
extern TimerHandle_t mqttReconnectTimer;
extern TimerHandle_t wifiReconnectTimer;
// extern ESP32_W5500 ETH;
// // extern WiFiClient clientWiFi;
// extern AsyncWebServer serverAsyncWeb;
// extern AsyncWebSocket WebSocketAsync;
#define NUMBER_OF_MAC 20
#define WIFI_SSID "Digisol"
#define WIFI_PASSWORD "12345678"
#define MQTT_HOST IPAddress(192, 168, 1, 2)
// #define MQTT_HOST "broker.emqx.io" // Broker address
#define MQTT_PORT 1883
#endif //defines_h Simple explanation
Such as in .arduino15/packages/esp32/hardware/esp32/2.0.6/tools/sdk/esp32/include/nghttp/port/include/http_parser.h
while in https://github.com/me-no-dev/ESPAsyncWebServer/blob/master/src/ESPAsyncWebServer.h#L64
Therefore, it suggest to use new AsyncWebServer_ESP32_W5500 library
In the near future, the AsyncWebServer_ESP32_W5500 library will be modified to use WebServer_ESP32_W5500 to avoid this type of duplication. But as a simple rule now, you don't need WebServer_ESP32_W5500 as it's already included. |
Beta Was this translation helpful? Give feedback.
Hi @otl1
Thanks for your trying the best to use my libraries. But the real-life scenario must govern what we have to do, and going with too easy and extreme way won't work.
By changing a little bit in your code, you can eliminate the error of your over-complex code. But you still have to verify if it's still working and then have a better knowledge of the libraries / cores.
I'll explain a little bit in the end to help you somehow. But I don't think I'll spend any more time on your issues. Sorry I wish I'd have all the time in the world to do everything.
Code changed
main.cpp