Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Hello there! I am trying to connect MQTT client application *umqtt.simple.py* running on ESP32 development board with Micropython code to an external MQTT broker. But its throwing **IndexError: bytes index out of range issue**. error every time, when start to execute the program. I have been tried and reviewed each and every posts here and there to fix this issue,, but didn't get any success at all to fix this **IndexError: bytes index out of range issue** and very disappointed how to fix this? Any help here on this to fix this issue will be greatly appreciated. Thanking you.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Please post a simple code sample which demonstrates the problem. Then someone may be able to comment.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Thanks for your prompt reply to my issue. Here is the code please:pythoncoder wrote: ↑Tue Jun 22, 2021 5:52 amPlease post a simple code sample which demonstrates the problem. Then someone may be able to comment.
Code: Select all
from time import sleep
from umqtt.simple import MQTTClient
from machine import Pin
from dht import DHT22
SERVER ='my.cloudmqtt.com'
CLIENT_ID='ESP32_TH'
PORT=1883
TOPIC=b'temp_humidity'
client=MQTTClient(CLIENT_ID,SERVER,PORT,"cloudmqttusername","cloudmqqttpassword")
client.connect()
sensor=DHT22(Pin(13,Pin.IN,Pin.PULL_UP))
while True:
try:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
if isinstance(t, float) and isinstance(h, float):
msg = (b'{0:3.1f},{1:3.1f}'.format(t,h))
client.publish(TOPIC,msg)
print(msg)
else:
print('invalid sensor value')
except OSError:
print ('failed to read sensor')
sleep(4)
Code: Select all
>>> client.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/umqtt/simple.py", line 97, in connect
IndexError: bytes index out of range
>>>
Code: Select all
94 def set_last_will(self, topic, msg, retain=False, qos=0):
95
96 assert 0 <= qos <= 2
97
98 assert topic
99
100 self.lw_topic = topic
101
102 self.lw_msg = msg
103
104 self.lw_qos = qos
105
106 self.lw_retain = retain
107
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
It's failing on connect. I think client_id needs to be a bytes object rather than a string. Try:
Code: Select all
CLIENT_ID=b'ESP32_TH'
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 15
- Joined: Mon Jun 14, 2021 12:49 am
Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Yeah and I think you also need to change your sever line too:
Code: Select all
SERVER =b'my.cloudmqtt.com'
Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.
Thanks for keep supporting, but i am sorry to say that error still persist. Please have check with the code.
Code: Select all
from time import sleep
from umqtt.simple import MQTTClient
from machine import Pin
from dht import DHT22
SERVER=b'mqtt.tago.io'
CLIENT_ID=b'85e.............................fff3'
PORT=1883
TOPIC='temp_humidity'
client=MQTTClient(CLIENT_ID,SERVER,PORT,"Token","85e.........................fff3")
client.connect()
sensor=DHT22(Pin(14,Pin.IN,Pin.PULL_UP))
while True:
try:
sensor.measure()
t = sensor.temperature()
h = sensor.humidity()
if isinstance(t, float) and isinstance(h, float):
msg = ('{0:3.1f},{1:3.1f}'.format(t,h))
client.publish(TOPIC,msg)
print(msg)
else:
print('invalid sensor value')
except OSError:
print ('failed to read sensor')
sleep(4)
Code: Select all
>>> client = MQTTClient('85e..................fff3','mqtt.....io')
>>> client.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/umqtt/simple.py", line 195, in connect
IndexError: bytes index out of range
>>>