Next stupid problem connecting to IOT Cloud

Basically I had a working script that connected to my WiFI and did the OTA flawlessly.

But my goal was to implement a connection to arduino IOT so I'm able to open my door from my mobile phone as well when I press a button on it.

And as soon as I did use that

ArduinoCloud.begin(ArduinoIoTPreferredConnection);

I didnt get a connectin to my WiFi any longer.

And here is the content of my thingproperties.h file:

// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char DEVICE_LOGIN_NAME[]  = "03e24af3-6ecd-49d2-ba8f-5a55deef6956";

const char SSID[]               = SECRET_SSID;    // Network SSID (name)
const char PASS[]               = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)
const char DEVICE_KEY[]  = SECRET_DEVICE_KEY;    // Secret device password

void onDoorOnlineOpenChange();

bool DoorOnlineOpen;
bool DoorOpenOnlineLED;

void initProperties(){

  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(DoorOnlineOpen, READWRITE, ON_CHANGE, onDoorOnlineOpenChange);
  ArduinoCloud.addProperty(DoorOpenOnlineLED, READ, ON_CHANGE, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

and my passwords are in the arduino_secrets.h and are correct.

And just to mention:
I only want to figure out how to get the device connected to my WiFi and IOT correctly.
The OTA is a secondary problem for later.
As of now with

ArduinoCloud.begin(ArduinoIoTPreferredConnection);

I dont get a connection to my WiFi any longer.

Please provide the full contents of the .ino file of your sketch.

THIS is my connection test script (Thats the script the properties.h i posted above is for ( thought I did post it already) that I want to connect to IOT (basically after it works I want to implent the connection in the script below finally)

#include "arduino_secrets.h"
#include "thingProperties.h"
#include <ArduinoOTA.h>

#define OTA_Passwort "grk"

void setup() {
  // Defined in thingProperties.h
  initProperties();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(14, OUTPUT);

  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
  digitalWrite(14, LOW);
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  // OTA Update starten
  ArduinoOTA.setHostname("Testschloss");
  ArduinoOTA.setPassword(OTA_Passwort);
  ArduinoOTA.begin();
  ArduinoOTA.handle();  // OTA Update starten
}

void loop() {
  ArduinoOTA.handle();  // OTA Update starten
  ArduinoCloud.update();
}

void onDoorOnlineOpenChange()  {
  if (DoorOnlineOpen == 1) {
    Open();
  }
  DoorOpenOnlineLED = 0;
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
  digitalWrite(14, LOW);
}

void Open() {
  DoorOpenOnlineLED = 1;
  ArduinoCloud.update();
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED on
  digitalWrite(14, HIGH);
  delay(3000);
}



/*

#include "thingProperties.h"

void setup() {
  // Defined in thingProperties.h
  initProperties();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(14, OUTPUT);

  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
  digitalWrite(14, LOW);
  
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
}

void loop() {
  ArduinoCloud.update();
}

void onDoorOnlineOpenChange()  {
  if (DoorOnlineOpen == 1) {
    Open();
  }
  DoorOpenOnlineLED = 0;
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
  digitalWrite(14, LOW);
}

void Open() {
  DoorOpenOnlineLED = 1;
  ArduinoCloud.update();
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED on
  digitalWrite(14, HIGH);
  delay(3000);
}

*/

And THIS is my formerly working script (Connecting to my WIFI and OTA working fine) (btw, forget my old IOT stuff in this code I did try to implement IOT alredy before without any success ant thats crap from that)

/*  TODO:
    1) Online Variable Stromausfall
    2) Online Türöffnung



Anleitung: 

Startsequenz:
Power An - LED grün
Wlan verbunden - LED aus
Initialisiert und startklar - LED auf entsprechender Farbe

Delete Data: 
Beim Einschalten Türöffner Taste gedrückt halten oder...
AdminFinger draufhalten bis es rot blinkt, Finger entfernen. Alle Finger werden gelöscht und resettet. Danach wird sofort nach Neustart der Adminfinger zum einlesen angefordert (lila Blinken).

Add Fingerprint:
AdminFinger draufhalten bis es lila blinkt, Finger entfernen, neuen Fingerabdruck einlesen, Finger entfernen, erneut neuen Fingerabdruck einlesen. Bestätigung ist blaues waberndes Licht 
*/



#include <WiFi.h>
//#include <Arduino_ConnectionHandler.h>
//#include <ArduinoIoTCloud.h>
#include <ArduinoOTA.h>
#include <HardwareSerial.h>
#include <Bounce2.h>
#include <AccelStepper.h>
#include <Adafruit_Fingerprint.h>
#include <DYPlayerArduino.h>
#include <EEPROM.h>

// Passwörter (WLAN etc.)
#define WLAN_Name "SSID"
#define WLAN_Passwort "WIFIPASS"
#define OTA_Passwort "OTAPASS"

// Lautsprecher Lautstärke
#define Speaker_Volume 18  // 100% Lautstärke = 30
#define Sound_Random 1     // 0 = Sound wird der Reihenfolge nach abgespielt  1 = Sound wird zufällig abgespielt

//Alarmcounter
#define ACount 5  // Ab wievieltem Finger-Fehlversuch soll erstmalig Alarm ausgelöst werden (Reset durch erfolgreiche Türöffnung)

// Liste der Sounds
char *Sound[] = {
  "/Sound/1.mp3",  // AkteX
  "/Sound/2.mp3",
  "/Sound/3.mp3",
  "/Sound/4.mp3",
  "/Sound/5.wav",  // Wölfe
  "/Sound/6.mp3",
  "/Sound/7.mp3",
  "/Sound/8.mp3",
};
size_t Playlistsize = sizeof(Sound) / sizeof(Sound[0]);
int AktuellerSound = 0;

//Serielle Schnittstelle2 initialisieren
HardwareSerial SerialPort1(1);  // use UART1
HardwareSerial SerialPort2(2);  // use UART2

// Variable für Online (ioT Cloud)
bool DoorOnlineOpen;
bool OpenLED;
bool StromausfallSpeicher;

// Variablen Fingerprint Sensor
byte AlarmCount = 0;
uint8_t id;
byte FingerID = 0;
byte AdminFinger = 0;
void (*resetFunc)(void) = 0;  //declare reset function at address 0
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&SerialPort2);

// Variablen Schloss
const byte DoorOPENAlarm = 14;   // Reed Kontakt HIGH = Tür geöffnet, LOW = Tür geschlossen
const byte LockDoorSwitch = 22;  // LOW = Schnapper, HIGH = Doppelt verriegelt                                                  ??????????????????? stimmt???
const byte OpenDoorButton = 23;  // Taster zum Tür öffnen (innen) LOW = gedrückt

Bounce DBDoorOPENAlarm = Bounce();   // 3 Debounce objecte für Taster/Schalter initialisieren
Bounce DBLockDoorSwitch = Bounce();  // 3 Debounce objecte für Taster/Schalter initialisieren
Bounce DBOpenDoorButton = Bounce();  // 3 Debounce objecte für Taster/Schalter initialisieren

// Variablen Motor
const bool EEpromOn = 0;  // Soll Stromausfall während Bewegung detektiert werden? Achtung nur ca 10.000 Zyklen möglich! 0=AUS 1=AN

const byte MicroSteps = 4;                  // Eingestellte Microsteps am Stepper Controller
const byte MotorSteps = 200;                // 200 Hardware-Steps per Umdrehung des Motors
const int Steps = MotorSteps * MicroSteps;  // Gesamte reelle Steps Pro Umdrehung

const byte StepPin = 18;
const byte DirPin = 5;
const byte ENPin = 19;

#define Zahnradfaktor 3                                // 20 Zähne auf 60 Zähne übersetzt am GT2 Riemen = Faktor 3
const int Verriegeln = 2 * Steps * Zahnradfaktor;      // Anzahl Umdrehungen zum Verriegeln (aus Nullstellung des Schlüssels)    (alles negativ,da 2 Zahnräder die Richtung imvertieren!)
const int Oeffnen = -68 * MicroSteps * Zahnradfaktor;  // Anzahl Motorsteps * MicroSteps zum öffnen des Schnappers (aus Nullstellung des Schlüssels)       //////////////////////// NOCH ANPASSEN !!!!!!

AccelStepper Motor(1, StepPin, DirPin);  // AccelStepper Objekt Motor erstellen 1= Motortreiber verwenden

// Schlüsselpositionen
#define Nullstellung 0
#define Verriegelt 1
bool SchluesselPos = Nullstellung;  // 0=Schlüssel Nullstellung (Schnapper zu), 1=Schlüssel verriegelt

// Variablen LED
const byte LEDRed = 27;
const byte LEDGreen = 26;

// Musik initialisieren
DY::Player Music(&SerialPort1);  // Initialise the player on UART1, pins TX: 33, RX: 25.

void setup() {
  // LEDs initialisieren
  pinMode(LEDRed, OUTPUT);
  pinMode(LEDGreen, OUTPUT);
  digitalWrite(LEDRed, HIGH);   // HIGH = AUS
  digitalWrite(LEDGreen, LOW);  // LED auf Grün

  // Serielle Schnittstellen initialisieren
  Serial.begin(115200);                          // Serielle Ausgabe an
  SerialPort1.begin(9600, SERIAL_8N1, 33, 25);   // Serielle Schnittstelle 1 für Audio an
  SerialPort2.begin(57600, SERIAL_8N1, 16, 17);  // Serielle Schnittstelle 2 für Fingerprint Sensor an
  delay(1000);                                   // 1 Sek warten auf Schnittstelle

  // Sound initialisieren
  Music.begin();
  delay(100);                                // Delay notwendig, damit Lautstärkeeinstellung funktioniert
  Music.setVolume(Speaker_Volume);           // Oben definiert bei define
  Music.setCycleMode(DY::PlayMode::OneOff);  // File einmal abspielen, dann stop

  //WLAN verbinden
  WiFi.begin(WLAN_Name, WLAN_Passwort);
  while (WiFi.status() != WL_CONNECTED) {
    delay(200);
    digitalWrite(LEDGreen, !digitalRead(LEDGreen));  // LED invertieren (blinken)
    Serial.println("Verbindung wird hergestellt...");
  }
  Serial.println("WLAN Verbunden!");
  digitalWrite(LEDGreen, HIGH);  // LED aus

  /*
  ArduinoCloud.setBoardId(BoardID);
  ArduinoCloud.setSecretDeviceKey(SecretKey);
  ArduinoCloud.addProperty(DoorOnlineOpen, READWRITE, ON_CHANGE, onDoorOnlineOpenChange);
  ArduinoCloud.addProperty(OpenLED, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(StromausfallSpeicher, READWRITE, ON_CHANGE, NULL);

  WiFiConnectionHandler ArduinoIoTPreferredConnection(WLAN_Name, WLAN_Passwort);
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
*/

  // OTA Update einrichten
  ArduinoOTA.setHostname("Tuerschloss");
  ArduinoOTA.setPassword(OTA_Passwort);
  ArduinoOTA.begin();
  ArduinoOTA.handle();  // OTA Update starten

  // Taster uns Schalter initialisieren
  DBDoorOPENAlarm.attach(DoorOPENAlarm, INPUT_PULLUP);  // 0 = Tür geschlossen, 1 = Tür offen
  DBDoorOPENAlarm.interval(5);
  DBLockDoorSwitch.attach(LockDoorSwitch, INPUT_PULLUP);  // 0 = Schalter Position                                ????????????????????????????? stimmt?
  DBLockDoorSwitch.interval(5);
  DBOpenDoorButton.attach(OpenDoorButton, INPUT_PULLUP);  // 0 = Taster gedrückt
  DBOpenDoorButton.interval(5);

  FingerprintSetup();  // Fingerprint Sensor initialisieren

  // Falls beim Einschalten Open-Taste gedrückt gehalten wird Fingerprint-Reset durchführen
  DBOpenDoorButton.update();            // Auslesen mit "!DBOpenDoorButton.read()" (INVERTIERT! Dann 1 = gedrückt)
  if (!DBOpenDoorButton.read() == 1) {  // HIGH = Taste gedrückt
    for (int i = 0; i <= 20; i++) {
      digitalWrite(LEDGreen, 0);  // LED grün AUS
      digitalWrite(LEDRed, 1);    // LED rot An
      delay(150);
      digitalWrite(LEDGreen, 1);  // LED grün AN
      digitalWrite(LEDRed, 0);    // LED rot AUS
      delay(150);
    }
    deletedata();
  }

  ////////////////////////////////////////////////////////////////////////////////////////////// STROM-AUS VARIABLE ONLINE prüfen!

  // Falls EEprom aktiv, initialisieren, einlesen und auf Stromausfall prüfen
  if (EEpromOn == 1) {
    EEPROM.begin(4);  // EEprom initialisieren 4 Byte lang (minimum)
    // Für EEPROM resett nach Stromausfall Kommentar dieser 2 Zeilen einmalig entfernen
    /* 
    EEPROM.write(0, 255);  // Zum Schluss EEprom wieder auf 255 setzen
     EEPROM.commit();
    */
    if (EEPROM.read(0) == 0) {
      Error();  // EEprom war auf 0 also Stromausfall während Bewegung (Position unsicher, abschalten!)
    }
  }

  // Motorvariablen initialisieren
  pinMode(StepPin, OUTPUT);
  pinMode(DirPin, OUTPUT);
  pinMode(ENPin, OUTPUT);

  Motor.setEnablePin(ENPin);
  Motor.setMaxSpeed(6000);                    // 6000-8000 (max. ca. 8000) [war 6000]
  Motor.setAcceleration(2000);                // 1000 Steps/Sek = 1 Sek bis volle Geschwindigkeit. 4000 = 1/4 Sek [war 4000]
  Motor.setPinsInverted(false, false, true);  // Enable muss low sein um an zu sein
}


void loop() {
  ArduinoOTA.handle();  // OTA Update starten

  // ArduinoCloud.update();  // Online Taste aktualisieren

  DBLockDoorSwitch.update();              // Auslesen mit "!DBLockDoorSwitch.read()" (INVERTIERT! Dann 1= Schalter auf AN)
  if (!DBLockDoorSwitch.read() == 0) {    // Wenn Schalter auf Schnapper steht
    if (SchluesselPos == Nullstellung) {  // Falls schon auf Nullstellung
      digitalWrite(LEDRed, LOW);          // LED auf gelb
      digitalWrite(LEDGreen, LOW);
    } else if (SchluesselPos == Verriegelt) {  // Falls verriegelt entriegeln und LED auf gelb
      Serial.println("MOTOR AN");
      Motor.enableOutputs();
      Motor.runToNewPosition(Nullstellung);
      Motor.disableOutputs();
      Serial.println("MOTOR AUS");
      SchluesselPos = Nullstellung;
      digitalWrite(LEDRed, LOW);  // LED auf gelb
      digitalWrite(LEDGreen, LOW);
    }
  } else if (!DBLockDoorSwitch.read() == 1) {
    if (SchluesselPos == Verriegelt) {  // Falls schon verriegelt
      digitalWrite(LEDRed, LOW);        // LED auf rot
      digitalWrite(LEDGreen, HIGH);
    } else if (SchluesselPos == Nullstellung) {  // Falls noch auf Nullstellung und verriegeln eingestellt, auf eingestellte Position fahren
      Serial.println("MOTOR AN");
      Motor.enableOutputs();
      Motor.runToNewPosition(Verriegeln);
      Motor.disableOutputs();
      Serial.println("MOTOR AUS");
      SchluesselPos = Verriegelt;
      digitalWrite(LEDRed, LOW);  // LED auf rot
      digitalWrite(LEDGreen, HIGH);
    }
  }

  DBOpenDoorButton.update();  // Auslesen mit "!DBOpenDoorButton.read()" (INVERTIERT! Dann 1 = Öffnen)
  if (!DBOpenDoorButton.read() == 1) {
    DoorOpen();
    DBOpenDoorButton.update();  // Nach DoorOpen() Nochmal updaten ansonsten fährt er 2x durch DoorOpen()
  }

  // START FINGERPRINT SENSOR LOOP
  getFingerprintID();  // Check Fingerprint
  Checkfinger();       // Entsprechend erkanntem Print handeln
}


void FingerprintSetup() {
  Serial.println("\n\nFinger detect test");
  finger.begin(57600);  // set the data rate for the sensor serial port
  delay(5);

  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor!");
    Error();
  }

  // LEDs aus falls noch an
  finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_BLUE);
  finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_RED);
  finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_PURPLE);

  // Einschaltsignal 10x15ms blau
  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 15, FINGERPRINT_LED_BLUE, 10);

  Serial.println(F("Reading sensor parameters"));
  finger.getParameters();
  Serial.print(F("Status: 0x"));
  Serial.println(finger.status_reg, HEX);
  Serial.print(F("Sys ID: 0x"));
  Serial.println(finger.system_id, HEX);
  Serial.print(F("Capacity: "));
  Serial.println(finger.capacity);
  Serial.print(F("Security level: "));
  Serial.println(finger.security_level);
  Serial.print(F("Device address: "));
  Serial.println(finger.device_addr, HEX);
  Serial.print(F("Packet len: "));
  Serial.println(finger.packet_len);
  Serial.print(F("Baud rate: "));
  Serial.println(finger.baud_rate);

  finger.getTemplateCount();

  if (finger.templateCount == 0) {
    Serial.print("Sensor doesn't contain any fingerprint Going to run 'enroll.");
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 100, FINGERPRINT_LED_RED, 50);
    delay(5000);
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_RED, 0);
    newfingerprint();
  } else {
    Serial.println("Waiting for valid finger...");
    Serial.print("Sensor contains ");
    Serial.print(finger.templateCount);
    Serial.println(" templates");
  }
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      FingerID = 0;
      AdminFinger = 0;
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  // fingerprint read ok

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }
  // fingerprint converted ok

  p = finger.fingerSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_RED, 10);
    AlarmCount += 1;             // Falscher Finger = AlarmCounter hochzählen
    if (AlarmCount >= ACount) {  // wenn Counter > Schwelle Alarm auslösen
      Music.setVolume(25);
      Music.playSpecifiedDevicePath(DY::Device::Sd, "/Alarm/Alarm.mp3");
      Music.setVolume(Speaker_Volume);
    }
    delay(1500);  // Delay 1,5 Sek, damit Finger nicht mehrfach erkannt wird
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }

  // found a match!
  Serial.print("Found ID #");
  Serial.print(finger.fingerID);
  Serial.print(" with confidence of ");
  Serial.println(finger.confidence);

  FingerID = finger.fingerID;

  return finger.fingerID;
}

