ESP32 WiFi MQTT client

Hi Guys,

I’m sure this has been solved before, but I cannot find solution. And it’s related to library which needs to be either ignored and correct one replaced.

Below is my simple project to get things working.

Below is my unit:
https://www.aliexpress.com/item/32830221599.html

Basically, this project is pretty much copy-paste from Arduino IDE where the code actually gets connected to the mqtt broker. But here in PlatformIO. It does not get connect to the mqtt-broker.

I had to add the #include <WiFiSTA.h> in my code for it to at least connect to my wifi, otherwise it would not. Like I said, the code actually works perfect using Arduino IDE. Below it the build output.

 Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    Verbose mode can be enabled via `-v, --verbose` option
    CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
    PLATFORM: Espressif 32 1.12.0 > Espressif ESP32 Dev Module
    HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
    DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
    PACKAGES: 
     - framework-arduinoespressif32 3.10004.200129 (1.0.4) 
     - tool-esptoolpy 1.20600.0 (2.6.0) 
     - toolchain-xtensa32 2.50200.80 (5.2.0)
    LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
    LDF Modes: Finder ~ chain, Compatibility ~ soft
    Found 38 compatible libraries
    Scanning dependencies...
    Dependency Graph
    |-- PubSubClient 2.7
    |-- WiFi 1.2.7
    |   |-- SPI 1.0
    |-- WiFi 1.0
    Building in release mode
    Compiling .pio/build/esp32dev/src/main.cpp.o
    Linking .pio/build/esp32dev/firmware.elf
    Building .pio/build/esp32dev/firmware.bin
    Retrieving maximum program size .pio/build/esp32dev/firmware.elf
    Checking size .pio/build/esp32dev/firmware.elf
    Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
    RAM:   [=         ]  12.0% (used 39392 bytes from 327680 bytes)
    Flash: [=====     ]  52.6% (used 689574 bytes from 1310720 bytes)
    esptool.py v2.6
    ================================================================================================ [SUCCESS] Took 4.55 seconds ================================================================================================
WiFi 1.2.7

in the dependency graph doesn’t seem right… it should only be the WiFi 1.0 library. Also, how did you get around PlatformIO thinking PubSubClient isn’t ESP32 compatible (probably because it has espressif in the library manifest instead of both espressif8266 and espressif32… :-/) nevermind, it seems it still installs it properly…

Anyway… it might be worth looking at this example while someone suggested be added to the PubSubClient library for the ESP32…

They included the WiFi.h, WebServer.h, PubSubClient.h headers… for their example… and it at least compiles, so hopefully still works. It just needs the function prototypes added to compile without change on PlatformIO.