20ms is pretty fast but doable…
Either you need a tcp connection that stays open and you just send more data, or you can use udp.
UDP does not gurantee delivery, especially if going over the internet, but if your data is for display anyway, a lost value may be preferable to a lagged value.
I made a small demo in python
import socket
import thread
import time
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
clients = []
class SimpleChat(WebSocket):
def handleMessage(self):
for client in clients:
if client != self:
client.sendMessage(self.address[0] + u' - ' + self.data)
def handleConnected(self):
print self.address, 'connected'
for client in clients:
client.sendMessage(self.address[0] + u' - connected')
clients.append(self)
def handleClose(self):
clients.remove(self)
print self.address, 'closed'
for client in clients:
client.sendMessage(self.address[0] + u' - disconnected')
def udplisten():
UDPSock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
listen_addr = ("",5000)
UDPSock.bind(listen_addr)
print 'UDP listen done'
while True:
data,addr = UDPSock.recvfrom(1024)
for client in clients:
client.sendMessage(data.decode());
print data.strip(),addr
thread.start_new_thread( udplisten, () )
server = SimpleWebSocketServer('', 8000, SimpleChat)
server.serveforever()
And a HTML page to show it
<!DOCTYPE html>
<meta charset="utf-8" />
<title>WebSocket Test</title>
<script language="javascript" type="text/javascript">
function init()
{
document.myform.url.value = "ws://188.166.166.132:8000/"
document.myform.inputtext.value = "Hello World!"
document.myform.disconnectButton.disabled = true;
}
function doConnect()
{
websocket = new WebSocket(document.myform.url.value);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
writeToScreen("connected\n");
document.myform.connectButton.disabled = true;
document.myform.disconnectButton.disabled = false;
}
function onClose(evt)
{
writeToScreen("disconnected\n");
document.myform.connectButton.disabled = false;
document.myform.disconnectButton.disabled = true;
}
function onMessage(evt)
{
writeToScreen("response: " + evt.data + '\n');
}
function onError(evt)
{
writeToScreen('error: ' + evt.data + '\n');
websocket.close();
document.myform.connectButton.disabled = false;
document.myform.disconnectButton.disabled = true;
}
function doSend(message)
{
writeToScreen("sent: " + message + '\n');
websocket.send(message);
}
function writeToScreen(message)
{
document.myform.outputtext.value += message
document.myform.outputtext.scrollTop = document.myform.outputtext.scrollHeight;
}
window.addEventListener("load", init, false);
function sendText() {
doSend( document.myform.inputtext.value );
}
function clearText() {
document.myform.outputtext.value = "";
}
function doDisconnect() {
websocket.close();
}
</script>
<div id="output"></div>
<form name="myform">
<p>
<textarea name="outputtext" rows="20" cols="50"></textarea>
</p>
<p style="display:none">
<textarea name="inputtext" cols="50"></textarea>
</p>
<p>
<textarea name="url" cols="50"></textarea>
</p>
<p>
<input type="button" name=sendButton value="Send" onClick="sendText();" style="display:none">
<input type="button" name=clearButton value="Clear" onClick="clearText();">
<input type="button" name=disconnectButton value="Disconnect" onClick="doDisconnect();">
<input type="button" name=connectButton value="Connect" onClick="doConnect();">
</p>
</form>
</html>
Photon script
UDP Udp;
IPAddress remoteIP(188, 166, 166, 132);
char buffer[10];
void setup() {
Particle.publish("Connected ...");
Udp.begin(5000);
for(int i=0; i<100; i++) {
//buffer[0] = 48+i; //Ascii value 48 = 0
itoa(i, buffer, 10);
Udp.sendPacket(buffer, sizeof(buffer), remoteIP, 5000);
delay(20); //delay 20ms
}
}
I tested it over the internet with 100 packages and while the log can keep up, 20ms is very fast to be making a graph
The server used in the code above are still running if you fancy a demo…