This repository was archived by the owner on Oct 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemphum.js
More file actions
118 lines (90 loc) · 2.05 KB
/
Copy pathtemphum.js
File metadata and controls
118 lines (90 loc) · 2.05 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
WebSocket = require('rwebsocket');
var fs = require('fs');
cold = 19;
confort = 23;
hot = 28;
veryhot = 32;
leds = new WebSocket("ws://127.0.0.1:42001");
leds.onopen = ledsOK;
current_humidity = null;
current_temp = null;
first_data = true;
data_timing = 1000;
data_point = 0;
sensor = new WebSocket("ws://127.0.0.1:42004");
sensor.onmessage = readSensor;
sensor.onopen = connectionOK;
sensor.onclose = ledsFailed;
sensor.onerror = ledsFailed;
leds.connect();
sensor.connect();
function ledsFailed(){
try{
leds.send("7;7;7;7;7");
}
catch (err){
console.log("Can't control leds right now");
}
}
function connectionOK(){
sensor.send("/info");
setInterval(function() {
save_data();
}, data_timing);
}
function ledsOK(){
leds.send("/info");
setTimeout(function(){
display_temp(current_temp,true);
},2000);
}
function readSensor(event){
if(event.data !== "temphum:42004"){
sensor_value = event.data.split(";");
humidity = sensor_value[0];
temperature = sensor_value[1];
display_temp(temperature,false);
current_humidity = humidity;
current_temp = temperature;
if(first_data){
save_data();
first_data = false;
}
}
}
function save_data(){
if(current_temp !== null){
data_point = data_point + 1;
console.log(data_point + "," + current_temp + "," + current_humidity);
fs.appendFile("sensors.csv", data_point + "," + current_temp + "," + current_humidity + "\n", function(err){
if(err) {
return console.log(err);
}
});
console.log("Saving sensors.csv");
}
}
function display_temp(temp,reset){
var temp_icon;
try{
if((temp != current_temp) || (reset)){
if(temp < cold){
leds.send("4");
}
if(temp >= cold && temp < confort){
leds.send("4;4");
}
if(temp >= confort && temp < hot){
leds.send("5;5;5");
}
if(temp >= hot && temp < veryhot){
leds.send("6;6;6;6");
}
if(temp >= veryhot){
leds.send("2;2;2;2;2");
}
}
} catch (err){
//console.log("Failed to send to leds");
}
}