Internet Controlled Smart Switch Using Arduino: Arduino Nano Hangs

So , I am a beginner to arduino and C and i have created arduino nano embedded smart switch .

Question:

My smart switch Arduino nano clone is performing VERY slowly! Turn off and on commands take 20 - 35 seconds to execute and then it returns current voltage value to master arduino node. What is causing this delay and how to correct it ?

NOTE:

a- There is no issue with the speed of Java Server/Internet connection as I totally eliminated internet comm from the system and tested the smart switch using programmed “on”/“off” commands from the arduino uno master node.

b- Upon trying to debug the code I found that the part of smart switch node’s code where it takes multiple readings using “emonlib” from the ZMPT101B sensor is the culprit as it taking more than 15 to 20 seconds to complete the entire process of recording voltage.

c- When I run a separate test code which contained only the ZMPT101B sensor voltage recording code on the SAME nano : it worked well and recorded voltage within 1-2 seconds. Clearly RAM is overloading on loading bluetooth comm and voltage reading code together .

HOW to modify my code to improve my timing? :frowning: :frowning:

Basics:

SMART SWITCH DESCRIPTION

  1. It contains a mechanical relay switched on and off by arduino nano clone’s PMW output

  2. It has a ZMPT101B AC voltage sensor attached in parallel to relay’s AC side which feeds signals to arduino nano clone’s analog input pin.

  3. Arduino nano clone is connected to NRF24L01 bluetooth module for communication with Master Arduino Client.

  • Switch “On” & “Off” commands from the Master Arduino Client can be received by the NRF24L01 bluetooth module on this smart switch and passed on to the arduino nano clone to turn on or off the relay.

  • Values of voltage read by arduino nano clone of smart switch can be transmitted over bluetooth to the Master Arduino Client.

MASTER ARDUINO CLIENT( centre point for all smart switches’ bluetooth comm )

  1. It has an Arduino Uno with arduino ethernet shield attached to it to communicate with the modem.

  2. A NRF24L01 bluetooth module( BASE NODE ) connected to arduino Uno master to talk to all smart switch arduino nano clones.

  3. This arduino master receives “switch on” and “switch off” commands over the ethernet from a remote Java Server and passes over these commands using NRF24L01 base node to individual smart switches’ bluetooth module. The smart switch concerned then processes the request using its nano and turns on or off the relay.

ARDUINO NANO SMART SWITCH NODE CODE SEGMENT CAUSING LAG

 void fetch_voltageCurrent(int i )
  {
    Serial.println(F("FETCHING VOLTAGE"));
    EnergyMonitor emonitor;
    avg_valV  = 0;
     avg_valI = 0.0;
    float Irms = 0;
    float Vrms = 0.0;
    switch(i)
    {
      case 0:
             emonitor = emon0;
             break;

      case 1:
             emonitor = emon1;
             break;

    
             
    }
    
   avg_valV = 0.0;
   avg_valI = 0.0;
   Irms = 0.0;
  Vrms = 0.0;  
 
for(byte i = 0 ;  i < 10 ; i ++ )
{
 
 emonitor.calcVI(20 , 1000 );
  Vrms = emonitor.Vrms - 3 ;
  Irms = emonitor.Irms;
 // Serial.println(F("PRE IRMS"));
//  Serial.println(F(Irms));
  if(Vrms < 1 )
  {
    Vrms = 0.0 ;
    avg_valV = 0.0;
    i = 0;
  }
  else
  {
    avg_valV += Vrms;
  }
   
    avg_valI += Irms;
  

}

    avg_valV = avg_valV/10;
    avg_valI = avg_valI/10;
    Serial.print(F("Vrms = "));
    Serial.println(avg_valV);
     Serial.print("Irms = ");
    Serial.println(avg_valI);
    
   
  }
/code]










ps: transmit_state has been set to 1 to debug , files attached

nrf_master.ino (9.6 KB)

nrf_smartswitch_nodes.ino (5.63 KB)