WIZnet Ethernet HAT[RP2040] + MQTT Pub/sub [mosquitto]

by louis_m in Circuits > Raspberry Pi

681 Views, 2 Favorites, 0 Comments

WIZnet Ethernet HAT[RP2040] + MQTT Pub/sub [mosquitto]

pico mqtt_pubsub.jpg

It is an MQTT that connects to a mosquito broker and exchanges messages for each topic. Sending data and receiving data

MQTT?

MQTT.png

The pub/sub model and subscriber/subscriber model are communication models between subscription clients and issuance clients.

  • Lightweight message protocols created for use in M2M and the Internet of Things.
  • Operation on TCP
  • <Publisher> issues Topic.
  • <Subscriber> subscribes to Topic.
  • <Broker> serves as a relay and is suitable for 1:N communication.
  • Suitable for messaging processing of "device to devices" such as low power power sensors, mobile devices, embedded computers, Arduinoes, and microcontrollers.


Overview

hat.png
chip.jpg
rp2040.jpg
WIZnet Ethernet HAT (Hardware Attached on Top) is a Raspberry Pi Pico pin-compatible board that utilizes W5100S and supports both 3.3V & 5V.

Please refer to this link to find more information about W5100S.

  • Raspberry Pi Pico Pin-Compatible
  • Ethernet (W5100S Hardwired TCP/IP CHIP)
  • Product page : https://docs.wiznet.io/Product/iEthernet/W5100S/overview
  • Support 4 Independent Hardware SOCKETs simultaneously
  • Support SOCKET-less new Command: ARP-Request, PING-Request
  • Support Auto-MDIX only when Auto-Negotiation mode


RP2040 Datasheet - https://www.raspberrypi.org/documentation/microcontrollers/raspberry-pi-pico.html

Prepare

Circuitpython.png
lib.jpg
Prepare Hardware

1. Combine WIZnet Ethernet HAT with Raspberry Pi Pico.

2. Connect ethernet cable to Ethernet HAT ethernet port.

3. Connect Raspberry Pi Pico to desktop or laptop using 5 pin micro USB cable.

- If you use W5100S-EVB-Pico, you can skip '1. Combine...'


Prepare Software

1.Installing CircuitPython

Install CircuitPython on Raspberry Pi Pico by referring to the link above.

https://learn.adafruit.com/getting-started-with-raspberry-pi-pico-circuitpython/circuitpython

  • Download the below uf2 file link on your computer

CircuitPython 7.0.0 - https://downloads.circuitpython.org/bin/raspberry_pi_pico/en_US/adafruit-circuitpython-raspberry_pi_pico-en_US-7.0.0.uf2

2.Setup WIZnet Ethernet Library

Copy the WIZnet library Into the lib folder inside the Raspberry Pi Pico.

https://learn.adafruit.com/ethernet-for-circuitpython/circuitpython-setup

Before continuing, make sure your board's lib folder has at least the following files and folders copied over: https://github.com/Wiznet/RP2040-HAT-CircuitPython

adafruit_bus_device

adafruit_wiznet5k

adafrui_minimqtt

Installing Programs

PORT.jpg
Terminal.jpg

The following serial terminal program is required for Loopback test, download and install from below links.


Tera Term

Teraterm is a terminal console program and is an interface that helps PCs and users communicate. The main purpose is to be used for serial communication purposes.

Mosquitto

Internet of Things efficiency can be achieved only when communication between objects and interactions of appropriate services are performed.

Before this, you should find the Raspberry Pi Pico COM port number from "Device Manager" on your PC.

How to MQTT_pub/sub Example

lib copy.jpg

Access the WIZnet github, get the Circuit Python source, and copy it to code.py inside the pico.

Setup MQTT_pub/subExample - download https://github.com/Wiznet/RP2040-HAT-CircuitPython/tree/master/examples/MQTT/PubSub


- Paste code into code.py on your Raspberry Pi Pico and Save it.

Mosquitto Broker

MQTT_pub_1.PNG

Create broker using mosquitto by executing the following command. If the broker is created normally, the broker's IP address is the current IP of your desktop or laptop, and the port is 1883 by default.

mosquitto -c mosquitto.conf -p 1883 -v

Connect to Mosquitto Broker

image_zN29rVwe4R.jpg
MQTT_pubsub.png

If the MQTT publish example works normally on Raspberry Pi Pico, you can see the network information of Raspberry Pi Pico, connecting to the broker and publishing the message.


Open three Prompt windows and connect three clients to the broker with the following instructions.

  • -h : It means the IP address of the host PC.
  • -t : Subscribe to Broker with Topic name
mosquitto_sub -h 192.168.1.11 -t Breakfast
mosquitto_sub -h 192.168.1.11 -t Lunch
mosquitto_sub -h 192.168.1.11 -t Dinner


Full Screen

MQTT_pubsub_2.png

The screen on which all brokers and clients are executed is as follows.

Run

MQTT_pubsub_3.png

When you open a new prompt and put a Public message in the topic through a broker, the text suitable for each Topic is output in response.

mosquitto_pub -h 192.168.1.11 -t Breakfast -m "B"
mosquitto_pub -h 192.168.1.11 -t Lunch -m "L"
mosquitto_pub -h 192.168.1.11 -t Dinner -m "D"

Appendix

MQTT_conf.png
  • In Mosquitto versions earlier than 2.0 the default is to allow clients to connect without authentication. In 2.0 and up, you must choose your authentication options explicitly before clients can connect. Therefore, if you are using version 2.0 or later, refer to following link to setup 'mosquitto.conf' in the directory where Mosquitto is installed.https://mosquitto.org/documentation/authentication-methods/

Schematics

HAT.png