Emonlib & arrays

Hi,

I’m using EmonLib.h to monitor current in a DIY domotics installation. I designed and produced PCB’s that connect directly to an Arduino Mega supporting 15 ZHT-103 (have a look at it there if you’re interested : Arduino MEGA + 15x ZHT-103 - EasyEDA open source hardware lab )

It works quite well but I’m having a ‘speed’ issue.

Actually, I’m currently ‘calculating’ the current of each sensor during the construction of the JSON objet that will be sent to the MQTT server for future use, this takes something like 3 seconds to complete which is too slow (I need it every second max).

There must be some possibilities to group this and calculates values at the same time (using an array?) but I couldn’t get it working so far :frowning:

Any help would be appreciated.

Here’s the code :

//Build json object
String jsonPayload = “{"ZHT_1_0":"”;
double Irms = emon0.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_1":"”;
Irms = emon1.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_2":"”;
Irms = emon2.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_3":"”;
Irms = emon3.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_4":"”;
Irms = emon4.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_5":"”;
Irms = emon5.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_7":"”;
Irms = emon7.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_8":"”;
Irms = emon8.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_9":"”;
Irms = emon9.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_10":"”;
Irms = emon10.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_11":"”;
Irms = emon11.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_12":"”;
Irms = emon12.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_13":"”;
Irms = emon13.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_14":"”;
Irms = emon14.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“","ZHT_1_15":"”;
Irms = emon15.calcIrms(1480);
jsonPayload += Irms;
jsonPayload +=“"}”;

Thanks a lot in advance for your help !

Your string functions are fast enough, so it looks like the culprit is calcIrms. You could take fewer samples at the possible cost of accuracy, but since the Mega is single threaded, I don't know whether there's a way to do the sensing in parallel - take a look at the library.

1 Like

Thank you for your return, I'll try to dig that way as I already posted in their forum

I lowered samples from 1480 to 500. it's much faster (1.2 second) and values are a bit higher with no current but under 1, so far trigger was 2 for a low consumption LED lamp. I can't test now as I'm working remotely. Will test it tomorrow.

Thanks a lot !

The post I found suggests that it should be a multiple of 106 samples because that's how many are sampled (with an Uno I expect) per cycle. Mega will be much the same speed.

1 Like

will try it (but 1480, the 'standard' sample amount ain't 106 multiple) :slight_smile:

Lowered samples to 318. Much faster without compromising the accuracy I need.

So far so good !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.