Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.

Post by kpin404 » Mon Jun 21, 2021 12:17 pm

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.

User avatar
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.

Post by pythoncoder » Tue Jun 22, 2021 5:52 am

Please post a simple code sample which demonstrates the problem. Then someone may be able to comment.
Peter Hinch
Index to my micropython libraries.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.

Post by kpin404 » Tue Jun 22, 2021 6:22 am

pythoncoder wrote:
Tue Jun 22, 2021 5:52 am
Please post a simple code sample which demonstrates the problem. Then someone may be able to comment.
Thanks for your prompt reply to my issue. Here is the code please:

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) 
Here is the code execution error:

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

User avatar
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.

Post by pythoncoder » Wed Jun 23, 2021 6:33 am

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.

katesimon123
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.

Post by katesimon123 » Wed Jun 23, 2021 12:57 pm

Yeah and I think you also need to change your sever line too:

Code: Select all

SERVER =b'my.cloudmqtt.com'

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: Micropython: Unable to fix IndexError: bytes index out of range issue. Help to fix this Please.

Post by kpin404 » Fri Jun 25, 2021 5:07 am

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
>>> 

Post Reply