Louis_m
Created January 10, 2022

W5100S-EVB-Pico [Micropython] - MQTT

In this tutorial, we’ll show you how to use MQTT to exchange data between two W5100S-Pico-EVB boards using MicroPython firmware

BeginnerFull instructions provided1
W5100S-EVB-Pico [Micropython] - MQTT

Things used in this project

Hardware components

W5100S-EVB-Pico
WIZnet W5100S-EVB-Pico
×2
Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1

Software apps and online services

MicroPython
MicroPython

Story

Read more

Code

MQTT_pub

MicroPython
#######################################
####         MQTT PICO #1          ####
#######################################
from umqttsimple import MQTTClient
import time
import ubinascii
from machine import Pin,SPI
from usocket import socket
import network
import rp2

#mqtt config
mqtt_server = '192.168.1.7'
client_id = 'wiz1'
topic_pub = b'hello'
topic_msg = b'Hello WIZnet'

last_message = 0
message_interval = 5
counter = 0

#W5x00 chip init
def w5x00_init():
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
    nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())
        
#MQTT connect
def mqtt_connect():
    client = MQTTClient(client_id, mqtt_server, keepalive=60)
    client.connect()
    print('Connected to %s MQTT Broker'%(mqtt_server))
    return client

#reconnect & reset
def reconnect():
    print('Failed to connected to MQTT Broker. Reconnecting...')
    time.sleep(5)
    machine.reset()

def main():
    w5x00_init()
    try:
        client = mqtt_connect()
    except OSError as e:
        reconnect()
    while True:
        client.publish(topic_pub, topic_msg)
        print("Send Topic msg = ", topic_msg)
        time.sleep(3)
        
    client.disconnect()
    
if __name__ == "__main__":
    main()

MQTT_sub

MicroPython
#######################################
####         MQTT PICO #2          ####
#######################################
from umqttsimple import MQTTClient
import time
import ubinascii
from machine import Pin,SPI
from usocket import socket
import network
import rp2

#mqtt config
mqtt_server = '192.168.1.7'
client_id = "wiz"
topic_sub = b'hello'


last_message = 0
message_interval = 5
counter = 0

#W5x00 chip init
def w5x00_init():
    spi=SPI(0,2_000_000, mosi=Pin(19),miso=Pin(16),sck=Pin(18))
    nic = network.WIZNET5K(spi,Pin(17),Pin(20)) #spi,cs,reset pin
    nic.ifconfig(('192.168.1.20','255.255.255.0','192.168.1.1','8.8.8.8'))
    while not nic.isconnected():
        time.sleep(1)
        print(nic.regs())

def sub_cb(topic, msg):
    print((topic.decode('utf-8'), msg.decode('utf-8')))

#MQTT connect
def mqtt_connect():
    client = MQTTClient(client_id, mqtt_server, keepalive=60)
    client.set_callback(sub_cb)
    client.connect()
    print('Connected to %s MQTT Broker'%(mqtt_server))
    return client

#reconnect & reset
def reconnect():
    print('Failed to connected to Broker. Reconnecting...')
    time.sleep(5)
    machine.reset()

#subscribe
def main():
    w5x00_init()
    try: 
        client = mqtt_connect()
    except OSError as e:
        reconnect()
    
    while True:
        client.subscribe(topic_sub)
        time.sleep(1)

    client.disconnect()

if __name__ == "__main__":
    main()

Credits

Louis_m

Louis_m

11 projects • 3 followers

Comments