Sem leitura com Conexão UART com ESP32

Projetos de ESP32
josepetri
Membro
Mensagens: 5
Registrado em: 27/Jul/2018, 01:59

Sem leitura com Conexão UART com ESP32

Mensagem por josepetri » 07/Out/2020, 16:18

Boa tarde a todos. Eu montei um codigo no arduino é funcionou muito bem, magora estou tentando portalo para o esp32 mas não estou coneseuindo nem começar com a leitura do CI.

Tenho um ci hlw8032 que faz comunicação uart, só que não estou recebindo nenhuma leitura, não sei se seria erro da programação ou do meu ESP32. Alguém pode me dar uma ajuda?

O codigo segue abaixo.

Código: Selecionar todos

#define RXD2 16
#define TXD2 17
#include <HardwareSerial.h>

HardwareSerial HWL8032Serial(2);

int hlw[25];
int soma =0; 

float volt_reg, volt_value, volt_co;
float current_reg, current_value, current_co;
float power_reg, power_value, power_co;
float app_power,power_factor;
float pulse, pwr_used;
float data_estado, data_check, data_reg, data_sum;


void setup() {
 
  Serial.begin(115200);
  HWL8032Serial.begin(4800, SERIAL_8N1, RXD2, TXD2);
}


bool read_data () {
  while (1) {
    if (HWL8032Serial.available() > 0) {
      // read first two bytes, 2nd bytes must be 5A to confirm
      int byte0 = HWL8032Serial.read();
      delay(2.083333); //Cada pacote de 24 bits demora 50ms, então 50/24 = 2,083 ms para cada leitura de bit.
      int byte1 = HWL8032Serial.read();
      delay(2.083333);

      if ( byte1 == 0x5A ) {  //Se o segundo bit for x5A então o pacote esta valido e correto para ser feita a leitura.
        hlw[0] = byte0;
        hlw[1] = byte1;
        for (int i = 2; i <= 23; i++) {
          int incomingByte = HWL8032Serial.read();
          delay(2.083333);
          hlw[i] = incomingByte;
        }
        return true;
        break;
      }else{
        return false; 
        break;
      }
    }
  }
}

void volt_cal () {
  
  float volt_coef = 1.89;
  float current_coef = 1;

  
  volt_reg   =  hlw[2] * 256L * 256L + hlw[3] * 256L + hlw[4];  //necessario adicionar o L para dizer que o numero é um long. Se não convete para int no compilador
  volt_value =  hlw[5] * 256L * 256L + hlw[6] * 256L + hlw[7];
  volt_co = (volt_reg / volt_value) * volt_coef;

  current_reg = hlw[8] * 256L * 256L + hlw[9] * 256L + hlw[10];
  current_value = hlw[11] * 256L * 256L + hlw[12] * 256L + hlw[13];
  current_co = (current_reg / current_value) * current_coef;

  power_reg = hlw[14] * 256L * 256L + hlw[15] * 256L + hlw[16];
  power_value = hlw[17] * 256L * 256L + hlw[18] * 256L + hlw[19];
  power_co = (power_reg / power_value) * volt_coef * current_coef;

  app_power = volt_co * current_co ;

  power_factor = power_co / app_power;

  
  pulse = hlw[21] * 256L + hlw[22];

  pwr_used = (256L* 256L * 2 + pulse) * ( power_reg * volt_coef * current_coef ) / (3600 * 1000000000);


data_estado = hlw[0];
data_check = hlw[1];
data_reg = hlw[20];
data_sum = hlw[23];


}


bool checksum(){ // esta função faz a checagem dos dados por checksum, se os dados estiverem corrompidos são descartados.
soma =0;    
for(int u=2;u<=22; u++){ //Checksum soma os bits 2 ao 22 e exibe a soma no bit 23. 
  soma=soma+hlw[u];
}
soma = (soma & 0xFF); //A soma irá dar um valor maior do que o bit é capaz de exibir, então é aplicada uma mascara. 
       if (hlw[23] == soma)
{
  return true; //retorna verdadeiro se checksum for igual a soma dos bits 2 até 22
}else{
return false; //Retorna falso se houve algum erro, não pode deixar flutuante. 
}
}
 
void loop() {
  if (read_data () && checksum()) {  //Se foi possivel fazer uma leitura a função volta verdadeira.
    volt_cal (); //É feito os calculos dos valores coletados

    Serial.print("Tensão: ");
    Serial.println(volt_co);
    Serial.print("Corrente: ");
    Serial.println(current_co);
    Serial.print("Potência: ");
    Serial.println(power_co);
    Serial.print("Potência Aparente:" );
    Serial.println(app_power);
    Serial.print("Fator de Potência:" );
    Serial.println(power_factor);    
    Serial.print("Pulso:" );
    Serial.println(pulse);
    Serial.print("Potência usada:" );
    Serial.println(pwr_used);

    Serial.print("Data Estado:" );
    Serial.println(hlw[0], BIN);
    Serial.print("State REG:" );
    Serial.println(hlw[0], HEX);    
    Serial.print("CHECK REG:" );
    Serial.println(hlw[1], HEX);
    Serial.print("CHECKSUM REG:" );
    Serial.println(hlw[23], HEX);
    Serial.print("SOMA VERIFICA CHECKSUM:" );

    Serial.println(soma, HEX);

    if ((hlw[23]) == (soma & 0xFF))
{
    Serial.println("Bateu"); 
}else{
  for(int x=0;x<=10;x++){
    Serial.println("ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO, ERRO");
  }
}


    Serial.println("");
    Serial.println("");
    Serial.println("");
    Serial.println("");
  }
}

Responder