Step 1: Set up D3 area


In [1]:
from IPython.core.display import display, HTML
from string import Template
import pandas as pd
import json, random

In [6]:
HTML('<script src="lib/d3/d3.min.js"></script>')
# HTML('<script src="https://d3js.org/d3.v4.min.js"></script>')


Out[6]:

In [7]:
html_template = Template('''
<svg id="graph-div"></div>
<script> $js_text </script>
''')

In [8]:
js_text_template = Template('''
var data = $data;

var barHeight = 5;
var barWidth = 3;
var width = 800 * barWidth;

var x = d3.scale.linear()
        .domain([0, 1000])
        .range(["black", "white"]);

var chart = d3.select('#graph-div')
    .attr('width', width)
    .attr('height', 800);

function update() {    
    var row = chart.selectAll('.row')
        .data(data)
        .enter()
        .append('g')
        .attr('class', 'row')
        .attr('transform', function(d, i){
            return 'translate(0, ' + barHeight * i + ')';
        });

    var bar = row.selectAll('.bar')
        .data(function(d) { return d; })
        .enter()
        .append('g')
        .attr('class', "bar")
        .attr('transform', function(d, i){
            return 'translate(' + barWidth * i + ', 0)'
        })
        .append('rect')
        .attr('width', barWidth)
        .attr('height', barHeight)
        .attr('fill', function(d) {
            return x(d);
        });
}

update();
''')

In [9]:
data = [[]];
js_text = js_text_template.substitute({'data': json.dumps(data)})
HTML(html_template.substitute({'js_text': js_text}))


Out[9]:

OK, the D3 area is set up

Now we'll focus on live updating. A manual test first.


In [6]:
js_text_template_2 = Template('''
data = $data;
update();
''')

In [7]:
# data = [2,2,5,0,1]
tests = [[500,500,500,500,1000,1000,1000,1000], [500,500,500,500,1000,1000,1000,1000]]

def update_graph(data):
    js_text = js_text_template_2.substitute({'data': json.dumps(tests)})
    display(HTML('<script>' + js_text + '</script>'))

update_graph(tests)


Step 2: Now use MQTT to update the graph

Now for the fun stuff. Using the update_graph(data) function set up above. Each group of data, separated by "start" messages, is saved in the tests array. As new data comes in, the live data graph is updated to show data from the last test. All data is neatly saved inside the tests array for replotting and further analysis later.

Using the CloudMQTT free Cat plan:


In [116]:
import paho.mqtt.client as mqtt

data = []
tests = [[]]

update_graph(tests)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("/outTopic")

def on_message(client, userdata, msg):
    global data, tests
    try:
        msg_json = json.loads(msg.payload)
    except:
        print "Error"
        print(msg.topic+" "+str(msg.payload))
        return
    if msg_json['type'] == "BREAK" and msg_json['label'] == "LOOP":  # and msg_json['value'] == "END":
        tests.append(data)
        data = []
        update_graph(tests)
    if msg_json['type'] == "BINARY" and msg_json['label'] == "A0":
        data.append(int(msg_json['value']))
        tests[-1] = data
        update_graph(tests)

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set("zettlmtm", "VOUbRcmhjffA")
client.connect("m11.cloudmqtt.com", 19280, 60)
client.loop_start()
# Make sure to call client.loop_stop() later


Connected with result code 0
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world
Error
/outTopic hello world

In [115]:
client.loop_stop()

In [117]:
print tests