// Add new fingerprint
void newfingerprint()  // run over and over again
{
  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 0);
  Serial.println("Ready to enroll a fingerprint!");
  Serial.println("Id will be given in order First finger print is Admin");
  finger.getTemplateCount();
  Serial.print("Sensor contains ");
  Serial.println(finger.templateCount);

  id = (finger.templateCount + 1);

  if (id == 0) {  // ID #0 not allowed, try again!
    Serial.println("first run setting id to 1");
    id = 1;
    return;
  }
  Serial.print("Enrolling ID #");
  Serial.println(id);

  while (!getFingerprintEnroll())
    ;
}

uint8_t getFingerprintEnroll() {

  int s = -1;
  Serial.print("Waiting for valid finger to enroll as #");
  Serial.println(id);

  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 0);
  while (s != FINGERPRINT_OK) {
    ArduinoOTA.handle();  // OTA Update starten während auf Finger getestet wird
    s = finger.getImage();
    switch (s) {
      case FINGERPRINT_OK:
        Serial.println("Image taken");
        finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 1);
        delay(2000);
        break;
      case FINGERPRINT_NOFINGER:
        break;
      case FINGERPRINT_PACKETRECIEVEERR:
        Serial.println("Communication error");
        break;
      case FINGERPRINT_IMAGEFAIL:
        Serial.println("Imaging error");
        break;
      default:
        Serial.println("Unknown error");
        break;
    }
  }

  // OK success!

  s = finger.image2Tz(1);
  switch (s) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return s;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return s;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return s;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return s;
    default:
      Serial.println("Unknown error");
      return s;
  }

  // Cherck if Finger already exists in Database and skip if so
  s = finger.fingerSearch();
  if (s == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find an existing match");
  } else if (s == FINGERPRINT_OK) {
    Serial.print("Found ID #");
    Serial.println(finger.fingerID);
    Serial.println("Known Fingerprint detected, skipping");
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 30, FINGERPRINT_LED_RED, 5);
    delay(2000);
    return s;
  }  // End of Check

  Serial.println("Remove finger");
  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_BLUE, 0);
  s = 0;
  while (s != FINGERPRINT_NOFINGER) {
    s = finger.getImage();
  }
  Serial.print("ID ");
  Serial.println(id);
  s = -1;
  Serial.println("Place same finger again");
  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 0);
  while (s != FINGERPRINT_OK) {
    s = finger.getImage();
    switch (s) {
      case FINGERPRINT_OK:
        Serial.println("Image taken");
        finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_PURPLE, 1);
        break;
      case FINGERPRINT_NOFINGER:
        break;
      case FINGERPRINT_PACKETRECIEVEERR:
        Serial.println("Communication error");
        break;
      case FINGERPRINT_IMAGEFAIL:
        Serial.println("Imaging error");
        break;
      default:
        Serial.println("Unknown error");
        break;
    }
  }

  // OK success!

  s = finger.image2Tz(2);
  switch (s) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return s;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return s;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return s;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return s;
    default:
      Serial.println("Unknown error");
      return s;
  }

  // OK converted!
  Serial.print("Creating model for #");
  Serial.println(id);

  s = finger.createModel();
  if (s == FINGERPRINT_OK) {
    Serial.println("Prints matched!");
  } else if (s == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return s;
  } else if (s == FINGERPRINT_ENROLLMISMATCH) {
    Serial.println("Fingerprints did not match");
    newfingerprint();
    // return s;
  } else {
    Serial.println("Unknown error");
    return s;
  }

  Serial.print("ID ");
  Serial.println(id);
  s = finger.storeModel(id);
  if (s == FINGERPRINT_OK) {
    Serial.println("Stored!");

    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_BLUE, 20);
    finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_ON, 200, FINGERPRINT_LED_BLUE);
    delay(2000);
    finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_OFF, 200, FINGERPRINT_LED_BLUE);
    delay(2000);
    finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_ON, 200, FINGERPRINT_LED_BLUE);
    delay(2000);
    finger.LEDcontrol(FINGERPRINT_LED_GRADUAL_OFF, 200, FINGERPRINT_LED_BLUE);
    delay(2000);

  } else if (s == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return s;
  } else if (s == FINGERPRINT_BADLOCATION) {
    Serial.println("Could not store in that location");
    return s;
  } else if (s == FINGERPRINT_FLASHERR) {
    Serial.println("Error writing to flash");
    return s;
  } else {
    Serial.println("Unknown error");
    return s;
  }
  return true;
}

