Coding problem

Hello and good evening

i want to ask about the coding…

the problem i faced rightnow is the current value. when connecting to IoT platform, the value of the current is always zero. but when i compile the sketch without the IoT coding, it can reading the value of current. what is the problem actually?

here is my coding with the IoT… the IoT plateform i use is ThingSpeak

 #include <ESP8266WiFi.h>  
 #include <WiFiClient.h>  
 #include <ThingSpeak.h>  
 #include"EmonLib.h"
EnergyMonitor emon1;;
int Power;
int energy;
float total, kWhUsed;
float TotalkWhUsed;

 const char* ssid = "Redmi3s";  
 const char* password = "xxxxxxxxxxx";  
 WiFiClient client;  
 unsigned long myChannelNumber = 470287;  
 const char * myWriteAPIKey = "XXXXXXXXXXXXXXXX";  
 uint8_t Irms, power, totalkWhUsed, k=0, l=0, m=0;  
 void setup()  
 {  
  Serial.begin(115200);  
  
  delay(10);  
  // Connect to WiFi network  
  Serial.println();  
  Serial.println();  
  Serial.print("Connecting to ");  
  Serial.println(ssid);  
  WiFi.begin(ssid, password);  
  while (WiFi.status() != WL_CONNECTED)  
  {  
   delay(500);  
   Serial.print(".");  
  }  
  Serial.println("");  
  Serial.println("WiFi connected");  
  // Print the IP address  
  Serial.println(WiFi.localIP());  
  ThingSpeak.begin(client);  
 }  
 void loop()   
 {  
  static boolean data_state = false;  
        
  long milisec = millis();
  long time = milisec/1000;
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only
  power = (Irms*240.0);         // Apparent power
  totalkWhUsed = ((power*time))/(3600);     //TotalkWhUsed = TotalkWhUsed + kWhUsed;
  
  Serial.print("Irms                        ");
  Serial.println(Irms);          // Irms
  delay(2000);
  Serial.print("power                       ");
  Serial.println(Power);
  delay(2000);
  Serial.print("totalkWhUsed                 ");
 Serial.println(totalkWhUsed);
 Serial.print("\n\n");
 delay(2000);

 k=Irms;
 l=power;
 m=totalkWhUsed;
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different  
  // pieces of information in a channel. Here, we write to field 1.  

  if( data_state )  
  {  
   ThingSpeak.writeField(myChannelNumber, 1, k, myWriteAPIKey);  
 
   ThingSpeak.writeField(myChannelNumber, 2, l, myWriteAPIKey);  
   data_state = false;
  }   
  else
  {
    ThingSpeak.writeField(myChannelNumber, 3, m, myWriteAPIKey);
    data_state = true;  
  }
  delay(1000); // ThingSpeak will only accept updates every 15 seconds.  
 }

I don’t know ThingSpeak, but there are two obvious problems:

What is the value of current? You are assigning a double to an integer in k=Irms;, so any value less than 1.0 will be truncated to zero. (And likewise 1.0 - 1.999 will become 1, etc.)

Also, beware that you have Irms declared twice, once globally and once inside loop( ) - and those are two different variables!

In addition to the errors pointed out above, the delay should be 15000 vice 1000.

And in addition to all of that, power is defined as uint8_t which means the biggest value it can hold is 255. I suspect this line of code:

power = (Irms*240.0); // Apparent power

will be doing a lot of overflowing.

all right… thank you !