Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Embedded Systems
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
Container 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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
my_awesome_code
Embedded Systems
Commits
fe77e80d
Commit
fe77e80d
authored
3 months ago
by
s84716
Browse files
Options
Downloads
Patches
Plain Diff
websocket code
parent
5d04ee47
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
websocket
+96
-0
96 additions, 0 deletions
websocket
with
96 additions
and
0 deletions
websocket
0 → 100644
+
96
−
0
View file @
fe77e80d
#!/usr/bin/python3
# Tornado web server
import
tornado.ioloop
import
tornado.options
import
tornado.web
import
tornado.websocket
# General functionalities
import
time
# Sensor libs
import
board
import
adafruit_dht
# sensor type is DHT22, 4 in D4 is the GPIO4 and *not* the actual pin number
dhtDevice
=
adafruit_dht
.
DHT22
(
board
.
D4
,
use_pulseio
=
False
)
from
tornado.options
import
define
,
options
define
(
"
port
"
,
default
=
8081
,
help
=
"
run on the given port
"
,
type
=
int
)
# HTML page to be returned by default
class
IndexHandler
(
tornado
.
web
.
RequestHandler
):
def
get
(
self
):
self
.
render
(
"
index.html
"
)
# Defining WebSocket Operations
class
WebSocketHandler
(
tornado
.
websocket
.
WebSocketHandler
):
# New incoming connection, remember clients for later replies
def
open
(
self
):
print
(
"
Incoming connection from: {}
"
.
format
(
self
.
request
.
connection
.
context
.
address
))
clients
.
append
(
self
)
# Message processing
def
on_message
(
self
,
message
):
print
(
"
Processing message:
"
+
message
)
# Helper variable
valueDisplayed
=
False
# repeat in case of failed measurements
while
(
not
valueDisplayed
):
try
:
# retrieve measured values
temperature
=
dhtDevice
.
temperature
humidity
=
dhtDevice
.
humidity
# convert to string
temperaturestr
=
"
{:.1f}
"
.
format
(
temperature
)
humiditystr
=
"
{:.1f}
"
.
format
(
humidity
)
# JSON-formatted data string
data
=
"
{
\"
temperature
\"
:
"
+
temperaturestr
+
"
,
\"
humidity
\"
:
"
+
humiditystr
+
"
}
"
# reply to clients
for
c
in
clients
:
c
.
write_message
(
data
)
print
(
"
reply to:
"
+
str
(
c
.
request
.
connection
.
context
.
address
))
# success
valueDisplayed
=
True
time
.
sleep
(
2.0
)
except
RuntimeError
as
error
:
# In case of error - repeat measurement
print
(
error
.
args
[
0
])
time
.
sleep
(
2.0
)
continue
except
Exception
as
error
:
dhtDevice
.
exit
()
raise
error
def
on_close
(
self
):
print
(
"
connection closed: {}
"
.
format
(
self
.
request
.
remote_ip
))
clients
.
remove
(
self
)
if
__name__
==
"
__main__
"
:
try
:
# clients of this webserver
clients
=
[]
# setting up Tornado
tornado
.
options
.
parse_command_line
()
app
=
tornado
.
web
.
Application
(
handlers
=
[(
r
"
/ws
"
,
WebSocketHandler
),(
r
"
/
"
,
IndexHandler
)])
httpServer
=
tornado
.
httpserver
.
HTTPServer
(
app
)
httpServer
.
listen
(
options
.
port
)
print
(
"
Listening on port:
"
+
str
(
options
.
port
))
tornado
.
ioloop
.
IOLoop
.
instance
().
start
()
except
KeyboardInterrupt
:
tornado
.
ioloop
.
IOLoop
.
instance
().
stop
()
\ 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