-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmqtt.js
More file actions
123 lines (115 loc) · 3.93 KB
/
Copy pathmqtt.js
File metadata and controls
123 lines (115 loc) · 3.93 KB
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
TEAM_ID = "1095";
USERNAME = "username";
PASSWORD = "password";
const mqtt = require('mqtt');
const client = mqtt.connect('cansat.info', {
username: USERNAME,
password: PASSWORD,
port: 1883
});
client.on(
'connect',
() => {
client.subscribe('presence', (err) => {
if(!err){
setInterval(() => {
client.publish('teams/' + TEAM_ID, getData());
}, 1000);
}
});
}
)
PACKET_TYPES = ['C', 'T']
function getContainerData(){
var containerData = {
team_id: TEAM_ID,
// time in HH:MM:SS
timestamp: new Date().toLocaleTimeString(options = {
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
packet_count: Math.round(Math.random() * 100),
packet_type: PACKET_TYPES[0],
mode: 'F',
tp_released: 'R',
// altitude with 0.1 resolution
altitude: Math.round(Math.random() * 100) / 10,
// temp in 0.1 C resolution
temperature: Math.round(Math.random() * 100) / 10,
// voltage in 0.01 V resolution
voltage: Math.round(Math.random() * 100) / 100,
gps_time: new Date().toLocaleTimeString(options = {
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
// gps latitude in 0.0001 resolution
gps_latitude: Math.round(Math.random() * 100) / 10000,
// gps longitude in 0.0001 resolution
gps_longitude: Math.round(Math.random() * 100) / 10000,
// gps altitude in 0.1 resolution
gps_altitude: Math.round(Math.random() * 100) / 10,
// gps sats in integer
gps_sats: Math.round(Math.random() * 100),
// Software state
software_state: 'DESCENT',
CMD_ECHO: 'CXON'
}
// create comma separated string
var dataString = "";
for (var key in containerData) {
dataString += containerData[key] + ",";
}
// remove last comma
dataString = dataString.substring(0, dataString.length - 1);
return dataString;
}
function getPayloadData(){
var containerData = {
team_id: TEAM_ID,
// time in HH:MM:SS
timestamp: new Date().toLocaleTimeString(options = {
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
}),
packet_count: Math.round(Math.random() * 100),
packet_type: PACKET_TYPES[1],
// altitude with 0.1 resolution
altitude: Math.round(Math.random() * 100) / 10,
// temp in 0.1 C resolution
temperature: Math.round(Math.random() * 100) / 10,
// voltage in 0.01 V resolution
voltage: Math.round(Math.random() * 100) / 100,
// gyro roll pitch and yaw in degrees per second
gyro_roll: Math.round(Math.random() * 100) / 100,
gyro_pitch: Math.round(Math.random() * 100) / 100,
gyro_yaw: Math.round(Math.random() * 100) / 100,
// accel roll pitch and yaw in g
accel_roll: Math.round(Math.random() * 100) / 100,
accel_pitch: Math.round(Math.random() * 100) / 100,
accel_yaw: Math.round(Math.random() * 100) / 100,
// mag roll pitch and yaw in gauss
mag_roll: Math.round(Math.random() * 100) / 100,
mag_pitch: Math.round(Math.random() * 100) / 100,
mag_yaw: Math.round(Math.random() * 100) / 100,
// pointing error in degrees
pointing_error: Math.round(Math.random() * 100) / 100,
// Software state
software_state: 'RELEASED',
CMD_ECHO: 'CXON'
}
// create comma separated string
var dataString = "";
for (var key in containerData) {
dataString += containerData[key] + ",";
}
// remove last comma
dataString = dataString.substring(0, dataString.length - 1);
return dataString;
}
console.log(getPayloadData());