Blynk App Webhook Multiple Values to Thingspeak

Done via pm

Add a Notification widget and value displays on V7, V8 and V9 to the project and then:

unsigned int countFailures = 0;

void myTimerEvent()  // This function sends PZEM data every 25.555 seconds to Virtual Pin (6).
{
  if(countFailures < 2)
  {
    Blynk.virtualWrite(V6, V6value);  // send to Webhook if we have had less than 2 failures
  }
  else
  {
    Blynk.notify("Webhook problem");
    countFailures = 0; // reset counter
  }
  Blynk.virtualWrite(V7, V6value);  // show in V7 display widget
}

BLYNK_WRITE(V6)
{
  String response = param.asStr();
  if(response.toInt() < 1)
  {
    countFailures = countFailures + response.toInt();
  }
  Blynk.virtualWrite(V9, countFailures);
  Blynk.virtualWrite(V8, response); // show Webhook response in V8 display widget
}

This is what it looks like without a Pzem connected:

Basically the 69 is confirmation the Webhook was processed and the 0 confirms no Webhook errors to date. If you have more than one error project will send you a PUSH message. When I had 15555ms I did have a few errors so I have pushed it up to 25555ms.

If you get an error disconnect the Pzem and run the project without live data. This will check if you are having problems with the Pzem.

Not sure as to where I needed the put the V6_WRITE(V6)

Placed it here :slight_smile:
// This function sends PZEM data every 30 seconds to Virtual Pin (6).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.

void myTimerEvent() // This function sends PZEM data every 25.555 seconds to Virtual Pin (6).
{
if(countFailures < 2)
{
Blynk.virtualWrite(V6, V6value); // send to Webhook if we have had less than 2 failures
}
else
{
Blynk.notify(“Webhook problem”);
countFailures = 0; // reset counter
}
Blynk.virtualWrite(V7, V6value); // show in V7 display widget
}

BLYNK_WRITE(V6)
{
String response = param.asStr();
if(response.toInt() < 1)
{
countFailures = countFailures + response.toInt();
}
Blynk.virtualWrite(V9, countFailures);
Blynk.virtualWrite(V8, response); // show Webhook response in V8 display widget
}
void setup()
{
//No Debug console
/// Serial.begin(9600); // Note : Do not use Serial0 port, for serial debugging!

pzem.setAddress(ip);
Blynk.begin(auth, ssid, pass);
timer.setInterval(30000L, myTimerEvent);
}

Had it running for a few minutes but get nothing in V8 or V9 :

Disconnect the Pzem.

What pin is the Webhook set to? Should be V6.

Webhook is set to V6 (did not change anything in the code nor in the settings in teh App, just added V7, V8 and V9…)

Disconnected the PZEM … V8 and V9 remain empty … V7 shows the fields string correctly

Did you make this declaration?

Yes I did … Including my full code again here :

/*--------------------------------------------------------------
Program: PzemTony.ino   Test Meter PZEM004 and Blynk App

Description: Communication test meter PZEM004 and platform IoT Blynk, visualization
Prueba de comunicacion medidor PZEM004 y plataforma IoT Blynk,

Hardware: ESP8266 12E NodeMCU Lolin .
PZEM-004

Software: Arduino IDE v1.8.3 , Blynk App

Date: 22 Feb 2018

Modified or Created: PDAControl http://pdacontroles.com http://pdacontrolen.com

Complete Tutorial English: http://pdacontrolen.com/meter-pzem-004-esp8266-platform-iot-blynk-app/

Tutorial Completo Espaol: http://pdacontroles.com/medidor-pzem-004-esp8266-plataforma-iot-blynk-app/

Based: Examples blynk virtual data : https://examples.blynk.cc/?board=ESP8266&shield=ESP8266%20WiFi&example=GettingStarted%2FVirtualPinWrite

      Blynk Github https://github.com/blynkkk/blynk-library/blob/master/examples/Boards_WiFi/ESP8266_Standalone/ESP8266_Standalone.ino

      PZEM004T library for olehs                          https://github.com/olehs/PZEM004T

      SoftwareSerial for esp8266                          https://github.com/plerup/espsoftwareserial
--------------------------------------------------------------*/

//#define BLYNK_PRINT Serial /* Comment this out to disable prints and save space */

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <BlynkSimpleEsp8266.h>
#include <PZEM004T.h> //https://github.com/olehs/PZEM004T
#include <ArduinoOTA.h>
//#include <SimpleTimer.h> // Allows us to call functions without putting them in loop() // library is part of Blynk so not needed

