duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

SSH command to check variable then requests.post to URL

Thu May 23, 2019 8:18 pm

I want to make a program that will execute upon receipt of an SSH command (from the Android app Tasker), where the program will do certain requests.posts. Basic idea is for the program to check a few variable states, then do certain steps depending on the variable states. I already have a program running that knows the variable values, maybe I could add it to that task? Have it do the reporting on demand, from the program that is already monitoring for changes?

Code: Select all

import requests
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Push notification that the service has started
r = requests.post("http://bit.ly/xxxxxxx")

# Light on breadboard showing service was started
ledONPin = 32
GPIO.setup(ledONPin, GPIO.OUT)
GPIO.output(ledONPin, GPIO.HIGH)

# Establish door switch assignments
buttonRUPin = 16  # Right door
buttonLUPin = 37  # Left door
GPIO.setup(buttonRUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonLUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Initialize door state upon start of program, assumed to be closed as this is a normal state
llogic = 1
rlogic = 1

# Monitor of door switches
while True:
    buttonState = GPIO.input(buttonRUPin)
    if buttonState == False:
        if rlogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")  # Right door closed
            rlogic = 1
    else:
        if rlogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")  # Right door open
            rlogic = 0
    buttonState = GPIO.input(buttonLUPin)
    if buttonState == False:
        if llogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")  # Left door closed
            llogic = 1
    else:
        if llogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")  # Left door open
            llogic = 0
    time.sleep(0.1)


So, the "rlogic" and "llogic" variables are the ones I want to check the value of on demand. I already get updates when they change, I just want to ask for them and get the appropriate requests.post to the bitly links as appropriate.

Also, If anyone has any guidance on setting up Tasker and AutoTools to do SSH I sure would appreciate it! The dev of Tasker and AutoTools says there is a line of communication this way, but I can't get any other help on this!
You can't know your limits until you exceed them.

Andyroo
Posts: 2864
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 8:53 pm

I have no idea on the Android side but normally under ssh you would set certificates up first to remove any password issues and then do something like:

Code: Select all

ssh [email protected] pgmname 
Where pgmname is a simple shell script that calls the Python program to do the work as needed.

From a very quick read, AutoTask just has the above code as it’s command line - how you set everything up before that I’ll leave to others :D
Need Pi spray - these things are breeding in my house...

User avatar
thagrol
Posts: 1465
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 9:12 pm

Doing what you want over just ssh is going to be problematic. Nothing outside your python program knows anything about those variables let alone what their values are.

You need to extend your program to handle remote clients. Given that it contains an infinite loop, starting a new instance every time you call it from tasker will put excesive load on your CPU and RAM.

Or, write a second script without the loop that checks the hardware state, does what you need it to then exits. However, that wouldn't update the variables in your existing program.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 10:30 pm

Andyroo wrote:
Thu May 23, 2019 8:53 pm
I have no idea on the Android side but normally under ssh you would set certificates up first to remove any password issues and then do something like:

Code: Select all

ssh [email protected] pgmname 
Where pgmname is a simple shell script that calls the Python program to do the work as needed.

From a very quick read, AutoTask just has the above code as it’s command line - how you set everything up before that I’ll leave to others :D
So the command would look something like

Code: Select all

ssh [email protected] GarageCheck.py
and that would be enough to initiate it, given that all the SSH password and authentication has already been established?

So I need to put something together like a non looping version of what I already have, calling it GarageCheck.py. Maybe this needs a few changes...

Code: Select all

import requests
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

# Establish door switch assignments
buttonRUPin = 16  # Right door
buttonLUPin = 37  # Left door
GPIO.setup(buttonRUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(buttonLUPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)


    buttonState = GPIO.input(buttonRUPin)
    if buttonState == True:
        if rlogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")  # Right door closed
        if rlogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")  # Right door open
    buttonState = GPIO.input(buttonLUPin)
    if buttonState == True:
        if llogic == 0:
            r = requests.post("http://bit.ly/xxxxxxx")  # Left door closed
        if llogic == 1:
            r = requests.post("http://bit.ly/xxxxxxx")  # Left door open
    time.sleep(0.1)
thagrol wrote:
Thu May 23, 2019 9:12 pm
Or, write a second script without the loop that checks the hardware state, does what you need it to then exits. However, that wouldn't update the variables in your existing program.
I wouldn't need it to update the variables. Just need to have a program that does a one time check of the variables and do the requests.posts. I don't mind if it exits after doing the job. In fact, that would be preferred.
You can't know your limits until you exceed them.

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 10:35 pm

Would there be any benefit to (or way of) making the garage switch states "Global" variables so they could be checked by other programs without having to check the GPIO state?

The reason I would have to check on these variables is because Tasker (my android app that does all the GUI for the Security system) sometimes has a few processes colliding and it doesn't process the incoming push generated by the requests.post.
You can't know your limits until you exceed them.

Andyroo
Posts: 2864
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 11:00 pm

The global function is within a single program :cry:

You could look at the ‘memcached memory cache’ suite of programs and the Python module. This lets you share data across programs without writing it to a file but I’ve never used it in anger.

If you are digging this deep in complexity it may be worth stepping back from the current code and design things from scratch...
Need Pi spray - these things are breeding in my house...

PhatFil
Posts: 1189
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: SSH command to check variable then requests.post to URL

Thu May 23, 2019 11:18 pm

duckredbeard wrote:
Thu May 23, 2019 10:35 pm
Would there be any benefit to (or way of) making the garage switch states "Global" variables so they could be checked by other programs without having to check the GPIO state?

The reason I would have to check on these variables is because Tasker (my android app that does all the GUI for the Security system) sometimes has a few processes colliding and it doesn't process the incoming push generated by the requests.post.
Yes, a common way of having a Pi system 'global' or 'Publishing' data to any subscribing service or program running locally on your network or even further afield on the web is to use MQTT, Host a broker on your Pi such as mosquitto and publish your door states as Mqtt Topic/payload packages.

So you can then run a 2nd service perhaps a python script to sit and subscribe to the relevant topics and react accordingly to the publishing of a state change.

I personally also host node red along side mosquitto mqtt broker on my Pi-0-W to handle any state change logic.

useful links
http://www.steves-internet-guide.com/ ( everything you need to know about mqtt and more)

User avatar
thagrol
Posts: 1465
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH command to check variable then requests.post to URL

Fri May 24, 2019 8:11 am

duckredbeard wrote:
Thu May 23, 2019 10:30 pm
thagrol wrote:
Thu May 23, 2019 9:12 pm
Or, write a second script without the loop that checks the hardware state, does what you need it to then exits. However, that wouldn't update the variables in your existing program.
I wouldn't need it to update the variables. Just need to have a program that does a one time check of the variables and do the requests.posts. I don't mind if it exits after doing the job. In fact, that would be preferred.
Don't check the variables, check the GPIO state.

As I've said, checking the variables from a second process is going to be tricky and will add complexity. It's also possible for the variables to be out of sync with reality or for your first program to have crashed (so the variables won't be available at all).

If I were writing this and had to use a "have background process running for mointoring but call a seperate program via ssh to get status" that's the way I'd do it.

I'd also be using callback functions triggered by a change in GPIO state in the background process. Faster response and less CPU load than a polling loop.

Oh, and I'd be using gpiozero rather than RPi.GPIO

Given this is for a security system, you should add some exception handling and some external (to the prgroam) method of restarting it on a crash. A systemd service maybe.

I'm not sure I understand the logic of your code and as written your one shot won't work as you define neither rlogic or llogic before using them.

From your code, you're using a GPIO as a trigger that the door state has changed and relying on a stored state. That's not good. If the door is in the oposite state to the one you expect when your code starts your code will always report the wrong state. Equally, if your code misses a change event (linux is not realtime) it'll be wrong.

Use the GPIO to reflect the actual state of the door not as a state change trigger. That'll simplyfy the logic and remove the dependancy on stored state. You may need to change your choice of hardware sensor as well as your code.l

[edit:]
Having taken another look at all your code and infered your hardware setup from that, I don't think a seperate one shot program is going to work.

Your GPIO connected hardware only tells you that the door has moved. It doesn't tell you whether it is open or closed, you're deriving that from the door's previous state. A one shot process has no way of knowing what that was.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: SSH command to check variable then requests.post to URL

Sat May 25, 2019 2:48 pm

Can you start me in another direction with the program? The door switches are SPDT switches, I could use both sides of the switch if that would help.

This is my first project and this program his evolved from what one member put together for me. If you have another way of monitoring the switch, I would like to see it.
You can't know your limits until you exceed them.

User avatar
thagrol
Posts: 1465
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: SSH command to check variable then requests.post to URL

Sat May 25, 2019 5:34 pm

duckredbeard wrote:
Sat May 25, 2019 2:48 pm
Can you start me in another direction with the program? The door switches are SPDT switches, I could use both sides of the switch if that would help.

This is my first project and this program his evolved from what one member put together for me. If you have another way of monitoring the switch, I would like to see it.
I'm away from home so can't provide much help text the middle of next week.

That said,...

Set up your switch so that they tell you whether the door is open or closed. One switch per door that goes low when the door is closed, high otherwise.

Poll the switches for state. You don't need to store anything as the gpio will directly reflect door state.

Or look at call back functions if you want to use interrupts instead.

Gpiozero and it's documentation would be a good starting place. There are examples in there.
Note to self: don't feed the trolls

If I've asked you a question, please answer it. I'm unlikely to be able to help without that information.

Return to “Python”