void deletedata() {
  finger.emptyDatabase();
  Serial.println("All fingerprints Deleted");
  finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 5, FINGERPRINT_LED_RED, 0);
  Serial.println("Doing a soft reset of the arduino");
  resetFunc();  //call reset
}

void Checkfinger() {
  if (FingerID == 1) {  // Adminfinger erkannt
    Serial.println("Admin Fingerprint detected");
    AlarmCount = 0;  // Alarmcounter zurücksetzen
    AdminFinger = (AdminFinger + 1);
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_BLUE, 1);
    if (AdminFinger == 5) {
      finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_PURPLE);
      delay(1000);
      getFingerprintID();
      if (FingerID == 0) {
        Serial.println("ADD NEW USER");
        newfingerprint();
      }
    }
    if (AdminFinger >= 10) {
      finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_RED);
      delay(1000);
      getFingerprintID();   // Prüfen ob Finger losgelassen
      if (FingerID == 1) {  // Wenn nicht, löschen und Reset
        Serial.println("DELETE ALL USERS");
        deletedata();
      }
      Serial.println("Doing a soft reset of the arduino");
      resetFunc();  //call reset
    }
  } else if (FingerID != 0) {
    Serial.println("Normal fingerprint detected");
    finger.LEDcontrol(FINGERPRINT_LED_FLASHING, 25, FINGERPRINT_LED_BLUE, 5);
    DoorOpen();
  }
}