PZEM004T pzem(&Serial); /// use Serial
IPAddress ip(192,168,1,1);

float voltage_blynk=0;
float current_blynk=0;
float power_blynk=0;
float energy_blynk=0;
String V6value;

unsigned int countFailures = 0;

//char auth[] = “6b364c717876480585f9c405a05e08ee”; //Solar
char auth[] = "3728b5d816234760b577c074081d6b45"; // Energy

char ssid[] = "IOT";
char pass[] = "Fr0mh3ll";
unsigned long lastMillis = 0;

///http://pdacontrolen.com/meter-pzem-004-esp8266-platform-iot-blynk-app/
//IMPORTANT : CORRECTION WITH 1K RESISTOR ON PZEM BOARD NEEDED !!!
// VIN 8266 - VCC PZEM
// TX-RX
// RX-TX
// GND - GND

BlynkTimer timer;

// This function sends PZEM data every 30 seconds to Virtual Pin (6).
// In the app, Widget’s reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.

void myTimerEvent()  // This function sends PZEM data every 25.555 seconds to Virtual Pin (6).
{
  if(countFailures < 2)
  {
    Blynk.virtualWrite(V6, V6value);  // send to Webhook if we have had less than 2 failures
  }
  else
  {
    Blynk.notify("Webhook problem");
    countFailures = 0; // reset counter
  }
  Blynk.virtualWrite(V7, V6value);  // show in V7 display widget
}

BLYNK_WRITE(V6)
{
  String response = param.asStr();
  if(response.toInt() < 1)
  {
    countFailures = countFailures + response.toInt();
  }
  Blynk.virtualWrite(V9, countFailures);
  Blynk.virtualWrite(V8, response); // show Webhook response in V8 display widget
}
void setup()
{
  //No Debug console
  /// Serial.begin(9600); // Note : Do not use Serial0 port, for serial debugging!
  
  pzem.setAddress(ip);
  Blynk.begin(auth, ssid, pass); 
  timer.setInterval(30000L, myTimerEvent);
}

void loop()
{
  Blynk.run();

  //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 1000) 
  {
    lastMillis = millis();

    //Read meter PZEM
    float v = pzem.voltage(ip);
    if(v >= 0.0){ voltage_blynk =v; } //V
    
    float i = pzem.current(ip);
    if(i >= 0.0){ current_blynk=i;    }  //A                                                                                                                      
    
    float p = pzem.power(ip);
    if(p >= 0.0){power_blynk=p;       } //kW
    
    float e = pzem.energy(ip);
    e = e/1000;          
    if(e >= 0.0){  energy_blynk =e;  } ///kWh  
      
    Blynk.virtualWrite(V1, voltage_blynk);
    Blynk.virtualWrite(V2, current_blynk );
    Blynk.virtualWrite(V3, power_blynk);
    Blynk.virtualWrite(V4, energy_blynk );
    Blynk.virtualWrite(V5, lastMillis );
    
    //Create Thingspeak String for V6
    V6value="";
    V6value= "&field1=";
    V6value += String(voltage_blynk);
    V6value += "&field2=";
    V6value += String(current_blynk);
    V6value += "&field3=";
    V6value += String(power_blynk);
    V6value += "&field4=";
    V6value += String(energy_blynk); 
  }
  timer.run(); // Initiates BlynkTimer  
}

Suggests your Webhook is not running for some reason.

Not running or not running with a positive result ? Strange thing is that I did not change anything to the Webhook, just adapteed my sketch with your enhancements …

Not running at all.

Finally I got one of my Pzem’s running.

And data on TS at https://thingspeak.com/channels/418060

I just want to check something else.

@Dmitriy is it by design that Webhook runs when the project is stopped? My assumption would have been that when the project is not running that the user doesn’t want to make Webhook calls but this doesn’t seem to be the case.

For now I will do a hack to add a button so the virtualWrite() to the Webhook pin is enabled / disabled

Yes. I remember I did that by purpose, but don’t remember anymore “why?”.

1 Like

The MOST enjoyable technical answer by far !!!

@Dmitriy Have a healthy fruitful new year Sir!

1 Like

Hello @Costas , can you share, how you correct the URL:?

Unfortunately @Costas hasn’t been seen on the forum for almost 4 years, so you probably won’t get a reply from him.

This discussion is about the Legacy Webhook functionality, which is significantly different from the Webhook in Blynk IoT (which I assume is what you are using?).

Maybe you should start a new topic and describe your issue in detail.

Pete.