Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import webiopi
- import time
- import socket
- GPIO = webiopi.GPIO
- server = webiopi.Server(port=8000, login="pi", password="raspberry")
- def connect():
- print "pressed connect"
- global myclient, address
- try:
- # TCP server
- serverTest = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #server open a socket
- serverTest.bind(("192.168.137.2", 5000)) # socket = ip addres + port
- serverTest.listen(5) # server can listen to 5 client
- print "TCP server listening on port 5000"
- myclient, address = serverTest.accept() # when a client want a connection, the server will accept
- print "Connected to ", address # print ip addres of the client
- # confirm connection
- myclient.send('0') # send 0 to the client to comfirm itself
- print "test"
- except:
- print "Somethin went wrong"
- def schrijven():
- data = ""
- data = myclient.recv(512)
- print data
- print "geschreven"
- myclient.send(data)
- return data
- def send():
- print "This is a test"
- server.addMacro(schrijven)
- server.addMacro(send)
- server.addMacro(connect)
- webiopi.runLoop()
- server.stop()
- <html>
- <head>
- <link rel="stylesheet" type="text/css" href="Style.css">
- <script src="jquery-1.11.0.min.js"></script>
- <script type="text/javascript" src="/webiopi.js"></script>
- </head>
- <body>
- <script>
- var t
- function readInterval() {
- t = setInterval(function(){
- webiopi().callMacro("schrijven", [], update);
- }, 1000);
- var update = function(macro, args, response) {
- var answer = response;
- $('#chatbox').append("geschreven: " + answer + "<br/>");
- }
- }
- function stopInterval() {
- alert("test");
- clearInterval(t);
- }
- function connect() {
- webiopi().callMacro("connect");
- }
- function send() {
- webiopi().callMacro("send");
- }
- </script>
- <h1>Pi Chatbox</h1>
- <div id="chatbox"></div>
- <input id="send" type="text" name="message"><br>
- <button id="btnSend" onClick="connect()" type="button">Connect</button>
- <button id="btnConnect" onClick="readInterval()" type="button">Recv</button>
- <button id="btnStopInterval" onClick="stopInterval()" type="button">STOP</button>
- </body>
- </html>
- # TCP client
- import socket
- client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- client.connect(("192.168.137.2", 5000))
- # connect with server
- data = client.recv(512)
- if data<>"0": # see its the correct server
- print "unexpected reply from the server, aborting"
- print data
- client.send("q")
- client.close()
- exit()
- #communication
- while 1:
- data = raw_input ("Send q to stop server:")
- client.send(data)
- if data == "q" :
- client.close()
- data = client.recv(512)
- print "received:" , data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement