Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example MicroPython MQTT over WebSocket script
# This example script requires the MicroPython changes in the following branches:
import usocket as socket
import utime as time
from websocket import websocket
from umqtt.simple import MQTTClient
# Create a TCP socket and connect it to the MQTT broker
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
addr = socket.getaddrinfo(b'', 8000)[0][-1]
# Negotiate the WebSocket protocol upgrade via HTTP. This is most easily
# done by simulating an HTTP request with the necessary headers. There is no
# need to bust out a full HTTP implementation.
# TODO: Generate the value for the "Sec-WebSocket-Key" header dynamically.
GET /mqtt HTTP/1.1\r
Connection: Upgrade\r
Upgrade: websocket\r
Sec-WebSocket-Key: aSk+xCH/A9ZbxvqjxF8itg==\r
Sec-WebSocket-Version: 13\r
# TODO: Validate the response to the websocket upgrade request. This can be
# done by checking the HTTP response code and validating the
# "Sec-WebSocket-Accept" header.
l = s.readline()
while l:
if l == b'\r\n':
l = s.readline()
# Now that the WebSocket protocol upgrade negotiation is complete, create a
# WebSocket from the TCP socket connected to the MQTT broker.
ws = websocket(s)
# Create an MQTT client
# TODO: Generate the client ID dynamically.
keepalive = 60
client = MQTTClient('clientId-g47Izb6dPK', sock=ws, keepalive=keepalive)
def on_message_received(topic, message):
print('Message received: topic=%s, message=%s' % (topic, message))
# Publish messages to and receive messages from the MQTT broker forever
connected = False
topic = b'testtopic/1'
while True:
if not connected:
connected = True
last_publish = 0
last_ping = time.ticks_ms()
print('Subscribing to topic %s...' % (topic,))
# Periodically ping the broker consistently with the "keepalive"
# argument used when connecting. If this isn't done, the broker will
# disconnect when the client has been idle for 1.5x the keepalive
# time.
if time.ticks_ms() - last_ping >= keepalive * 1000:
last_ping = time.ticks_ms()
# Every 5 minutes publish a message for fun.
if time.ticks_ms() - last_publish >= 5 * 60 * 1000:
print('Publishing a message to topic %s...' % (topic,))
client.publish(topic, 'Coming from MicroPython via a websocket @%d' % (time.ticks_ms(),))
last_publish = time.ticks_ms()
# Receive the next message if one is available, and handle the case that
# the broker has disconnected.
except OSError as e:
if str(e) == '-1':
connected = False
print('Connection closed')
# Sleep a little to not needlessly burn processor cycles.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment