---
title: "M5Stack#LAN W5500 with MQTT"
url: "https://maker.wiznet.io/eric_g/projects/M5StackLAN-W5500-with-MQTT/"
markdown_url: "https://maker.wiznet.io/eric_g/projects/M5StackLAN-W5500-with-MQTT/md"
type: "UCC: User Created Content"
author: "eric_g"
author_url: "https://maker.wiznet.io/eric_g/"
original_author: "eric_g"
original_url: "http://soup01.com/en/2022/03/31/m5stacklan-w5500-with-mqtt-2/"
published: "2022-04-22"
language: "en"
hardware: ["M5Stack"]
likes: 0
views: 136
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# M5Stack#LAN W5500 with MQTT

> M5Stack#LAN W5500 with MQTT

Original author: eric_g (source: http://soup01.com/en/2022/03/31/m5stacklan-w5500-with-mqtt-2/)

## Components

- **M5Stack** x 1: with W5500

## Article

## M5Stack#LAN W5500 with MQTT

2022/3/31?[M5Stack_EN](http://soup01.com/en/category/m5stack_en/),?[MQTT](http://soup01.com/en/category/protocol-en/mqtt-en/),?[Protocol_EN](http://soup01.com/en/category/protocol-en/),?[Raspberry_EN](http://soup01.com/en/category/raspberry_en/)

Here is a tutorial to show you how to use M5Stack core2 with their W5500 Lan Modules. This is the link that you can buy it;) <https://shop.m5stack.com/products/lan-module?variant=16804778803290>

![](https://lh5.googleusercontent.com/Av9jfy_dCftNXu2W8jCN0zGnjllJUNU8eMihfKEyNP9oIHaqn_9LB0vUYps1ccwpJQxCgDHKz_yQxt4qt-7VHwl8N3bCsaks_Z8_Cxu0YVGy1-PpJ6HJa1caX1a0tTMvYwDq9ceu)

## Note

if Core2 or Core2-for-aws is your main controller to plug with this module, DC power input must be used and set the input mode as power mode. (otherwise the module will not start normally) Note: When using the CORE2 or CORE2-FOR-AWS main control to drive the module, please use the DC power input of the base and set the power mode to input mode (refer to the code below), otherwise it will not start normally

![](https://lh3.googleusercontent.com/Z7oXwO1xr_GeUCmRw-V3Uix1SV0okpwWkJxI2q8DkFRD1A3_6n7iYv9Z376A3tCyckWMCyw7crqueKaKcBx_LYDSmFoGrXRng1DQ5XRiG6BcwJeVNiExNrcFWjsMAxp4xipG1TXX)

You can easily find this type of power supply from Amazon;).

![](https://lh5.googleusercontent.com/REvt4q25WP-X2YmkCh0S7vglDtqW0wRKSi0vOhV6591SL__ugvJkEzaza8CQryVvKEndOheGHruC5k6DUVy1pMg-GkY8M9e_kwCd_17fRThJrxxdgOmxCvWywE36rm67zBleovQB)

### Example to power mode

Here is the Example Code to show you how to set the M5Stack as Power Mode.

| //mbus_mode_t: //kMBusModeOutput:?**Use**?USB?**or**?battery?**power** //kMBusModeInput:?**Use**?**external**?**power**?supply 5V,?**or**?DC?**interface**M5.begin(**true**,?**true**,?**true**,?**false**, kMBusModeInput);//Initialize Serial according to the actual connected pins Serial2.begin(115200, SERIAL_8N1, 13, 14); |
| --- |

## Configuration

Here is the configuration in this tutorial. I will use M5Stack W5500 LAN Module to connect with Raspberry, and publish the message by using MQTT.

![](https://maker.wiznet.io/uploads/2022/05/m5stackUcc-300x170.png)

## Reference Link

> [MQTT#Broker Setup](http://soup01.com/en/2022/03/15/mqttbroker-setup/)

> [MQTT#Connection is refuesd solution](http://soup01.com/en/2022/03/14/mqttconnection-is-refuesd-solution-2/)

> [MQTT#using Python Library as a Subscriber and Publisher](http://soup01.com/en/2022/03/16/mqttusing-python-library-as-a-subscriber-and-publisher-2/)

## Code

| #include &lt;M5Core2.h> #include &lt;SPI.h> #include &lt;Ethernet2.h> #include &lt;PubSubClient.h>#define SCK 18 #define MISO 19 #define MOSI 23 #define CS 26**byte**?mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x08, 0xE1 }; IPAddress Local(192, 168, 1, 211); EthernetClient ethClient; PubSubClient mqttClient(ethClient); //MQTT Broker **const**?**char**?*ENDPOINT=”192.168.001.050″; **const**?**int**?PORT=1883; **char**?*MYID=”M5Stack”; **char**?*PUBLISHTOPIC=”M5Stack/pub”; **char**?*SUBTOPIC=”RoboDK/Station/s1/#”; **void**?setup(){ //If LAN Modules is used,Please use this operation mode M5.begin(true, true, true, false, kMBusModeInput); Serial2.begin(115200, SERIAL_8N1, 13, 14); // START M5.Lcd.fillScreen(BLACK); M5.Lcd.setCursor(10, 10); M5.Lcd.setTextColor(WHITE); M5.Lcd.setTextSize(3); M5.Lcd.printf(“Inited..\n”); M5.Lcd.printf(“IP:192.168.1.211\n”); M5.Lcd.printf(“Mac:90-A2-DA-x0F-08-E1\n”); mqttClient.setServer(ENDPOINT, PORT); SPI.begin(18, 19, 23, -1); Ethernet.init(26); Ethernet.begin(mac, Local); connectMQTT(); } **void**?mqttLoop() { if (!mqttClient.connected()) { connectMQTT(); } mqttClient.loop(); } **void**?connectMQTT() { while (!mqttClient.connected()) { Serial.print(“Connection Loop..”); if (mqttClient.connect(MYID)) { Serial.println(“Connected.”); **int**?qos = 0; mqttClient.subscribe(SUBTOPIC, qos); Serial.println(“Subscribed.”); } else { Serial.print(“Failed. Error state=”); Serial.print(mqttClient.state()); // Wait 5 seconds before retrying delay(1000); } } } **long**?messageSentAt = 0; **int**?count = 0; **char**?pubMessage[128]; **int**?led,red,green,blue; **int**?status=0; **void**?loop() { mqttLoop(); M5.update(); **long**?now = millis(); if (now ? messageSentAt > 1000) { if (status ==0){ M5.Lcd.fillScreen(BLACK); M5.Lcd.fillCircle(80, 120, 30, GREEN); M5.Lcd.fillCircle(160, 120, 30, YELLOW); M5.Lcd.fillCircle(240, 120, 30, RED); status=1; }else{ M5.Lcd.fillScreen(BLACK); M5.Lcd.drawCircle(80, 120, 30, GREEN); M5.Lcd.drawCircle(160, 120, 30, YELLOW); M5.Lcd.drawCircle(240, 120, 30, RED); status=0; } messageSentAt = now; sprintf(pubMessage, “{\”count\”: %d}”, count++); Serial.print(“Publishing message to topic “); Serial.println(PUBLISHTOPIC); Serial.println(pubMessage); mqttClient.publish(PUBLISHTOPIC, pubMessage); Serial.println(“Published.”); } } |
| --- |

## Result

The message is Published to the MQTT Broker if connection is established.

| Publishing?**message**?**to**?topic M5Stack/pub {“count”: 9} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 10} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 11} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 12} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 13} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 14} **Published**. Publishing?**message**?**to**?topic M5Stack/pub {“count”: 15} **Published**. |
| --- |

## Find ME

Twitter:＠3threes2 Email:soup01threes*gmail.com (* to ＠） YoutubeChannel:https://www.youtube.com/channel/UCQ3CHGAIXZAbeOC_9mjQiWQ

[gomamenotes](http://soup01.com/en/author/gomamenotes/)

### ?連記事

[![MQTT#using Python Library as a Subscriber and Publisher](https://maker.wiznet.io/upload/mirror/body/2731bd204b9c431ac1f9.jpg)](http://soup01.com/en/2022/03/16/mqttusing-python-library-as-a-subscriber-and-publisher-2/)

#### [MQTT#using Python Library as a Subscriber and Publisher](http://soup01.com/en/2022/03/16/mqttusing-python-library-as-a-subscriber-and-publisher-2/)

Here is a tutorial to explain how to implement the MQTT Client by usi...

[記事を?む](http://soup01.com/en/2022/03/16/mqttusing-python-library-as-a-subscriber-and-publisher-2/)

---

Source: https://maker.wiznet.io/eric_g/projects/M5StackLAN-W5500-with-MQTT/
