5

I've been reading a lot on the ESP8266, but still my understanding is shaky. I have the Arduino IDE running on a PC, which uses wires to connect to the ESP8266 via a USB device (Device Manager > Ports (COM & LPT) > Silicon Labs CP210x USB to UART Bridge). This is how I uploaded a simple sketch to the ESP8266.

It works and has been installed in the wall. I'm happy and relieved to complete the project... except for one small thing: I noticed that it shows up on WiFi. I can connect to it without a password, with a "Limited" connection. After that I'm lost, but I wonder if someone who knows what they are doing can use this unsecured connection to hack my install. I'd like to disable the WiFi, if possible, since I'm not using it.

The worst part is my learning curve now starts over. I thought that when my sketch gets uploaded, it replaces the original firmware. Now "firmware" is an elusive concept. I keep thinking that it shows up on WiFi because of something running in firmware. My sketch hasn't any code for WiFi. What am I missing/do I have wrong?

6
  • 1
    Please post the code (sketch) you are running. And to turn of the WiFi use WiFi.forceSleepBegin();
    – MatsK
    Commented Aug 6, 2017 at 9:11
  • @ MatsK I use the Arduino IDE to flash my esp8266, and tried adding "WiFi.forceSleepBegin();" in the "void setup () {" section, but uploading gave an error about not being in the "scope". So I tried Sketch > Include Library > Contributed Libraries > WiFi This time the uploading error was "'class WiFiClass' has no member named 'forceSleepBegin'" I don't know how to post the code I'm running, but I'm trying to learn how. When I do, I will post the code as per your request. Thanks
    – user37116
    Commented Aug 7, 2017 at 5:21
  • Did you #include <ESP8266WiFi.h> ? Commented Aug 7, 2017 at 6:07
  • 1
    @ Jaromanda X Thanks so much, I had included the wrong library because I forgot to change from "Arduino Pro Mini" to "Generic ESP8266 Module". After selecting the right board, the code uploaded without error and this seems to have solved my problem since I no longer see the esp8266 in the list of available WiFi connections. I'm still very confused as to how the WiFi got turned on in the first place, and about the question of firmware. I think I will post another question about this subject.
    – user37116
    Commented Aug 7, 2017 at 6:51
  • 1
    @ MatsK Since I can now disable the WiFi, I don't think there is any point in posting the code. However, if you could help with my confusion about firmware I mentioned in my question, it would be most appreciated
    – user37116
    Commented Aug 7, 2017 at 7:02

4 Answers 4

5
  1. To turn of the WiFi add the WiFi library and use WiFi.forceSleepBegin();

  2. Firmware is the program/sketch that controls a MicroController Unit aka MCU (and in this case a Arduino).

    Best explanation I know is here: https://en.wikipedia.org/wiki/Firmware

2

Many esp8266 devices by default start in (WIFI_AP) SoftAP mode, and will appear as a wifi network access point, waiting for stations to connect.

User code in any sketch, should explicitly set wifi to the appropriate mode. If you do not need wifi, set the wifi mode off. To do this with the Arduino IDE, an esp8266 must run a sketch at least once that includes the wifi class to use its .mode() method.

#include <ESP8266WiFi.h>

void setup() {
   Serial.begin(115200);
   WiFi.mode(WIFI_OFF);
   if (WiFi.getMode() == WIFI_OFF)
      Serial.println(F("\nWifi mode is WIFI_OFF, until it is explicitly changed"));
}

The mode setting persists across power on and restart/reset cycles, until user code specifies a different mode. So once run on the esp8266, the all the lines above (except setup()) could optionally be removed from subsequent sketches uploaded to the esp8266, and the wifi will remain off.

Firmware is lower-level software that runs between the hardware and your higher-level sketch. Many esp8266 came with Espressif firmware and a sketch installed.

If you are concerned about energy consumption, investigate waking up the esp8266 with the radio disabled, eg.

ESP.deepSleepInstant (microseconds, WAKE_RF_DISABLED);
1

You control WiFi using wifi.setmode().

ESP8266's WiFi is very versatil. You can be a client and/or an access point. You can get your IP from DHCP or static.

