Serial read string HEX ,find byte buffer to HEX to DEC as a float02

//code Cent0 by Github whit help Cattledog
// Import required libraries
//#include <Arduino.h>      //
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include "ESPAsyncWebServer.h"

                                                                //      my code
#include <SoftwareSerial.h>
SoftwareSerial SSerial(2, 3);
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
 
// SCL GPIO5
// SDA GPIO4
#define OLED_RESET 0  // GPIO0
Adafruit_SSD1306 display(OLED_RESET);

//int buffer[40];

int ProdI;
int VDc;
int VoltAc;
 int   Watt;
 int   VDC;
 int   Temp;
 int   WattHome;
 
const byte startMarker = 0x94;
const byte messageLength = 21;          //18 
byte receivedBytes[messageLength + 1]; //include start marker
boolean newData = false;
boolean newData2 = false;
static boolean recvInProgress = false;
//                                                                   Set your access point network credentials
const char* ssid = "XXX";//ESP8266-Access-Point
const char* password = "XXX";//123456789

// Set your Static IP address
IPAddress local_IP(192, 168, 23, 40);
// Set your Gateway IP address
IPAddress gateway(192, 168, 23, 3);

IPAddress subnet(255, 255, 255, 0);
//IPAddress primaryDNS(8, 8, 8, 8);   //optional
//IPAddress secondaryDNS(8, 8, 4, 4); //optional

//String WattHome 

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
String readTemp() {
   return String(WattHome);
 }
String readHumi() {
  return String(ProdI);                // return String(bme.readHumidity());
}
String readdc() {
  return String(VDc);                // aggiunto;
}
String readPres() {
  return String(VoltAc);
}

void setup(){
  // Serial port for debugging purposes
  Serial.begin(500000);   // 115200
  SSerial.begin(9600);
  Serial.println();
    // tolto messo nel loop
     // Setting the ESP as an access point
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open

  if (!WiFi.config(local_IP, gateway, subnet)) {    // , primaryDNS, secondaryDNS
    Serial.println("STA Failed to configure");
  }
  
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();



//  WiFi.softAP(ssid, password);

 // IPAddress IP = WiFi.softAPIP();
 // Serial.print("AP IP address: ");
 // Serial.println(IP);


  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 64x48)
  // init done
 
  display.display();
  delay(50);
}

void loop()
{
  recvBytesWithStartMarker();
 
  delay(10);

  showNewData();
 delay(10);
  
  if (newData2 == true)
 {
collectData();
//  printNewData();
  displayNewData();
  funzione();
  }
}

void recvBytesWithStartMarker()
{
newData2 = false;     // add for stable cycle loop LAST EDIT 17/12/2022
  static boolean recvInProgress = false;
  static int ndx = 0;
  byte rc;

  while (SSerial.available() > 0 && newData == false) 
  {
    rc = SSerial.read();

    if (recvInProgress == true)
    {
      receivedBytes[ndx] = rc;
    //  if (ndx == 8 and rc != 0x05)
    //  if ((ndx == 12 and rc != 0x04) and (ndx == 21 and rc != 0x34))
     if (ndx == 21 and rc != 0x34)
      {
  //__________________________________________________________________________________
       Serial.println("This just in false ");
    for (byte i = 0; i <= messageLength; i++)
    {
      Serial.print(receivedBytes[i], HEX);
      Serial.print(" ");
    }
//____________
        
        recvInProgress = false;
 //       newData = false;          // aggiunto _________________________
        ndx = 0;
      }
      else
        ndx++;
      if (ndx == messageLength + 1)
      {
        newData = true;
        
        recvInProgress = false;
        ndx = 0;
      }
    }
 //   else if (rc == startMarker || rc == 0x95 )
 //   else if ( rc == 0x95 )
      else if ((rc == 0x94)|| (rc == 0x95) || (rc == 0x96)  )
   // else if ( rc == 0x94 || rc == 0x95 || rc == 0x96 || rc == 0x93 )
    
    {
      recvInProgress = true;
      ndx = 0;  // save start marker
      receivedBytes[ndx] = rc;
      ndx++;
  // if (ndx == 1 and rc == 0x94 or rc == 0x95 && ndx == 13 and rc == 0x04)
  // if ((ndx == 12 and  rc != 0x04) and (ndx == 21 and rc != 0x34))
  //    {
  //    
  // recvInProgress = false;
      
        }
    }
  //__________________________________________________________________________________
 //       Serial.println("This just in false ");
 //   for (byte i = 0; i <= messageLength; i++)
 //   {
//      Serial.print(receivedBytes[i], HEX);
//      Serial.print(" ");
//    }
//__________________________________________________________________________________
   
  }
