I am trying to connect to Adafruit io and get : could not connect to MQTT server OSError23 I can connect to my wifi Hope you can help The following is the code. I removed the key
from machine import Pin import machine from time import sleep import network import time from umqtt.robust import MQTTClient import sys import dht import os import onewire import ds18x20 ADAFRUIT_IO_URL = b'io.adafruit.com' ADAFRUIT_USERNAME = b'sprink' ADAFRUIT_IO_KEY = b'' ADAFRUIT_IO_FEEDNAME1 = b'temperature' ADAFRUIT_IO_FEEDNAME2 = b'humidity' ADAFRUIT_IO_FEEDNAME3 = b'pool' ADAFRUIT_IO_FEEDNAME4 = b'solar' MESUREMENT_INTERVAL = 5 # create a dynamicnest318random MQTT clientID random_num = int.from_bytes(os.urandom(3), 'little') mqtt_client_id = bytes('client_'+str(random_num), 'utf-8') client = MQTTClient(client_id=mqtt_client_id, server=ADAFRUIT_IO_URL, user=ADAFRUIT_USERNAME, password=ADAFRUIT_IO_KEY, ssl=False) #ADAFRUIT_USERNAME/feeds/ADAFRUIT_IO_FEEDNAME1 mqtt_feedname1 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_IO_FEEDNAME1), 'utf-8') mqtt_feedname2 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_IO_FEEDNAME2), 'utf-8') mqtt_feedname3 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_IO_FEEDNAME3), 'utf-8') mqtt_feedname4 = bytes('{:s}/feeds/{:s}'.format(ADAFRUIT_USERNAME, ADAFRUIT_IO_FEEDNAME4), 'utf-8') def do_connect(): wlan = network.WLAN(network.STA_IF) wlan.active(True) if not wlan.isconnected(): print('connecting to network...') wlan.connect('NETGEAR83', 'dynami') while not wlan.isconnected(): pass print('network config:', wlan.ifconfig()) try: client.connect() except Exception as e: print('could not connect to MQTT server {}{}'.format(type(e).__name__, e)) sys.exit() def Send_Data(tTempf,tHum,tPool,tSolar) : client.publish(mqtt_feedname1,bytes(str(tTempf), 'utf-8'), qos=0) client.publish(mqtt_feedname2,bytes(str(tHum), 'utf-8'),qos=0) client.publish(mqtt_feedname3,bytes(str(tPool), 'utf-8'),qos=0) client.publish(mqtt_feedname4,bytes(str(tSolar), 'utf-8'),qos=0) def read_dht(): do_connect() sensor = dht.DHT11(Pin(5)) sleep(2) sensor.measure() temp = sensor.temperature() hum = sensor.humidity() tempf = temp * (9 / 5) + 32.0 # print('Temperature: %3.1f C' %temp) # print('Temperature: %3.1f F' %tempf) # print('Humidity: %3.1f %%' %hum)return return (str(tempf)), (str(hum)) def read_pool(): do_connect() # the device is on GPIO12 dat = machine.Pin(4) # create the onewire object ds = ds18x20.DS18X20(onewire.OneWire(dat)) # scan for devices on the bus roms = ds.scan() # print('found devices:', roms) # print('temperature:', end=' ') ds.convert_temp() time.sleep_ms(750) temp = ds.read_temp(roms[0]) pool = round((temp * 9 / 5) + 32.0, 1) return (str(pool)) def read_solar(): do_connect() # the device is on GPIO12 dat = machine.Pin(25) # create the onewire object ds = ds18x20.DS18X20(onewire.OneWire(dat)) # scan for devices on the bus roms = ds.scan() # print('found devices:', roms) # print('temperature:', end=' ') ds.convert_temp() time.sleep_ms(750) temp = ds.read_temp(roms[0]) solar = round((temp * 9 / 5) + 32.0, 1) return (str(solar)) def colect_Data(): tempf, hum, = read_dht() pool = read_pool() solar = read_solar() return tempf, hum, pool, solar def main(): while True: tempf, hum, pool, solar = colect_Data() #print("Solar") #print(str(solar)) #print("Pool") #print(str(pool)) #print("Hum") #print(str(hum)) #print("Tempf") #print(str(tempf)) if current_time - last_mesurement_time > MESUREMENT_INTERVAL: print('Temperature:',tempf,'Humidity',hum,) Send_Data(tempf,hum,pool,solar) last_mesurement_time = current_time main()
Hi.
What board are you using to run that code? ESP32 with MicroPython? Or are you using a Raspberry Pi?
Esp32 with MicroPython. It connect to my wifi but does not connect to Mqtt.
The last error code was the following
download ok exec(open('AdaFruit_io.py').read(),globals()) [0;32mI (5990) modsocket: Initializing[0m I (6020) wifi: wifi driver task: 3ffd2fc8, prio:23, stack:3584, core=0 [0;32mI (12345) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m [0;32mI (12345) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m I (12385) wifi: wifi firmware version: ac331d7 I (12385) wifi: config NVS flash: enabled I (12385) wifi: config nano formating: disabled I (12385) wifi: Init dynamic tx buffer num: 32 I (12385) wifi: Init data frame dynamic rx buffer num: 32 I (12395) wifi: Init management frame dynamic rx buffer num: 32 I (12395) wifi: Init management short buffer num: 32 I (12405) wifi: Init static rx buffer size: 1600 I (12405) wifi: Init static rx buffer num: 10 I (12415) wifi: Init dynamic rx buffer num: 32 [0;32mI (12515) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0[0m I (12515) wifi: mode : sta (3c:71:bf:83:c3:40) [0;32mI (12515) wifi: STA_START[0m connecting to network... I (12765) wifi: new:<3,0>, old:<1,0>, ap:<255,255>, sta:<3,0>, prof:1 I (13615) wifi: state: init -> auth (b0) I (13615) wifi: state: auth -> assoc (0) I (13625) wifi: state: assoc -> run (10) I (13655) wifi: connected with NETGEAR83, aid = 4, channel 3, BW20, bssid = 78:d2:94:b8:ed:16 I (13655) wifi: security type: 3, phy: bgn, rssi: -69 I (13665) wifi: pm start, type: 1 [0;32mI (13665) network: CONNECTED[0m I (13715) wifi: AP's beacon interval = 102400 us, DTIM period = 2 [0;32mI (16805) event: sta ip: 192.168.1.15, mask: 255.255.2551') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') network config: ('192.168.1.15', '255.255.255.0', '192.168.1.1', '192.168.1.1') could not connect to MQTT server OSError23
Hi Ray.
I’m not sure what’s wrong with your code.
We’ve built an example using the umqttsimple library.
Test the example and see if it works for your (enter your SSID, password, username and key).
If it works, then you should be able to modify it to publish to the topics you want. As an example, we’re just publishing on the temperature topic and the read_sensor function is just returning 1. You should replace with actual sensor readings.
Here’s the code: https://gist.github.com/sarasantos/201c48eae29d575f8ed889498e6a26c8
We’ve tested it and it works.
Regards,
Sara