Issues with python boolean

udbytossen
Posts: 2
Joined: Mon Jul 14, 2025 4:07 pm

Issues with python boolean

Postby udbytossen » Mon Jul 14, 2025 4:53 pm

First of all - Hi to Anybody in the forum.
I'm a later beginner with ESP32-S3 - so bought a package ESP32-S3 Wroom Freenove.

Which I'm planning to learn to program python, and have som fun while, I've bought the Millinium Falcon in Lego, and want to do some thing like light in cockpitt - canons firing - Flying with light in the back etc as a little hoppy

I want it logon to my network, check the clock, and if myhourly number is between 9 & 20 I want it to run a sequence.
So how can I get a boolean to change it value by all - and sorry for my programming (could possibly be done more nice or original, but I just want to have fun with my Lego :D :D
I'm ended up with the forllowing as Code for a little start:

Code: Select all

import time
import network

ssidRouter     = 'XXX'
passwordRouter = 'Not_D3clared'
millinium = bool()

# our connectiown method
#######################
def STA_Setup(ssidRouter,passwordRouter):
    print("Setup start")
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to',ssidRouter)
        sta_if.active(True)
        sta_if.connect(ssidRouter,passwordRouter)
        while not sta_if.isconnected():
            pass
    print('Connected, IP address:', sta_if.ifconfig())
    current_time = time.localtime()
    hour = current_time[3]
    print('local timehour is : ', hour)
    if ( hour >= 9 ):
        print('Clock above 9')
        if ( hour <= 20 ):
            print('And below 20')
            millinium = True
        else:
            print('And above 20')
            millinium = False
    else:
        print('And below 9')
        millinium = False
  
try:
    STA_Setup(ssidRouter,passwordRouter)
    print('Started out connecting')
    print('Millinium Statement is : ', millinium)
    
    while millinium:
        print('Execution of script')

except:
    sta_if.disconnect()
# EOF
But from this - I'm not getting it to work as a boolean in this - since my output is not changing the boolean millinium as I want it to be

Code: Select all

>>> %Run -c $EDITOR_CONTENT

MPY: soft reboot
Setup start
Connected, IP address: ('192.168.0.125', '255.255.255.0', '192.168.0.1', '192.168.0.1')
local timehour is :  18
Clock above 9
And below 20
Started out connecting
Millinium Statement is :  False
>>> 
What am I doing wrong - since I'm not changing the bool at all here as my intension.

Thanks in Advance for any input to this question?

U

Xuxin
Espressif staff
Espressif staff
Posts: 94
Joined: Thu Sep 22, 2022 3:35 am

Re: Issues with python boolean

Postby Xuxin » Tue Jul 15, 2025 5:39 am

Hi and welcome to the world of ESP32 and MicroPython – and your LEGO Millennium Falcon project sounds awesome! 😄

You're on the right track with your code, but the issue you're facing is due to Python's variable scope rules.

In your code, you defined:

Code: Select all

millinium = bool()
This creates a global variable. But inside your STA_Setup() function, you wrote:

Code: Select all

millinium = True
This creates a new local variable (only visible inside the function), which means it doesn't actually update the global one you're checking later.

To tell Python that you want to modify the global millinium variable from within the function, add this line at the start of your function:

Code: Select all

def STA_Setup(ssidRouter, passwordRouter):
    global millinium
    ...

udbytossen
Posts: 2
Joined: Mon Jul 14, 2025 4:07 pm

Re: Issues with python boolean

Postby udbytossen » Tue Jul 22, 2025 1:47 pm

Thanks you so much for both.

I do have another question.
I have my ESP32-S3 commected toUSB-C Cable for power.
How should my try/while/catch look like - creating a loop based on time.
I would like my script only to run in certain hours - lets say between 9 - 20 as shown in the script
When the millinium is true - the script is running, and when the milllinium is False - the scrips just exits (where I would like it to sleep for X minutes and then run again - in a loop and then run agaiin

TIA
U

Who is online

Users browsing this forum: No registered users and 0 guests