# Low level comm code originally derived from
# https://github.com/andrewmk/uMQTT. Thus, it is under the GPL license.
# We will replace this with the standard mqtt client for micropython,
# once it become available.

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

def connect(name, host, port=1883):
  addr = socket.getaddrinfo(host, port)[0][4]
  s = socket.socket()
  print('Connecting...')
  s.connect(addr)
  s.send(mtpConnect(name))
  resp_header = s.recv(2)
  resp_len = int(resp_header[1])
  s.recv(resp_len)
  return s

# end of code from uMQTT

import json
import utime

class MQTTWriter:
  __slots__ = ('socket', 'topic', 'name', 'host', 'port')
  def __init__(self, name, host, port, topic):
    self.topic = topic
    self.name = name
    self.host = host
    self.port = port
    self._connect()

  def _connect(self):
    print("Connecting to %s:%s" % (self.host, self.port))
    self.socket = connect(self.name, self.host, self.port)
    print("Connection successful")

  def on_next(self, x):
    data = bytes(json.dumps(x), 'utf-8')
    while True:
      try:
        if self.socket==None:
          self._connect()
        self.socket.send(mtpPub(self.topic, data))
        return
      except Exception as e:
        print("Error: %s" % repr(e))
        try:
          self.socket.close()
        except:
          pass
        self.socket = None
        utime.sleep(10)

  def on_completed(self):
    print("mqtt_completed, disconnecting")
    self.socket.send(mtpDisconnect())
    self.socket.close()

  def on_error(self, e):
    print("mqtt on_error: %s, disconnecting" %e)
    self.socket.send(mtpDisconnect())
    self.socket.close()


@@ -1,1141 +1,87 @@
 # 
-Low level comm code originally derived from%0A# https://github.com/andrewmk/uMQTT. Thus, it is under the GPL license.%0A# We will replace this with the standard mqtt client for micropython,%0A# once it become available.%0A%0Adef mtStr(s):%0A  return bytes(%5Blen(s) %3E%3E 8, len(s) & 255%5D) + s.encode('utf-8')%0A%0Adef mtPacket(cmd, variable, payload):%0A  return bytes(%5Bcmd, len(variable) + len(payload)%5D) + variable + payload%0A%0Adef mtpConnect(name):%0A  return mtPacket(%0A           0b00010000,%0A           mtStr(%22MQTT%22) + # protocol name%0A           b'%5Cx04' +       # protocol level%0A           b'%5Cx00' +       # connect flag%0A           b'%5CxFF%5CxFF',    # keepalive%0A           mtStr(name)%0A  )%0A%0Adef mtpDisconnect():%0A  return bytes(%5B0b11100000, 0b00000000%5D)%0A%0Adef mtpPub(topic, data):%0A  return  mtPacket(0b00110001, mtStr(topic), data)%0A%0Aimport socket%0A%0Adef connect(name, host, port=1883):%0A  addr = socket.getaddrinfo(host, port)%5B0%5D%5B4%5D%0A  s = socket.socket()%0A  print('Connecting...')%0A  s.connect(addr)%0A  s.send(mtpConnect(name))%0A  resp_header = s.recv(2)%0A  resp_len = int(resp_header%5B1%5D)%0A  s.recv(resp_len)%0A  return s%0A%0A# end of code from uMQTT%0A%0Aimport json%0Aimport utime
+Writer interface over umqtt API.%0A%0Afrom umqtt.robust import MQTTClient%0Aimport json
 %0A%0Acl
@@ -112,21 +112,27 @@
 s__ = ('
-socke
+host', 'por
 t', 'top
@@ -141,27 +141,13 @@
 ', '
-name', 'host', 'por
+clien
 t')%0A
@@ -220,29 +220,8 @@
 pic%0A
-    self.name = name%0A
     
@@ -258,16 +258,63 @@
  = port%0A
+    self.client = MQTTClient(name, host, port)%0A
     self
@@ -419,56 +419,23 @@
 elf.
-socket = connect(self.name, self.host, self.port
+client.connect(
 )%0A  
@@ -541,342 +541,60 @@
     
-while True:%0A      try:%0A        if self.socket==None:%0A          self._connect()%0A        self.socket.send(mtpPub(self.topic, data))%0A        return%0A      except Exception as e:%0A        print(%22Error: %25s%22 %25 repr(e))%0A        try:%0A          self.socket.close()%0A        except:%0A          pass%0A        self.socket = None%0A        utime.sleep(10
+self.client.publish(bytes(self.topic, 'utf-8'), data
 )%0A%0A 
@@ -674,58 +674,25 @@
 elf.
-socket.send(mtpDisconnect())%0A    self.socket.close
+client.disconnect
 ()%0A%0A
@@ -778,58 +778,25 @@
 elf.
-socket.send(mtpDisconnect())%0A    self.socket.close
+client.disconnect
 ()%0A%0A

Change the mqtt writer for the micropython port to use the standard library's mqtt implementation