a problem about MQTT

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
wfljianshen
Posts: 1
Joined: Tue Apr 27, 2021 3:05 pm

a problem about MQTT

Post by wfljianshen » Tue Apr 27, 2021 3:10 pm

how to do when this happen:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 34, in <module>
File "umqttsimple.py", line 60, in connect
OSError: 23

my code:

Code: Select all

# Complete project details at https://RandomNerdTutorials.com

from machine import Pin, I2C,Timer
from time import sleep
import dht 
import ssd1306

from umqttsimple import MQTTClient
import network

SERVER='183.230.40.96'
CLIENT_ID="edp-dev2" 
username="422432"
password='version=2018-10-31&res=products%2F422432%2Fdevices%2Fesp-dev2&et=1619535361&method=sha1&sign=b54icKl6lqiux01DsctcrlWUNoU%3D'
publish_TOPIC= '$sys/422432/edp-dev2/dp/post/json'


wlan = network.WLAN(network.STA_IF)
wlan.active(True)                   #激活接口
start_time=time.time()              #记录时间做超时判断
if not wlan.isconnected():
      print('connecting to network...')
      wlan.connect('Redmi', '12345678') #输入WIFI账号密码
      if time.time()-start_time > 15 :
            print('WIFI Connected Timeout!')
if wlan.isconnected():
  print('wifi connet')
client = MQTTClient(CLIENT_ID, SERVER, 1883, username, password,60)
print(client)
client.connect()




i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
sensor = dht.DHT11(Pin(14))
#sensor = dht.DHT11(Pin(14))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)

while True:
  try:
    sleep(2)
    sensor.measure()
    temp = sensor.temperature()
    hum = sensor.humidity()
    temp_f = temp * (9/5) + 32.0
    print('Temperature: %3.1f C' %temp)
    print('Temperature: %3.1f F' %temp_f)
    print('Humidity: %3.1f %%' %hum)
    sleep(0.5)
    oled.fill(0)
    oled.text('Temperature: %3.1f C' %temp,0,0)
    oled.text('Temperature: %3.1f F' %temp_f,0,20)
    oled.text('Humidity: %3.1f %%' %hum,0,30)
    oled.show()
    
    
    mymessage='{"id": 123,"dp": {"CurrentTemperature": [{ "v": %d,}],"CurrentHumidity": [{"v": %d,}]}}'%(temp,hum)
    client.publish(topic=publish_TOPIC,msg= mymessage, retain=False, qos=0)
  except OSError as e:
    print('Failed to read sensor.')

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: a problem about MQTT

Post by karfas » Wed Apr 28, 2021 4:51 pm

What do you think the MQTT client will need to connect ?

I can't imagine that the password for your MQTT broker starts with version= and contains a lot of URL-escaped values.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

Post Reply