void DoorOpen() {
  if (EEpromOn == 1) {   // Falls EEpromON Variable gesetzt
    EEPROM.write(0, 0);  // Vor Beginn EEprom auf 0 setzen (Stromausfallerkennung)
    EEPROM.commit();
    Serial.print("EEprom: ");
    Serial.println(EEPROM.read(0));
  }

  /////////////////////////////////////////////////////////////////////////////////////////// STROM-AUS VARIABLE ONLINE SPEICHERN!

  AlarmCount = 0;  // Alarmcounter zurücksetzen, da Tür erfolgreich geöffnet wurde
  Serial.println("MOTOR AN");
  Motor.enableOutputs();
  Serial.println("MOTOR öffnet");
  Motor.runToNewPosition(Oeffnen);
  digitalWrite(LEDRed, HIGH);  // LED auf grün
  digitalWrite(LEDGreen, LOW);
  finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_BLUE);  // Fingerprint LED AN

  while (DBDoorOPENAlarm.read() == 0) {  // Warten bis Tür geöffnet wurde
    DBDoorOPENAlarm.update();
    //////////////////////////////////////// Ggf. Zeit einfügen nach der autom. verriegelt wird
  }

  Music.playSpecifiedDevicePath(DY::Device::Sd, Sound[AktuellerSound]);  // Titel abspielen
  if (Sound_Random == 0) {                                               // Wenn Sound nach Playlist abspielt
    if (AktuellerSound == (Playlistsize - 1)) {                          // Wenn letzter Sound der Liste gerade gespielt wurde, wieder zum ersten springen, ansonsten zum nächsten in der Liste spulen
      AktuellerSound = 0;
    } else {
      AktuellerSound += 1;
    }
  } else if (Sound_Random == 1) {  // Wenn Sound zufällig ausgewählt abspielt
    AktuellerSound = random(0, (Playlistsize - 1));
    Serial.print("Nächster Sound ist Playlist Nr: ");
    Serial.println(AktuellerSound);
  }

  Serial.println("MOTOR Fährt auf Schnapper");
  Motor.runToNewPosition(Nullstellung);  // Sobald Tür geöffnet wurde Schnapper freigeben
  SchluesselPos = Nullstellung;
  finger.LEDcontrol(FINGERPRINT_LED_OFF, 0, FINGERPRINT_LED_BLUE);
  digitalWrite(LEDRed, LOW);  // LED auf gelb
  digitalWrite(LEDGreen, LOW);

  while (DBDoorOPENAlarm.read() == 1) {
    DBDoorOPENAlarm.update();  // Warten bis Tür geschlossen wurde
  }

  DBLockDoorSwitch.update();  // Auslesen mit "!DBLockDoorSwitch.read()" (INVERTIERT! Dann 1= Schalter auf AN)
  if (!DBLockDoorSwitch.read() == 1) {
    delay(500);  // Sicherheitshalber delay damit Tür auch ganz zu ehe verschlossen wird
    Serial.println("MOTOR verriegelt da eingestellt");
    Motor.runToNewPosition(Verriegeln);  // Falls verriegeln eingestellt, auf eingestellte Position fahren
    SchluesselPos = Verriegelt;
    digitalWrite(LEDRed, LOW);  // LED auf rot
    digitalWrite(LEDGreen, HIGH);
  }

  Music.stop();  // Musik stoppen (sobald Tür zu)

  Serial.println("MOTOR AUS");
  Motor.disableOutputs();

  /////////////////////////////////////////////////////////////////////////////////////////// STROM-AUS VARIABLE ONLINE ZURÜCKSETZEN

  if (EEpromOn == 1) {     // Falls EEpromON Variable gesetzt
    EEPROM.write(0, 255);  // Zum Schluss EEprom wieder auf 255 setzen
    EEPROM.commit();
    Serial.print("EEprom: ");
    Serial.println(EEPROM.read(0));
  }
  return;
}

