I am currently following the micropython book content along with a NodeMCU ESP8266.
Again this is a fantastic course you’ve been providing us (I’ve got almost all of them 🙂 )
I just have a couple of questions and remark.
First remark is that I was struggling with simply changing the state of the built in led and after searching other resources on the internet I learned that the GPIO2 is working the other way around compared to others GPIO meaning that by default it is initialized with status 1 instead of 0 because this is needed when the ESP is booting up. Consequently you have to swap all .on() and .off (or 0/1 or True/False) statements whenever you want to turn the led on or off.
Maybe this is something you should point out in the book as it might be very confusing for beginners (and no luck this is the first experiment, not a good start if you don’t understand this 🙂 )
Then I have a couple of questions for you 🙂
- I am currently on the DHT11/DHT22 tutorial and I am just wondering whether it would be possible to auto refresh the web page displaying the 2 graphs with Temp and Hum…? This would be more elegant than having to manually refresh the page.
- I am still struggling understanding the variable scope in Python (I am much more familiar with C) . At one point you mentioned a variable must be global so that we can use it everywhere in the code but I also see that when you define a (not global) variable in boot.py (like ‘led’ in the Web server examples) it is also usable in main.py therefore I don’t really understand the concept of variable scope
Thanks a lot!
Yes, you are right about the built-in LED on the ESP8266. That info should definitely be added to the eBook.
1) The easiest way to do that without having to change a lot of things is by adding the following tag to the head of the HTML file:
<meta http-equiv="refresh" content="30">
This will automatically refresh the web page every 30 seconds.
A more elegant solution would be using server-sent events to update the readings without the need to refresh, but I haven’t searched how to do this in MicroPython.
2) I’m not familiar with any software that does that. If you find something, please share it.
3) Why do you say the variable defined in boot.py is not global? You can think of the main.py file as an extension of the boot.py file. As if the instructions were on the same file. I think the following link explains briefly and simply python’s variables scope: https://www.w3schools.com/python/python_scope.asp
I hope this helps.
Thank you very much for your time answering my questions!
Regarding 3) ok now I think I understand. You must define a variable with ‘global’ only if the variable is part of a function and you want to make visible globally, throughout your code, right?
I understand then if the variable is defined outside a function then it is global.
For me the tricky point to understand with Python compared to C is when you have multiple files making up your project.
If I undertand correcly, any variable declared outside a function in any .py file will be seen as a global variable in any others .py files part of the project, right?
First question: Yes, that’s true about global variables.
Sedond question: That’s true for micropython with main.py and boot.py. With other python files, you would need to import the file as a module to access the variables.