Andrey Vetlugin
Andrey Vetlugin

Reputation: 1073

How to detect hardware type (ESP32 or ESP8266) in MicroPython?

How can I detect if my MicroPython script is running on ESP32 or ESP8266? I want to make it work on both platforms, but deep sleep requires different implementation depending on the hardware.

Upvotes: 0

Views: 936

Answers (1)

Andrey Vetlugin
Andrey Vetlugin

Reputation: 1073

You can use uos.uname().sysname to detect the hardware platform.

Here is an example script:

import uos

print(uos.uname())

sysname = uos.uname().sysname
if sysname == 'esp32':
    print('detected ESP32')
elif sysname == 'esp8266':
    print('detected ESP8266')
else:
    print('something else')

Demo script output on ESP8266:

$ ampy run detect.py
(sysname='esp8266', nodename='esp8266', release='2.2.0-dev(9422289)', version='v1.11-8-g48dcbbe60 on 2019-05-29', machine='ESP module with ESP8266')
detected ESP8266

Demo script output on ESP32:

$ ampy run detect.py
(sysname='esp32', nodename='esp32', release='1.11.0', version='v1.11 on 2019-05-29', machine='ESP32 module with ESP32')
detected ESP32

Upvotes: 4

Related Questions