ESP32 Energy Monitor program execution time

After setting up the Energy Monitor code from Dan Pieg's Github webpage:

https://github.com/danpeig/ESP32EnergyMonitor/tree/main

I noticed the added blink routine was not cycling the onboard LED at the 1 sec on 1 second off rate provided in the code. It was much longer. I timed the void loop() and then commented out and reinserted each of the lines:

emon1.calcVI(20, 2000); 

and

emon2.calcVI(20, 2000); 

What I discovered is each "emon instance" is requiring roughly 4 secs to execute.
Is it to be expected the energy calculations being performed will require this processing time period or is something else going on that is creating the long loop time?
I do not think it is a problem necessarily as I am able to successfully interrogate the ESP with browser requests. If anyone else has experience with this program I would welcome their input.
thanks....

Please post your sketch here to avoid the need to visit another site and guess what code you are using

// EmonLibrary examples openenergymonitor.org, Licence GNU GPL V3
#include <SPI.h>
#include <WiFi.h>
#include <math.h>
#include "time.h"

#include "EmonLib.h"  // Include Emon Library
#define ONBOARD_LED 2
//                     *******************************************************************************
#include "hashtag_includes.h"  // see tabs at top
#include "constants.h"




void setTimezone(const char* timezone) {
  Serial.print("Setting Timezone to ");
  Serial.println(timezone);
  setenv("TZ", timezone, 1);
  tzset();
}

void initTime(const char* timezone) {
  struct tm timeinfo;

  Serial.println("Getting time from NTP server");
  configTime(0, 0, "pool.ntp.org");  // First connect to NTP server, use 0 TZ offset
  if (!getLocalTime(&timeinfo)) {
    Serial.println("  Failed to obtain time");
    return;
  }
  Serial.println("OK, Got the time from NTP");
  setTimezone(timezone);  // then set your timezone
}

/* void printLocalTime() {  // this function monitors current time of day to initiate
                         // pump counter reset at midnight
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Failed to obtain time");
    return;
  }
  int time_hour = timeinfo.tm_hour;  // tm structure element "hour" from time.h
  int time_min = timeinfo.tm_min;    //                     "minute"  "    "
  int time_sec = timeinfo.tm_sec;    //                      "second"

 if (time_hour == hour_trigger && time_min == min_trigger && time_sec == sec_trigger) {
    Serial.println("Reset counters...");
    counter_reset = true;
    delay(1000);  // used to avoid conflict as time rolls over to new day
  }

  Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S zone %Z %z ");
*/
EnergyMonitor emon1;  // Create an instance
EnergyMonitor emon2;