void onDoorOnlineOpenChange() {  // Add your code here to act upon DoorOnlineOpen change
                                 //if (DoorOnlineOpen == 1) {
  DoorOpen();
  //}
}

void Error() {
  Serial.println("FEHLER! MOTOR AUS, LED AN, PROGRAMMENDE BIS RESET!");
  finger.LEDcontrol(FINGERPRINT_LED_ON, 0, FINGERPRINT_LED_RED);
  Motor.disableOutputs();
  digitalWrite(LEDGreen, HIGH);  // grüne LED aus
  digitalWrite(LEDRed, LOW);     // LED rot an
  while (1) {                    // LED rot blinken
    delay(180);
    digitalWrite(LEDRed, !digitalRead(LEDRed));  // LED rot invertieren (blinken)
    ArduinoOTA.handle();                         // OTA Update starten
  }
}

Did edit the first code because I initially posted the wrong one.

You didn't fix the problem I explained two weeks ago in post #20:

Please read what I wrote there very carefully and fix the bug in your code.

If there is something you don't understand, then provide a detailed description of your doubt and I'll try to provide further assistance.

I understood what you said but as I said ITS NOT ABOUT OTA!

Using the test script above I cant connect to my WiFi and therefore not to Arduino IOT as well.

While the script below was working fine connecting to my WIFI and later doing normal OTA (but not IOT because it was not implemented back then).

