Skip to content

Commit 21769fa

Browse files
committed
Add example of logging to a file
1 parent ef0777e commit 21769fa

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ Espruino devices straight from the web browser.
1414
UART.write('LED1.set();\n');
1515
```
1616

17-
[try it out](https://espruino.github.io/EspruinoWebTools/examples/uart.html)
17+
* [Simple test](https://espruino.github.io/EspruinoWebTools/examples/uart.html)
18+
* [Log data to a file](https://espruino.github.io/EspruinoWebTools/examples/logtofile.html)
19+
1820

1921
## imageconverter.js
2022

examples/logtofile.html

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<html>
2+
<head>
3+
</head>
4+
<body>
5+
<h1>Log to file</h1>
6+
<p>This page connects to an Espruino device, calls a
7+
function called <code>getData()</code> (which you should
8+
have created previously) and then stored all data received
9+
in the text box below. You can click 'Save Data' to save it
10+
to a file.
11+
</p>
12+
<button id="connect">Connect</button>
13+
Status: <span id="status"></span><br/>
14+
<span>Received data:</span><br/>
15+
<textarea id="result" style="width:100%;height:150px">
16+
</textarea>
17+
<button id="save">Save Data</button>
18+
19+
<script src="../uart.js"></script>
20+
<script type="text/javascript">
21+
var resultData = "";
22+
var domResult = document.getElementById('result');
23+
var domStatus = document.getElementById('status');
24+
function setStatus(t) {
25+
domStatus.innerHTML = t;
26+
}
27+
28+
// When clicked, connect or disconnect
29+
var connection;
30+
document.getElementById('connect').addEventListener("click", function() {
31+
if (connection) {
32+
connection.close();
33+
connection = undefined;
34+
}
35+
setStatus("Connecting...");
36+
UART.connect(function(c) {
37+
if (!c) {
38+
setStatus("Couldn't connect!");
39+
alert("Couldn't connect!");
40+
return;
41+
}
42+
setStatus("Connected");
43+
connection = c;
44+
// Handle the data we get back, and call 'onLine'
45+
// whenever we get a line
46+
resultData = "";
47+
connection.on("data", function(d) {
48+
resultData += d;
49+
setStatus(resultData.length+" bytes received");
50+
result.value = resultData;
51+
});
52+
// First, tell the device
53+
connection.write("\x03\x10getData();\n", function() {
54+
setStatus("getData() sent.");
55+
});
56+
});
57+
});
58+
document.getElementById('save').addEventListener("click", function() {
59+
var a = document.createElement("a"),
60+
file = new Blob([resultData], {type: "text/plain"});
61+
var url = URL.createObjectURL(file);
62+
a.href = url;
63+
a.download = "data.txt";
64+
document.body.appendChild(a);
65+
a.click();
66+
setTimeout(function() {
67+
document.body.removeChild(a);
68+
window.URL.revokeObjectURL(url);
69+
}, 0);
70+
});
71+
</script>
72+
</body>
73+
</html>

0 commit comments

Comments
 (0)