//  ***************** VOID SETUP ***************
void setup() {


  startup_Millis = millis();

  pinMode(ONBOARD_LED, OUTPUT);  //   CANNOT USE INPUT 2 FOR A VOLTAGE OR CURRENT INPUT !!!!!!!!!!1

  

  Serial.begin(115200);

  while (WiFi.status() != WL_CONNECTED) {
    Initial_Connect_Attempts++;
    Serial.println("Attempting to Connect To Local Network:   ");
    Serial.println(wifissid);
    WiFi.begin(wifissid, wifipass);
    delay(4000);

    if (Initial_Connect_Attempts == 5) {
      Serial.println("5 LAN connect attempts unsuccessful; going to connectToWifi()");

      //  connectToWiFi();
    }
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
  Serial.println("Attempting to connect to local WPA network...");

  //  initTime("CST6CDT,M3.2.0,M11.1.0");  // https://sites.google.com/a/usapiens.com/opnode/time-zones

  server.begin();
  // you're connected now, so print out the status:
  Serial.print("WiFi Status:  ");
  Serial.println(WiFi.status());
  Serial.println();
  Serial.print("ESP Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  //                     *******************************************************************************
  EnergyMonitor emon1;  // Create an instance
  EnergyMonitor emon2; 


  emon1.voltage(24, 234.26, 1.7);  // Voltage: input pin; GIOP #, calibration, phase_shift
  emon1.current(34, 111.1);        // Current: input pin, GIOP #, calibration.
  emon2.voltage(26, 234.26, 1.7);
  emon2.current(33, 111.1);
}

void loop() {

  current_Millis = millis();
  UNO_run_cycle_start = current_Millis;
  Serial.println("    Cycle Start/ Beginning of Void (Loop)");
  digitalWrite(ONBOARD_LED, HIGH);  // turn the LED on (HIGH is the voltage level)
  Serial.println("    LED ON Command Given");
  delay(1000);                      // wait for a second
  digitalWrite(ONBOARD_LED, LOW);   // turn the LED off by making the voltage LOW
  Serial.println("    LED OFF Command Given");
  delay(1000);

  emon1.calcVI(20, 2000);  // Calculate all. No.of half wavelengths (crossings), time-out
  Serial.println("    emon1 Command Given");
  emon1.serialprint();     // Print out all variables (realpower, apparent power, Vrms, Irms, power factor)

  emon2.calcVI(20, 2000);
  Serial.println("    emon2 Command Given");
  emon2.serialprint();

  float ph1_realPower = emon1.realPower;          //extract Real Power into variable
  float ph1_apparentPower = emon1.apparentPower;  //extract Apparent Power into variable
  float ph1_powerFactor = emon1.powerFactor;      //extract Power Factor into Variable
  float ph1_supplyVoltage = emon1.Vrms;           //extract Vrms into Variable
  float ph1_Irms = emon1.Irms;

  float ph2_realPower = emon1.realPower;
  float ph2_apparentPower = emon1.apparentPower;
  float ph2_powerFactor = emon1.powerFactor;
  float ph2_supplyVoltage = emon1.Vrms;
  float ph2_Irms = emon1.Irms;
 
  //                ************************************** Webpage ************************

  if (WiFi.status() != WL_CONNECTED && current_Millis - startup_Millis > wait_interval)  //  && millis() - last_Connection_Attempt_At_Ms  > 15000ul )
  {
    WiFi.disconnect();  // last_Connection_Attempt_At_Ms = millis();
    delay(2000);
    WiFi.begin(wifissid, wifipass);
    delay(4000);
    restart_Flag = true;
    connectToWiFi();
  }
  if (WiFi.status() == WL_CONNECTED && restart_Flag == true) {
    restart_Flag = false;
    startup_Millis = millis();
    server.begin();
  }
  //   ******************** WiFiWebServer Code***********

  WiFiClient client = server.available();  // listen for incoming clients
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    currentLineIsBlank = true;

    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n')  //     && currentLineIsBlank)
        {
          
          client.println();
          StackString<55> AString = StackString<55>("           200 Amp Service Panel Energy Monitor");
          client.println(AString.c_str());
          StackString<55> BString = StackString<55>("           ************************************");
          client.println(BString.c_str());
          client.println();
          StackString<65> CString = StackString<65>("               Real Power Phase 1 Input =  ");
          client.print(CString.c_str());
          client.print(ph1_realPower);
          client.println();
          client.println();
          StackString<65> DString = StackString<65>("           Apparent Power Phase 1 Input =  ");
          client.print(DString.c_str());
          client.print(ph1_apparentPower);
          client.println();
          client.println();
          StackString<65> EString = StackString<65>("             Power Factor Phase 1 Input =  ");
          client.print(EString.c_str());
          client.print(ph1_powerFactor);
          client.println();
          client.println();
          StackString<65> FString = StackString<65>("           Supply Voltage Phase 1 Input =  ");
          client.print(FString.c_str());
          client.print(ph1_supplyVoltage);
          client.println();
          client.println();
          StackString<65> GString = StackString<65>("                     Irms Phase 1 Input =  ");
          client.print(GString.c_str());
          client.print(ph1_Irms);
          client.println();
          client.println();
          StackString<65> HString = StackString<65>("               Real Power Phase 2 Input =  ");
          client.print(HString.c_str());
          client.print(ph2_realPower);
          client.println();
          client.println();
          StackString<65> IString = StackString<65>("           Apparent Power Phase 2 Input =  ");
          client.print(IString.c_str());
          client.print(ph2_apparentPower);
          client.println();
          client.println();
          StackString<65> JString = StackString<65>("             Power Factor Phase 2 Input =  ");
          client.print(JString.c_str());
          client.print(ph2_powerFactor);
          client.println();
          client.println();
          StackString<65> KString = StackString<65>("           Supply Voltage Phase 2 Input =  ");
          client.print(KString.c_str());
          client.print(ph2_supplyVoltage);
          client.println();
          client.println();
          StackString<65> LString = StackString<65>("                     Irms Phase 2 Input =  ");
          client.print(LString.c_str());
          client.print(ph2_Irms);
          client.println();
          client.println();

          break;
        }
      }
    }
    delay(3000);    // give the web browser time to receive the data
    client.stop();  // close the connection:
    Serial.println("client disonnected");
  }

  // this write the response from PushingBox Server.
  // You should see a "200 OK"

  if (client.available()) {
    char c = client.read();
    if (DEBUG) { Serial.print(c); }
  }

  //   if there's no net connection, but there was one last time
  //                through the loop, then stop the client:

  if (!client.connected() && lastConnected) {
    if (DEBUG) { Serial.println(); }
    if (DEBUG) { Serial.println("             disconnecting"); }
    client.stop();
  }
  lastConnected = client.connected();
  UNO_run_cycle_stop = millis();
  UNO_cycle_time = (UNO_run_cycle_stop - UNO_run_cycle_start);

  Serial.println();
  Serial.print("UNO run cycle start = ");
  Serial.print(UNO_run_cycle_start);

  Serial.println();
  Serial.print("UNO run cycle stop = ");
  Serial.print(UNO_run_cycle_stop);

  Serial.println();
  Serial.print("UNO cycle time (Ms) = ");
  Serial.print(UNO_cycle_time);

  Serial.println("    Cycle Stop/ END of Void (Loop)");
}

