WiFi.end() disables connection but not the Wifi Module

Okay, Ive thought it's maybe smart to start from the beginning. Ive wrote a script, which does what I want albeit without saving any power:

  • measure distance with an ultrasonic sensor
  • if the sensor measures a length below a set threshold, send an alarm sms to my phone

Now it's time to think about power savings! The script below has always WiFi on, but WiFi is only needed, when the webhook is needed. My idea is to use WiFi.end() in the beginning to disable the wifi module during measurements and only if an intruder is detected (sensor measured a value below a threshold), the wifi will get activated again and gets disabled again, after the the alert sms was sent.

My first endeavors with my toy examples earlier in this thread weren't so successfull. Looking at my code, could anybody give me suggestions, where I should start disabling my Wifi module in my code? I'm new to that whole power saving topic and would love to hear some suggestions of far more experienced people.

// Libraries
#include <SPI.h>
#include <WiFiNINA.h>

// Constants
#define trigPin 6   // Trigger to Pin 6
#define echoPin 7   // Echo to Pin 7 

// Variables 
float duration, distance;

#include "arduino_secrets.h" 


///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
String ifttt_webhook_key = SECRET_IFTTT_WEBHOOK_KEY; // your API key for the webhook

int status = WL_IDLE_STATUS;
const char* host = "maker.ifttt.com";
WiFiSSLClient sslClient;



void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  wifiSetup();
  Serial.println("Patrol Mode Initiated...");
}

void loop() { 
  
 // Write a pulse to the HC-SR04 Trigger Pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(1);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(trigPin, LOW);

  // Measure the response from the HC-SR04 Echo Pin
  duration = pulseIn(echoPin, HIGH);
 
  // Determine distance from duration
  // Use 343 metres per second as speed of sound
 
  distance = (duration / 2) * 0.0343;
  
  // Send results to Serial Monitor
 
  Serial.print("Distance = ");
  Serial.println(distance);

  // distance > 1 because sensor measures 0 wrongly sometimes
  // sensor can measure distances between 1 cm and 450 cm
  if (distance < 10 && distance > 1) {
  Serial.println(distance);
  Serial.println("Intruder Detected!");
  Serial.println("Sending text Notification...");
  sendNotification();
  delay(200);
  }

}


void sendNotification()
{
  // connect to web server on port 443:
  if(sslClient.connectSSL(host, 443)) {
    // if connected:
    Serial.println("Connected to server");

    // make a HTTP request:
    sslClient.println("GET /trigger/Einbruch_Vaters_Garage_MKR_WAN_1010/with/key/" + ifttt_webhook_key + " HTTP/1.1");
    sslClient.println("Host: maker.ifttt.com");
    sslClient.println("Connection: close");
    sslClient.println();
    Serial.println("IFTTT request Sucessful");
  }
   else {
    Serial.println("IFTTT request failed");
  }

  // if there are incoming bytes available 
  // from the server, read them and print them
  while(sslClient.connected()) {
    if(sslClient.available()) {
      char c = sslClient.read();
      Serial.write(c);
    }
  }

  Serial.println();
  Serial.println("disconnecting from server.");
  sslClient.stop();
}



void wifiSetup() {
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }
  printWifiStatus();                        // you're connected now, so print out the status
}


void printWifiStatus() {
  // print the SSID of the network:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
 
  // print WiFi IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
 
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}