SHARE
TWEET

Untitled

a guest May 11th, 2014 139 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import webiopi
  2. import time
  3. import socket
  4. GPIO = webiopi.GPIO
  5. server = webiopi.Server(port=8000, login="pi", password="raspberry")
  6.  
  7. def connect():
  8.     print "pressed connect"
  9.     global myclient, address
  10.  
  11.     try:
  12.         # TCP server
  13.         serverTest = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #server open a socket
  14.         serverTest.bind(("192.168.137.2", 5000))    # socket = ip addres + port
  15.         serverTest.listen(5)                        # server can listen to 5 client
  16.         print "TCP server listening on port 5000"
  17.         myclient, address = serverTest.accept() # when a client want a connection, the server will accept
  18.         print "Connected to ", address      # print ip addres of the client
  19.         # confirm connection
  20.         myclient.send('0')                  # send 0 to the client to comfirm itself
  21.         print "test"
  22.     except:
  23.         print "Somethin went wrong"
  24.  
  25. def schrijven():
  26.     data = ""
  27.     data = myclient.recv(512)
  28.     print data
  29.     print "geschreven"
  30.     myclient.send(data)
  31.     return data
  32.  
  33. def send():
  34.     print "This is a test"
  35.  
  36. server.addMacro(schrijven)
  37. server.addMacro(send)
  38. server.addMacro(connect)
  39.  
  40. webiopi.runLoop()
  41.  
  42. server.stop()
  43.        
  44. <html>
  45. <head>
  46. <link rel="stylesheet" type="text/css" href="Style.css">
  47. <script src="jquery-1.11.0.min.js"></script>
  48. <script type="text/javascript" src="/webiopi.js"></script>
  49. </head>
  50. <body>
  51. <script>
  52. var t
  53.  
  54. function readInterval() {
  55.         t = setInterval(function(){
  56.             webiopi().callMacro("schrijven", [], update);
  57.         }, 1000);
  58.  
  59.         var update = function(macro, args, response) {
  60.             var answer = response;
  61.  
  62.             $('#chatbox').append("geschreven: " + answer + "<br/>");
  63.         }
  64.     }
  65.  
  66. function stopInterval() {
  67.     alert("test");
  68.     clearInterval(t);
  69. }
  70.  
  71. function connect() {
  72.     webiopi().callMacro("connect");
  73. }
  74. function send() {
  75.     webiopi().callMacro("send");
  76. }
  77. </script>  
  78. <h1>Pi Chatbox</h1>
  79. <div id="chatbox"></div>
  80. <input id="send" type="text" name="message"><br>
  81. <button id="btnSend" onClick="connect()" type="button">Connect</button>
  82. <button id="btnConnect" onClick="readInterval()" type="button">Recv</button>
  83. <button id="btnStopInterval" onClick="stopInterval()" type="button">STOP</button>
  84. </body>
  85. </html>
  86.        
  87. # TCP client
  88. import socket
  89. client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  90. client.connect(("192.168.137.2", 5000))
  91. # connect with server
  92. data = client.recv(512)
  93. if data<>"0":       # see its the correct server
  94.     print "unexpected reply from the server, aborting"
  95.     print data
  96.     client.send("q")
  97.     client.close()
  98.     exit()
  99. #communication
  100. while 1:
  101.     data = raw_input ("Send q to stop server:")
  102.     client.send(data)
  103.     if data == "q" :
  104.         client.close()
  105.     data = client.recv(512)
  106.     print "received:" , data
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top