Bokeh-Server: Terima UDP dengan x axis datetime

From OnnoWiki
Jump to navigation Jump to search

client.py

python script untuk testing server bokeh


import sys
import time
import socket
import random

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('192.168.0.100', 8888)
sock.connect(server_address)

try:
    for i in xrange(100000):
        m0 = random.random()
        message = "%1.4f" % m0  
        print >>sys.stderr, 'sending ' , message
        sock.sendall(message)
        time.sleep(0.5) 

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

Arduino Program

/*
 UDPSendAnalogInput:

 created 27 Nov 2015
 by Onno W. Purbo 

 This code is in the public domain.
*/

#include <SPI.h>   // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 0, 3);
unsigned int localPort = 8888; // local port to listen on

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
IPAddress remoteIP(192,168,0,100);
unsigned int remotePort = 8888;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
} 

void loop() {
  Udp.beginPacket(remoteIP, remotePort);
  int sensorReading = analogRead(0);
  Udp.print(sensorReading);
  Udp.write("\n");
  Serial.println(sensorReading);
  Udp.endPacket(); 
  delay(1000);
}

Server

Untuk bokeh versi baru

bokeh serve app_script.py --address=0.0.0.0
bokeh serve udp-server.py --address=192.168.0.80

Akan keluar

DEBUG:bokeh.server.tornado:Allowed Host headers: ['localhost:5006']
DEBUG:bokeh.server.tornado:These host origins can connect to the websocket: ['localhost:5006']
DEBUG:bokeh.server.tornado:Patterns are: [('/udp-server/?', <class 'bokeh.server.views.doc_handler.DocHandler'>, {'application_context': <bokeh.server.application_context.ApplicationContext object at 0x7f9e6b139850>, 'bokeh_websocket_path': '/udp-server/ws'}), ('/udp-server/ws', <class 'bokeh.server.views.ws.WSHandler'>, {'application_context': <bokeh.server.application_context.ApplicationContext object at 0x7f9e6b139850>, 'bokeh_websocket_path': '/udp-server/ws'}), ('/udp-server/autoload.js', <class 'bokeh.server.views.autoload_js_handler.AutoloadJsHandler'>, {'application_context': <bokeh.server.application_context.ApplicationContext object at 0x7f9e6b139850>, 'bokeh_websocket_path': '/udp-server/ws'}), ('/static/(.*)', <class 'bokeh.server.views.static_handler.StaticHandler'>)]
INFO:bokeh.command.subcommands.serve:Starting Bokeh server on port 5006 address 192.168.0.80 with applications at paths ['/udp-server']



Untuk bokeh versi lama

bokeh-server --ip 192.168.0.100
bokeh-server --ip 192.168.0.80

Akan keluar

Bokeh Server Configuration
==========================
python version : 2.7.11
bokeh version  : 0.10.0
listening      : 192.168.0.100:5006
backend        : memory
python options : debug:OFF, verbose:OFF, filter-logs:OFF, multi-user:OFF
js options     : debugjs:OFF

udp-server.py

import sys
import time
import datetime
import socket
from bokeh.plotting import figure, output_server, show, cursession

output_server("raw",url='http://192.168.0.100:5006')

# Visualization workflow
rx = [0]
x = [0]
n = datetime.datetime.now()
x = ['%d.%d'%(n.minute*60+n.second,n.microsecond)]
p = figure(x_axis_type="datetime")
p.line(x, rx, name='raw')

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind the socket to the port
server_address = ('192.168.0.80', 8888)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

show(p)

renderer = p.select(dict(name="raw"))
ds = renderer[0].data_source

while True:
     data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
     print data

     if len(data) == 0:
         break

     rx += [data]
     n  = datetime.datetime.now()
     x  += ['%d.%d'%(n.minute*60+n.second,n.microsecond)]

     ds.data["x"] = x
     ds.data["rmag_x"] = rx
     cursession().store_objects(ds)

     time.sleep(0.01)

Jalankan

python udp-server.py