Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
python-mqtt-gpio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Alexander Wellbrock
python-mqtt-gpio
Commits
bb3107d3
Commit
bb3107d3
authored
Jan 2, 2020
by
BenjiU
Browse files
Options
Downloads
Patches
Plain Diff
fixes for interrupts
parent
63c96028
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pi_mqtt_gpio/server.py
+33
-29
33 additions, 29 deletions
pi_mqtt_gpio/server.py
pytest.ini
+2
-2
2 additions, 2 deletions
pytest.ini
with
35 additions
and
31 deletions
pi_mqtt_gpio/server.py
+
33
−
29
View file @
bb3107d3
...
@@ -117,7 +117,7 @@ def output_by_name(output_name):
...
@@ -117,7 +117,7 @@ def output_by_name(output_name):
_LOG
.
warning
(
"
No output found with name of %r
"
,
output_name
)
_LOG
.
warning
(
"
No output found with name of %r
"
,
output_name
)
def
set_pin
(
output_config
,
value
):
def
set_pin
(
topic_prefix
,
output_config
,
value
):
"""
"""
Sets the output pin to a new value and publishes it on MQTT.
Sets the output pin to a new value and publishes it on MQTT.
:param output_config: The output configuration
:param output_config: The output configuration
...
@@ -144,7 +144,7 @@ def set_pin(output_config, value):
...
@@ -144,7 +144,7 @@ def set_pin(output_config, value):
)
)
def
handle_set
(
msg
):
def
handle_set
(
topic_prefix
,
msg
):
"""
"""
Handles an incoming
'
set
'
MQTT message.
Handles an incoming
'
set
'
MQTT message.
:param msg: The incoming MQTT message
:param msg: The incoming MQTT message
...
@@ -165,10 +165,10 @@ def handle_set(msg):
...
@@ -165,10 +165,10 @@ def handle_set(msg):
output_config
[
"
off_payload
"
],
output_config
[
"
off_payload
"
],
)
)
return
return
set_pin
(
output_config
,
payload
==
output_config
[
"
on_payload
"
])
set_pin
(
topic_prefix
,
output_config
,
payload
==
output_config
[
"
on_payload
"
])
def
handle_set_ms
(
msg
,
value
):
def
handle_set_ms
(
topic_prefix
,
msg
,
value
):
"""
"""
Handles an incoming
'
set_<on/off>_ms
'
MQTT message.
Handles an incoming
'
set_<on/off>_ms
'
MQTT message.
:param msg: The incoming MQTT message
:param msg: The incoming MQTT message
...
@@ -260,6 +260,7 @@ def init_mqtt(config, digital_outputs):
...
@@ -260,6 +260,7 @@ def init_mqtt(config, digital_outputs):
:return: Connected and initialised MQTT client
:return: Connected and initialised MQTT client
:rtype: paho.mqtt.client.Client
:rtype: paho.mqtt.client.Client
"""
"""
global
topic_prefix
topic_prefix
=
config
[
"
topic_prefix
"
]
topic_prefix
=
config
[
"
topic_prefix
"
]
protocol
=
mqtt
.
MQTTv311
protocol
=
mqtt
.
MQTTv311
if
config
[
"
protocol
"
]
==
"
3.1
"
:
if
config
[
"
protocol
"
]
==
"
3.1
"
:
...
@@ -368,11 +369,11 @@ def init_mqtt(config, digital_outputs):
...
@@ -368,11 +369,11 @@ def init_mqtt(config, digital_outputs):
try
:
try
:
_LOG
.
info
(
"
Received message on topic %r: %r
"
,
msg
.
topic
,
msg
.
payload
)
_LOG
.
info
(
"
Received message on topic %r: %r
"
,
msg
.
topic
,
msg
.
payload
)
if
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_TOPIC
):
if
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_TOPIC
):
handle_set
(
msg
)
handle_set
(
topic_prefix
,
msg
)
elif
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_ON_MS_TOPIC
):
elif
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_ON_MS_TOPIC
):
handle_set_ms
(
msg
,
True
)
handle_set_ms
(
topic_prefix
,
msg
,
True
)
elif
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_OFF_MS_TOPIC
):
elif
msg
.
topic
.
endswith
(
"
/%s
"
%
SET_OFF_MS_TOPIC
):
handle_set_ms
(
msg
,
False
)
handle_set_ms
(
topic_prefix
,
msg
,
False
)
else
:
else
:
_LOG
.
warning
(
"
Unhandled topic %r.
"
,
msg
.
topic
)
_LOG
.
warning
(
"
Unhandled topic %r.
"
,
msg
.
topic
)
except
InvalidPayload
as
exc
:
except
InvalidPayload
as
exc
:
...
@@ -522,8 +523,8 @@ def validate_sensor_input_config(sens_conf):
...
@@ -522,8 +523,8 @@ def validate_sensor_input_config(sens_conf):
def
initialise_sensor_input
(
sens_conf
,
sensor
):
def
initialise_sensor_input
(
sens_conf
,
sensor
):
"""
"""
Initialises sensor input.
Initialises sensor input.
:param
in
_conf: Sensor config
:param
sens
_conf: Sensor config
:type
in
_conf: dict
:type
sens
_conf: dict
:param sensor: Instance of GenericSensor to use
:param sensor: Instance of GenericSensor to use
:type sensor: pi_mqtt_gpio.modules.GenericSensor
:type sensor: pi_mqtt_gpio.modules.GenericSensor
:return: None
:return: None
...
@@ -612,18 +613,21 @@ def gpio_interrupt_callback(module, pin, value):
...
@@ -612,18 +613,21 @@ def gpio_interrupt_callback(module, pin, value):
module
,
module
,
exc
exc
)
)
_LOG
.
info
(
"
Input %r state changed to %r
"
,
in_conf
[
"
name
"
],
True
if
value
else
False
)
# publish each value
# publish each value
client
.
publish
(
client
.
publish
(
"
%s/%s/%s
"
%
(
"
%s/%s/%s
"
%
(
topic_prefix
,
OUTPUT_TOPIC
,
in_conf
[
"
name
"
]
topic_prefix
,
OUTPUT_TOPIC
,
in_conf
[
"
name
"
]
),
),
payload
=
value
,
payload
=
in_conf
[
"
on_payload
"
if
value
else
"
off_payload
"
]
,
retain
=
in_conf
[
"
retain
"
]
retain
=
in_conf
[
"
retain
"
]
)
)
def
main
(
args
):
def
main
(
args
):
global
digital_outputs
global
client
with
open
(
args
.
config
)
as
f
:
with
open
(
args
.
config
)
as
f
:
config
=
yaml
.
safe_load
(
f
)
config
=
yaml
.
safe_load
(
f
)
validator
=
ConfigValidator
(
CONFIG_SCHEMA
)
validator
=
ConfigValidator
(
CONFIG_SCHEMA
)
...
@@ -671,12 +675,11 @@ def main(args):
...
@@ -671,12 +675,11 @@ def main(args):
sys
.
exit
(
1
)
sys
.
exit
(
1
)
for
in_conf
in
digital_inputs
:
for
in_conf
in
digital_inputs
:
print
(
in_conf
)
initialise_digital_input
(
in_conf
,
GPIO_MODULES
[
in_conf
[
"
module
"
]])
initialise_digital_input
(
in_conf
,
GPIO_MODULES
[
in_conf
[
"
module
"
]])
in_conf
[
"
startup_read
"
]
=
False
LAST_STATES
[
in_conf
[
"
name
"
]]
=
None
LAST_STATES
[
in_conf
[
"
name
"
]]
=
None
for
out_conf
in
digital_outputs
:
for
out_conf
in
digital_outputs
:
print
(
out_conf
)
initialise_digital_output
(
out_conf
,
GPIO_MODULES
[
out_conf
[
"
module
"
]])
initialise_digital_output
(
out_conf
,
GPIO_MODULES
[
out_conf
[
"
module
"
]])
for
sens_conf
in
sensor_inputs
:
for
sens_conf
in
sensor_inputs
:
...
@@ -722,6 +725,7 @@ def main(args):
...
@@ -722,6 +725,7 @@ def main(args):
while
True
:
while
True
:
for
in_conf
in
digital_inputs
:
for
in_conf
in
digital_inputs
:
if
(
in_conf
[
"
interrupt
"
]
==
"
none
"
or
in_conf
[
"
startup_read
"
]
==
False
):
gpio
=
GPIO_MODULES
[
in_conf
[
"
module
"
]]
gpio
=
GPIO_MODULES
[
in_conf
[
"
module
"
]]
state
=
bool
(
gpio
.
get_pin
(
in_conf
[
"
pin
"
]))
state
=
bool
(
gpio
.
get_pin
(
in_conf
[
"
pin
"
]))
sleep
(
0.01
)
sleep
(
0.01
)
...
@@ -737,12 +741,12 @@ def main(args):
...
@@ -737,12 +741,12 @@ def main(args):
retain
=
in_conf
[
"
retain
"
],
retain
=
in_conf
[
"
retain
"
],
)
)
LAST_STATES
[
in_conf
[
"
name
"
]]
=
state
LAST_STATES
[
in_conf
[
"
name
"
]]
=
state
in_conf
[
"
startup_read
"
]
=
True
scheduler
.
loop
()
scheduler
.
loop
()
sleep
(
0.01
)
sleep
(
0.01
)
except
KeyboardInterrupt
:
except
KeyboardInterrupt
:
print
(
""
)
print
(
""
)
finally
:
finally
:
client
.
publish
(
client
.
publish
(
"
%s/%s
"
%
(
topic_prefix
,
config
[
"
mqtt
"
][
"
status_topic
"
]),
"
%s/%s
"
%
(
topic_prefix
,
config
[
"
mqtt
"
][
"
status_topic
"
]),
config
[
"
mqtt
"
][
"
status_payload_stopped
"
],
config
[
"
mqtt
"
][
"
status_payload_stopped
"
],
...
...
This diff is collapsed.
Click to expand it.
pytest.ini
+
2
−
2
View file @
bb3107d3
[pytest]
[pytest]
markers
=
markers
=
slow:
marks
tests
as
slow
(deselect
with
'-m
"not
slow"')
slow:
marks
tests
as
slow
(deselect
with
'-m
"not
slow"')
hw_raspberrypi
hw_raspberrypi:
marks
tests,
that
require
special
hardware
modification
for
rasperry
pi
mqtt
mqtt:
marks
tests,
that
require
a
mqtt
server
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment