Run code when wifi is dropped

Very new here. Could use help with my 1st project.
I have a Verizon mifi mobile hotspot, its way off-grid and provides internet access for my IP cameras. If the hotspot gets an auto update, overheats or has a power surge it shuts off. Cant remotely do anything.
SO…I built a circuit for push-pull solenoid controller with relay. I wrote my first successful code to have the solenoid “push” a physical “power-on” button on my mobile hotspot. Tested! Works perfect!
However, Its important the solenoid code will run automatically when the Raspberry pi 3B loses network connectivity. Thus rebooting the hotspot.
Does anyone know how to write the code to have the Raspberry pi monitor the wifi and run the code that activates the solenoid?

Thanks in advance for keeping it dumbed-down.

1 Like

Most solutions I looked at say to ping a known IP address and if it does not respond do something. ( ie activate the solenoid) The IP address could be the Verizon Mobile Hotspot.

The time between checking would depend on long you can tolerate the system being down and how often you want to retry.
Every 30 minutes, 30 seconds, 100 milliseconds, what ever works. Too often and the hotspot might not perform its main function correctly.
You say ‘way off-grid’ so I assume you could not ping Google or other ‘always on’ web sites.

The code for ping checking is below. This would run in a terminal window on the Pi 3B. You would start in manually or could start when the Pi boots by calling it in ‘rc.local’. The code runs all the time.
Using crontab might be better. Code has not been tested.

Regards
Jim

#!/bin/sh
while true; do
    if ping -c5 xxx.xxx.xxx.xxx; then   # IP address of Mobile Hotspot, eg 192.168.0.1
        sleep 15m     # 15 minutes, wait before testing wifi again
    else
        # add code to activate solenoid
        sleep 5m      # 5 minute, wait before trying again, so solenoid has chance to activate and wifi to start
    fi
done
3 Likes

Dude! Thanks for your time and effort.

The property is way off-grid but at high enough elevation (LOS to Verizon cell tower) to provide stable 4g lte.

A ping check can happen every 360 minutes or once a day.
This whole project is to avoid driving 600 miles to press a button.

3 Likes

Hi John,

Wow! That’s remote indeed!

In my other reply, I linked to some code that should work to ping. Just to make sure, I tested it and it worked as expected:

For reference, what I used was:

import os
hostname = "google.com" #example
response = os.system("ping -c 1 " + hostname)

#and then check the response...
if response == 0:
  print(hostname, 'is up!')
else:
  print (hostname, 'is down!')

Give this a go with your solenoid, and let us know how it goes!

-James

3 Likes

Hi John,

(btw I am a Core Electronics customer, @James is an employee, we are not the same person)

If you can ping google that would be great.
8.8.8.8 is the main Google DNS address.

The code @James has provided is a good example of using Python to run OS commands.
What you do it up to you.

cheers
Jim

3 Likes

Thank you Jim and James.
I’m truly surprised and grateful for how much effort and quick response you guys have put in to my struggle.
I may be in over my head with python… writing code, punctuation, format all to make my solenoid script run.
I will definitely post my results!