Autor Thema: TCRT5000­ erkennt Ferraris Scheibe nicht - was kann ich tun?  (Gelesen 2976 mal)

Offline Maui

  • Full Member
  • ***
  • Beiträge: 436
Antw:TCRT5000­ erkennt Ferraris Scheibe nicht - was kann ich tun?
« Antwort #15 am: 25 August 2018, 20:47:50 »
Danke dir. Endlich läuft es. Dein Modell hat echt noch optimierungsbedarf  ;)
Aber faul wie ich bin habe ich’s genommen und mit heisskleber nachgeholfen.
Jetzt mal die erste Nacht schauen, wie stabil es läuft. Aber bisher ziemlich gut.
Endlich kann ich da ein Haken hinter machen

Offline Maui

  • Full Member
  • ***
  • Beiträge: 436
Antw:TCRT5000­ erkennt Ferraris Scheibe nicht - was kann ich tun?
« Antwort #16 am: 26 August 2018, 11:46:54 »
So, nach der ersten Nacht kann ich sagen. Läuft super. Danke für deine Hilfe und den Code. Habe ihn fast 1:1 laufen, habe nur die Kalibrierung raus genommen und den Counter von 2 auf 4 gesetzt. Habe einen Zähler mit 75 Umdrehungen bis zur kWh und hab mal grob überschlagen. Im schlimmsten Fall bekomme ich nur 8 Signale mit red. Du hast ja einen 150er Zähler, also quasi das gleiche, da deiner doppelt so schnell dreht. Auch die Berechnungen habe ich von dir übernommen und angepasst. Ich bau mir jetzt noch eine für Standby (Strom nachts) und eine für den letzten Tag. Stelle ich dann noch hier rein, falls es mal wem hilft.
Bei mir ist zwar der Abstand zwischen rot und silber ziemlich klein, 8 gegen 12, aber dafür läuft es erstaunlich sauber.

Offline Dr.E.Witz

  • Jr. Member
  • **
  • Beiträge: 68
Antw:TCRT5000­ erkennt Ferraris Scheibe nicht - was kann ich tun?
« Antwort #17 am: 02 April 2019, 23:08:11 »
Hallo mal wieder.
Ich hatte die letzten Tage bissi mehr zu tun und konnte nicht mehr wirklich basteln geschweige denn mich melden.

Einen Schaltplan habe ich mal gezeichnet und auch meinen aktuellen Quellcode werde ich anhängen. Evtl. kann jemand etwas damit anfangen.
Ganz nach dem Motto "der Schuster hat die schlechtesten Schuhe" den Quellcode mit Bedacht einsetzen wobei es mittlerweile echt gut läuft ;)


SourceCode:


Hättest Du noch die verwendeten Bauteile?
Danke und Gruß,

Michael
#include "ESP8266WiFi.h"
#include <PubSubClient.h>

WiFiClient espClient;
PubSubClient client(espClient);



#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)



// WiFi parameters
const char* ssid = "SID";
const char* password = "HierKönnteIhrPasswortStehen";
const char* mqtt_server = "Zahlen oder Hostname";


const char* outTopicPower     = "MCU_STROM/CurrentPower";
const char* outTopicTrigger   = "MCU_STROM/Trigger";
const char* outTopicLight     = "MCU_STROM/LightValue";
const char* inTopicValueRed   = "MCU_STROM/ValueRed";
const char* inTopicValueSilver= "MCU_STROM/ValueSilver";
const char* inTopicDebugMode  = "MCU_STROM/DebugMode";


int sensorValue;


int sensorValueRed;
int sensorValueSilver;

int sensorValueAutoCalibrationCheckTimes = 50;

int sensorValueRedAutoCalibration = 0;
int sensorValueRedAutoCalibrationTimes = 0;

int sensorValueSilverAutoCalibration = 0;
int sensorValueSilverAutoCalibrationTimes = 0;


boolean oRedDetected = false;
boolean oDebugMode = false;

unsigned long timeStamp = 0;
unsigned long timeStampForCheck = 0;
unsigned long timeToCheck = 25;

int nBufferCountRed     = 0;
int nBufferCountSilver  = 0;
 
