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
ESP8266 - micropython global variable does not work
Re: ESP8266 - micropython global variable does not work
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.
Another method would be to hand over the status as function parameter
or do it OOP style with get and set members of a class, to change and read the state.
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
Code: Select all
isledOn = False
def myfun(status = isledOn):
if status:
# more code
return status
isledOn = myfun()
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: ESP8266 - micropython global variable does not work
The worst of which is the one you correctly identified above
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: ESP8266 - micropython global variable does not work
[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
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
the easy ways was to add global keyword in front of isLedOn variables inside onMessage method.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: ESP8266 - micropython global variable does not work
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.
Index to my micropython libraries.
-
- Posts: 3
- Joined: Fri May 07, 2021 10:40 am
Re: ESP8266 - micropython global variable does not work
How can we use global variable on other module/files.
suppose I declare in start.py
want to use in another_file.py
suppose I declare in start.py
Code: Select all
x = 'something'
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.
Re: ESP8266 - micropython global variable does not work
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.mr-engin3er wrote: ↑Wed Jul 21, 2021 6:16 amHow 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)
Take a look at this https://stackoverflow.com/questions/145 ... other-file.