Hi all,
I have a BIG problem which i'm really begging for help on.
I'm afraid this is still Python at this point, not uPython. I've got my Pico W sending data reliably and before i try to make it more fancy, I simply want to complete the data path life-cycle from there to my overall end Python script for my desired functionality.
I've got my MQTT code working in my RP4B. Not the way i wanted, but it's collecting three individual messages, and i *think* i've managed to associate each of these readings with global variables for use outside of the defined message variables (please tell me if i've not done this at all, because i clearly have no way of testing this due to the next issue.)
I'm trying to call in my global variables in the script where i want to use these from the MQTT message collecting script.
This is a copy of my MQTT script called "MQTT_PW_Vars.py":
Code: Select all
#!/usr/bin/env python
#!/bin/bash
#MQTT_PW_Vars.py
import time
import paho.mqtt.client as mqtt
broker = '192.168.0.51'
port = 1883
#keepalive = 120
mqttclient = "PiLogger"
client = mqtt.Client(mqttclient)
topic1 = "PicoW/OS_Temp"
topic2 = "PicoW/OS_Hum"
topic3 = "PicoW/Pico_Temp"
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(f"Connected to MQTT Broker with result: {rc}")
else:
print("Failed to connect to Broker, return code = ", rc)
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Unexpected disconnection!")
def on_message1(client, userdata, message):
print(f"Received: {(message.payload.decode('utf-8'))} from Topic: {message.topic}")
GVAR = message.payload.decode("utf-8")
global OS_Temp
OS_Temp = float(GVAR)
def on_message2(client, userdata, message):
print(f"Received: {(message.payload.decode('utf-8'))} from Topic: {message.topic}")
GVAR = message.payload.decode("utf-8")
global OS_Hum
OS_Hum = float(GVAR)
def on_message3(client, userdata, message):
print(f"Received: {(message.payload.decode('utf-8'))} from Topic: {message.topic}")
GVAR = message.payload.decode("utf-8")
global Pico_Temp
Pico_Temp = float(GVAR)
client.connect(broker, port)
client.on_connect = on_connect
client.on_disconnect = on_disconnect
time.sleep(3)
client.subscribe(topic1)
client.subscribe(topic2)
client.subscribe(topic3)
client.on_message = on_message1
client.on_message = on_message2
client.on_message = on_message3
time.sleep(2)
client.loop_forever()
This produces the following:
Connected to MQTT Broker with result: 0
Received: 25.2 from Topic: PicoW/Pico_Temp
Received: 57.3 from Topic: PicoW/OS_Hum
Received: 22.2 from Topic: PicoW/OS_Temp
Received: 25.2 from Topic: PicoW/Pico_Temp
Received: 57.3 from Topic: PicoW/OS_Hum
Received: 22.1 from Topic: PicoW/OS_Temp
When I use the following lines in my overall desired script, called "PicoW_Vars.py":
Code: Select all
from MQTT_PW_Vars.py import OS_Temp as PW_Temp
from MQTT_PW_Vars.py import OS_Hum as PW_Hum
from MQTT_PW_Vars.py import Pico_Temp as Pi_Temp
The above "MQTT_PW_Vars.py" code completely hijacks the "PicoW_Vars.py" programs actual programming for its own!?
I only want the three variables as a product of the MQTT scripted code, not the functionality to completely take over anything else i have written in my end script!
Please. I'm really confused. I'm not sure where else to ask for help.