beetle wrote: ↑Mon Aug 22, 2022 5:55 pm
Rissy,
Try to only use the on-message callback function to examine the message topic and to pass the data to another function appropriate to the message.
This is all i wanted to do in all fairness. I just couldn't move my GV made from the message payload outside of that function. It was trapped!? So that's why i had no choice but to load it up with writing my GV to a file instead. That's still literally all its doing though.
beetle wrote: ↑Mon Aug 22, 2022 5:55 pm
But thats an aside, does the following illustrate a way of using your pesky global variables.
In a file called module1.py put in the following code:
Code: Select all
# Set up a global varialbe and initialise it with a value
gVar = 0.0
# define a fuction that will set the global variable
def set_me_var():
global gVar
gVar = 22.23
in another file called module_main.py put in the following code:
Code: Select all
import module1
module1.set_me_var() # run the fuction in module1
print(module1.gVar) # print the global var in module1
Then run module_main.py and I hope all is clear.
So what you're saying is that the one thing i did wrong is not declare the GV at the beginning of my script outside of the function and assign it an arbitrary value of zero and then reiterate its existence as a global variable
AGAIN from within the function too?
OMG.
Why didn't you only bring in the global variable from module 1 using:
Code: Select all
from module1 import gVar gVar_from_module1
? or somthing to that effect? Instead of the whole module1.py?
Is that not a more accepted, more minimalist approach to passing global variables between scripts (you're calling them modules)?
Also, you said "module1.set_me_var() # run the fuction in module1". Does this run the function script where the GV is produced in module1.py? Why would you need to do this if that script is already running in parallel to the module_main.py script?
beetle wrote: ↑Mon Aug 22, 2022 5:55 pm
If your global vars are shared between many modules then create a file just for them e.g. my_naughty_globals.py and import this module into the other modules that may need to reference them. Thus a global var of 'rissy_pesky_one' set up in this shared module will be referenced in these modules by - 'my_naughty_globals.rissy_pesky_one' .
I must emphasise the naming of this file as 'my_naughty_globals' is
mandatory
Thy shall be done.
beetle wrote: ↑Mon Aug 22, 2022 5:55 pm
And a edit to remind that global variables and only global to the module in which they are defined. They are not global outside of the module thus you have to directly access them with the module.var way of accessing them, and you have to import the module as well.
I don't get what you said here?
My second RP4B and my second Pico W arrived today. I'm going to be also buying some other play things, like a small LCD screen, resistors, LEDs, potentiometer so that i can have a play with more real worldy type items. Once i've got my RP4B initialised and setup similar to my first one, I'll set up MQTT on there too, and bring in the same values from the first RP4B and have a go at manipulating my scripts more now that you've shown me where i went wrong. (thank you, finally).
Ultimately, I want to put my Pico W in the loft instead of my RP4B, so i'll be spending time inversing each of their current functionality by using these second ones here to do the testing with and leave my current monitoring system running the whole time until i'm happy to do the switcheroony dealy bopper.