Wiznet makers

scott

Published November 01, 2022 ©

75 UCC

17 WCC

33 VAR

0 Contests

0 Followers

0 Following

Original Link

W5500を使ってESP32でEthernet MQTT通信する

Let's try MQTT communication with ESP32 ( Arduino core for the ESP32) using WIZnet's W5500 .

COMPONENTS Hardware components

Espressif - ESP32

x 1


WIZnet - W5500

x 1

Software Apps and online services

Arduino - Arduino IDE

x 1


PROJECT DESCRIPTION

1. Prepare

Perform up to 3 with reference to past articles .

  • 1. Prepare
  • 2. Connection
  • 3. Library modification

hikoleaf.hatenablog.jp

2. Install MQTT library

Install the following libraries for MQTT communication.

github.com

The library can be searched by name from the library manager of the Arduino IDE and installed.

f:id:hik0leaf:20190615212349j:plain

API documentation is available for PubSubClient .

Arduino Client for MQTT

3. Code

Write the following code to ESP32.

#include <Ethernet.h>
#include <PubSubClient.h>


byte mac[] = { 0x70, 0x69, 0x69, 0x2D, 0x30, 0x31 };
byte server[] = { 10, 0, 1, 5 };

void callback(char* topic, byte* payload, unsigned int length) {
  payload[length] = '\0';
  String msg = String((char*) payload);
  Serial.println(msg);
}

EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);

void reconnect() {
  while (!client.connected()) {
    Serial.println("Attempting MQTT connection...");
    
    if (client.connect("myClient")) {
      Serial.println("MQTT PubSub Ready");
      client.publish("output", "ready");
      client.subscribe("input");
      Serial.println("subscribe input");
    } else {
      Serial.println("MQTT PubSub failer");
      Serial.println("try again in 2 seconds");
      delay(2000);
    }
  }  
}


void setup() {
  Serial.begin(115200);
  Serial.println("starting...");
  
  Ethernet.init(33);// CS pin 33
  Ethernet.begin(mac);// use DHCP

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }

  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
  
  delay(1500);
}


void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Change the MAC address according to your environment. DHCP is used to set the IP address , but you can also set a fixed IP by specifying the IP address or gateway as an argument to Ethernet .begin() .

Default values ​​are set for dns , gateway , and subnet when setting with a fixed IP, so it may work only with the MAC address and IP address , but if you do not specify it, it will not work or communication will be unstable. So it is better to specify everything even if it is troublesome. You can check the details of Ethernet .begin() from the official reference below.

Arduino - EthernetBegin

Also, the server address is the address of the MQTT Broker, and the MQTT Broker is required to check the operation. The easiest and recommended way to build MQTT Broker is to install mosqitto on Raspberry Pi .

4. Run

After confirming that the MQTT Broker is working, check the log on the serial monitor connected to the ESP32. If everything works fine, you should see something like this:

f:id:hik0leaf:20190616110611j:plain

When you install mosquitto, PubSub client is installed in addition to Broker, so let's check the operation by launching terminals for Publish (Pub) and Subscribe (Sub).

 

f:id:hik0leaf:20190615214945j:plain

Subscribe screen

 

Subscribe to topic output. When the ESP32 starts up, you can see ready being sent. If you miss it, reset the ESP32 and it will be sent again.

 

f:id:hik0leaf:20190615214907j:plain

Publish screen

 

If you send a message (payload) to the input topic, you can check the reception on the serial monitor.

5. Summary

I tried MQTT communication using Ethernet with ESP32 . Since the ESP32 is equipped with WiFi , wireless communication can be tried more easily, but Ethernet has overwhelming communication stability that cannot be obtained with wireless, so I think it would be better to use it according to the application. .

Documents
Comments Write