ESP8266 - micropython global variable does not work

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

ESP8266 - micropython global variable does not work

Post by lazarvgd » Sun May 20, 2018 9:07 am

Hi there,

A few days ago I have flashed my ESP board and set up micropython. The first thing I have setup is temperature/humidity monitoring and led strip control over mqtt protocol. The problem for me was that I did not know whether am I leave led strip turned on or off, so I added flag(boolean) in order to track the led strip status. So, this sounded as a good solution for me,but I have found the problem, I have declared isLedOn variable as False at the beginning of the main.py file. Every time when I turn on or off led strip this variable gets updated with he appropriate value, but in main loop I am reading value False all the time, even though I have updated it with True value.

Please fin my code on pastebin and please take a look at it and see if I made any mistake during the coding.

https://pastebin.com/jmwqThJf

can somebody tell me where is the problem :?:

thanks

torwag
Posts: 220
Joined: Fri Dec 13, 2013 9:25 am

Re: ESP8266 - micropython global variable does not work

Post by torwag » Sun May 20, 2018 2:41 pm

Hi,

the problem is namespacing.

The isledOn variable is defined once in the root name space and in functions.
Thus, you actually have two different variables here, which simply have the same name.

What you want to do, is to create a global variable.
They are usually a bad idea, as they quickly can create all kind of funny problems. However, sometimes they are a quick solution, thus, it depends on your code and its complexity whether it is a good idea or not.

What you need.

Code: Select all

isledOn = False

def myfun():
   global isledOn #that is the part you are missing
   isledOn = True
Another method would be to hand over the status as function parameter

Code: Select all

isledOn = False

def myfun(status = isledOn):
    if status:
       # more code
    return status

isledOn = myfun()
or do it OOP style with get and set members of a class, to change and read the state.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP8266 - micropython global variable does not work

Post by pythoncoder » Mon May 21, 2018 6:32 am

torwag wrote:
Sun May 20, 2018 2:41 pm
...they quickly can create all kind of funny problems...
The worst of which is the one you correctly identified above ;)
Peter Hinch
Index to my micropython libraries.

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: ESP8266 - micropython global variable does not work

Post by lazarvgd » Mon May 21, 2018 7:31 am

[quote=torwag post_id=27751 time=1526827309 user_id=48]
Hi,

the problem is namespacing.

The isledOn variable is defined once in the root name space and in functions.
Thus, you actually have two different variables here, which simply have the same name.

What you want to do, is to create a global variable.
They are usually a bad idea, as they quickly can create all kind of funny problems. However, sometimes they are a quick solution, thus, it depends on your code and its complexity whether it is a good idea or not.

What you need.
[code]
isledOn = False

def myfun():
global isledOn #that is the part you are missing
isledOn = True
[/code]

Another method would be to hand over the status as function parameter

[code]
isledOn = False

def myfun(status = isledOn):
if status:
# more code
return status

isledOn = myfun()
[/code]

or do it OOP style with get and set members of a class, to change and read the state.
[/quote]

Hah, I am java dev. and really have hard time beating all these indentation, missing curly brackets and namespaceing thing.
So, I thought that I just can declare the variable at the beginning and later use ti as global variable(like I do i java when I have to remember flags).

Thanks for your time and effort buddy :)

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: ESP8266 - micropython global variable does not work

Post by lazarvgd » Mon May 21, 2018 2:51 pm

the easy ways was to add global keyword in front of isLedOn variables inside onMessage method.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP8266 - micropython global variable does not work

Post by pythoncoder » Tue May 22, 2018 7:54 am

lazarvgd wrote:
Mon May 21, 2018 7:31 am
Hah, I am java dev. and really have hard time beating all these indentation, missing curly brackets and namespaceing thing.
That was my initial reaction coming to Python from C/C++. To my surprise I quickly became a total convert to mandatory indentation. The way code looks is the way it behaves. Curly brackets + incorrect indentation = a pig.

Namespaces do take effort to grasp, I'll grant you. Also Python's pass by value/pass by reference conventions.
Peter Hinch
Index to my micropython libraries.

mr-engin3er
Posts: 3
Joined: Fri May 07, 2021 10:40 am

Re: ESP8266 - micropython global variable does not work

Post by mr-engin3er » Wed Jul 21, 2021 6:16 am

How can we use global variable on other module/files.
suppose I declare in start.py

Code: Select all

x = 'something'
want to use in another_file.py

Code: Select all

def foo():
    print(x)
Last edited by mr-engin3er on Wed Jul 21, 2021 9:11 am, edited 1 time in total.

lazarvgd
Posts: 39
Joined: Sun May 20, 2018 8:57 am

Re: ESP8266 - micropython global variable does not work

Post by lazarvgd » Wed Jul 21, 2021 6:19 am

mr-engin3er wrote:
Wed Jul 21, 2021 6:16 am
How can we use global variable on other module/files.
suppose I declare in start.py
x = 'something'

want to use in another_file.py
def foo():
print(x)
You need to import start.py file and then you can get access to the variable, as far as I remember. If I am wrong, please correct me.
Take a look at this https://stackoverflow.com/questions/145 ... other-file.

Post Reply