And just to mention it doesnt connect to my wifi no matter how long it takes because I can see in my router its not connected even way later,

Ah ok I understand what you meant. But shouldnt it then connect way later and I see it in the router?

No. If your sketch program calls ArduinoOTA.begin(); before the Wi-Fi is in initialized, then you crash the processor on the ESP32 and it simply stops running the program forever after that. So it will never connect if your bugged code crashes the processor.

Ok now I got it working finally and I did'nt know until now that the ESP would crash in that case which explains the problems I had all the time finally.

Thanks very much for your help. Now the rest implementing it in my final code should work without problems.

Got just one question still:
I used this test code but when clicking the dashboard button the first time nothing happens.
Once I press the button second time lateron it works as it should an the online LED is lighting up correctly.

Can you find any reason in the following testcode or is that an issue with IOT?

#include "arduino_secrets.h"
#include "thingProperties.h"
#include <ArduinoOTA.h>

#define OTA_Passwort "grk"

void setup() {
  // Defined in thingProperties.h
  initProperties();

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(14, OUTPUT);

  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED off
  digitalWrite(14, LOW);

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  while (!ArduinoCloud.connected()) {
    ArduinoCloud.update();
  }

  // OTA Update starten
  ArduinoOTA.setHostname("Testschloss");
  ArduinoOTA.setPassword(OTA_Passwort);
  ArduinoOTA.begin();
  ArduinoOTA.handle();  // OTA Update starten
}

