I/O thread seem to be causing resets


#1

I’m using a NodeMCU V3. I wrote a small class to control a LED in a thread. However when I use it, it seems to be causing random resets. I tried using a thread lock but resets continued to occur.

Has anyone else seen this? Is manipulating hardware in a thread a bad idea? How can it be accomplished safely?


#2

hi @lncc63
Could you please post the code here?


#3

Thank you for the reply. The code is a bit long so I’ve replace some of it with “//…”.

# FILE: myio.py

import threading

iolock = threading.Lock()

class LED( threading.Thread ):
    """ Controls an LED """
    
    def __init__( self, pin, enabled=False, timeON=1000,timeOFF=1000 ):
        self.pin = pin
        self.enabled = enabled
        self.timeON = timeON
        self.timeOFF = timeOFF
        iolock.acquire()
        pinMode( self.pin, OUTPUT )
        iolock.release()
        self.e = threading.Event()
        try:
            threading.Thread.__init__( self )
            self.start()
        except Exception as e:
            print( "ERROR: ", e )
            sleep( 1000 )
        
    def on( self ):
        iolock.acquire()
        self.enabled = True
        iolock.release()
        
    def off( self ):
        iolock.acquire()
        self.enabled = False
        digitalWrite( self.pin, LOW )   # turn the LED OFF by setting the voltage LOW
        iolock.release()
         
    def blink( self, timeON=2000,timeOFF=2000 ):
        iolock.acquire()
        self.timeON = timeON
        self.timeOFF = timeOFF
        iolock.release()
        
    def run( self ):
        while True:
            if self.enabled:
                iolock.acquire()
                digitalWrite( self.pin, HIGH )  # turn the LED ON by setting the voltage HIGH
                iolock.release()
                self.e.wait( self.timeON )
                iolock.acquire()
                digitalWrite( self.pin, LOW )   # turn the LED OFF by setting the voltage LOW
                iolock.release()
                self.e.wait( self.timeOFF )

# FILE: main.py

//...
import threading
import myio
//...

lock = threading.Lock()

def Thread_Serve( name ):
    myio.iolock.acquire()
    print("Entering operating mode ...")
    sleep( 1000 )
    
    try:
        sock = socket.socket()
        sleep(10)
        sock.bind(80)
        sock.listen()
    except Exception as e:
        print("ERROR - Openning socket: ", e)
        while True:
            sleep( 1000 )
    
    while True:
        try:
            # Type in your browser the board ip!
            print("Waiting for connection...")
            
            myio.iolock.release()
            clientsock,addr = sock.accept()
            myio.iolock.acquire()
            
            print("Incoming connection from",addr)
            //...
        except Exception as e:
            print("ooops, something wrong:",e)
        
        myio.iolock.release()


streams.serial()
wifi_driver.auto_init()
sleep( 2000 )
print("Booting ...")

LED_Activity = myio.LED( D1, True, 1000, 750 )
LED_Blue = myio.LED( D2, True, 750, 500 )
//...
if wifi.is_linked() == False:
    LED_Activity.blink( 500, 500 )
    try:
        setup.get_wifi_credentials()
    except Exception as e:
        print( "  Failed: ", e )
    config.save()
        
    print( "Rebooting ...")
    sleep( 3000 )
    mcu.reset()

thread( Thread_Serve, "Main" )

while True:
    myio.iolock.acquire()
    print( random(0,100) )
    myio.iolock.release()
    sleep( 3000 )