micropython 结合mtqq上报sht20温湿度

import network, utime
from machine import I2C, Pin, Timer
from sht2x import SHT2x
from simple import MQTTClient

sensor = SHT2x(I2C(scl=Pin(5), sda=Pin(4)))
led1 = Pin(2, Pin.OUT)
led1.value(1)
LED = Pin(15, Pin.OUT)
LED.value(1)
SERVER = "mq.tongxinmao.com"
PORT = 18830
CLIENT_ID = "umqtt_sht20"

c = None
def getsht20th():
    sht20th=[]
    sht20t = sensor.getTemperature()
    sht20h = sensor.getHumidity()
    sht20th.append('%.2f' % sht20t)
    sht20th.append('%.2f%%' % sht20h)
    return sht20th

def do_connect():
        sta_if = network.WLAN(network.STA_IF)
        ap_if = network.WLAN(network.AP_IF)
        if ap_if.active():
            ap_if.active(False)
        if not sta_if.isconnected():
            print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('iPhonexx', '123456798') #wifi的SSID和密码
        while not sta_if.isconnected():
            pass
        print('network config:', sta_if.ifconfig())

def pubmsg(server=SERVER, port=PORT):
    c = MQTTClient(CLIENT_ID, server, port)
    c.connect()
    c.publish(b"/public/TEST/sht20th", "T:" + getsht20th()[0] + ",H:" + getsht20th()[1])
    c.disconnect()

if __name__ == "__main__":
    do_connect()
    while True:
       try:
           LED.value(0)
           LED.value(1)
           pubmsg(server=SERVER, port=PORT)
           LED.value(0)
           LED.value(1)
           utime.sleep(1)
       except Exception as ValueError:
           pass

[2021/11/12 15:26:41 097] 30.30,25.60%
[2021/11/12 15:26:43 303] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:43 305] 30.30,25.55%
[2021/11/12 15:26:45 289] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:45 292] 30.30,25.55%
[2021/11/12 15:26:47 015] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:47 020] 30.31,25.55%
[2021/11/12 15:26:48 775] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:48 777] 30.31,25.60%
[2021/11/12 15:26:50 709] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:50 712] 30.32,25.60%
[2021/11/12 15:26:52 478] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:52 481] 30.32,25.64%
[2021/11/12 15:26:54 714] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:54 716] 30.32,25.60%
[2021/11/12 15:26:56 815] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:56 817] 30.33,25.64%
[2021/11/12 15:26:59 023] MQTT[TOPIC:sht20th]接收12字节:
[2021/11/12 15:26:59 025] 30.32,25.60%