Wydobycie wartości mocy z kanału miernika jednofazowaego

User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

Witajcie po świętach. Mam problem kolejny problem w moim projekcie. Dokonuje pomiaru prądu z układu HLW8012 , pomocna w tym celu była biblioteka od Krycha88 z GUI-Generic. Układ działa , jest dokonywany pomiar napiecia, pradu jest moc. Chciałbym jeszcze w programie wstawić warunek - powiadomienie gdy spadnie pobór mocy. Jak wydobyć aktualną wartość mocy z kanału one_phase_electricity_meter , poniżej zawartość pliku HLW_8012.h od Krycha88

Code: Select all


#include <SuplaDevice.h>
#include <SuplaSomfy.h>

#include <SuplaDevice.h>
/*
  Copyright (C) krycha88

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the ,License, or (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef _hlw8012_h
#define _hlw8012_h

#include <Arduino.h>

// https://github.com/xoseperez/hlw8012
#include <HLW8012.h>
#include <supla/sensor/one_phase_electricity_meter.h>
#include <supla/storage/storage.h>

namespace Supla {
namespace Sensor {

class HLW_8012 : public OnePhaseElectricityMeter {
 public:
  HLW_8012(int8_t pinCF, int8_t pinCF1, int8_t pinSEL, bool useInterrupts = true);

  void onInit();
  void readValuesFromDevice();
  void onSaveState();
  void onLoadState();

  float getCurrentMultiplier();
  float getVoltageMultiplier();
  float getPowerMultiplier();
  bool getMode();
  _supla_int64_t getCounter();

  void setCurrentMultiplier(float value);
  void setVoltageMultiplier(float value);
  void setPowerMultiplier(float value);
  void setMode(bool value);
  void setCounter(_supla_int64_t value);
  int handleCalcfgFromServer(TSD_DeviceCalCfgRequest *request);

  static IRAM_ATTR void hjl01_cf1_interrupt();
  static IRAM_ATTR void hjl01_cf_interrupt();
  void calibrate(float calibPower, float calibVoltage);

 protected:
  static HLW8012 *sensor;
  int8_t pinCF;
  int8_t pinCF1;
  int8_t pinSEL;

  bool useInterrupts;
  float currentMultiplier;
  float voltageMultiplier;
  float powerMultiplier;
  bool currentWhen;

  unsigned _supla_int64_t energy = 0;
  unsigned _supla_int64_t _energy = 0;  // energy value read from memory at startup
};

};  // namespace Sensor
};  // namespace Supla

#endif
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

OK, sam sobie odpowiem. W klasie HLW_8012 w sekcji public ,od Krycha88 dodałem

Code: Select all

float getPowerRaw();
w pliku HLW_8012.cpp dodałem

Code: Select all

float HLW_8012::getPowerRaw() {
  bool valid;
  return sensor->getActivePower(valid);
a w loop mogę to odczytać np. tak

Code: Select all

 Serial.print(EnergyMeter->getPowerRaw());
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

Cześć , pracując nad własnym projektem wykorzystując stare poczciwe GUI znalazłem coś takiego

Code: Select all


 for (auto element = Supla::Element::begin(); element != nullptr; element = element->next()) {
      if (element->getChannel()) {
        auto channel = element->getChannel();
        if (channel->getChannelType() == SUPLA_CHANNELTYPE_ELECTRICITY_METER) {
          TSuplaChannelExtendedValue* extValue = channel->getExtValue();
          if (extValue == nullptr)
            continue;

          TElectricityMeter_ExtendedValue_V2* emValue = reinterpret_cast<TElectricityMeter_ExtendedValue_V2*>(extValue->value);
          if (emValue->m_count < 1 || emValue == nullptr)
            continue;

          String voltage = "";
          String power_active = "";
          String current = "";

          _supla_int_t flags = channel->getFlags();

          if (flags & (SUPLA_CHANNEL_FLAG_PHASE2_UNSUPPORTED | SUPLA_CHANNEL_FLAG_PHASE3_UNSUPPORTED)) {
            Serial.println(" ****************** Single phase electricity meter detected ****************** ");
            Serial.print(" Napięcie : "); Serial.println(emValue->m[0].voltage[0]);
            Serial.print(" Moc czynna : "); Serial.println(emValue->m[0].power_active[0]);
            Serial.print(" Prąd : "); Serial.println(emValue->m[0].current[0]);
            
            voltage += String(emValue->m[0].voltage[0] / 100.0) + " | ";
            power_active += String(emValue->m[0].power_active[0] / 100000.0) + " | ";
            current += String(emValue->m[0].current[0] / 1000.0) + " | ";
          }
          else {
            for (size_t i = 0; i < MAX_PHASES; i++) {
              voltage += String(emValue->m[0].voltage[i] / 100.0) + " | ";
              power_active += String(emValue->m[0].power_active[i] / 100000.0) + " | ";
              current += String(emValue->m[0].current[i] / 1000.0) + " | ";
            }
          }

          
          voltage.setCharAt(voltage.length() - 2, 'V');
          power_active.setCharAt(power_active.length() - 2, 'W');
          current.setCharAt(current.length() - 2, 'A');
            Serial.println(" ****************** Single phase electricity meter detected ****************** ");
            Serial.print(" Napięcie : "); Serial.println(voltage);
            Serial.print(" Moc czynna : "); Serial.println(power_active);
            Serial.print(" Prąd : "); Serial.println(current);


        }
      }
  }
Powinno wyświetlać wartości pomiaru ale pokazuje same zera no co najwyżej prąd jest 0.09A, w apce pokazuje poprawnie wszystkie wartości, gdzie jest błąd ? Coś się zmieniło ? Mam dwa kanały 0 to relay , a 1 to HLW8012.
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
klew
Posts: 13906
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

shimano73 wrote: Wed Jan 28, 2026 9:53 am Cześć , pracując nad własnym projektem wykorzystując stare poczciwe GUI znalazłem coś takiego
Aktualnie jest używana nowsza wersja struktury: TElectricityMeter_ExtendedValue_V3
Możesz te "v2" podmienić na "v3". Nie sądzę, aby to pomogło, ale warto sprawdzić :)

Jak to poprawisz, to wrzuć proszę logi z urządzenia.
Najlepsze suple dla Twojego domu :mrgreen:
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

Dzięki , działa. Wystarczyło tylko zmienić V2 na V3 i odczyty się pojawiły

Code: Select all

11:35:34.321 -> Send: [53 55 50 4C 41 17 5A 03 00 00 67 00 00 00 0E 00 00 00 01 00 00 00 00 00 01 63 01 00 00 00 00 00 ]
11:35:34.321 -> Send: [53 55 50 4C 41 ]
11:35:34.321 -> Send: [53 55 50 4C 41 17 5B 03 00 00 69 00 00 00 D4 00 00 00 01 0E CE 00 00 00 46 6E 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7E 01 00 00 05 00 00 00 01 00 00 00 00 00 57 61 00 00 00 00 33 02 00 00 00 00 0B 22 84 00 00 00 00 00 00 00 00 00 37 BE A8 00 00 00 00 00 00 00 00 00 15 52 D6 00 00 00 00 00 00 00 00 00 68 02 00 00 00 00 00 00 00 00 00 00 ]
11:35:34.321 -> Send: [53 55 50 4C 41 ]
11:35:36.520 ->  ****************** Single phase electricity meter detected ****************** 
11:35:36.520 ->  Napięcie : 249.19 V 
11:35:36.520 ->  Moc czynna : 86.59 W 
11:35:36.520 ->  Prąd : 0.56 A 
11:35:39.298 -> Send: [53 55 50 4C 41 17 5C 03 00 00 67 00 00 00 0E 00 00 00 01 00 00 00 00 00 01 63 01 00 00 00 00 00 ]
11:35:39.298 -> Send: [53 55 50 4C 41 ]
11:35:39.298 -> Send: [53 55 50 4C 41 17 5D 03 00 00 69 00 00 00 D4 00 00 00 01 0E CE 00 00 00 52 6E 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7E 01 00 00 05 00 00 00 01 00 00 00 00 00 8A 61 00 00 00 00 3F 02 00 00 00 00 1F EF 84 00 00 00 00 00 00 00 00 00 2F 46 AE 00 00 00 00 00 00 00 00 00 DF 2F DB 00 00 00 00 00 00 00 00 00 5E 02 00 00 00 00 00 00 00 00 00 00 ]
11:35:39.332 -> Send: [53 55 50 4C 41 ]
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

OK, działało dla HLW8012 a dla CSE7766 pokazuje głupoty , chociaż w aplikacji jest ok

Code: Select all

#ifdef GUI_ALL_ENERGY
        if (channel->getChannelType() == SUPLA_CHANNELTYPE_ELECTRICITY_METER) {
          TSuplaChannelExtendedValue* extValue = channel->getExtValue();
          if (extValue == nullptr)
            continue;

          TElectricityMeter_ExtendedValue_V3* emValue = reinterpret_cast<TElectricityMeter_ExtendedValue_V3*>(extValue->value);
          if (emValue->m_count < 1 || emValue == nullptr)
            continue;

          String voltage = "";
          String power_active = "";
          String current = "";

          _supla_int_t flags = channel->getFlags();

          if (flags & (SUPLA_CHANNEL_FLAG_PHASE2_UNSUPPORTED | SUPLA_CHANNEL_FLAG_PHASE3_UNSUPPORTED)) {
            voltage += String(emValue->m[0].voltage[0] / 100.0) + " | ";
            power_active += String(emValue->m[0].power_active[0] / 100000.0) + " | ";
            current += String(emValue->m[0].current[0] / 1000.0) + " | ";
          }
          else {
            for (size_t i = 0; i < MAX_PHASES; i++) {
              voltage += String(emValue->m[0].voltage[i] / 100.0) + " | ";
              power_active += String(emValue->m[0].power_active[i] / 100000.0) + " | ";
              current += String(emValue->m[0].current[i] / 1000.0) + " | ";
            }
          }

          voltage.setCharAt(voltage.length() - 2, 'V');
          power_active.setCharAt(power_active.length() - 2, 'W');
          current.setCharAt(current.length() - 2, 'A');

          addLabel(voltage);
          addLabel(power_active);
          addLabel(current);
        }
#endif

You do not have the required permissions to view the files attached to this post.
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
klew
Posts: 13906
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

shimano73 wrote: Wed Mar 04, 2026 8:52 am OK, działało dla HLW8012 a dla CSE7766 pokazuje głupoty , chociaż w aplikacji jest ok
Na oko wygląda jakby dodawnie stringów robiło konwersję do floata i dodawło napięcie 3x ;)

Wstaw sobie logi debugowe na każdym kroku i będziesz widział co tam się dzieje.
Najlepsze suple dla Twojego domu :mrgreen:
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

Mea culpa , wziąłem się za nowy projekt w Platformio na podstawie GUI-Generic a tam stara biblioteka , po uaktualnieniu pokazuje prawidłowo.
You do not have the required permissions to view the files attached to this post.
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
shimano73
Posts: 2174
Joined: Sun Feb 28, 2016 12:27 pm
Location: Orzesze
Been thanked: 1 time

Post

Pytanie jeszcze jedno mam a jak z TElectricityMeter_ExtendedValue_V3 wyrwać "kWh" , wiem jak

Code: Select all

	   voltage += String(emValue->m[0].voltage[0] / 100.0) + " | ";
            power_active += String(emValue->m[0].power_active[0] / 100000.0) + " | ";
            current += String(emValue->m[0].current[0] / 1000.0) + " | ";
            
ale nie wiem jakiej metody użyć do odczytu wartości z "kWh"
W elektronice jak nie wiadomo o co chodzi to zwykle chodzi o zasilanie

Wezmę udział w Supla Offline Party 2024 :)
User avatar
klew
Posts: 13906
Joined: Thu Jun 27, 2019 12:16 pm
Location: Wrocław
Has thanked: 134 times
Been thanked: 137 times

Post

shimano73 wrote: Wed Mar 04, 2026 4:42 pm Pytanie jeszcze jedno mam a jak z TElectricityMeter_ExtendedValue_V3 wyrwać "kWh" , wiem jak

Code: Select all

	   voltage += String(emValue->m[0].voltage[0] / 100.0) + " | ";
            power_active += String(emValue->m[0].power_active[0] / 100000.0) + " | ";
            current += String(emValue->m[0].current[0] / 1000.0) + " | ";
            
ale nie wiem jakiej metody użyć do odczytu wartości z "kWh"
Tutaj jest ta struktura "emValue" zdeklarowana:
https://github.com/SUPLA/supla-device/b ... to.h#L1986

A tutaj ta, którą masz pod m[0]:
https://github.com/SUPLA/supla-device/b ... to.h#L1881
Najlepsze suple dla Twojego domu :mrgreen:

Return to “Arduino IDE”