ESP32 with Ethernet and WiFiClientSecure

nmaas87
Posts: 2
Joined: Tue Aug 21, 2018 6:24 am

ESP32 with Ethernet and WiFiClientSecure

Postby nmaas87 » Sat Feb 20, 2021 6:05 pm

Dear all,

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:
  1. // This example uses an Arduino Uno together with
  2. // an Ethernet Shield to connect to shiftr.io.
  3. //
  4. // You can check on your device after a successful
  5. // connection here: https://shiftr.io/try.
  6. //
  7. // by Joël Gähwiler
  8. // https://github.com/256dpi/arduino-mqtt
  9.  
  10. #include <SPI.h>
  11. #include <Ethernet.h>
  12. #include <MQTT.h>
  13.  
  14. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  15. byte ip[] = {192, 168, 4, 177};  // <- change to match your network
  16.  
  17. EthernetClient net;
  18. MQTTClient client;
  19.  
  20. unsigned long lastMillis = 0;
  21.  
  22. void connect() {
  23.   Serial.print("connecting...");
  24.   while (!client.connect("arduino", "public", "public")) {
  25.     Serial.print(".");
  26.     delay(1000);
  27.   }
  28.  
  29.   Serial.println("\nconnected!");
  30.  
  31.   client.subscribe("/hello4711");
  32.   // client.unsubscribe("/hello");
  33. }
  34.  
  35. void messageReceived(String &topic, String &payload) {
  36.   Serial.println("incoming: " + topic + " - " + payload);
  37.  
  38.   // Note: Do not use the client in the callback to publish, subscribe or
  39.   // unsubscribe as it may cause deadlocks when other things arrive while
  40.   // sending and receiving acknowledgments. Instead, change a global variable,
  41.   // or push to a queue and handle it in the loop after calling `client.loop()`.
  42. }
  43.  
  44. void setup() {
  45.   Serial.begin(115200);
  46.   Ethernet.init(33);
  47.   Ethernet.begin(mac, ip);
  48.  
  49.   // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  50.   // by Arduino. You need to set the IP address directly.
  51.   client.begin("public.cloud.shiftr.io", net);
  52.   client.onMessage(messageReceived);
  53.  
  54.   connect();
  55. }
  56.  
  57. void loop() {
  58.   client.loop();
  59.   delay(10);  // <- fixes some issues with WiFi stability
  60.  
  61.   if (!client.connected()) {
  62.     connect();
  63.   }
  64.  
  65.   // publish a message roughly every second.
  66.   if (millis() - lastMillis > 1000) {
  67.     lastMillis = millis();
  68.     client.publish("/hello4711", "world");
  69.   }
  70. }
However, I now need to use MQTTS via Ethernet.
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
  1. // This example uses an ESP32 Development Board
  2. // to connect to shiftr.io.
  3. //
  4. // You can check on your device after a successful
  5. // connection here: https://shiftr.io/try.
  6. //
  7. // by Joël Gähwiler
  8. // https://github.com/256dpi/arduino-mqtt
  9.  
  10. #include <WiFiClientSecure.h>
  11. #include <MQTT.h>
  12.  
  13. const char ssid[] = "";
  14. const char pass[] = "";
  15.  
  16. WiFiClientSecure net;
  17. MQTTClient client;
  18.  
  19. unsigned long lastMillis = 0;
  20.  
  21. void connect() {
  22.   Serial.print("checking wifi...");
  23.   while (WiFi.status() != WL_CONNECTED) {
  24.     Serial.print(".");
  25.     delay(1000);
  26.   }
  27.  
  28.   Serial.print("\nconnecting...");
  29.   while (!client.connect("arduino", "public", "public")) {
  30.     Serial.print(".");
  31.     delay(1000);
  32.   }
  33.  
  34.   Serial.println("\nconnected!");
  35.  
  36.   client.subscribe("/hello4711");
  37.   // client.unsubscribe("/hello");
  38. }
  39.  
  40. void messageReceived(String &topic, String &payload) {
  41.   Serial.println("incoming: " + topic + " - " + payload);
  42.  
  43.   // Note: Do not use the client in the callback to publish, subscribe or
  44.   // unsubscribe as it may cause deadlocks when other things arrive while
  45.   // sending and receiving acknowledgments. Instead, change a global variable,
  46.   // or push to a queue and handle it in the loop after calling `client.loop()`.
  47. }
  48.  
  49. void setup() {
  50.   Serial.begin(115200);
  51.   WiFi.begin(ssid, pass);
  52.  
  53.   // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
  54.   // by Arduino. You need to set the IP address directly.
  55.   //
  56.   // MQTT brokers usually use port 8883 for secure connections.
  57.   client.begin("public.cloud.shiftr.io", 8883, net);
  58.   client.onMessage(messageReceived);
  59.  
  60.   connect();
  61. }
  62.  
  63. void loop() {
  64.   client.loop();
  65.   delay(10);  // <- fixes some issues with WiFi stability
  66.  
  67.   if (!client.connected()) {
  68.     connect();
  69.   }
  70.  
  71.   // publish a message roughly every second.
  72.   if (millis() - lastMillis > 1000) {
  73.     lastMillis = millis();
  74.     client.publish("/hello", "world");
  75.   }
  76. }

Thanks!

SamLaserray
Posts: 1
Joined: Wed Apr 05, 2023 12:41 pm

Re: ESP32 with Ethernet and WiFiClientSecure

Postby SamLaserray » Wed Apr 05, 2023 12:47 pm

Hello!
Did you manage to make it work?

nmaas87
Posts: 2
Joined: Tue Aug 21, 2018 6:24 am

Re: ESP32 with Ethernet and WiFiClientSecure

Postby nmaas87 » Fri Apr 07, 2023 1:04 am

Nope

FiwiDev
Posts: 19
Joined: Wed Jan 22, 2020 12:32 am

Re: ESP32 with Ethernet and WiFiClientSecure

Postby FiwiDev » Mon Feb 26, 2024 2:54 pm

You cannot use the WiFiClientSecure library with anything else--it's internally bound to the WiFi module.
However, you can use a wrapper library that utilizes the exact same mbedTLS functionality under the hood--and assign it to any device library that implements Client. I personally suggest https://github.com/alkonosst/SSLClientESP32/ as it utilizes the mbedTLS instance provided in the Arduino-ESP32 library.

Who is online

Users browsing this forum: Majestic-12 [Bot], tore-espressif and 78 guests