On the Client side HTML and script is used to construct and edit the following object:
/* LogObj1 on client sent via POST JSON*/
var LogObj1={
Fname:"xxx.csv",
Interval:1000, //milliseconds between samples
items:[
// header is text at top of column in csv file
// cmd source for data in each column
// can be in a different order
// and have more or fewer items
{header:"N",cmd:"return(x.LL.count)"},
{header:"Date",cmd:"return(x.Day+\" \"+x.Month+\" \"+x.Year)"},
{header:"Time",cmd:"return(x.Hour+\":\"+x.Minute+\":\"+x.Second)"},
{header:"A0",cmd:"return(analogRead(A0).toFixed(2))"},
{header:"A1",cmd:"return((analogRead(A1)*x.LL.items[4].slope+x.LL.items[4].offset).toFixed(2))",slope:100,offset:-50
},
{header:"A2",cmd:"return((analogRead(A2)*100-50).toFixed(2))"},
{header:"A3",cmd:"return(SanalogRead(A3).toFixed(2))"},
{header:"Board Temp C",cmd:"return(E.getTemperature().toFixed(2))"},
{header:"Board Temp F",
cmd:"return((E.getTemperature()*9/5+32).toFixed(2))"
}
],
count:0, // Number of lines logged
N:10, // stop logging after count>N
}; //end LogObj1
Notice several methods are used to scale the analog inputs using a slope/offset (intercept).
The Client script stringifies the object and sends it after the HTTP POST header.
On the Server side the HTTP header is used to determine that the client request is a post and that the Filename in the HTTP header matches “LOG”.
The stringified object is buffered using the Length in the HTTP header.
At this point I’m testing the code to process the request. It writes to console.log instead of a file.
//An object constructor
function LogObj(){
this.Month=1,
this.Day=1,
this.Year=0,
this.Hour=0,
this.Minute=0,
this.Second=0,
this.LL={}, //LL is where the Posted object fits
this.channels=[], // the cmds in LL are converted to functions in channels
//Methods for this object
this.doHeaders=function(){
var str="";
for(var j=0;j in this.LL.items;j++)str+=this.LL.items[j].header+",";
console.log(str+"\n\r");
},//end OutputHeaders
this.doData=function(id){
//openfile append
//setup date and time
var t = new Date();
this.Month=t.getMonth();
this.Day=t.getDate();
this.Year=t.getFullYear();
this.Hour =t.getHours();
this.Minute=("0"+t.getMinutes()).substr(-2);
this.Second= ("0"+t.getSeconds()).substr(-2);
//use the channels to get readings
var readings=[];
this.LL.count++;
if(this.LL.count <= this.LL.N){
for(i=0;i in this.channels;i++)readings[i]= this.channels[i](this);
console.log(readings.join(", ")+"\n\r");
}else{
clearInterval(id);
}//endif
//close file
};//end doData
}//end LogObj
The JSON.stringify() doesn’t work with embedded methods. The transmitted LogObj1 object is JSON.parsed into the LL field of the LogObj object.
The code that executes the LogObj follows:
// Test the LogObj methods using data in LogObj1
function Test(){
// The client stringifies and JSON POSTs LogObj1
var W=JSON.stringify(LogObj1);//the client does this and sends it over
console.log(W);
// The server creates an instance of LogObj
var Y=new LogObj();
//The server then embeds the JSON data in the object
Y.LL=JSON.parse(W);
//how the cmd strings convert to functions
for(var j=0;j in Y.LL.items;j++)
Y.channels.push(Function("x",Y.LL.items[j].cmd));
console.log("Y= ",Y);
console.log("channels\n\r",Y.channels);
var ID;
//check to see if file exists
//open file overwrite here
Y.doHeaders();
//close file
ID=setInterval(function(ID){Y.doData();},Y.LL.Interval,ID);
console.log("Test End");
}//end Test
//Pretend the client has POSTed LogObj1
//Use the info to create a csv file, write headers and collect data
Test();
The “eval()” command is not used and each Client request creates a new LogObj.
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.
On the Client side HTML and script is used to construct and edit the following object:
Notice several methods are used to scale the analog inputs using a slope/offset (intercept).
The Client script stringifies the object and sends it after the HTTP POST header.
On the Server side the HTTP header is used to determine that the client request is a post and that the Filename in the HTTP header matches “LOG”.
The stringified object is buffered using the Length in the HTTP header.
At this point I’m testing the code to process the request. It writes to console.log instead of a file.
The JSON.stringify() doesn’t work with embedded methods. The transmitted LogObj1 object is JSON.parsed into the LL field of the LogObj object.
The code that executes the LogObj follows:
The “eval()” command is not used and each Client request creates a new LogObj.
The left side screen using WebIDE:
Notice that the Test function ends after the CSV headers are printed but before the data are printed.
1 Attachment