Page 1 of 1

ESP8266 - micropython global variable does not work

Posted: Sun May 20, 2018 9:07 am
by lazarvgd
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

Re: ESP8266 - micropython global variable does not work

Posted: Sun May 20, 2018 2:41 pm
by torwag
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.

Re: ESP8266 - micropython global variable does not work

Posted: Mon May 21, 2018 6:32 am
by pythoncoder
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 ;)

Re: ESP8266 - micropython global variable does not work

Posted: Mon May 21, 2018 7:31 am
by lazarvgd
[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 :)

Re: ESP8266 - micropython global variable does not work

Posted: Mon May 21, 2018 2:51 pm
by lazarvgd
the easy ways was to add global keyword in front of isLedOn variables inside onMessage method.

Re: ESP8266 - micropython global variable does not work

Posted: Tue May 22, 2018 7:54 am
by pythoncoder
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.