Search code examples
esp8266micropythonos.system

Working on ESP8266 flashed with micropython firmware. I need help figuring out an issue with code when tested via <mperemote>


Just to summarise my issue I've been trying to clear the screen of a text that is displayed on a built in OLED display from my ESP8266EX board. I have uploaded the script to the board that includes the parameters and classes required to make the screen work so that part is fine. My goal is quite simple, I´m trying to load a main.py script that when restarting or powering up the board a text shows for around 3 seconds then is cleared and only a blinking dot remains waiting for any input of the user. However, when I import the 'os' library, call the 'system' module and input the 'cls' command, the terminal returns an error that says that is not defined. Find below my main script and the error output:

from machine import Pin, SoftI2C
import SSD1306Lib
import os
import time

# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(14), sda=Pin(12))

# Especification of OLED pixel dimensions and class inheritance 
oled_width = 128
oled_height = 64

oled = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled2 = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)

oled.text('Hola amigo!', 0, 0)
oled.text('ESP8266EX', 0, 10)
oled.text('Micropython', 0, 20)
oled2.text('.', 0, 30)
        
oled.show()
time.sleep(3)

os.system('cls')

oled2.show()

And the error output from mpremote running on cmd:

# ESP8266 Pin assignment
i2c = SoftI2C(scl=Pin(14), sda=Pin(12))

# Especification of OLED pixel dimensions and class inheritance
oled_width = 128
oled_height = 64

oled = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)
oled2 = SSD1306Lib.SSD1306_I2C(oled_width, oled_height, i2c)

oled.text('Hola amigo!', 0, 0)
oled.text('ESP8266EX', 0, 10)
oled.text('Micropython', 0, 20)
oled2.text('.', 0, 30)

oled.show()
time.sleep(3)

os.system('cls')

Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'system'

Tried using different clearing strategies including the click.clear() function but no success. I am beginner at programming and microcontrollers, so I am learning from discussions and trial/error


Solution

  • You're very close. You just need a few adjustments.

    1. You don't need to declare the second ssd1306 (oled2).
    2. You have to use oled.fill(0) to clear the screen as mentioned in the other answer.
    3. I'm not sure about import SSD1306Lib. On my ESP it's just ssd1306 with no Lib at the end. I installed it with mpremote mip install ssd1306.

    The code below should do what you want provided all the pin assignments are correct. I added a couple lines that will verify the ssd1306 is found on the i2c bus.

    I tested on an ESP32-C3 and it works. It should work just as well on an 8266.

    from machine import Pin, SoftI2C
    from ssd1306 import SSD1306_I2C
    from time import sleep
    
    # ESP8266 Pin assignment
    i2c = SoftI2C(scl=Pin(14), sda=Pin(12))
    if 0x3C not in i2c.scan():
        raise RuntimeError("SSD1306 I2C display not found.")
    
    # Especification of OLED pixel dimensions and class inheritance 
    oled_width = 128
    oled_height = 64
    
    oled = SSD1306_I2C(oled_width, oled_height, i2c)
    oled.fill(0)  # Clear the screen to color 0 (which is black)
    oled.text('Hola amigo!', 0, 0)
    oled.text('ESP8266EX', 0, 10)
    oled.text('Micropython', 0, 20)
    oled.show()
    
    sleep(3)
    oled.fill(0)  # Clear the screen
    oled.text('.', 0, 30)
    oled.show()