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.')