OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Starting ospi.py automatically

  • This topic is empty.
Viewing 18 posts - 26 through 43 (of 43 total)
  • Author
    Posts
  • #24710

    Andrew
    Participant

    Michael is exactly right about the & at the end of the command to send it to the background and let rc.local complete. I go a few steps further. I send stderr to stdout and I invoke it via nohup to make stop any signals getting sent to it about terminals closing. Using nohup is redundant in rc.local since the system terminal can’t be closed, but it does mean that if I copy and paste the commands to start it on the command line it is always okay.

    Back to getting it to run automatically… change the your /etc/rc.local to the following:

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.

    # Print the IP address
    _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
    printf "My IP address is %sn" "$_IP"
    fi

    ### Setup the OSPi's RTC
    #echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
    #hwclock -s

    ### Start the OSPi interval program
    #host=$(hostname -I | sed 's/ *$//g')
    host=0.0.0.0
    port=80
    cd /home/pi/OSPi/
    nohup /usr/bin/python ospi.py $host:$port 2>&1 &

    exit 0

    This includes two commands (commented out so they won’t run) for using the OSPi’s on board clock as well. There are a few other steps to getting that running so I commented them out here. I’ve also told it to listen on all available interfaces (0.0.0.0) so that it can be connected to by local services (like apache) on the loopback interface (127.0.0.1).

    #24711

    kkpwrh2o
    Member

    Changed my rc.local file to reflect the recommended code above.

    Couple of questions:
    Should my port be 8080 if I want to use Samer’s Sprinkler web app?
    How do I determine ospi is running?

    #24712

    Samer
    Keymaster

    @kkpwr&h2o wrote:

    Couple of questions:
    Should my port be 8080 if I want to use Samer’s Sprinkler web app?

    Yes. For the sake of clarity, it can be anything except what Apache is using (port 80). With that said, 8080 is a great port to use.

    @kkpwr&h2o wrote:

    How do I determine ospi is running?

    The best way is navigating to the web interface. If you are not greeted by the usual home page of the firmware then it’s not running. Seems like the simplest, most reliable test.

    #24713

    kkpwrh2o
    Member

    After resolving a hardware problem, loading the latest ospi.py file, I modified my rc.local file with the above code and rebooted.

    It works!

    I can ssh to it via my iMac, It is running automatically, and I can control all (5) zones with the local home page.

    I have 8080 as the port in the rc.local file, however I am still not able to get the Web app to work.

    When I go to http://rayshobby.net/apps/sprinklers, the app opens but I am unable to add a new device when using my IP addr 71.85.xxx.xxx:8080. After I hit the Submit button I keep getting, “IP Check IP/port and try again”.

    Port 80 is forwarded on my router (and as I read in above posts, it being used by apache by default) so I am thinking that is the port the local OpenSprinkler web page is using.

    It has gotta be something simple at this point to get it running??

    #24685

    Samer
    Keymaster

    Update: Ray below is right.

    #24714

    Ray
    Keymaster

    @kkpwr&h2o wrote:

    When I go to http://rayshobby.net/apps/sprinklers, the app opens but I am unable to add a new device when using my IP addr 71.85.xxx.xxx:8080. After I hit the Submit button I keep getting, “IP Check IP/port and try again”.

    Port 80 is forwarded on my router (and as I read in above posts, it being used by apache by default) so I am thinking that is the port the local OpenSprinkler web page is using.

    Since you are using port 8080, you need to forward port 8080 on your router, instead of the default 80, right?

    #24715

    kkpwrh2o
    Member

    seems I have already done this at some point, as I get the following “file exists” reply

    fatal: destination path '/var/www/sprinklers' already exists and is not an empty directory

    Perhaps I should delete the directory and let it reinstall?

    #24716

    kkpwrh2o
    Member

    Ray,

    Changed the port to be forwarded to 8080.

    That was the ticket!! Success!

    Thank You Samer and Ray!! WooHoo!

    #24717

    Michael
    Member

    This is great. I never thought of 0.0.0.0.. that works better and starts up every time.

    #24718

    Dan in CA
    Participant

    Michael,

    That’s interesting. Whenever I try that it never works.

    What is your setup like? Wifi, hardwared?

    Dan

    #24719

    Dan in CA
    Participant

    OK. I think I understand whats happening.

    I normally interact with the interval program through its own web interface since that’s what I’ve been developing. I also use a wired network connection for my test system.

    For someone using Samer’s Mobile Web App, using 0.0.0.0 as the URL for the program works great but you loose access through the underlying interface. It can be handy to be able to use both interfaces. For example, if you encounter a problem and are trying to determine if its with the web app or the Interval program.

    The server used by the Interval program defaults to 0.0.0.0:8080 so if you are using Samer’s app and are willing to give up access to the interval program though its own interface you could use a simplified start up script:

    cd /home/pi/OSPi/
    /usr/bin/python ospi.py &

    If you connect to the Raspberry Pi via WiFi, you probably want to add a delay to the start of the script:

    sleep 60
    cd /home/pi/OSPi/
    /usr/bin/python ospi.py $host:$port &

    I’ll wait for any additions or other comments and then add this to the wiki page.

    Dan

    #24720

    Michael
    Member

    @Dan in CA wrote:

    Michael,

    That’s interesting. Whenever I try that it never works.

    What is your setup like? Wifi, hardwared?

    Dan

    I am using wifi. I did not know there were defaults if you did not specify anything. That makes sense since it is in the config that way. This is what my rc.local looks like and I have no issues, I have apache running on 80 for samer’s website and the interval program on 8080 so I can access both websites as needed.

    #wait to make sure everything is ready to go (probably not needed)
    sleep 30
    #run a script that periodically makes sure wifi is running
    /bin/sh /network-monitor.sh &

    # change to the /OSPi directory
    cd /OsPi/
    # run the interval program
    /usr/bin/python ospi.py 0.0.0.0:8080 &

    Here is what the network monitor script looks like, This helps because sometimes my router may reboot and I want the pi to reconnect if there is any issue. It checks every 5 minutes for a connection. I found it somewhere on the web I forget who wrote it sorry for not giving credit.

    #!/bin/sh

    while true ; do
    if /sbin/ifconfig wlan0 | grep -q “inet addr:” ; then
    sleep 600
    else
    echo “Network connection down! Attempting reconnection.”
    /sbin/ifup –force wlan0
    sleep 10
    fi
    done

    #24721

    Dan in CA
    Participant

    Thanks Michael,

    That’s really useful info. Especially the script for checking the network connection.

    I tried the start up code just as you posted it but still no-go using 0.0.0.0. I wonder if it might be something in my router setup.

    I’ll investigate further.

    Dan

    #24722

    Dan in CA
    Participant

    Yep. It was a router setting 😳 . Something left over from some experiments a couple of years ago.

    Looks like I need to revise the instructions on the wiki page.

    Dan

    #24723

    mikethechap
    Participant

    So, as I read this (and I’m pretty much a novice with **nux), if I put an & before the exit statement, then the OSPi program running in my SSH interface should go to the background and I should be able to resume my SSH session. Is that right? My SSH is still running OSPi the whole time. I’m just trying to see if there’s some way to get OSPi to run in a way where I can do other stuff with my SSH.

    Am I barking up the wrong tree?

    Thanks!

    #24724

    mikethechap
    Participant

    In addition to the question posted just above, I’m also having problems with it auto-starting and then going into the background. I am using a hard-wired version to the network (as of yesterday afternoon and evening – that was a ton of work). Here’s my rc.local file:


    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.

    # Print the IP address
    _IP=$(hostname -I) || true
    if [ "$_IP" ]; then
    printf "My IP address is %sn" "$_IP"
    fi

    #echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-0/new_device
    #hwclock -s

    ### Start the OSPi interval program
    $host=$hostname -I | sed 's/ *$//g')
    host=0.0.0.0
    port=8080
    cd /home/pi/OSPi/
    nohup /usr/bin/python ospi.py $host:$port 2>&1 &


    exit 0

    I still have to manually start the program (change directory / sudo python ospi.py). And, it never goes into the background meaning I have to shut down putty. What could I be missing. It’s as though doesn’t know that it needs to go to the ospi on startup.

    #24725

    Dan in CA
    Participant

    Hi Mike,

    Try this simplified script in RC.local:


    ### Start the OSPi interval program
    cd /home/pi/OSPi/
    python ospi.py 0.0.0.0:8080 &

    To make sure the rc.local file is executable, issue the following command:


    sudo chmod +x /etc/rc.local

    Once you reboot the Pi the program should start running in the background and you can ssh in and do other stuff.

    There are more details in the wiki at:
    http://rayshobby.net/mediawiki/index.php?title=Python_Interval_Program_for_OSPi

    Dan

    #24726

    mikethechap
    Participant

    Thanks so much Dan!

    I was just getting ready to post a reply to say that it didn’t work I thought I would make sure I put the python reference in there and saw that I had typed 0.0.0:8080. Ooops, no wonder “His” code didn’t work. I fixed that and rebooted. Sweeeeeet! 😀

    Thank you, Dan!

Viewing 18 posts - 26 through 43 (of 43 total)
  • You must be logged in to reply to this topic.

OpenSprinkler Forums Hardware Questions OpenSprinkler Pi (OSPi) Starting ospi.py automatically