mpp-solar-8048-automation's People
mpp-solar-8048-automation's Issues
Better Code reuseable command_loop
import paho.mqtt.client as mqttClient
import time
import os
Define the devices (assuming device and port names are the same)
DEVICE_NAMES = [
"/dev/hidraw0",
"/dev/hidraw1",
"/dev/ttyUSB0"
]
NUM_DEVICES = len(DEVICE_NAMES)
def execute_command(device, command):
print(f"EXEC {device}")
full_str_cmd = f"mpp-solar -p {device} -P PI30 -c {command}"
os.system(full_str_cmd)
time.sleep(0.1)
os.system(full_str_cmd)
def on_message(client, userdata, message):
print("Message received: ", message.payload)
print("Message Topic: ", message.topic)
if message.topic.startswith("homeassistant/sensor/inverter"):
device_num = int(message.topic.split("_")[-1]) - 1
if 0 <= device_num < NUM_DEVICES:
device_name = DEVICE_NAMES[device_num]
execute_command(device_name, message.payload.decode())
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to broker")
global Connected
Connected = True
client.on_message = on_message
for device_num in range(1, NUM_DEVICES + 1):
client.subscribe(f"homeassistant/sensor/inverter_{device_num}")
else:
print("Connection failed")
Connected = False
broker_address = "192.168.1.102"
port = 1883
user = "sonoff"
password = "sonoff"
client = mqttClient.Client("Python", clean_session=False)
client.username_pw_set(user, password=password)
client.on_connect = on_connect
client.on_message = on_message
is_mqtt_connected = False
while not is_mqtt_connected:
try:
client.connect(broker_address, port=port)
is_mqtt_connected = True
except:
print("Cannot connect to MQTT broker")
time.sleep(1)
client.loop_forever()
while not Connected:
time.sleep(0.1)
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Exiting")
client.disconnect()
client.loop_stop()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.