This runs on an Espruino board connected to an ESP8266 running the AT commands.
The ESP8266 is on a separate 3.3V supply and connects to Serial port 4 of the Espruino board
Put an HTML and a CSV file on an SD card and insert it into the Espruino board,
Load the following code into the Espruino board via USB.
Open a browser and use the IP address to access the hardware via Wi-Fi.
Example: http://192.168.1.3:8080/
You should see a directory of the SD card.
If you click on an HTML file, it will load the Webpage instead of displaying the content as text.
If the HTML file loads a .js file on the SD card it should work as well.
From the SD card directory, if you clicl on a .csv file, your browser will launch Excel and download the csv file into a workbook. You can then save the data to your hard drive.
The code follows:
//Use for Espruino board wired to AT ESP8266
//Uses HTTP to access and load a web site and display on console
var serial=Serial4;
var SSID="ssid";
var key= "passcode";
var filename,ext;
serial.setup(115200, { rx: C11, tx : C10 });
var wifi = require("ESP8266WiFi_0v25").connect(serial, function(err) {//5
if (err) throw err;
wifi.reset(function(err) {//4
if (err) throw err;
console.log("Connecting to WiFi");
wifi.connect(SSID,key, function(err) {//3
if (err) throw err;
console.log("Connected");
// Now you can do something, like an HTTP request
var l="";
serveHTML();
});//3
});//4
});//5
function onPageRequest(req, res) {
var a = url.parse(req.url, true);
if (req.method=="POST") {
console.log("Post");
doPost(req,res);
}else{
doGet(req,res);
}//endif
}//end on PageRequest
function doPost(req,res){ //Stub function for now
var info = url.parse(req.url, true);// console.log("res= ",res);
res.writeHead(200);
res.end();
}//end doPost
function doGet(req,res){
var a = url.parse(req.url, true);
filename=a.pathname;
ext=filename.split(".").pop();
console.log("Get"+filename);
if (a.pathname.substr(-1)=="/") { // a slash at the end, list the directory
res.writeHead(200, {'Content-Type': 'text/html'});
res.write("<html><body><p>Contents of "+a.pathname+"</p><ul>");
require("fs").readdir(a.pathname.substr(0,a.pathname.length-1)).map(function(f) {
res.write('<li><a href="'+a.pathname+f+'">'+f+'</a></li>');
});
res.end("</ul></body></html>");
} else { // No slash, try and open file
var f = E.openFile(a.pathname, "r"); //1
if (f !== undefined) { // File open succeeded - send it!
console.log(ext);
switch (ext){
case "html":
res.writeHead(200, {'Content-Type': 'text/HTML'});
f.pipe(res); // streams the file to the HTTP response
break;
case "js":
res.writeHead(200,{'Content-Type':'text/javascript'});
f.pipe(res); // streams the file to the HTTP response
break;
case "csv":
res.writeHead(200,{'Content-Type':'text/csv'});
f.pipe(res); // streams the file to the HTTP response
break;
default:
res.writeHead(200, {'Content-Type': 'text/plain'});
f.pipe(res); // streams the file to the HTTP response
break;
}//end switch ext
} else { // couldn't open file //1
// first check if this was a directory
if (require("fs").readdir()!==undefined) {
// it was a directory - forward us to a page with the '/' on the end
res.writeHead(301, {'Location': a.pathname+"/", 'Content-Type': 'text/plain'});
res.end("Moved");
} else {
// else not found - send a 404 message
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end("404: Page "+a.pathname+" not found");
}
}
}
}
function serveHTML(){
var http=require("http").createServer(onPageRequest).listen(8080);
}//end serveFile
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
This runs on an Espruino board connected to an ESP8266 running the AT commands.
The ESP8266 is on a separate 3.3V supply and connects to Serial port 4 of the Espruino board
Put an HTML and a CSV file on an SD card and insert it into the Espruino board,
Load the following code into the Espruino board via USB.
Open a browser and use the IP address to access the hardware via Wi-Fi.
Example:
http://192.168.1.3:8080/
You should see a directory of the SD card.
If you click on an HTML file, it will load the Webpage instead of displaying the content as text.
If the HTML file loads a .js file on the SD card it should work as well.
From the SD card directory, if you clicl on a .csv file, your browser will launch Excel and download the csv file into a workbook. You can then save the data to your hard drive.
The code follows:
Note the POST function is a stub for now.
These links may be of use in the switch statement
https://en.wikipedia.org/wiki/Media_type
https://www.iana.org/assignments/media-types/media-types.xhtml#application
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields