Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
IC-Interface Control
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
João Santos
IC-Interface Control
Commits
4130a551
Commit
4130a551
authored
Oct 09, 2023
by
João Santos
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update main.py
parent
cb93ce97
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
30 deletions
+22
-30
main.py
main.py
+22
-30
No files found.
main.py
View file @
4130a551
...
@@ -10,7 +10,7 @@ import signal
...
@@ -10,7 +10,7 @@ import signal
import
sys
import
sys
host
=
'
localhost
'
host
=
'
192.168.0.113
'
port
=
8086
port
=
8086
dbname
=
'telegraf'
dbname
=
'telegraf'
...
@@ -41,14 +41,6 @@ NHRs = {
...
@@ -41,14 +41,6 @@ NHRs = {
# "9430" : interface.getNhr9430()[0],
# "9430" : interface.getNhr9430()[0],
}
}
def
signal_handler
(
sig
,
frame
):
print
(
'You pressed Ctrl+C!'
)
sys
.
exit
(
0
)
signal
.
signal
(
signal
.
SIGINT
,
signal_handler
)
print
(
'Press Ctrl+C'
)
signal
.
pause
()
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
if
rc
==
0
and
client
.
is_connected
():
if
rc
==
0
and
client
.
is_connected
():
print
(
"Connected to MQTT Broker!"
)
print
(
"Connected to MQTT Broker!"
)
...
@@ -96,9 +88,14 @@ def on_disconnect(client, userdata, rc):
...
@@ -96,9 +88,14 @@ def on_disconnect(client, userdata, rc):
FLAG_EXIT
=
True
FLAG_EXIT
=
True
def
on_message
(
client
,
userdata
,
message
):
def
on_message
_set
(
client
,
userdata
,
message
):
print
(
f'Received `
{
message
.
payload
.
decode
()
}
` from `
{
message
.
topic
}
` topic'
)
print
(
f'Received `
{
message
.
payload
.
decode
()
}
` from `
{
message
.
topic
}
` topic'
)
prefix
,
selector
,
fn
=
message
.
topic
.
split
(
'/'
)
splitted
=
message
.
topic
.
split
(
'/'
)
if
len
(
splitted
)
!=
3
:
print
(
f"not enough values o unpack. expected 3, got
{
len
(
splitted
)
}
"
)
return
prefix
,
selector
,
fn
=
splitted
# message = json.loads(message.payload.decode())
# message = json.loads(message.payload.decode())
# selector = message['selector']
# selector = message['selector']
...
@@ -128,31 +125,22 @@ def on_message(client, userdata, message):
...
@@ -128,31 +125,22 @@ def on_message(client, userdata, message):
def
connect_mqtt
():
def
connect_mqtt
():
client
=
mqtt_client
.
Client
(
CLIENT_ID
)
client
=
mqtt_client
.
Client
(
CLIENT_ID
)
client
.
on_connect
=
on_connect
client
.
on_connect
=
on_connect
client
.
on_message
=
on_message
client
.
on_message
=
on_message
_set
client
.
connect
(
BROKER
,
PORT
,
keepalive
=
3
)
client
.
connect
(
BROKER
,
PORT
,
keepalive
=
3
)
client
.
on_disconnect
=
on_disconnect
client
.
on_disconnect
=
on_disconnect
return
client
return
client
def
publish
(
client
):
def
on_message_get
():
msg_count
=
0
while
not
FLAG_EXIT
:
while
not
FLAG_EXIT
:
msg_dict
=
{
nhr
=
NHRs
.
get
(
"9410"
,
False
)
'msg'
:
msg_count
values
=
{
'NHR'
:
nhr
,
'voltage'
:
nhr
.
getVoltage
(),
}
}
msg
=
json
.
dumps
(
msg_dict
)
# values = json.dumps(values)
if
not
client
.
is_connected
():
logging
.
error
(
"publish: MQTT client is not connected!"
)
write_influxdb
(
'voltage'
,
)
time
.
sleep
(
1
)
continue
result
=
client
.
publish
(
TOPIC_PUB
,
msg
)
# result: [0, 1]
status
=
result
[
0
]
if
status
==
0
:
print
(
f'Send `
{
msg
}
` to topic `
{
TOPIC_SUB
}
`'
)
else
:
print
(
f'Failed to send message to topic
{
TOPIC_SUB
}
'
)
msg_count
+=
1
time
.
sleep
(
1
)
time
.
sleep
(
1
)
def
run
():
def
run
():
...
@@ -161,8 +149,12 @@ def run():
...
@@ -161,8 +149,12 @@ def run():
client
=
connect_mqtt
()
client
=
connect_mqtt
()
client
.
loop_start
()
client
.
loop_start
()
time
.
sleep
(
1000
)
time
.
sleep
(
1000
)
print
(
NHRs
.
getVoltage
())
if
client
.
is_connected
():
if
client
.
is_connected
():
publish
(
client
)
# on_message_get()
print
(
"connect"
)
else
:
else
:
client
.
loop_stop
()
client
.
loop_stop
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment