Giter Site home page Giter Site logo

Comments (4)

vortigont avatar vortigont commented on June 16, 2024 1

I will update example with more detailed code like the one above.
Thanks for the tip.

from pzem-edl.

vortigont avatar vortigont commented on June 16, 2024

Hi @Petros144, what kind of compile error do you get?
For callback function library pass the pointer to the struct, so you do not need to request it.

As for getting integer values you can do like this

unsigned u, i, p;

void mycallback(uint8_t id, const RX_msg* m){
  if (meters->getState()->dataStale())
    return;   // something is wrong, message is bad or data stale

   const auto *metrics =(const pz004::metrics*)meters->getMetrics(id);

    u = metrics->voltage;
    i = metrics->current;
    p = metrics->power;

    Serial.printf("PZEM voltage: %d (decivolts)\n", u);
    Serial.printf("PZEM current: %u (mA)\n", i);
    Serial.printf("PZEM power: %u (dW)\n", p);
}

mind that integer values are given in the way that PZEM sends it, i.e. voltage is in decivolts, not volts, current is in mA.

from pzem-edl.

Petros144 avatar Petros144 commented on June 16, 2024

unsigned u, i, p;

void mycallback(uint8_t id, const RX_msg* m){
if (meters->getState()->dataStale())
return; // something is wrong, message is bad or data stale

const auto metrics =(const pz004::metrics)meters->getMetrics(id);

u = metrics->voltage;
i = metrics->current;
p = metrics->power;

Serial.printf("PZEM voltage: %d (decivolts)\n", u);
Serial.printf("PZEM current: %u (mA)\n", i);
Serial.printf("PZEM power: %u (dW)\n", p);

}

Thank you for the fast reply! this will help me a lot! I will test this tomorrow.

so to get my values of interesst I have to change the getMetrics(id); (id ist not the adress, but the custom ID for one of my PZEMs), correct?

So for every Phase I need to implement :

const auto metrics =(const pz004::metrics)meters->getMetrics(41) ; // Phase 1

u1 = metrics->voltage;

const auto metrics =(const pz004::metrics)meters->getMetrics(42) ; // Phase 2

u2 = metrics->voltage;

const auto metrics =(const pz004::metrics)meters->getMetrics(42) ; // Phase 3

u3 = metrics->voltage;

from pzem-edl.

vortigont avatar vortigont commented on June 16, 2024

so to get my values of interesst I have to change the getMetrics(id); (id ist not the adress, but the custom ID for one of my PZEMs), correct?

that is possible but would be incorrect. What callback does it just says "Hey, pzem with this 'id' just updated with fresh data".
So iterating through all your devices in callback is useless because only one of those has been updated. You will receive three different callbacks - each for one of your devices, other two are unchanged.

Also you do not really need to have separate variables u1, u2, u3 and copy metrics values there. Actually it would be wise to use values directly from your pzem objects. Like in my example if you need to print the data (or i.e. send it to some mqtt topic)
you should do it like this

void mycallback(uint8_t id, const RX_msg* m){
  const auto *metrics =(const pz004::metrics*)meters->getMetrics(id);
  Serial.printf("PZEM ID: %u, name: %s, voltage:%u, current:%u, etc...\n", id,  meters->getDescr(id), metrics->voltage, metrics->current);

  someSendToMQTTFunction("topic\voltage", metrics->voltage);
  someSendToMQTTFunction("topic\current", metrics->current);
}

but just in case if you do really want to keep your own copy of the data, you'd better use pzem struct for this.

// somewhere in main.cpp
pz004::metrics phase1data;
pz004::metrics phase2data;
pz004::metrics phase3data;


void mycallback(uint8_t id, const RX_msg* m){
  // copy data to your strucs
  if (id == YOUR_PHASE1_ID){
    phase1data = meters->getMetrics(id)
    return;
  }

  if (id == YOUR_PHASE2_ID){
    phase2data = meters->getMetrics(id)
    return;
  }

  if (id == YOUR_PHASE3_ID){
    phase3data = meters->getMetrics(id)
    return;
  }
}

// in some other function you can get to your copy of the data
void some_other_function(){
  // print your data
  Serial.printf("Phase 1 device name: %s, voltage:%u, current:%u, etc...\n",  meters->getDescr(id), phase1data->voltage, phase1data->current);

  Serial.printf("Phase 2 device name: %s, voltage:%u, current:%u, etc...\n",  meters->getDescr(id), phase2data->voltage, phase2data->current);

  Serial.printf("Phase 3 device name: %s, voltage:%u, current:%u, etc...\n",  meters->getDescr(id), phase3data->voltage, phase3data->current);

}

from pzem-edl.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.