-
Notifications
You must be signed in to change notification settings - Fork 1
/
victron_broadcastpower.py
34 lines (25 loc) · 1.33 KB
/
victron_broadcastpower.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*
from __future__ import print_function
from __future__ import division
from victron_nps_utils import *
import socket
import time
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
# Enable port reusage so we will be able to run multiple clients and servers on single (host, port).
# Do not use socket.SO_REUSEADDR except you using linux(kernel<3.9): goto https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ for more information.
# For linux hosts all sockets that want to share the same address and port combination must belong to processes that share the same effective user ID!
# So, on linux(kernel>=3.9) you have to run multiple servers and clients under one user to share the same (host, port).
# Thanks to @stevenreddie
#server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# Enable broadcasting mode
server.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Set a timeout so the socket does not block
# indefinitely when trying to receive data.
server.settimeout(0.2)
def sendUdpBroadcast(message):
server.sendto(str.encode(message), ('<broadcast>', 2323))
for i in range(0,5):
ac_pout=int(loaddata2('com.victronenergy.system','/Ac/Grid/L1/Power'))
message = "P1: "+str(ac_pout)+" W.\n"
sendUdpBroadcast(message)
time.sleep(10)