[[], [686, 791, 863, 856, 900, 896, 896, 918, 885, 909, 918, 889, 919, 896, 909, 909, 877, 916, 902, 902, 924, 892, 911, 903, 887, 909, 925, 926, 917, 895, 913, 872, 889, 919, 879, 916, 901, 885, 908, 876, 913, 902, 877, 921, 895, 893, 912, 921, 893, 925, 887, 906, 901, 873, 922, 894, 903, 918, 889, 917, 899, 891, 906, 893, 918, 908, 884, 915, 886, 906, 904, 869, 922, 902, 908, 912, 873], [662, 819, 858, 882, 915, 883, 919, 903, 886, 914, 883, 916, 910, 883, 925, 900, 901, 908, 873, 917, 909, 902, 920, 886, 918, 904, 882, 924, 894, 913, 910, 883, 920, 904, 909, 911, 870, 913, 900, 888, 916, 887, 917, 916, 890, 920, 887, 902, 909, 878, 921, 903, 893, 917, 889, 910, 905, 881, 911, 882, 909, 902, 881, 921, 900, 912, 929, 901, 915, 902, 889, 916, 880, 914, 906, 885, 915], [662, 819, 858, 882, 915, 883, 919, 903, 886, 914, 883, 916, 910, 883, 925, 900, 901, 908, 873, 917, 909, 902, 920, 886, 918, 904, 882, 924, 894, 913, 910, 883, 920, 904, 909, 911, 870, 913, 900, 888, 916, 887, 917, 916, 890, 920, 887, 902, 909, 878, 921, 903, 893, 917, 889, 910, 905, 881, 911, 882, 909, 902, 881, 921, 900, 912, 929, 901, 915, 902, 889, 916, 880, 914, 906, 885, 915], [912, 911, 881, 923, 910, 905, 921, 892, 918, 923, 896, 922, 903, 907, 918, 891, 921, 900, 892, 921, 890, 919, 923, 900, 929, 901, 914, 919, 891, 928, 900, 894, 917, 888, 912, 905, 893, 925, 897, 922, 920, 886, 928, 916, 915, 920, 884, 921, 909, 901, 914, 883, 920, 916, 891, 917, 886, 901, 901, 876, 911, 892, 902, 924, 891, 921, 908, 891, 912, 885, 924, 920, 893, 921, 895, 906, 916], [923, 895, 920, 901, 903, 920, 898, 923, 907, 911, 930, 892, 924, 922, 897, 927, 898, 914, 917, 889, 918, 911, 909, 911, 878, 918, 904, 889, 920, 893, 911, 908, 890, 923, 895, 904, 908, 880, 912, 898, 898, 922, 889, 913, 899, 885, 911, 888, 916, 917, 893, 927, 910, 908, 911, 877, 925, 908, 908, 921, 884, 908, 905, 884, 920, 900, 915, 918, 887, 919, 907, 904, 922, 887, 919, 907, 891], [923, 895, 920, 901, 903, 920, 898, 923, 907, 911, 930, 892, 924, 922, 897, 927, 898, 914, 917, 889, 918, 911, 909, 911, 878, 918, 904, 889, 920, 893, 911, 908, 890, 923, 895, 904, 908, 880, 912, 898, 898, 922, 889, 913, 899, 885, 911, 888, 916, 917, 893, 927, 910, 908, 911, 877, 925, 908, 908, 921, 884, 908, 905, 884, 920, 900, 915, 918, 887, 919, 907, 904, 922, 887, 919, 907, 891], [885, 905, 900, 877, 911, 898, 907, 917, 886, 926, 920, 909, 921, 888, 914, 909, 891, 926, 895, 916, 916, 896, 917, 897, 906, 911, 878, 922, 909, 904, 920, 889, 923, 917, 895, 912, 892, 910, 915, 891, 921, 912, 905, 923, 891, 917, 925, 911, 927, 894, 917, 915, 891, 924, 898, 908, 918, 888, 923, 907, 901, 912, 880, 919, 908, 889, 911, 887, 914, 915, 889, 927, 909, 909, 927, 894, 924], [918, 906, 910, 924, 891, 910, 910, 899, 920, 898, 918, 915, 892, 933, 901, 905, 912, 882, 911, 905, 902, 919, 893, 921, 915, 891, 918, 892, 908, 917, 893, 931, 909, 907, 921, 890, 927, 916, 907, 923, 889, 921, 922, 891, 909, 886, 903, 917, 884, 916, 899, 901, 912, 884, 923, 916, 895, 917, 893, 912, 906, 888, 927, 908, 914, 932, 901, 921, 903, 890, 918, 886, 915, 909, 893, 926, 897], [918, 906, 910, 924, 891, 910, 910, 899, 920, 898, 918, 915, 892, 933, 901, 905, 912, 882, 911, 905, 902, 919, 893, 921, 915, 891, 918, 892, 908, 917, 893, 931, 909, 907, 921, 890, 927, 916, 907, 923, 889, 921, 922, 891, 909, 886, 903, 917, 884, 916, 899, 901, 912, 884, 923, 916, 895, 917, 893, 912, 906, 888, 927, 908, 914, 932, 901, 921, 903, 890, 918, 886, 915, 909, 893, 926, 897], [1147, 1052, 951, 976, 926, 920, 916, 889, 1002, 996, 970, 979, 923, 928, 901, 888, 921, 893, 915, 916, 892, 914, 898, 901, 914, 885, 919, 898, 890, 922, 893, 925, 919, 901, 929, 904, 912, 914, 894, 955, 919, 900, 919, 888, 916, 914, 907, 926, 893, 924, 928, 904, 928, 902, 915, 927, 899, 926, 906, 905, 919, 906, 924, 916, 894, 914, 887, 917, 917, 891, 922, 901, 907, 924, 889, 931, 928], [1055, 1003, 997, 943, 973, 950, 918, 936, 902, 924, 926, 900, 928, 901, 920, 921, 891, 926, 909, 900, 925, 896, 915, 909, 893, 920, 893, 922, 918, 902, 928, 907, 916, 930, 897, 931, 921, 908, 917, 896, 919, 909, 891, 914, 891, 911, 910, 886, 926, 909, 917, 922, 892, 920, 900, 923, 938, 916, 926, 923, 898, 927, 901, 917, 911, 884, 919, 908, 908, 915, 895, 915, 904, 887, 919, 891, 918], [1055, 1003, 997, 943, 973, 950, 918, 936, 902, 924, 926, 900, 928, 901, 920, 921, 891, 926, 909, 900, 925, 896, 915, 909, 893, 920, 893, 922, 918, 902, 928, 907, 916, 930, 897, 931, 921, 908, 917, 896, 919, 909, 891, 914, 891, 911, 910, 886, 926, 909, 917, 922, 892, 920, 900, 923, 938, 916, 926, 923, 898, 927, 901, 917, 911, 884, 919, 908, 908, 915, 895, 915, 904, 887, 919, 891, 918], [371, 589, 740, 784, 801, 863, 845, 895, 883, 848, 902, 861, 877, 888, 836, 891, 874, 862, 895, 841, 888, 874, 846, 890, 851, 887, 891, 849, 902, 870, 868, 892, 853, 893, 874, 845, 901, 855, 880, 882, 848, 903, 869, 874, 889, 851, 904, 881, 867, 899, 857, 891, 885, 854, 896, 851, 889, 891, 849, 886, 856, 858, 887, 842, 893, 869, 847, 894, 841, 893, 893, 862, 905, 869, 885, 887, 842], [324, 618, 717, 789, 843, 824, 880, 876, 847, 896, 842, 890, 892, 848, 905, 864, 885, 894, 843, 893, 866, 871, 895, 845, 897, 874, 854, 888, 838, 875, 876, 842, 903, 864, 879, 887, 844, 901, 873, 857, 887, 837, 893, 881, 847, 892, 869, 889, 893, 849, 907, 877, 875, 893, 850, 883, 868, 849, 893, 847, 885, 884, 847, 893, 855, 870, 893, 847, 902, 873, 869, 897, 841, 901, 883, 852, 884], [324, 618, 717, 789, 843, 824, 880, 876, 847, 896, 842, 890, 892, 848, 905, 864, 885, 894, 843, 893, 866, 871, 895, 845, 897, 874, 854, 888, 838, 875, 876, 842, 903, 864, 879, 887, 844, 901, 873, 857, 887, 837, 893, 881, 847, 892, 869, 889, 893, 849, 907, 877, 875, 893, 850, 883, 868, 849, 893, 847, 885, 884, 847, 893, 855, 870, 893, 847, 902, 873, 869, 897, 841, 901, 883, 852, 884], [1914, 1530, 1362, 1172, 1060, 1028, 930, 973, 968, 929, 981, 928, 960, 959, 917, 974, 929, 928, 946, 901, 954, 938, 921, 955, 908, 945, 934, 902, 942, 912, 937, 955, 909, 953, 913, 918, 938, 895, 939, 920, 907, 944, 902, 935, 928, 920, 913, 958, 909, 947, 937, 930, 971, 918, 938, 936, 899, 957, 912, 916, 935, 904, 956, 936, 925, 972, 923, 964, 958, 923, 969, 913, 920, 928, 892, 945, 924], [1602, 1371, 1239, 1076, 1052, 1000, 971, 1024, 927, 961, 960, 913, 945, 911, 938, 945, 900, 943, 916, 910, 927, 893, 950, 926, 907, 950, 906, 944, 946, 907, 947, 906, 920, 920, 885, 948, 919, 919, 943, 904, 950, 945, 922, 959, 916, 952, 950, 908, 953, 913, 925, 935, 901, 951, 920, 910, 935, 894, 937, 930, 905, 952, 910, 934, 928, 893, 946, 917, 928, 947, 911, 960, 935, 917, 946, 904, 970], [1602, 1371, 1239, 1076, 1052, 1000, 971, 1024, 927, 961, 960, 913, 945, 911, 938, 945, 900, 943, 916, 910, 927, 893, 950, 926, 907, 950, 906, 944, 946, 907, 947, 906, 920, 920, 885, 948, 919, 919, 943, 904, 950, 945, 922, 959, 916, 952, 950, 908, 953, 913, 925, 935, 901, 951, 920, 910, 935, 894, 937, 930, 905, 952, 910, 934, 928, 893, 946, 917, 928, 947, 911, 960, 935, 917, 946, 904, 970], [245, 230, 226, 226, 234, 228, 227, 249, 227, 229, 282, 234, 230, 223, 227, 228, 226, 293, 225, 230, 226, 231, 226, 221, 227, 301, 230, 226, 241, 227, 229, 228, 223, 231, 226, 230, 290, 235, 226, 249, 298, 226, 225, 230, 222, 228, 228, 229, 233, 221, 234, 226, 230, 232, 223, 227, 226, 226, 229, 246, 228, 230, 225, 237, 224, 230, 233, 220, 236, 226, 226, 230, 224, 231, 230, 222, 228], [251, 221, 238, 226, 296, 234, 222, 238, 229, 236, 229, 225, 241, 235, 221, 294, 229, 226, 232, 224, 227, 323, 233, 230, 285, 232, 228, 222, 230, 226, 229, 233, 225, 229, 225, 311, 266, 222, 226, 234, 225, 232, 228, 226, 233, 225, 231, 227, 228, 231, 225, 231, 230, 225, 229, 225, 228, 232, 288, 231, 224, 227, 236, 226, 229, 227, 228, 230, 222, 232, 229, 228, 227, 224, 231, 232, 224], [251, 221, 238, 226, 296, 234, 222, 238, 229, 236, 229, 225, 241, 235, 221, 294, 229, 226, 232, 224, 227, 323, 233, 230, 285, 232, 228, 222, 230, 226, 229, 233, 225, 229, 225, 311, 266, 222, 226, 234, 225, 232, 228, 226, 233, 225, 231, 227, 228, 231, 225, 231, 230, 225, 229, 225, 228, 232, 288, 231, 224, 227, 236, 226, 229, 227, 228, 230, 222, 232, 229, 228, 227, 224, 231, 232, 224], [249, 234, 249, 247, 235, 325, 323, 283, 258, 232, 312, 248, 232, 302, 239, 250, 309, 246, 283, 244, 312, 281, 232, 251, 244, 249, 261, 234, 262, 249, 237, 261, 239, 258, 250, 234, 307, 246, 243, 309, 245, 259, 242, 239, 260, 241, 269, 257, 238, 265, 244, 259, 253, 236, 269, 260, 297, 268, 242, 258, 245, 234, 259, 238, 260, 251, 234, 268, 244, 252, 276, 232, 259, 246, 242, 262, 237], [244, 254, 253, 234, 283, 240, 259, 286, 241, 296, 242, 245, 277, 235, 308, 244, 242, 293, 242, 261, 323, 252, 269, 234, 306, 261, 229, 305, 247, 244, 297, 238, 265, 249, 249, 265, 238, 269, 253, 237, 267, 245, 260, 254, 234, 268, 252, 249, 273, 235, 257, 270, 243, 253, 235, 262, 255, 238, 268, 248, 259, 260, 236, 267, 242, 243, 261, 239, 313, 255, 234, 255, 243, 263, 257, 234, 270], [244, 254, 253, 234, 283, 240, 259, 286, 241, 296, 242, 245, 277, 235, 308, 244, 242, 293, 242, 261, 323, 252, 269, 234, 306, 261, 229, 305, 247, 244, 297, 238, 265, 249, 249, 265, 238, 269, 253, 237, 267, 245, 260, 254, 234, 268, 252, 249, 273, 235, 257, 270, 243, 253, 235, 262, 255, 238, 268, 248, 259, 260, 236, 267, 242, 243, 261, 239, 313, 255, 234, 255, 243, 263, 257, 234, 270], [262, 455, 491, 577, 591, 574, 643, 593, 593, 598, 582, 631, 603, 604, 624, 569, 611, 610, 577, 619, 584, 608, 597, 557, 603, 572, 593, 610, 571, 623, 629, 604, 625, 572, 608, 606, 577, 621, 580, 604, 605, 568, 615, 594, 597, 613, 601, 631, 620, 594, 621, 579, 612, 600, 558, 605, 573, 600, 640, 588, 621, 600, 592, 613, 563, 605, 597, 570, 616, 577, 605, 602, 564, 615, 630, 615, 623], [298, 423, 557, 555, 564, 620, 568, 615, 614, 586, 628, 607, 624, 612, 580, 631, 588, 598, 607, 553, 605, 611, 593, 619, 564, 605, 604, 571, 619, 576, 600, 600, 559, 620, 595, 602, 626, 612, 639, 602, 581, 627, 593, 608, 594, 552, 608, 572, 601, 606, 559, 614, 593, 591, 615, 564, 610, 602, 601, 634, 590, 616, 612, 568, 620, 589, 601, 613, 564, 615, 603, 591, 620, 602, 626, 611, 570], [298, 423, 557, 555, 564, 620, 568, 615, 614, 586, 628, 607, 624, 612, 580, 631, 588, 598, 607, 553, 605, 611, 593, 619, 564, 605, 604, 571, 619, 576, 600, 600, 559, 620, 595, 602, 626, 612, 639, 602, 581, 627, 593, 608, 594, 552, 608, 572, 601, 606, 559, 614, 593, 591, 615, 564, 610, 602, 601, 634, 590, 616, 612, 568, 620, 589, 601, 613, 564, 615, 603, 591, 620, 602, 626, 611, 570], [290, 476, 587, 645, 635, 698, 683, 693, 706, 672, 711, 698, 665, 703, 674, 695, 712, 701, 712, 698, 686, 710, 669, 708, 690, 672, 709, 672, 702, 694, 673, 706, 710, 701, 706, 664, 711, 702, 674, 706, 666, 700, 697, 678, 704, 669, 696, 709, 672, 703, 668, 679, 707, 708, 715, 693, 679, 711, 665, 695, 701, 669, 709, 672, 690, 709, 664, 709, 719, 695, 710, 669, 708, 694, 674, 716, 677], [292, 510, 605, 606, 673, 666, 676, 700, 656, 705, 683, 680, 713, 684, 715, 708, 678, 730, 691, 708, 703, 660, 706, 695, 683, 718, 664, 698, 684, 677, 710, 670, 695, 703, 672, 708, 674, 685, 714, 711, 716, 692, 682, 703, 663, 721, 707, 673, 716, 677, 694, 709, 674, 709, 683, 686, 714, 679, 711, 700, 673, 746, 686, 714, 711, 670, 709, 671, 682, 708, 667, 711, 690, 682, 702, 665, 742], [292, 510, 605, 606, 673, 666, 676, 700, 656, 705, 683, 680, 713, 684, 715, 708, 678, 730, 691, 708, 703, 660, 706, 695, 683, 718, 664, 698, 684, 677, 710, 670, 695, 703, 672, 708, 674, 685, 714, 711, 716, 692, 682, 703, 663, 721, 707, 673, 716, 677, 694, 709, 674, 709, 683, 686, 714, 679, 711, 700, 673, 746, 686, 714, 711, 670, 709, 671, 682, 708, 667, 711, 690, 682, 702, 665, 742]]

In [ ]: