-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
133 lines (107 loc) · 3.14 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Project:
// Author: Yun-Chen Lee [email protected]
// Date: 2022/06/05
// Description:
// ================================================
var scene;
let mouseDragging = false;
// -------------------
// /* MQTT */
// MQTT broker setting
let broker = {
hostname: 'public.cloud.shiftr.io',
port: 443
};
// MQTT client:
let client;
let creds = {
clientID: 'p5Client', // client id
userName: 'public',
password: 'public'
}
let subscribeTopics = ['neuronsDV'];
let publishTopic = 'neuronsDV';
// millis setting
let currentTime;
let preTime = 0;
let timePeriod = 1000; //ms
// ===============================================
function setup() {
createCanvas(windowWidth, windowHeight);
// create scene
scene = new Scene({});
scene.setup();
// mode
angleMode(DEGREES);
colorMode(HSB, 255);
// MQTT --------------------------
// Create an MQTT client:
client = new Paho.MQTT.Client(broker.hostname, broker.port, creds.clientID);
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect to the MQTT broker:
client.connect({
onSuccess: onConnect, // callback function for when you connect
userName: creds.userName, // username
password: creds.password, // password
//useSSL: true // use SSL
useSSL: true
});
}
function draw() {
background(0);
mouseDragging = false;
scene.run();
scene.draw();
// MQTT ------------------------
// send message every timePeriod
// currentTime = int(millis() / timePeriod);
// if (preTime != currentTime) {
// preTime = currentTime;
// // send message
// let msg = random().toFixed(3);
// sendMqttMessage(msg, publishTopic);
// }
}
// ===============================================
// -------------------
// /* MOUSE EVENT */
function mouseDragged() {
scene.nodes.forEach(n => {
if (n.ifInside(mouseX, mouseY)) {
n.setPosition(mouseX, mouseY);
}
})
mouseDragging = true;
}
// -------------------
// /* MQTT */
// called when the client connects
function onConnect() {
console.log('client is connected');
subscribeTopics.forEach(topic => {
client.subscribe(topic)
console.log("done subscribe: " + topic);
});
}
// called when the client loses its connection
function onConnectionLost(response) {
if (response.errorCode !== 0) console.log('onConnectionLost:' + response.errorMessage);
}
// called when a message arrives
function onMessageArrived(message) {
console.log('message arrived from topic[' + message.destinationName + "]: " + message.payloadString);
// scene check if need to add a new node
scene.addNode(message.payloadString);
}
// called when you want to send a message:
function sendMqttMessage(msg, topic) {
// if the client is connected to the MQTT broker:
if (client.isConnected()) {
// start an MQTT message:
message = new Paho.MQTT.Message(msg);
message.destinationName = topic;
client.send(message);
console.log('message send to topic[' + topic + "]: " + message.payloadString);
}
}