motioneyeos icon indicating copy to clipboard operation
motioneyeos copied to clipboard

Button on GPIO to turn ON/OFF wifi (for Access Point conf)

Open BirdiPiduino opened this issue 3 years ago • 51 comments
trafficstars

Preliminary Docs

I confirm that I have read the CONTRIBUTING guide before opening this issue.

I confirm that I have read the FAQ before opening this issue.

motionEyeOS Version

I am running motionEyeOS version: 20200606.

Board Model

I am using the following board/model: Raspberry Pi Zero W But for some tests, I use a Raspberry Pi 3 for the ethernet and usb ports (keyboard and network)

Network Connection

My motionEyeOS unit is not connected to the network but it use is own Access Point IP adress : 192.168.42.1

Peripherals

Button on GPIO Led indicator on GPIO

RTC DS1307 (because of isolated situation in Access Point)

Improvement needed

In a configuration using the Access Point, make it possible to turn ON or OFF the WiFi. This function will be very useful for use in an isolated situation (no power -> use batteries, no network). It will offer more autonomy because it decrease the need of energy.

It is then possible to use an external button on the GPIO to activate the WiFi and a led on the GPIO to indicate that the WiFi is active. WiFi should turn on at boot. Turn OFF the WiFi can be automatically with a timer. Re-turn ON the wifi or re-init the timer can be done by pressing the button.

I wrote the script (attached) for manage the button and the led, but it miss the commands for the WiFi. What would be the command lines to enable/disable WiFi ?

It will better if it avoid to reboot when the wifi turn on or turn off.

Example of the utility of this issue : With motionEyeOs, I built a standalone camera-trap to study wilds birds at nest. It use batteries, USB storage, Wifi Access Point and Real Time Clock for date/time because it's in an isolated situation. There is a success. And now, i try to decrease the consumption of energy. That is what, stop the wifi when I don't use it, could be a way. I actually need the Wifi just 5 min every 4 days to check every things.

To test the script attached, copy it in /data/etc/ simulation_WiFi_button.zip

To use the complete config use it : motioneye-config.tar.gz SSID : Button_Wifi_AP || Pass : WIFI2022 IP adress : 192.168.42.1 Hostname : Button_Wifi || user admin || no password Raspberry Pi Zero W

20221007_095455

BirdiPiduino avatar Oct 07 '22 07:10 BirdiPiduino

I find the commands to enable and disable WiFi without rebooting (modprobe brcmfmac).

The solution into the script of the button : WiFi_with_button.zip

BirdiPiduino avatar Oct 11 '22 16:10 BirdiPiduino

Does it actually shut off the WiFi chip, or does it simply disable it? Big difference in what you originally requested...

starbasessd avatar Oct 11 '22 17:10 starbasessd

I'm not sure. But during my lasts tests on RPI3, I 've noted a difference of 20 mA (average, sometime null, somtime 40 mA). Your question is very interresting. I don't know where i can find the response. But I will test all methods and continue to measure the consumption of current method by method. I will find if any methods are similars or if some of them are very more efficients.

BirdiPiduino avatar Oct 11 '22 17:10 BirdiPiduino

And a next step is to start the script WiFi_with_button after booting.

BirdiPiduino avatar Oct 11 '22 17:10 BirdiPiduino

It miss also some msg for the log file (button pressed detected, GPIO active, WiFi disable and enable)

BirdiPiduino avatar Oct 11 '22 17:10 BirdiPiduino

Removing the driver (modprobe brcmfmac) as far as I know, doesn't 'shut the chip off', just disables / removes the driver for the chip. You could ask in the RPi forums on RPiFoundations site...

starbasessd avatar Oct 11 '22 17:10 starbasessd

If you need a script to run at bootup to monitor the GPIO, here's the instructions

starbasessd avatar Oct 11 '22 17:10 starbasessd

I did some tests and find a best command (to economize energy) : sudo ifconfig wlan0 down (or up). I have compared the consumption on RPi 3 with Bullseye between 3 methods (desktop/brcmfmac/ifconfig).

image

BirdiPiduino avatar Oct 12 '22 16:10 BirdiPiduino

Good to know, thank you for the research.

starbasessd avatar Oct 12 '22 17:10 starbasessd

Do you know if a webpage show how to write a log file in motioneyeos. It could be util to know when button is pressed, if wifi have restarted, when it was stopped... ?

BirdiPiduino avatar Oct 12 '22 17:10 BirdiPiduino

the path you want is /var/log/{name_of_file} it's the same as any other log file. You just need to have the utility or script do something like

echo " $(/bin/date) button was pressed" >> /var/log/button.log

This will then put " Wed Oct 12 14:33:42 EDT 2022 button was pressed" in a file called /var/log/button.log

starbasessd avatar Oct 12 '22 18:10 starbasessd

