Wiznet makers

scott

Published November 01, 2022 ©

75 UCC

18 WCC

33 VAR

0 Contests

0 Followers

0 Following

Original Link

W5500を使ってArduinoでMQTT通信する

Let's try MQTT communication with Arduino using WIZnet's W5500 .

COMPONENTS Hardware components

WIZnet - W5500

x 1


Arduino - Arduino UNO

x 1

Software Apps and online services

Arduino - Arduino IDE

x 1


PROJECT DESCRIPTION

1. Prepare

Purchase W5500 on Aliexpress etc.

1pcs USR ES1 W5500 Chip New SPI LAN Ethernet Convert TCPIP Mod - AliExpress.com | Integrated Circuits - AliExpress

 

f:id:hik0leaf:20190615222738j:plain

very compact

 

Arduino uses UNO R3.

2. Connection

To communicate with Arduino via SPI, connect the W5500 as follows. The IO level is 3.3V, but it is also 5V tolerant, so it can be directly connected to Arduino . Another point to note is that the current consumption of the W5500 is large, so the 3.3V is supplied externally instead of from the Arduino .

W5500 | WIZnet Co., Ltd.

 

f:id:hik0leaf:20190616105511j:plain

Connection diagram

 

ArduinoW5500
D10 (SS)CS
D11 (MOSI)SMOKE
D12 (MISO)MISO
D13 (SCK)SCLK
GNDGND

According to the data sheet, it consumes 132mA at 100M Transmitting, so it is better to prepare an external power supply that can supply 200mA or more.

products:w5500:datasheet [Document Wiki]

 

f:id:hik0leaf:20190615134207j:plain

Current consumption of W5500 (Excerpt from data sheet)

 

 

f:id:hik0leaf:20190615134554j:plain

W5500 module pinout

 

[Caution] When connecting with a breadboard, keep the wires as short as possible. If the wire is long, the communication will be unstable.

3. Library installation

When using W5500 , unlike ENC28J60, you can control the module with the standard Ethernet library , so install only PubSubClient that handles the MQTT part.

github.com

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

f:id:hik0leaf:20190615212349j:plain

4. Code

Write the following code to Arduino .

#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(9600);
  Serial.println("starting...");
  
  Ethernet.init(10);// CS pin 10
  Ethernet.begin(mac);

  // 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();
}

PubSubClient also has API documentation.

Arduino Client for MQTT

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

5. Run

After confirming that the MQTT Broker is working, check the log on the serial monitor connected to the Arduino . 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. You can confirm that ready is sent when Arduino starts up. If you miss it , reset the Arduino 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 Arduino 's serial monitor.

6. Summary

In this blog, I have introduced that TCP and UDP communication is possible even with weak microcomputers by using W5500 and ENC28J60 . Recently, with the advent of ESP8266 and ESP32 equipped with Wi-Fi , it has become possible to easily create IoT devices by themselves. I think there are situations where you want it.

When communicating with a microcomputer , it would be nice to be able to select wired, wireless, and protocols in consideration of cost, application, usage environment, etc.

Documents
Comments Write