void loop() {
  ArduinoOTA.handle();  // OTA Update starten
  ArduinoCloud.update();
}

void onDoorOnlineOpenChange() {
  if (DoorOnlineOpen == 1) {
    Open();
  }
  DoorOpenOnlineLED = 0;
  digitalWrite(LED_BUILTIN, LOW);  // turn the LED off
  digitalWrite(14, LOW);
}

void Open() {
  DoorOpenOnlineLED = 1;
  ArduinoCloud.update();
  digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on
  digitalWrite(14, HIGH);
  delay(3000);
}

The code of your onDoorOnlineOpenChange function doesn't really make sense.

The essential thing to understand is that this function will be called whenever the value of the DoorOnlineOpen Variable changes. This means it is called once when you press the button on your mouse down while the mouse pointer is on the Button Widget in the dashboard (with the Variable value true), and then once again when you release the mouse button (with the Variable value false).

I see that you added logic to handle the call of the function with DoorOnlineOpen Variable value of false differently than when the value is true:

however, that only controls whether or not the Open function is called. The rest of the code in the function is always called. This can lead to pseudo-random results depending on how long you hold the button down.

Another problem is your use of a single ArduinoCloud.update(); call in the Open function to update the DoorOpenOnlineLED Variable:

ArduinoCloud.update is designed to be called continuously. It handles various tasks, and only performs a single task before returning. So this isn't guaranteed to update the dashboard. So you must redesign this code.

Part of that redesign is avoiding the use of long delay calls:

You must instead use millis, as explained here:

https://docs.arduino.cc/arduino-cloud/cloud-interface/variables/

I recommend you add Serial.println calls at various strategic locations in your sketch and then leave Serial Monitor open while your sketch is running. This will allow you to understand what the sketch program is doing.