void setup ()
{
  Serial.begin (115200);

  sensorValueRed   = 58;
  sensorValueSilver  = 65;

  // Turn on LED for contrast
  pinMode(D1, OUTPUT);
  digitalWrite(D1, HIGH);

  Serial.println("Setup WiFi");
  setup_wifi();

  Serial.println("Setup Client");
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

  Serial.println("Setup done"); 
}

void loop ()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop(); 

  // read sensor every X milliseconds
  if (millis() - timeStampForCheck > timeToCheck)
  {
    timeStampForCheck = millis();
 
 
    // get value from sensor
    sensorValue = analogRead (A0);
 //   Serial.println (sensorValue, DEC);

    if (oDebugMode == true)
    {
      String sSensorValue = String(sensorValue);
      client.publish(outTopicLight, (char*)sSensorValue.c_str());
      Serial.println (sensorValue, DEC);
   }
 
    if (sensorValue <= sensorValueRed && oRedDetected == false)
    {
      if (nBufferCountRed >= 2)
      {
        oRedDetected = true;
   
        // set bufferCount to 0. Used to check before setting trigger to False.
        nBufferCountSilver = 0;
        nBufferCountRed = 0;
   
        // sent trigger value 1 to MQTT
        String sTriggerCount = "1";
        client.publish(outTopicTrigger, (char*)sTriggerCount.c_str());

        Serial.print("timeStamp: ");
        Serial.println(timeStamp);

       // start counting for energy
        if (timeStamp != 0)
        {
            unsigned long timeDiff = millis() - timeStamp;
            Serial.print ("timeDiff: ");
            Serial.println (timeDiff);
   
            float timeDiffSec = timeDiff / 1000;
   
            float energy = 0.006666 * (1/(timeDiffSec/3600));
            Serial.print ("energy in kW: ");
            Serial.println (energy);
   
            // get in Watt
            energy = energy * 1000;   
   
            String sCurrentPower = String(energy);
            client.publish(outTopicPower, (char*)sCurrentPower.c_str());

            // start auto calibration for red
            if (sensorValueRedAutoCalibrationTimes >= sensorValueAutoCalibrationCheckTimes)
            {
              Serial.print("use sensorValueRedAutoCalibration [");
              Serial.print(sensorValueRedAutoCalibration);
              Serial.print("] for red calculation divided by [");
              Serial.print(sensorValueAutoCalibrationCheckTimes);
              Serial.print("] and base value: ");
              Serial.println(sensorValueRed);
             
              // calculate new red value
              int sensorValueRedTemp = sensorValueRedAutoCalibration / sensorValueAutoCalibrationCheckTimes;
              if (sensorValueRedTemp < sensorValueSilver)
              {
                // check if the RED value is below Silver - otherwise it will not work ; )

               
                sensorValueRed = sensorValueRedTemp;
 
                // add buffer to actual value
                sensorValueRed = sensorValueRed + 3;
 
                sensorValueRedAutoCalibration = 0;
                     
                Serial.print("new calculated sensorValueRed: ");
                Serial.println(sensorValueRed);
                sensorValueRedAutoCalibrationTimes = 0;
               
                String sNewSensorValueRed = String(sensorValueRed);
                client.publish(inTopicValueRed, (char*)sNewSensorValueRed.c_str());                   
              }
              else
              {
                // cleanup values.
                sensorValueRedAutoCalibration = 0;
                sensorValueRedAutoCalibrationTimes = 0;
              }
         
            }
            else
            {
              // sum value to get average
              sensorValueRedAutoCalibration = sensorValueRedAutoCalibration + sensorValue;
              sensorValueRedAutoCalibrationTimes++;

              Serial.print("have now sensorValueRedAutoCalibration [");
              Serial.print(sensorValueRedAutoCalibration);
              Serial.print("] for the [ ");
              Serial.print(sensorValueRedAutoCalibrationTimes);
              Serial.print("] run for current sensor value: ");
              Serial.println(sensorValue);

            }
 
        }
   
        timeStamp = millis();
      }
      else
      {
        nBufferCountRed++;
      }

    }
    else if (sensorValue >= sensorValueSilver && oRedDetected == true)
    {
      // check 10 times before set trigger back to false
      if (nBufferCountSilver >= 5)
      {
        nBufferCountSilver = 0;
        nBufferCountRed = 0;
        oRedDetected = false;

        // start auto calibration for silver
        if (sensorValueSilverAutoCalibrationTimes >= sensorValueAutoCalibrationCheckTimes)
        {
          // calculate new red value
          Serial.print("use sensorValueSilverAutoCalibration [");
          Serial.print(sensorValueSilverAutoCalibration);
          Serial.print("] for silver calculation divided by [");
          Serial.print(sensorValueAutoCalibrationCheckTimes);
          Serial.print("] and base value: ");
          Serial.println(sensorValueSilver);

          int sensorValueSilverTemp = sensorValueSilverAutoCalibration / sensorValueAutoCalibrationCheckTimes;
          if (sensorValueSilverTemp > sensorValueRed)
          {
            // check if SILVER is above red value

            sensorValueSilver = sensorValueSilverTemp;
                           
            Serial.print("new calculated sensorValueSilver: ");
            Serial.println(sensorValueSilver);
            sensorValueSilverAutoCalibrationTimes = 0;

            // add buffer to actual value
            sensorValueSilver = sensorValueSilver - 3;
             
            sensorValueSilverAutoCalibration = 0;

            String sNewSensorValueSilver = String(sensorValueSilver);
            client.publish(inTopicValueSilver, (char*)sNewSensorValueSilver.c_str()); 
 
          }
          else
          {
            sensorValueSilverAutoCalibration = 0;
            sensorValueSilverAutoCalibrationTimes = 0;
          }
 
        }
        else
        {
          // sum value to get average
          sensorValueSilverAutoCalibration = sensorValueSilverAutoCalibration + sensorValue;
          sensorValueSilverAutoCalibrationTimes++;

          Serial.print("have now sensorValueSilverAutoCalibration [");
          Serial.print(sensorValueSilverAutoCalibration);
          Serial.print("] for the [ ");
          Serial.print(sensorValueSilverAutoCalibrationTimes);
          Serial.print("] run for current sensor value: ");
          Serial.println(sensorValue);
        }       
      }
      else
      {
        nBufferCountSilver++;
      }
     
    }

  }

}




