MQTT publish

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
User avatar
andrew
Posts: 22
Joined: Sun Aug 10, 2014 9:22 am

MQTT publish

Post by andrew » Mon Nov 02, 2015 9:51 pm

This is a hack. I repeat, this is a hack. Do not run your nuclear power station control system with it.

This code is a conversion of some javascript I found here http://forum.espruino.com/conversations/258515/ for the Espruino.

Code: Select all

def mtStr(s):
  return bytes([len(s) >> 8, len(s) & 255]) + s.encode('utf-8')

def mtPacket(cmd, variable, payload):
  return bytes([cmd, len(variable) + len(payload)]) + variable + payload

def mtpConnect(name):
  return mtPacket(
           0b00010000,
           mtStr("MQTT") + # protocol name
           b'\x04' +       # protocol level
           b'\x00' +       # connect flag
           b'\xFF\xFF',    # keepalive
           mtStr(name)
  )

def mtpDisconnect():
  return bytes([0b11100000, 0b00000000])

def mtpPub(topic, data):
  return  mtPacket(0b00110001, mtStr(topic), data)

import socket
import binascii
import time

addr = socket.getaddrinfo("test.mosquitto.org", 1883)[0][4]
s = socket.socket()

print('Connecting...')
s.connect(addr)
s.send(mtpConnect("WiPy1"))
print(binascii.hexlify(s.recv(4096)))

print("Publishing...")

time.sleep(10)

s.send(mtpPub("wipytopic", b'wipy-data'))

time.sleep(10)

s.send(mtpPub("wipytopic", b'wipy-data-again'))
##print(binascii.hexlify(s.recv(4096)))

print('Disconnecting...')
s.send(mtpDisconnect())
s.close()
Andrew

Post Reply