Monday, August 19, 2013

Post 21: Solenoid Success At Last

Thanks to Joe Dohm, my water valve now works! I should have looked up the pins on the transistor. Here's the image Joe sent me from a Fairchild web page:
And here is my revised (and working!) wiring diagram:
It helps to know what you are doing.

A different topic -- and my first practical use of my Rpi:
Several time a week my Frontier-supplied, Netgear-built DSL modem/wifi stops working. The Frontier complaint guy explained my problem as a "feature" rather than a failure. Apparently, there are a limited number of locally-assignable IP numbers and sometimes a number gets reused on a connected device that really hadn't gone away (merely quiet for a while). When the sleeping gadget wakes up the modem doesn't know what to do, so it stops working (forever?). Cycling power to the Netgear fixes things -- but it takes a few minutes and is irritating. Now, I admit that my immediate neighbors and I may sometimes have a dozen computers, phones and tablets connected -- all at once. But, so what?

The solution I dreamed up is as follows:

Leave the Rpi "on" 24/7 running the following BASH script (appended to the .profile file):
 ps ax >/tmp/j
 grep 'python ping.py' </tmp/j >/dev/null
 if [ $? -ne 0 ] ; then
   nohup python ping.py &
   echo starting DSL test
 fi

The first two lines above are to avoid starting another copy of the ping program.

Here's ping.py:
import time
last = 0
res = 9
res2 = 9

def post(msg):
tm = time.strftime("%y, %m/%d:%H:%M:%S")
s = "echo "+msg+": "+tm+" >>stats/DSL"
os.system(s)

post("Rpi Up")
while 1:
time.sleep(60) # probably 300 sec. would be better
res = os.system("ping -c 1 google.com >/dev/null")
if res != 0: # internet failed?
    os.sleep(5)
    res2 = os.system("ping -c 1 yahoo.com >/dev/null")
    if res2 != 0: # 2nd failure?
       if last == 0: # previously ok
        res2 = os.system("ping -c 1 www.wired.com >/dev/null")
        if res2 != 0: # 3rd fail
  post("DSL Fail") # turn modem off/on!
last = 1
else:
    if last == 1: # just stopped failing
        last = 0
post("DSL Up")

# don't print repeat messages but keep looping

I was planning to attach a 110V switch to the Rpi  to cycle the modem power (see "DSL Fail" line above). But since I started letting this run, have had NO failures. No doubt my pinging is resource-piggy, but they brought it on themselves.  A few lines of code added to the Netgear's software could fix the problem but the Telcos are probably saving bandwidth the way things are.

As mentioned in the code, I probably should increase the sleep time. Also, I chose to ping google.com because the site gave me quick echos. Very casual sampling, however.

No comments:

Post a Comment