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

Hey guys,

i have the following problem. I want to build an alert system and to save battery power my alarm system is only allowed to use Wifi and connect to my WLAN network, when it detects something. It will alarm me by using a webhook, which tells my smartphone to send a sms to my phone. The Wifi module in my MKRWIFI 1010 can literally be disabled all the time besides, when it has to send an alert.

I want to use Wifi.end(), because the WiFiNINA documentation says, that it turns off the WiFi module of my MKRWIFI 1010. My test code only tells me on my Serial monitor, that the board is not connected to the WIFI network though. It seems like the WiFi module doesnt get turned off. Can somebody tell me why?

The Wifi module works fine, because the example "ConnectWithWPA" works. I can connect to my WiFi network, if I want.

Also, I'm still a newb. is it good practice to disable the WiFi module in the setup? Probaly not. Probably it would be better, that I do it in the loop section with an if statement (if WiFi module on, disable), because I will have to enable the WIFI module at some point in the loop (if dectection, enable WIFI module then use webhook to send alarm), when my sensor detects something. It has to be disabled at some point in the loop section again anyways, so I think it makes more sense to realize the disabling only in the loop. I think after the detection, I will put the board to sleep. I dont want to pay for thousands of SMS. :wink:

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

#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)
char ifttt_api_key[] = SECRET_API_KEY; // your API key for the webhook
char ifttt_event_name[] = SECRET_EVENT_NAME; // your event name

const char* host = "maker.ifttt.com";
WiFiSSLClient sslClient;
 
void setup() {
  delay(10000);
  Serial.begin (9600);

  WiFi.end(); // Powersave, only allowed to use WIFI, if intruder is detected
  
  //WiFi.begin(ssid, pass); 


  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Not connected to Wifi");
  }

 // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
}

}


void loop() {
  // put your main code here, to run repeatedly:

}

Thanks for the help!

int status;

in front of void setup ()

When I put the following Code after WiFi.end():

 WiFi.end(); 
  
status = WiFi.status();
Serial.println(status);
Serial.println(WL_IDLE_STATUS);
Serial.println(WL_CONNECTED);
Serial.println(WL_NO_MODULE);

The output on my serial console is:

0
0
3
255
Not connected to Wifi

Looks like WiFi.end() doesnt work.

I'm using the program Connectify Hotspot to monitor the behaviour of my board live and found something weird.

Here I connect in void setup():

WiFi.begin(ssid, pass); // Powersave, only allowed to use WIFI, if intruder is detected
delay(10000);

if (WiFi.status() != WL_CONNECTED) {
    Serial.println("Not connected to Wifi.");
  }

    if (WiFi.status() == WL_CONNECTED) {
    Serial.println("Connected to Wifi");
  }

Here I want to disable the WiFi module in void setup():

  WiFi.end();
  delay(60000);
  // check for the WiFi module:
  if (WiFi.status() != WL_NO_MODULE) {
    Serial.println("WiFi module is not disabled!");
}
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("WiFi module is disabled!");
}

  if (WiFi.status() == WL_IDLE_STATUS) {
    Serial.println("WIFI module is idling");

I see live, how my MKR Wifi 1010 gets disconnect, when I use Wifi.end(), but I get the following output on the serial monitor:

15:04:30.587 -> Connected to Wifi
15:05:31.285 -> WiFi module is not disabled!
15:05:31.285 -> WIFI module is idling

Okay - looking at the serial monitor output and the code - with WiFi.End() I clearly disable the Wifi, but I do not disable the module? It feels like, I have to measure the actual current to see, if the Module is disabled or not (comparing the current during idling and after I use WiFi.end()). You can also see the described behaviour in the pictures in the attachement.

When I comment WiFi.end() my board never disconnects from the wifi network.

P.S. I should measure the current like this, right (See attachement fritzing_blueprint)

Does anybody have an idea, where this behaviour comes from?

In Idle mode, my microcontroller draws a current of 60 mA. If connected to the WiFi, it draws 160 mA. With WiFi.end(), my 1010 just goes into the Idle mode again and draws a current of 60 mA again. So it is not a bug and the 1010 just refuses to disable the WiFi module after using WiFi.end. That's so irritating and I dont know what to do.....

My battery only survives for 18 days with this power consumption. I really dont know, what I do wrong....

Hello Gaebel90, I am also trying to use battery power supply for MKR1010. I am not sure which battery is good to use with MKR1010 though. I tried a couple of options like AA, 9V2 etc but it won't provide sufficient power.

I would like to know what battery are you using that can last more than a week?

Also, does disabling WiFi when it is not uploading data helps in saving power?

Sneha7:
Hello Gaebel90, I am also trying to use battery power supply for MKR1010. I am not sure which battery is good to use with MKR1010 though. I tried a couple of options like AA, 9V2 etc but it won't provide sufficient power.

I would like to know what battery are you using that can last more than a week?

Also, does disabling WiFi when it is not uploading data helps in saving power?

Hey Sneha.

Ive bought these batteries for my project: Batteries

I have to say, that my project only runs during the nighttime (Alarm system) and not 24/7. So It can run multiple days for my purpose (18 days). With the current powersave for my setup and one of these batteries, my project would only run for less than 5 days, if my project would run 24/7. A current of 60mA is still too high.

Yeah, with disabling the WiFi and the WiFi module you save alot of power.

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");
}

Ive managed to reduce the power consumption between 20 mA and 30 mA with the following code:

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  wifiSetup();
  delay(15000);
  Serial.println("WiFi Lowpower Mode Initiated...");
  WiFi.lowPowerMode();
  delay(15000);
  Serial.println("WiFi Module disabled...");
  WiFi.end();
  delay(15000);

  Serial.println("Patrol Mode Initiated...");
}

Something I've noted: When checking in void setup(), if the WiFi module is disabled with the code below the consumption can't be reduced between 20 mA and 30 mA anymore. The power consumption stays in this case around 60 mA. The code below influences the Wifi module in a way it seems.

     // check for the WiFi module:
     //this check causes a higher current draw back to 60 mA from originally 30 mA
     //checking the status enables the WiFi Module again?
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("WiFi module is disabled!");
}

Now I have the following problem. I can't activate the WiFi module in my if statement.

if (distance < 10 && distance > 1) {
  Serial.println(distance);
  Serial.println("Intruder Detected!");
  Serial.println("Sending text Notification...");
  wifiSetup();
  delay(15000);
  sendNotification();
  delay(200);
  Serial.println("WiFi Lowpower Mode Initiated...");
  WiFi.lowPowerMode();
  delay(15000);
  Serial.println("WiFi Module disabled...");
  WiFi.end();
  delay(15000);
  }

Beforehand, when I used the function wifi.setup() in void.setup(), I get the following output:

16:41:53.589 -> SSID: telekom-WLAN21
16:41:53.589 -> IP Address: 191.158.1.131
16:41:53.589 -> signal strength (RSSI):-81 dBm
16:42:08.602 -> WiFi Lowpower Mode Initiated...
16:42:23.622 -> WiFi Module disabled...
16:42:38.594 -> Patrol Mode Initiated...

Now it seems like, I can't use the WiFi module anymore, when I look at the output from the if statement.

16:56:47.463 -> SSID: 
16:56:48.243 -> IP Address: 0.0.0.0
16:56:48.243 -> signal strength (RSSI):0 dBm

Any suggestions how to make the WiFi module work again?

I'm still stuck on the problem above. Does anybody have a suggestion?