Hey Guys,
sorry my english is not really better since last time. But give my Best !
I have a PH Elektrode with "Driver Board" and want to send the Value via MQTT to Home-Assistant.
Thats the plan but i m saddly not a Programmer and make this Hobby only recently for a couple of months.
I have a sketch from the PH seller and one from home assistant and make them once.
also ive edited this from esp2866wifi to arduino ethernet.
Now my problem is the "avergearray" ( i dont know what this is) where i cant solve i try it since Wednesday.
When i compile the Sketch the following issue will display:
/Users/nedsin/Documents/Arduino/mqtt_PH/mqtt_PH.ino: In function 'void loop()':
mqtt_PH:103: error: 'avergearray' was not declared in this scope
   voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
                        ^
No plan how to edit this to make it work.
There are a few more issues shown but i dont know if there go away when the array is solved.
Hope Someone have the Time to look at the sketches.
PH Sensor Sketch ---------- MQTT HASS Sketch
My Version till now :
#include <SPI.h>
#include <Ethernet.h>
// #include <Wire.h>
#include <PubSubClient.h>
#define SensorPin A5Â Â Â Â Â Â //pH meter Analog output to Arduino Analog Input 0
#define Offset 0.00Â Â Â Â Â Â //deviation compensate
#define LED 13
#define samplingInterval 20
#define printInterval 800
#define ArrayLenth 40  //times of collection
int pHArray[ArrayLenth];Â //Store the average value of the sensor feedback
int pHArrayIndex = 0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// use the numeric IP instead of the name for the server:
IPAddress server(192, 168, 0, 1); // numeric IP for Google (no DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 985);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
#define mqtt_server "192.168.0.987"
#define mqtt_user "us"
#define mqtt_password "pw"
#define ph_topic "spfo/sensor/ph"
// PubSubClient client(server, 1883, callback, ethClient);
EthernetClient ethClient;
PubSubClient mqttClient;
void setup() {
 {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("PH Sensor Started ...");  //Test the serial monitor
  while (!Serial) {
   ; // wait for serial port to connect. Needed for native USB port only
  }
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
   Serial.println("Scheiße kein Netzwerk !!!");
   Ethernet.begin(mac, ip);
  }
  delay(3000);
  Serial.println("Verbinde ... Juhu ...");
  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
   Serial.println("Verbunden, JUHUUUU ...");
   client.println("Connection: close");
   client.println();
   Serial.println("Na dann Heiter Weiter ...");
  } else {
   // if you didn't get a connection to the server:
   Serial.println("Mist, konnte nicht zum Broker Telefonieren ...");
  }
 }
 mqttClient.setClient(ethClient);
 mqttClient.setServer(mqtt_server, 1883);
 Serial.println(F("MQTT Client getartet ... Es geht aufwärts !!!"));
}
// Kunst oder kann das weg ?
bool checkBound(float newValue, float prevValue, float maxDiff) {
 return !isnan(newValue) &&
    (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}
//// Kunst Ende ///
long lastMsg = 0;
float PH = 0.0;
void loop(void)
{
 if (!client.connected()) {
  //  reconnect();
 }
 mqttClient.loop();
 {
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval)
  {
   pHArray[pHArrayIndex++] = analogRead(SensorPin);
   if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
   voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
   pHValue = 3.5 * voltage + Offset;
   samplingTime = millis();
  }
  if (millis() - printTime > printInterval) //Every 800 milliseconds, print a numerical, convert the state of the LED indicator
  {
   double avergearray(int* arr, int number) {
    int i;
    int max, min;
    double avg;
    long amount = 0;
    if (number <= 0) {
     Serial.println("Error number for the array to avraging!/n");
     Serial.print("Voltage:");
     Serial.print(voltage, 2);
     Serial.print("  pH value: ");
     Serial.println(pHValue, 2);
     digitalWrite(LED, digitalRead(LED) ^ 1);
     printTime = millis();
     return 0;
     long now = millis();
     if (now - lastMsg > 1000) {
      lastMsg = now;
      newph digitalRead(pHValue, 2);
      if (checkBound(newph)) {
       newph = (newph);
       Serial.print("PH-Wert Aktuell:");
       Serial.println(String(newph));
       mqttClient.publish("spfo/sensor/ph", String(newph), (true));
       {
        mqttClient.publish("spfo/sensor/ip", (localip));
       }
      } else {
       Serial.println("Unknown value");
       mqttClient.publish("spfo/sensor/nb", "Syntax Error");
      }
     }
     if (number < 5) { //less than 5, calculated directly statistics
      for (i = 0; i < number; i++) {
       amount += arr[i];
      }
      avg = amount / number;
      return avg;
     } else {
      if (arr[0] < arr[1]) {
       min = arr[0]; max = arr[1];
      }
      else {
       min = arr[1]; max = arr[0];
      }
      for (i = 2; i < number; i++) {
       if (arr[i] < min) {
        amount += min;   //arr<min
        min = arr[i];
       } else {
        if (arr[i] > max) {
         amount += max; //arr>max
         max = arr[i];
        } else {
         amount += arr[i]; //min<=arr<=max
        }
       }//if
      }//for
      avg = (double)amount / (number - 2);
     }//if
     return avg;
    }
   }
  }
 }
}
The Issues Part ( i think ) is a little bit under the void(loop) section:
 mqttClient.loop();
 {
  static unsigned long samplingTime = millis();
  static unsigned long printTime = millis();
  static float pHValue, voltage;
  if (millis() - samplingTime > samplingInterval)
  {
   pHArray[pHArrayIndex++] = analogRead(SensorPin);
   if (pHArrayIndex == ArrayLenth)pHArrayIndex = 0;
   voltage = avergearray(pHArray, ArrayLenth) * 5.0 / 1024;
   pHValue = 3.5 * voltage + Offset;
   samplingTime = millis();
  }
I really really grateful for every second who someone look at my Problem.
I take every littlest Note that could give me a way to go ...
Who could Solve This ???
Thank you ...
Best Wishes
Ned