just tested the improved code for grabbing a short bit of the waveform.
it reads from I2C to blobs, then creates 2D arrays, then adds it to a JSON table that will go to Highcharts.
It is my first code using blobs so I am definitely interested to hear if I have done something bad - and other should copy snippets with caution.
`
function getawave(){
local IAblob = blob(180); //current
local Vblob = blob(180); //volts
local tblob = blob(240); //time
//pre-compute the I2C address
local Vaddr = format("%c%c", (V>>8), V&0xff)
local IAaddr = format("%c%c", (IA>>8), IA&0xff)
//establish the variable by reading once. required?
local res = i2c.read(0x71, Vaddr, 3);
local resIA = i2c.read(0x71, IAaddr, 3);
local start = hardware.micros();
//TRY to read as FAST as possible.
//loop is 845 microseconds on average
for(local a=0;a<60;a+=1){
res = i2c.read(0x71, Vaddr, 3);
resIA = i2c.read(0x71, IAaddr, 3);
tblob.writen(hardware.micros(), 'i');
IAblob.writen(resIA[2],'b');
IAblob.writen(resIA[1],'b');
IAblob.writen(resIA[0],'b');
Vblob.writen(res[2],'b');
Vblob.writen(res[1],'b');
Vblob.writen(res[0],'b');
}
// now get the data ready for output
//extract from blob, scaled and inserted to ARRAY for JSON for Highcharts
local Iwave = array(60);
local Vwave = array(60);
//This seems to be a necessary part of creating a new array with zeros
for(local a=0;a<60;a+=1){
Vwave[a] = [0,0];//
}
for(local a=0;a<60;a+=1){
Iwave[a] = [0,0];//
}
//Get value of RMS current
local ICOUNTA = readi2c (i2c, 0x21a)
//Get value of RMS potential
local VCOUNT = readi2c(i2c, VRMS);
//scale the waveform to volts
local _VRMS = (1.0* VCOUNT/CAL._VOLTSCALE);
//scale the waveform to amps
local _IRMS = (1.0* ICOUNTA/CAL.CURRENTSCALARA);
//scale the waveform so 100 is the peak of a pure sine wave
//with waveforms different than ‘sine’ the peak will be other than +100/-100
_VRMS = (141.42/CAL._VOLTSCALE)/_VRMS;
//for power factor other than 1.0, the current waveform will grow
_IRMS = (141.42/_IRMS) /(CAL.CURRENTSCALARA*previousjson.meterdata.PF);
//set the blob points to the beginning
Vblob.seek(0,‘b’);
tblob.seek(0,‘b’);
IAblob.seek(0,‘b’);
local mask = (1<<24);//for converting to signed numbers
for(local a=0;a<60;a+=1){
//build the blob back into signed integers
local _V= Vblob.readn('b');
_V = _V | (Vblob.readn('b')<<8);
_V = _V | (Vblob.readn('b')<<16);
if (_V > (mask>>1)) {
_V=_V - mask;
}
local IA= IAblob.readn('b');
IA = IA | (IAblob.readn('b')<<8);
IA = IA| (IAblob.readn('b')<<16);
if (IA > (mask>>1)) {
IA=IA-mask;
}
local tm= tblob.readn('b');
tm = tm | (tblob.readn('b')<<8);
tm = tm| (tblob.readn('b')<<16);
tm = tm| (tblob.readn('b')<<24);
//Format the data so that Highcharts will get the JSON Array it needs
//and apply the scaling
Vwave[a][1] = (_VRMS * _V);
Iwave[a][1] = (_IRMS * IA);
Vwave[a][0] = 0.001* (tm-start);
Iwave[a][0] = 0.001* (tm-start+423);
//server.log(a + " , " + b + " , " + tm + " , " + IA);
}
responsejson[“waveIA”] <- Iwave;
responsejson[“waveV”] <- Vwave;
server.log(“waveform added to output JSON”);
}
`