Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
testbed-software
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
Container Registry
Model registry
Operate
Environments
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Robert Hartung
testbed-software
Commits
69e94e0f
Commit
69e94e0f
authored
7 years ago
by
Robert Hartung
Browse files
Options
Downloads
Patches
Plain Diff
added example for controlling a chamber (untested)
parent
e75125c8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/chamber.py
+54
-0
54 additions, 0 deletions
examples/chamber.py
with
54 additions
and
0 deletions
examples/chamber.py
0 → 100644
+
54
−
0
View file @
69e94e0f
#!/usr/bin/env python3
import
paho.mqtt.client
as
paho
import
paho.mqtt.publish
as
publish
import
time
import
socket
import
struct
from
threading
import
Event
CHAMBER_NAME
=
"
lctc-c0X
"
TARGET_TEMPERATURE
=
10.0
DELTA
=
0.5
TIMEOUT
=
60.0
CHAMBER_TEMPERATURE_TOPIC
=
"
sht21/
"
+
CHAMBER_NAME
+
"
/temp
"
temperature_reached
=
Event
()
client
=
paho
.
Client
(
client_id
=
"
chamber_controller_
"
+
socket
.
gethostname
(),
clean_session
=
True
)
def
on_message
(
client
,
userdata
,
msg
):
if
msg
.
topic
==
CHAMBER_TEMPERATURE_TOPIC
:
temp
,
=
struct
.
unpack
(
"
d
"
,
msg
.
payload
)
if
abs
(
temp
-
TARGET_TEMPERATURE
)
<
DELTA
:
temperature_reached
.
set
()
else
:
print
(
"
[on_message] {} {}
"
.
format
(
msg
.
payload
,
msg
.
topic
)
)
def
on_disconnect
(
client
,
userdata
,
rc
):
print
(
"
[on_disconnect] {}
"
.
format
(
rc
))
def
on_connect
(
client
,
userdata
,
flags
,
rc
):
client
.
subscribe
(
CHAMBER_TEMPERATURE_TOPIC
,
qos
=
2
)
def
set_temp
(
temp
):
client
.
publish
(
"
chamber/
"
+
CHAMBER_NAME
+
"
/temp
"
,
struct
.
pack
(
"
d
"
,
temp
),
qos
=
2
)
if
__name__
==
"
__main__
"
:
client
.
connect
(
"
localhost
"
,
port
=
1883
,
keepalive
=
5
)
client
.
on_connect
=
on_connect
client
.
on_message
=
on_message
client
.
on_disconnect
=
on_disconnect
client
.
loop_start
()
while
True
:
# Wait until temperature has been reached
if
temperature_reached
.
wait
(
TIMEOUT
):
print
(
"
timeout reached.
"
)
# Do something here for 1 minute
time
.
sleep
(
60.0
)
# Increase target temperature
TARGET_TEMPERATURE
+=
1.0
set_temp
(
TARGET_TEMPERATURE
)
temperature_reached
.
clear
()
\ 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
register
or
sign in
to comment