Turn off WIFI

hi, is there a way to switch the wifi on and off on esp8266. I want to initially have the unit start and connect to network (so I can do updates OTA if required), then have a physical switch to turn the wifi off as its going to run on batteries, and connect via sim800l.

I have a binary sensor set up as a switch and tried :-

on_press:
  then:
    - lambda: WiFi.mode(WIFI_OFF);
on_release:
  then:
    - lambda: WiFi.mode(WIFI_STA);

the unit disconnects, but after a few seconds (with the switch still connected) it automaticcly reconnects to the wifi ???

If you remove the on_release, does it stay off the network?
Also, I don’t know if on_press has debounce. If it doesn’t, perhaps it’s seeing some on_release action during the press which you could reduce with a simple series resistor and cap to ground.

I removed the ‘on_release’, but it still reconnects after a few seconds, here’s all my code …

esphome:
  name: sim800l
  platform: ESP8266
  board: d1_mini

wifi:

  networks:

  - ssid: "xxxxx"
    password: "xxxxxx"
  - ssid: "xxxxxx"
    password: "xxxxxxx"
  manual_ip:
    static_ip: 192.168.0.34
    gateway: 192.168.0.1
    subnet: 255.255.254.0 
 
  
#  reboot_timeout: 0s 
#  power_save_mode: high

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Sim800L Fallback Hotspot"
    password: "xxxxxx"

captive_portal:

# Enable logging
logger:
  level: DEBUG

# Enable Home Assistant API
ota:


binary_sensor:
  - platform: gpio
    pin:
      number: D4
      inverted: false  
    name: "PIR Sensor"

    device_class: motion
    id: my_motion
    on_press:
      then:
        - sim800l.send_sms:
            recipient: '+77xxxxxxxx'
            message: PIR activated -- send help!
  - platform: template
    name: temp_motion
    lambda: 'return id(my_motion).state;'
    filters:
      - delayed_off: 1min

  - platform: gpio
    pin: D8
    name: "wifi switch"
#    device_class: 
    id: my_wifi_switch
    on_press:
      then:
        - lambda: WiFi.mode(WIFI_OFF);
#    on_release:
#      then:
#       - lambda: WiFi.mode(WIFI_STA);

api:
  services:
  - service: send_sms
    variables:
      recipient: string
      message: string
    then:
    - sim800l.send_sms:
        recipient: !lambda 'return recipient;'
        message: !lambda 'return message;'

text_sensor:
- platform: template
  id: sms_sender
  name: "Sms Sender"
- platform: template
  id: sms_message
  name: "Sms Message"

uart:
  baud_rate: 9600
  tx_pin: D0
  rx_pin: D1

sim800l:
  on_sms_received:
    - lambda: |-
        id(sms_sender).publish_state(sender);
        id(sms_message).publish_state(message);

```

api: needs a network connection, maybe it forces wifi up?

on_press:
      then:
        - lambda: WiFi.mode(WIFI_OFF);

where are you getting this lambda from? Where is it documented?

I saw on the net that the ESP8266 has four modes,

  • Station mode (WIFI_STA), where the NodeMCU device joins an existing network
  • Access point (WIFI_AP) mode, where it creates its own network that others can join
  • Station + AP mode (WIFI_AP_STA), where it both creates its own network while at the same time being joined to another existing network
  • WiFi off (WIFI_OFF)

It does state that it uses ESP8266WiFi.h Library, not sure if esphome uses this library, is there a way to check and if not force the use of this library ?

It is open source, look at the code.

And as I said, what makes you think api can work without a network?

What if you want to wakeup from sleep, check the sensor data without being connected (to save battery power), and either connect afterwards if a threshold has been surpassed? That would actually make the attached sensor a ‘smart’ battery powered sensor.

@nickrout I want to use ESP Home nodes to control the shades everywhere in our house. We don´t like the idea of having ~ 20 nodes active on WiFi around-the-clock.
That´s why I use the CAN Bus option to communicate with the nodes controlling the shades. I only need WiFi for OTA and tried to switch WiFi on/off via CAN. But even if I configure reboot_timeout : 0s it seems that the node is rebooting after WiFi is turned off via CAN and enables WiFi again. Is this a bug?

You are aware of the other components which could cause a reboots like for example ‘api’ or ‘mqtt’ ?

@picohassiotts thanks for your hints.

Is there anything I could do to promote the PR you were referring?

Test it and report your findings in the PR :upside_down_face:

@picohassiotts I am not a µC developer so I have no idea how I can compile a branch with a config and load it to a device. I also couldn´t find a docu describing this. If cou could give me some directions, I´d be happy to do that.

Made this guide for another project (on w10 though)
Maybe of help?
Below my command lines used to compile:

cd c:\workshop\esphome

pip install esphome
pip install esphomeflasher
pip install --upgrade git+ https://github.com/esphome/esphome

esphome mideahvac.yaml wizard
esphome mideahvac.yaml compile

esphomeflasher

using the compiled C:\_workshop\esphome\mideahvac\.pioenvs\mideahvac\firmware.bin

image
Assuming you already installed python :wink:

PS:You can replace
https://github.com/esphome/esphome
with any ESPHome branch of your liking :wink:
as well as using your own yaml configuration :nerd_face:

2 Likes

thanks to the hints from @aceindy I now tested the PR and reported my findings: Allow WIFI to be disabled and enabled by ashmckenzie · Pull Request #1249 · esphome/esphome · GitHub

1 Like

Out of topic but really need to know: what kind of sim card are you using with sim800l module ?

micro sim card

I know it is micro sim card, but what carrier/plan ? (if in US)

That higly depends in your usage. I went for some 1nce card to start/play with. Mostly because of their 10 years runtime and wide coverage: https://1nce.com