//}

void showNewData()
{
 // if ((newData == true) and (recvInProgress == true))
    if (newData == true) 
  {
    Serial.println("This just in ...OK ");
    for (byte i = 0; i <= messageLength; i++)
    {
      Serial.print(receivedBytes[i], HEX);
      Serial.print(" ");                        // IN LINEA
    }
     Serial.println(" ");
 
newData = false;
newData2 = true;
}
}

void collectData()
{
VoltAc = ((receivedBytes[1]*256+receivedBytes[2])/10);
ProdI =  ((receivedBytes[3]*256+receivedBytes[4])/10);
VDc =    ((receivedBytes[5]*256+receivedBytes[6])/8);
Temp =   ((receivedBytes[13]*256+receivedBytes[14])/24);
WattHome = (((int32_t)receivedBytes[15] << 24) |((int32_t)receivedBytes[16] <<16) |
             ((int32_t)receivedBytes[17] << 8 ) |(int32_t)receivedBytes[18])/10;
    }
void printNewData()
{
    Serial.println("_____________________");
    Serial.print("|Voltaggio AC|= ");
    VoltAc = ((receivedBytes[1]*256+receivedBytes[2])/10);
 //   if (VoltAc > 200 && VoltAc < 260) { 
    Serial.print(VoltAc,DEC);
    Serial.println(" V");
    

   // Serial.println("_____________________");
    Serial.println("|Produzione  |");
    Serial.print  ("|Istantanea  |= ");
    ProdI = ((receivedBytes[3]*256+receivedBytes[4])/10);
  //  ProdI = ((receivedBytes[4]*256+receivedBytes[5])/10);
    Serial.print(ProdI);
    Serial.println(" W");
  //  Serial.println("_____________________");
    Serial.print("|Voltaggio DC|= ");
    VDc =   ((receivedBytes[5]*256+receivedBytes[6])/8);
    Serial.println(VDc);
  //  Serial.println("_____________________");
  //  VDC  = (receivedBytes[6]*256+receivedBytes[7]);
 //   Serial.println(VDC);
 // Serial.println("_____________________");
  Serial.print("|Temperatura |= ");
  Temp = ((receivedBytes[13]*256+receivedBytes[14])/24);
  Serial.print(Temp);
   Serial.println("°C");
 // Serial.println("_____________________");
  Serial.println("|Consumo     |"); 
  Serial.print  ("|Istantaneo  |= ");
 // WattHome = ((receivedBytes[17]*256+receivedBytes[18])/10);
  WattHome = (((int32_t)receivedBytes[15] << 24) |((int32_t)receivedBytes[16] <<16) |
             ((int32_t)receivedBytes[17] << 8 ) |(int32_t)receivedBytes[18])/10;
  
  Serial.print(WattHome);
  Serial.println(" W");
  Serial.println("_____________________");
 //   Serial.println("This just in .please this work ??? please ? HOOOOOOOO.. ");
     
    
   
     Serial.println("INVERTER LOG");
}


void displayNewData()
 {

  display.display();  //  FA LAMPEGGIARE
  //delay(50);
 
  // Clear the buffer.
  display.clearDisplay();
 
  // text display tests
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Casa:" );
  display.print(WattHome);
  display.println("W" );
  display.print("VAc :" );
  display.print(VoltAc);
  display.println("V" );
  display.print("Temp:");
  display.print(Temp);
  display.println("C" );
  display.print("VDc :");
  display.print(VDc);
  display.println("V" );
  display.println("---------" );
  display.print("Prod:");
  display.print(ProdI);
  display.println("W" );

}

void funzione()

{ 
  server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){                //______________________
    request->send_P(200, "text/plain", readTemp().c_str());
  });
  server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){                 //___________________
    request->send_P(200, "text/plain", readHumi().c_str());
  });
    server.on("/vdc", HTTP_GET, [](AsyncWebServerRequest *request){                 //___________________
    request->send_P(200, "text/plain", readdc().c_str());
  });
  server.on("/pressure", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", readPres().c_str());                            //____________________
  });
  server.begin(); 
  newData2 = false;
} 

added video

Thank you for the complete code. Please continue to post complete code.

None of this code explains to me where the logdata shown in Cool Term comes from, and how it relates to the received data over software serial.