Thank you. It's donne with your help.

BirdiPiduino avatar Oct 13 '22 06:10 BirdiPiduino

Glad to help.

starbasessd avatar Oct 13 '22 12:10 starbasessd

I replace ifconfig by ip link. In MotionEyeOs, it will work better. Also, ifconfig is now old and replace by ip link. So i use : ip link set dev ap0 down and ip link set dev ap0 up. The wifi switch correctly and i can connect the computer to the AP of MotionEyeOS-RPiZero.

But, after to switch up, it's impossible to connect in SSH (putty) or in HTTPs (on web brower), no response...

I record in a specific log file the answer of ip a after each command. I obtain exactly the same before to switch down and after to switch up.

example (after sitch up the wifi) : 3:ap0 <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether b8:27:eb:aa:36:2c brd ff:ff:ff:ff:ff:ff inet 192.168.42.1/24 brd 192.168.42.255 scope global ap0 valid_lft forever preferred_lft forever inet6 fe80::ba27:ebff:feaa:362c/64 scope link tentative valid_lft forever preferred_lft forever

Do you think some services need to be restarted ? Do you think I need to restart the Access Point ?

I didn't find any command to have the list of services. I wanted to compare before and after.

BirdiPiduino avatar Oct 14 '22 15:10 BirdiPiduino

You should be able to connect to http://ip_address with a web browser. I am not sure why you couldn't. Once in the WebGUI, you can turn on SSH if you forgot to turn it on with a file called ssh or ssh.txt in the /boot partition when you created the SDCard.

ip a

gives you all active IP info

netstat -tulpn

gives you all active TCP/IP ports and statuses. You are looking for port 80, this line specifically:

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 645/python

Unfortunately in mEOS, the command

system motioneye status

returns Usage: /sbin/service {start|stop|restart}, status was forgotten... and systemctl is not installed...

starbasessd avatar Oct 14 '22 15:10 starbasessd

Excuse me if I was not clear.

When I use the command ip link set dev ap0 down, the wifi switch down, as i want (economy of energy). Whe I use the command ip link set dev ap0 up, the wifi switch up correctly.

If I compare the answers of the commands lines ip a and 'netstat -tulpn', before to switch off the wifi and after to switch on, they are exactly the same.

So, normaly, all are done. When I connect my computer to the AP-wifi, it's ok, that's right. But it's impossible to connect with url : 192.168.42.1 on the web browser and impossible to connect in SSH. So I reboot the system, and that's fine, I can connect the computer to the AP-wifi and open MEO in the web browser or access to it in SSH.

BirdiPiduino avatar Oct 15 '22 13:10 BirdiPiduino

I'm sure MOE does'nt reboot after the switch OFF/ON of the wifi because, I watch the screen of MOE with a screen directly connected on HMDI.

BirdiPiduino avatar Oct 15 '22 13:10 BirdiPiduino

Then you can add the command

service motioneye restart

after the command that restarts your wifi.. You might need a 5-10 second delay for the wifi to come all the way back up, though which would be:

sleep 10 && service motioneye restart

starbasessd avatar Oct 15 '22 14:10 starbasessd

After the command service motioneye restart. It answer Stopping motioneye : done and Starting motioneye : done. But there is'nt possible to open MOE in web-browser or SSH terminal.

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

If you run

netstat -tulpn | grep ":80"

what does it report?

My example: [root@meye-4cec78b8 ~]# netstat -tulpn | grep ":80" tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 651/python tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 4367/motion

starbasessd avatar Oct 15 '22 14:10 starbasessd

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1079/python tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1092/motion

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

Should be there. Did you check the ip address?

ip a

starbasessd avatar Oct 15 '22 14:10 starbasessd

Yes, I've checked and also with the commande line ip a

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

Can you ping it?

starbasessd avatar Oct 15 '22 14:10 starbasessd

I can not. Destination Host Unreachable

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

I've checked the log files

What seems to be done 👍 in dmes.log, I can read : IPv6 ADRCONF (NETDEV_CHANGE) link becomes ready. in hostapd.conf, I can read : ap0 interface enabled

not done 👎 in message.log, I can read : Oct 15 14:57:54 daemon.info hostapd : ap0 : STA 74:c6:3b:...d5 IEEE 802.1 : dissociated

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

But on MEO from MEO, i can ping itself well.

I'm not sure it's an interesting indication...

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

And from MEO, i can ping my computer...

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino

Can you disconnect whatever phone or tablet you are using, and re-connect? Not the "automatic connect"...

starbasessd avatar Oct 15 '22 14:10 starbasessd

Yes I've tested with my phone (Android), my Ipad, my laptop (ubuntu) and my computer (windows). Disconnect/reconnect/forget this network, ...

BirdiPiduino avatar Oct 15 '22 14:10 BirdiPiduino