-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-bucket.js
54 lines (43 loc) · 1.15 KB
/
data-bucket.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
/*
Author: Fredy Lopez
Created: Spring Semester 2020
Class: IoT
*/
//Title: Data Bucket (in Javascript)
var port = 81;
var server;
var count = 0;
function setup() {
// create server
server = new TCPServer();
// when new client is connected
server.onNewClient = function(client) {
Serial.println("new client: " + client.remoteIP()
+ ":" + client.remotePort());
// when the client state changes
client.onConnectionChange = function(type) {
Serial.println("connection changed: " + type);
};
// when receiving data from client
client.onReceive = function(data) {
// print it and then disconnect
Serial.println("received: " + client.remoteIP()
+ ":" + client.remotePort() + ", " + data);
//client.close();
saveData(data);
};
// send a msg to client
//client.send("hello " + (count++));
};
// start listening on port
Serial.println(server.listen(port));
}
function saveData(d) {
// write to disk
console.log("Saving door status ",d," to disk...");
//console.log(FileSystem.exists("/door_status.txt"));
var file = FileSystem.open('/door_status.txt',
File.WRITE | File.READ);
file.println(d);
file.close();
}