void setup_wifi() {

  delay(50);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.hostname("ESP_STROM");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  delay(500);

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP_STROM")) {
      Serial.println("connected");

      client.subscribe(inTopicDebugMode);
      client.subscribe(inTopicValueRed);
      client.subscribe(inTopicValueSilver);
     
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 1,5 seconds");
      // Wait 5 seconds before retrying
      delay(1500);
    }
  }
}


void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");

  String sMessage = "";
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
    sMessage += (char)payload[i];
  }
  Serial.println();

  if (strcmp (topic, inTopicValueRed) == 0 )
  {
    sensorValueRed = sMessage.toInt();
   
  }
  else if (strcmp (topic, inTopicValueSilver) == 0 )
  {
    sensorValueSilver = sMessage.toInt();
  }
  else if (strcmp (topic, inTopicDebugMode) == 0 )
  {
    oDebugMode = sMessage == "1";
    Serial.print("oDebugMode ");
    Serial.println(oDebugMode);
  }


 
}




fhem Code:

## define Stromzähler NodeMCU MQTT Device

define MCU_STROM MQTT_DEVICE
attr MCU_STROM subscribeReading_CurrentPower MCU_STROM/CurrentPower
attr MCU_STROM subscribeReading_Trigger MCU_STROM/Trigger
attr MCU_STROM subscribeReading_LightValue MCU_STROM/LightValue
attr MCU_STROM subscribeReading_ValueRed MCU_STROM/ValueRed
attr MCU_STROM subscribeReading_ValueSilver MCU_STROM/ValueSilver

attr MCU_STROM publishSet_DebugMode MCU_STROM/DebugMode
attr MCU_STROM publishSet_ValueRed MCU_STROM/ValueRed
attr MCU_STROM publishSet_ValueSilver MCU_STROM/ValueSilver



define MCU_STROM_DEBUG dummy
attr MCU_STROM_DEBUG webCmd state
attr MCU_STROM_DEBUG setList state:on,off
define N_MCU_STROM_DEBUG_OM notify MCU_STROM_DEBUG:on set MCU_STROM DebugMode 1
define N_MCU_STROM_DEBUG_OFF notify MCU_STROM_DEBUG:off set MCU_STROM DebugMode 0


Viel Spaß und viele Grüße ; )
Christian


*Edit - Bugfix in der Auto-Calibration der Helligkeit des Sensors für Rot/Silber...*