Per default, ESP8266 start in STATIONAP mode (as client and access point).

This is the pertinent part from the official page

wifi.setmode()

Configures the WiFi mode to use. NodeMCU can run in one of four WiFi modes:

  • Station mode, where the NodeMCU device joins an existing network

  • Access point (AP) mode, where it creates its own network that others can join

  • Station + AP mode, where it both creates its own network while at the same time being joined to another existing network

  • WiFi off

When using the combined Station + AP mode, the same channel will be used for both networks as the radio can only listen on a single channel.

Note

WiFi configuration will be retained until changed even if device is turned off.

Syntax

wifi.setmode(mode[, save])

Parameters

mode value should be one of
    wifi.STATION for when the device is connected to a WiFi router. This is often done to give the device access to the Internet.
    wifi.SOFTAP for when the device is acting only as an access point. This will allow you to see the device in the list of WiFi networks (unless you hide the SSID, of course). In this mode your computer can connect to the device, creating a local area network. Unless you change the value, the NodeMCU device will be given a local IP address of 192.168.4.1 and assign your computer the next available IP address, such as 192.168.4.2.
    wifi.STATIONAP is the combination of wifi.STATION and wifi.SOFTAP. It allows you to create a local WiFi connection and connect to another WiFi router.
    wifi.NULLMODE changing WiFi mode to NULL_MODE will put wifi into a low power state similar to MODEM_SLEEP, provided wifi.nullmodesleep(false) has not been called.

save choose whether or not to save wifi mode to flash
    true WiFi mode configuration will be retained through power cycle. (Default)
    false WiFi mode configuration will not be retained through power cycle.

Returns

current mode after setup

Example

wifi.setmode(wifi.STATION);

See also

wifi.getmode() wifi.getdefaultmode()

1
  • Note: this answer refers to NodeMCU Lua code. The ideas can be used by Arduino ESP8266, but method names must be modified.
    – DWB
    Commented Dec 19, 2022 at 16:18
1

This is the best solution I have found so far (source: GitHub gist)

The two functions below are all you need to toggle your WiFi chip. The huge advantage of using these is that they work without having to invoke an ESP.deepsleep() call.

Mind you, if you have already done an ESP.deepsleep(time, WAKE_RF_DISABLED) that means that your WiFi chip has been disabled altogether, and calling WiFiOn() will NOT be able to toggle it back ON. In this case you MUST do ESP.deepsleep(time, WAKE_RF_DEFAULT). But once you WiFi chip has been re-enabled, you can call WiFiOn() and WiFiOff() as many times as you please, they will work fine.

extern "C" {
    #include "user_interface.h"  // Required for wifi_station_connect() to work
}

#include <Arduino.h>
#include <ESP8266WiFi.h>

#define FPM_SLEEP_MAX_TIME 0xFFFFFFF

void WiFiOn();
void WiFiOff();

//------------------------------------------------------------------------------

void setup() {

    WiFiOn();
    delay(5000);
    WiFiOff();
    delay(5000);
    ESP.deepSleep(1e6 * 10, WAKE_RF_DEFAULT); // sleep 10 seconds

}

void loop() {
    ;
}
//------------------------------------------------------------------------------

void WiFiOn() {

    wifi_fpm_do_wakeup();
    wifi_fpm_close();

    //Serial.println("Reconnecting");
    wifi_set_opmode(STATION_MODE);
    wifi_station_connect();
}


void WiFiOff() {

    //Serial.println("diconnecting client and wifi");
    //client.disconnect();
    wifi_station_disconnect();
    wifi_set_opmode(NULL_MODE);
    wifi_set_sleep_type(MODEM_SLEEP_T);
    wifi_fpm_open();
    wifi_fpm_do_sleep(FPM_SLEEP_MAX_TIME);

}
2
  • WiFiOff caused a crash for me. I called it from the program loop once, and it triggered a Soft WDT reset.
    – Nathan
    Commented Jul 22, 2020 at 14:57
  • 1
    Note: the code in this answer relies on SDK functions, which may be unfamiliar and confusing to Arduino IDE users, especially early in their learning, like the OP. Better to focus on one system or the other first.
    – DWB
    Commented Dec 19, 2022 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.