the server is starting without error, but webpage isn't loaded.
here is the code of the server:
Code: Select all
#! /usr/bin/python
import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import RPi.GPIO as GPIO
import datetime
import json
import time
from time import sleep # lets us have a delay
#Initialize Raspberry PI GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT, initial=1) # LED
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_UP) # BUTTON
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
self.connected = True
print("New Connection.")
self.write_message("LED = "+ str(GPIO.input(17)))
self.write_message("Connection was Opened.")
self.timeout_loop()
def on_message(self, message):
print("Incoming message:", message)
if message == "led on":
GPIO.output(17, False)
self.write_message("LED = "+ str(GPIO.input(17)))
elif message == "led off":
GPIO.output(17, True)
self.write_message("LED = "+ str(GPIO.input(17)))
def on_close(self):
self.connected = False
print("Connection was closed.")
def timeout_loop(self):
if self.connected:
if GPIO.input(4) == 0:
print("Button pressed")
self.write_message("pressed button")
tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=.1), self.timeout_loop)
application = tornado.web.Application([
(r'/mycode', WSHandler),
])
if __name__ == "__main__":
try:
GPIO.output(17, True)
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)
main_loop = tornado.ioloop.IOLoop.instance()
print("Tornado Server started")
main_loop.start()
except:
print("Exception triggered - Tornado Server stopped.")
GPIO.cleanup()
#End of Program
Code Webform:
Code: Select all
<html>
<head>
<title> Art1 - Websocket </title>
<meta charset="UTF-8">
<script src="jquery-1.11.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
</style>
<script>
var ws = new WebSocket("ws://192.168.1.131:8888/mycode");
ws.onopen = function(evt) {
$("#log").text("Opening the websocket...");
}
ws.onmessage = function(evt) {
$("#log").text(evt.data);
var n = evt.data.search("=");
LEDs = parseInt((evt.data).substring(n+2));
if(((evt.data).substring(0, n-1))== "LED"){
if(!LEDs){
$("#led1_on").hide(); $("#led1_on_build").hide();
$("#led1_off").show(); $("#led1_off_build").show();
}
else{
$("#led1_on").show(); $("#led1_on_build").show();
$("#led1_off").hide(); $("#led1_off_build").hide();
}
if(!LEDs){
$("#t1_on").hide(); $("#t1_off").show();
}
else{
$("#t1_on").show(); $("#t1_off").hide();
}
}
}
ws.onclose = function(evt) {
$("#log").text("Connection Closed...");
}
function test(y) {
ws.send(y);
}
</script>
</head>
<body>
<h2>Play with Relay's <i><br><small><small>(Python & Tornado)</small></small></i></h2>
<p><img id = "led1_on_bild" src="ON.jpg" width="92" height="146"/></p>
<p><button id = "led1_on" style="color: #aaa" onclick="test('led1 off');">LED 1 is   ON</button></p>
<p><img id = "led1_off_bild" src="OFF.jpg" width="92" height="146"/></p>
<p><button id = "led1_off" style="color: #aaa" onclick="test('led1 on');">LED 1 is OFF</button></p>
<b>Drukknop:</b>
<p><img id = "t1_on" src="button.jpg" width="30" height="30"/></p>
<p><img id = "t1_off" src="button_off.jpg" width="30" height="30"/></p>
<b>Message:</b>
<div id="log" style="overflow: auto; position: absolute ;overflow-x: hidden;
width: 350px; height: 30px; background-color: #c2c7af">...</div>
<br><br>
</body>
</html>