Featured Image - Common MicroPython Problems

Posted on

by

in

Common MicroPython Problems

Introduction

This post will show the common MicroPython errors and problems that I encounter while working with this platform and the solution that I think would fix the issue. I will keep updating this page as I continue encountering them and hopefully help you at the same time.

TypeError: ‘id’ argument required

Problem

Programming a machine.I2C() class like the below code will cause the exception above.

import machine
import bme280

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
bme = bme280.BME280(i2c=i2c)

Solution

The machine.I2C class API requires the id parameter to be passed. “The id identifies a particular I2C peripheral. Allowed values depend on the particular port/board

import machine
import bme280

i2c = machine.I2C(id=0, scl=machine.Pin(5), sda=machine.Pin(4))
bme = bme280.BME280(i2c=i2c)

OSError: [Errno 5] EIO

Problem

Using a Raspberry Pi Pico W and connecting to a BME280 sensor will cause the following error above to be displayed.

import machine
import bme280

i2c = machine.I2C(id=0, scl=machine.Pin(5), sda=machine.Pin(4))
bme = bme280.BME280(i2c=i2c)

Solution

First, check that your pin assignment is okay and appropriate to the SDA and SCL pin of your I2C sensor. In my case, when using a Raspberry Pi Pico W, the correct value should be the following when using the default I2C pins.

import machine
import bme280

i2c = machine.I2C(id=0, scl=machine.Pin(1), sda=machine.Pin(0))
bme = bme280.BME280(i2c=i2c)

If this still fails then check your wiring as there could be loose connections.

ValueError: bad SCL pin

Problem

The below code is causing the exception above in a Raspberry Pi Pico board. I am using the default I2C pins which are GPI0 and GPI1.

import machine
import bme280

i2c = machine.I2C(id=1, scl=machine.Pin(1), sda=machine.Pin(0))
bme = bme280.BME280(i2c=i2c)

print(bme.values)

Solution

Set the correct “id” parameter since I am using the hardware I2C pins then it should match the device peripheral bus which has the id of “zero” (e.g. 0).

import machine
import bme280

i2c = machine.I2C(id=0, scl=machine.Pin(1), sda=machine.Pin(0))
bme = bme280.BME280(i2c=i2c)

print(bme.values)

MQTTException: 2 – Bad socket read/write on client <client id>: Invalid arguments provided

Problem

When using umqtt.simple and trying to publish or subscribe to some MQTT topics using the Mosquitto broker than an error is shown in the MicroPython shell

Traceback (most recent call last):
  File "main.py", line 36, in <module>
  File "main.py", line 22, in main
  File "umqtt/simple.py", line 110, in connect
MQTTException: 2

If you take a look at Moquitto logs then you will see the following message displayed.

Bad socket read/write on client <Client ID>: Invalid arguments provided.

Solution

When creating a client

c = MQTTClient(CLIENT_ID, server)

Add the following option “keepalive

c = MQTTClient(CLIENT_ID, server, keepalive=60)

Error installing packages from Thonny IDE

Problem

While trying to install MicroPython packages in Thonny IDE, I kept encountering the following issues when using the upip package installer.

  • Error installing ‘micropython-umqtt.robust‘: memory allocation failed, allocating 32768 bytes, packages maybe partially installed.
  • Error installing ‘micropython-umqtt.simple‘: memory allocation failed, allocating 32768 bytes, packages maybe partially installed.
MicroPython Problem : Thonny IDE Error Installing Memory Allocation Failed

Or when trying to install using Thonny IDE Tools and then Manage Packages, I kept encountering the following issues.

Looking in indexes: http://127.0.0.1:36628
127.0.0.1 - - [31/Jan/2023 12:04:49] "GET /micropython-umqtt-simple/ HTTP/1.1" 200 -
Collecting micropython-umqtt.simple
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18637)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18639)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18641)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
  WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18643)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
  WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18645)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
  WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))': /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/
----------------------------------------
Exception occurred during processing of request from ('127.0.0.1', 18647)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 316, in _handle_request_noblock
    self.process_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 347, in process_request
    self.finish_request(request, client_address)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 204, in __init__
    super().__init__(request, client_address, server)
  File "C:\Program Files (x86)\Thonny\lib\socketserver.py", line 747, in __init__
    self.handle()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 432, in handle
    self.handle_one_request()
  File "C:\Program Files (x86)\Thonny\lib\http\server.py", line 420, in handle_one_request
    method()
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 211, in do_GET
    self._serve_file(*path.split("/"))
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 245, in _serve_file
    tweaked_bytes = self._tweak_file(dist_name, file_name, original_bytes)
  File "C:\Program Files (x86)\Thonny\lib\site-packages\thonny\vendored_libs\pipkin\proxy.py", line 312, in _tweak_file
    assert normalize_dist_name(wrapper_dir).startswith(normalize_dist_name(dist_name))
AssertionError
----------------------------------------
ERROR: Could not install packages due to an OSError: HTTPConnectionPool(host='127.0.0.1', port=36628): Max retries exceeded with url: /micropython-umqtt-simple/micropython-umqtt.simple-1.3.4.tar.gz/ (Caused by ProtocolError('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')))

Error Command '['C:\\Users\\DONSKY\\AppData\\Local\\pipkin\\cache\\workspaces\\1c7bfecf12769f9897e393af77815ec0\\Scripts\\python.exe', '-I', '-m', 'pip', '--no-color', '--disable-pip-version-check', '--trusted-host', '127.0.0.1', 'install', '--no-compile', '--use-pep517', '--upgrade-strategy', 'only-if-needed', 'micropython-umqtt.simple', '--index-url', 'http://127.0.0.1:36628']' returned non-zero exit status 1.

Solution

First, try to reboot your MicroPython device prior to installing any packages or libraries.

Next, verify if you have the latest MicroPython firmware installed on your microcontroller boards or devices.

Lastly, check if you have the latest Thonny IDE version.

If you like my post then please consider sharing this. Thanks!

3 responses to “Common MicroPython Problems”

  1. Micropyhon OSError: -1 umqtt | Markurion

    […] This gives me a bit to think cause i do not have keepalive value. On this website i found more about keepalive:https://www.donskytech.com/common-micropython-problems/ […]

  2. Ahmad Reza Avatar
    Ahmad Reza

    hello
    my read_sensor function return two value hum and temp
    if (time.time() – last_message) > message_interval:
    temp ,hum=read_sensor()
    print(‘hum’,hum)
    print(‘temp’,temp)
    after run:
    File “main.py”, line 88, in
    ValueError: too many values to unpack (expected 2)
    MicroPython v1.20.0 on 2023-04-26; ESP32 module with ESP32

    1. donsky Avatar
      donsky

      Can you check what is the actual type and output?
      Maybe type(read_sensor()) and then print(read_sensor())

Leave a Reply

Your email address will not be published. Required fields are marked *