Although it is not causing any problems currently, I also recommend removing the ArduinoOTA stuff from your sketch until you have the basic functionality working 100% perfectly. It is best to avoid introducing any unnecessary complexity while you are still trying to learn the fundamentals.

Thanks! That delay was only for testing purposes but the background explanaition is worthy.

1 Like

Just another question:

When compiling my now finished script online he cant find the DYPlayer.h
I can download and upload the script to my device in the IDE but what to do when a lib is missing online and why is the lib online missing at all?

Found out you can upload libraries.
So basically all clear now.

1 Like

For testing I changed my code and removed everything exept the minimum needed to run:

Loop:

void loop() {
  ArduinoOTA.handle();    // OTA Update starten
  ArduinoCloud.update();  // Online Taste aktualisieren
}

onDoorOnlineOpenChange():

void onDoorOnlineOpenChange() {  // Add your code here to act upon DoorOnlineOpen change
  if (DoorOnlineOpen == 1) {
    DoorOpenOnlineLED = 1;  // Online LED für Tür offen setzen
  }
  else if (DoorOnlineOpen == 0) {
    DoorOpenOnlineLED = 0;
  }
}

Basically even with that minimalistic code I need to press the button around 2 seconds before the LED in the dashboard lights up.

When I do not hold that button that long the LED wont light up at all.

Therefore it makes it basically random if the LED lights up after you press the dashboard button depending on how log you press.

On the other hand if I call my door open function at

if (DoorOnlineOpen == 1) {

the door always instantly opens when I press the dashboard button no matter how long I press it.

Thats not useable this way and to me it makes no sense at all why my door open function gets always and instantly called but the dashboard LED needs a press for around 2 seconds before it even lights up.

Maybe I dont see the easy solution and are doing it completely wrong.

Can you maybe show me how the onDoorOnlineOpenChange() function would need to look like to instantly light up the dashboard LED when I press the dashboard button do a function and go off instantly when I release the dashboard button?

I assume im completely on the wrong track.

EDIT:
Just to mention I need to let the OTA stuff in since by now the board is already built back into the electronics and not at my PC. So I need the OTA functionality to test around with it

Please provide the full content of the .ino file.

Please tell me us which "Variable update policy" you have configured for your DoorOpenOnlineLED Variable. You can get this information by the following procedure:

  1. Open the Thing in Arduino Cloud.
  2. Select the "Setup" tab at the top of the page.
  3. Click the DoorOpenOnlineLED entry in the list of Variables.

A "Variable Info" dialog will open. The dialog will show the Variable's update policy configuration.

I recommend you buy a spare board that you can use for development work.

Thats pretty crazy.

Yesterday and this morning I needed to press the button around 2 seconds before the dashboard would bring up the LED.
I yesterday tried update periodically every second as well as at change. Both with the same bad result.

As I was on my dogwalk today I read your reply and wanted to post my code here etc. when I get home.

Now as I m back I did load the script in my device again and suddenly it just works as expected NO CLUE WHY!
And it works in both varians suddenly wich neither did before (periodic or on change)!

I only have 2 Ideas what might be the reason:

There was an update of the connection handler library earlier today (no idea if that might have something to do with my problem).

Yesterday i got many disconnects from the internet and wondered because I got a 100MBit fiber cable into our house directly from a very reliable and professional provider that nearly never has any problems in his system.
Therefore then I looked into my router and saw someon were trying to log in using different names and passwords.
finally he somehow logged in with my personal credentials onto my router no idea how he could have gotten them.
That guy accessed my router all night until this morning when I changed my password and blocked access from the internet entirely lateron.
So maybe that guy did let a ddos tool run on my IP as target or any script to get into my router.

So my Idea is maybe he blocked up my internet with his scripts and therefore caused the LED problem maybe because my stuff couldnt get through?

Anyway now at least at the moment I got a working version from where I can go on and look if the problem will reappear once I put my code back in piece for piece.

Will tell if I still got problems later.

EDIT:
And yes I could take another of the same boards for testing purposes but then I'd have to setup another device and so on and manage 2 of them so they have exactly the same settings online etc.

Therefore when I can use the existing one and an able to comment stuff out for testing purposes I prefer that so I'm not bringing in another error source.

And basically the only difference in this case is 1 line for the OTA where I was sure is not the source of the problem.

This is great news! Thanks for taking the time to post an update. Although it is not so satisfying as finding the cause of the misbehavior, we can at least be glad it is working now.

You definitely want to use the "on change" policy for this application where you want immediate responsiveness to actions in the dashboard. Periodic is suitable for applications such as a monitoring system or data logger, where some latency in reacting to the value of a Variable changing on the Thing is fine.

1 Like

Yeah I set it to on change again I understand the difference.
Thanks for your help especially with the connection stuff I would have never figured it out.