I try to get an ESP32 running with an W5500 Ethernet controller and the possibility to use Arduino-MQTT ( https://github.com/256dpi/arduino-mqtt ) for MQTTS / secure MQTT.
First things first, the Arduino-Ethernet Library is widely used, however it does not work with ESP32 out of the box anymore.
According to the developers, Espressif changed libaries and are to blame for that. They had an pull request which would have fixed the issue in the library, but closed it now ( https://github.com/arduino-libraries/Ethernet/pull/107 )
So you have to get this https://github.com/arduino-libraries/Et ... Ethernet.h Ethernet.h and replaces it with the one in your default Ethernet library.
After this, I wired my W5500 module according to this
MISO = GPIO19
MOSI = GPIO23
RST = GPIO5
SCS = GPIO33
SCLK = GPIO18
GND = GND
and had to set Ethernet.init(33);
The resulting/working code to connect via Ethernet to MQTT was this:
- // This example uses an Arduino Uno together with
- // an Ethernet Shield to connect to shiftr.io.
- //
- // You can check on your device after a successful
- // connection here: https://shiftr.io/try.
- //
- // by Joël Gähwiler
- // https://github.com/256dpi/arduino-mqtt
- #include <SPI.h>
- #include <Ethernet.h>
- #include <MQTT.h>
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- byte ip[] = {192, 168, 4, 177}; // <- change to match your network
- EthernetClient net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- void connect() {
- Serial.print("connecting...");
- while (!client.connect("arduino", "public", "public")) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("\nconnected!");
- client.subscribe("/hello4711");
- // client.unsubscribe("/hello");
- }
- void messageReceived(String &topic, String &payload) {
- Serial.println("incoming: " + topic + " - " + payload);
- // Note: Do not use the client in the callback to publish, subscribe or
- // unsubscribe as it may cause deadlocks when other things arrive while
- // sending and receiving acknowledgments. Instead, change a global variable,
- // or push to a queue and handle it in the loop after calling `client.loop()`.
- }
- void setup() {
- Serial.begin(115200);
- Ethernet.init(33);
- Ethernet.begin(mac, ip);
- // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
- // by Arduino. You need to set the IP address directly.
- client.begin("public.cloud.shiftr.io", net);
- client.onMessage(messageReceived);
- connect();
- }
- void loop() {
- client.loop();
- delay(10); // <- fixes some issues with WiFi stability
- if (!client.connected()) {
- connect();
- }
- // publish a message roughly every second.
- if (millis() - lastMillis > 1000) {
- lastMillis = millis();
- client.publish("/hello4711", "world");
- }
- }
For Wifi with ESP32, the library uses WifiClientSecure.
Is there any way to somehow connect the WifiClientSecure with the already existing socket of the Ethernet library and use the SSL functionality to make the connection?
Code for ESP32 MQTTS via Wifi
- // This example uses an ESP32 Development Board
- // to connect to shiftr.io.
- //
- // You can check on your device after a successful
- // connection here: https://shiftr.io/try.
- //
- // by Joël Gähwiler
- // https://github.com/256dpi/arduino-mqtt
- #include <WiFiClientSecure.h>
- #include <MQTT.h>
- const char ssid[] = "";
- const char pass[] = "";
- WiFiClientSecure net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- void connect() {
- Serial.print("checking wifi...");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print(".");
- delay(1000);
- }
- Serial.print("\nconnecting...");
- while (!client.connect("arduino", "public", "public")) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("\nconnected!");
- client.subscribe("/hello4711");
- // client.unsubscribe("/hello");
- }
- void messageReceived(String &topic, String &payload) {
- Serial.println("incoming: " + topic + " - " + payload);
- // Note: Do not use the client in the callback to publish, subscribe or
- // unsubscribe as it may cause deadlocks when other things arrive while
- // sending and receiving acknowledgments. Instead, change a global variable,
- // or push to a queue and handle it in the loop after calling `client.loop()`.
- }
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, pass);
- // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
- // by Arduino. You need to set the IP address directly.
- //
- // MQTT brokers usually use port 8883 for secure connections.
- client.begin("public.cloud.shiftr.io", 8883, net);
- client.onMessage(messageReceived);
- connect();
- }
- void loop() {
- client.loop();
- delay(10); // <- fixes some issues with WiFi stability
- if (!client.connected()) {
- connect();
- }
- // publish a message roughly every second.
- if (millis() - lastMillis > 1000) {
- lastMillis = millis();
- client.publish("/hello", "world");
- }
- }
Thanks!