Can you explain more about the logdata.

I think you said that when the inverter is not active, the log data matches the serial output. During the day when the inverter is active and you get a valid message over Serial, is the data correct to the display on the inverter?

If you want to focus on the difference between the log data and the software serial data I would suggest that you simplify the program and eliminate the wireless transfer and the oled. Work just with Serial output of the the received data and a comparison to the log.

None of the video data presented so far appears to come from the last posted code.

One thing which will help clean up the serial output and make the valid(index21 = 0x34) data similar to the not valid data is to add a new line after the false message. It will make the serial output posted on the videos less confusing for me.

Serial.println("This just in false ");
        for (byte i = 0; i <= messageLength; i++)
        {
          Serial.print(receivedBytes[i], HEX);
          Serial.print(" ");
        }
        Serial.println(" "); //add this line

so that is is the same as

Serial.println("This just in ...OK ");
    for (byte i = 0; i <= messageLength; i++)
    {
      Serial.print(receivedBytes[i], HEX);
      Serial.print(" "); 
    }
    Serial.println(" ");

Although I don't think it is relevant to the log and the serial data, this looks wrong to me and the passing of the returns to the data sent over wireless will be hard to understand.

String readTemp()
{
return String(WattHome);
}

String readHumi()
{
return String(ProdI); // return String(bme.readHumidity());
}

String readdc()
{
return String(VDc); // aggiunto;
}

String readPres()
{
return String(VoltAc);
}
void loop()
{
  recvBytesWithStartMarker();
 
 // delay(10);

  showNewData();
 //delay(10);
  
  if (newData2 == true)
 {
//collectData();
//  printNewData();
//  displayNewData();
//  funzione();
  }
}

disable in this mode ?
and test ?

i wait the sun :frowning:
thanks for sugestion good night

Edit post
Test in the day same problem

19 12 2022

I have no idea why the inverter data read by the Arduino over a software serial connection and echoed back out to the Serial monitor looks so different from that show in the terminal program reading a log of the inverter output.

Furthermore, some of the data which is supposed to be a valid reading does not look very stable when there are three or four valid readings in a row.

I am really too far removed from the hardware to be of much assistance.

I tried to shorten the communication cables, it seems like a mirage, there wasn't much sun in today's video test, you can hear from the video that when the inverter makes a certain noise, the data seem to be wrong. Did you also find that the data captured by the serial seem to be correct? the pc slows down a bit after the capture operation for a long time. tomorrow i hope to try better shield the cables or twist?

Are you still working by issuing a command for data from the inverter and then reading the response (350 bytes?).
What happens if you only issue a request for data every 60 seconds?

The code in use is up post.not use receiving 350byte.
The original display send automatically request.is around 1sec.
A this time test with short cable.
And receiving approx one string just data around 7second to 15sec.
Need other time for check if work fine.
I need time for make new video in the day with sun.
Thank s
If have other suggestion for perfection my code. Thank you

thanks for the help, the code seems to work, I think the errors are due to some interference, we will do better tests with shielded cables. the code occasionally crashes
it depends on the connected devices, I also used static ip. then it will check on the serial to identify the error and if possible have a hand to solve it.
In the meantime would it be possible to create a routine to restart every 2 hours or so?
the only problem is that the reset must not take place when it receives the data otherwise it won't start,
thank you

I have semplified code
If remove lcd display the code work fine. .
I need more test in this day...i have used 0707 startbyte

Please post the complete code.
What processor are you using? The last posted code would indicate an esp8266.

In the meantime would it be possible to create a routine to restart every 2 hours or so?

See this tutorial on software reset of the 8266
https://circuits4you.com/2017/12/31/software-reset-esp8266/

Hello we have tested two code

EspMeterGtil2.ino

EspMeterGtil2new.ino

And have found this problem.

To next step we have connected esp8266 direct to pin uart
Mcu stm.

I need new test with summer day.
I have enabled Exception in the arduino ide

Thank you🙏

I have a question, inserting this check never seems to enter the if.
Am I spelling it wrong?

if ((ndx == 21 and rc == 0x34) && (ndx == 15 and rc == 0x00))

when I receive the printout from the serial number, only a ZERO (0) is printed for receivedbyte 15 this condiction is ok ? (ndx == 15 and rc == 0x00)
or need this ? (ndx == 12 and rc == 0x0)

new update work ...more test for stability
i heve used this (receivedBytes[15] == 0x00)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.