void connectToWiFi() {
  Try_Count = 0;
  while (WiFi.status() != WL_CONNECTED) {
    Try_Count++;
    WiFi.disconnect();
    WiFi.begin(wifissid, wifipass);
    vTaskDelay(10000);
    if (Try_Count == 10) {
      Serial.println("..... 10 attempts failed....rebooting processor...");
      ESP.restart();  // reboot processor!!!
    }
  }
  // WiFi.onEvent( WiFiEvent );    > define WiFiEvent to do ???
}
int status = WL_IDLE_STATUS;  // the Wifi radio's status

WiFiClient client;

WiFiServer server(80);  // added for testing 2/4

int Initial_Connect_Attempts = 0;
int Try_Count = 0;

char wifissid[18] = "Homestead-LAN_EXT";  //     "Homestead-LAN_EXT" for in house testing
char wifipass[13] = "********";           //

char DEVID1[] = "";  //   
char DEVID2[] = "";  //   
char DEVID3[] = "";  //  

unsigned long UNO_run_cycle_start = 0;;
unsigned long UNO_run_cycle_stop = 0;;
unsigned long UNO_cycle_time = 0;

unsigned long startup_Millis = millis();  // static uint32_t last_Connection_Attempt_At_Ms = millis();
unsigned long current_Millis = 0;
int wait_interval = 300000;  // 5 minutes of no wifi connection
int hour = 3600000;              // 3,600,000 millis per hr
int minute = 60000;
// int min = 60;                 // 60,000 millis per minute
// int hr  = 60;


static bool restart_Flag = false;


boolean DEBUG = true;  // Debug mode     !!!!

boolean currentLineIsBlank = false;  //added 10/31/22 debugging

boolean lastConnected = false;

char serverName[19] = "api.pushingbox.com";  //  [  api.pushingbox.com   ]

//  int keyIndex = 0;       WiFi Communication Parameter      NEEDED????


#include <SPI.h>
#include <WiFi.h>
#include <math.h>
#include "time.h"

#include <StackString.hpp>  // save memory using this function: character arrays
using namespace Stack;


#define SENSOR_PIN 14  // ESP32 pin GPIO14; D14 silkscreen connected to DS18B20
#define ONBOARD_LED 2

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