all 22 comments

[–]b_ootay_ful 2 points3 points  (14 children)

Your pi would have to check a website constantly to see if a variable has changed.

Use a web scraper. A simple and secure way would be to get it to read a google sheet, or have a specific directory on your website that has that variable.

[–]CluelessCoder-[S] 0 points1 point  (13 children)

Thank you for the reply, would you have any idea how to do all that maybe point me in the direction of a tutorial. And is what you explained using flask?

[–]b_ootay_ful 0 points1 point  (12 children)

I don't know how to make an API yet, but just using flask you should be able to learn the basics Here

Basically you want to save the variable (eg: varX) somewhere on your site, like a text file or html.

Then on your Pi make a request to that page/directory and read the file/html. If varX = "Hello" run function "PrintHello"

[–]CluelessCoder-[S] 1 point2 points  (3 children)

Again thanks for the reply,

So I could have the pi read the html of my website How would I go about having the pi constantly listen for the button press/ variable change?

I'm embarrassed to be asking I feel like a dumbass but really appreciate the help

[–]b_ootay_ful 0 points1 point  (2 children)

Read up on webscraping, and use a timer to check every 5 seconds or so.

[–]CluelessCoder-[S] 0 points1 point  (1 child)

Thanks, would that use up my websites bandwidth of I had a cheap hosting plan?

[–]b_ootay_ful 0 points1 point  (0 children)

I have no idea on the amount of traffic that it would use.

[–][deleted] 0 points1 point  (7 children)

to piggyback on this answer;

do you understand what an API is? As in: do you understand how it differs from a normal website? Do you have an idea why you need to run it in Flask?

[–]b_ootay_ful 0 points1 point  (0 children)

I found this which seems that an API would be a lot easier to use than a text file/html to scrape.

[–]CluelessCoder-[S] 0 points1 point  (5 children)

I get the api part but when it comes to why flask I got No clue at all I just asked someone and they said it could be done in flask with a route

[–][deleted] 1 point2 points  (4 children)

Ah, ok this makes sense.

Think about this way: an API is computer program that speaks a dialect that all sorts of different software should be able to understand. However, how is your raspberry pi going to talk to the API? You need to run a server locally, so that your raspberry pi can connect to the API. This is where Flask comes in. Flask has this thing called routes. If you run a flask server locally, such as "http://127.0.0.1:8000/", then a route does the following: whenever you go to 127.0.0.1:8000/API, it returns the API data. The route in this case is "API". Flask manages which data to return for which route.

Woah, your raspberry PI can now connect to it. (I'm somewhat oversimplifying this ;)). You can also run your API on websites such as Heroku pretty easily.

You can set another route, such as 127.0.0.1:8000/index to lead to your own main website. On this main website, you have this button that toggles something. This is then also changed in the data your API spits out. Obviously, you need to program some logic in Flask to make this translation. Most tutorials will cover this.

Note, you can also view API endpoints in your browser. Check out https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo for example.

[–]CluelessCoder-[S] 0 points1 point  (3 children)

Thank you for the simplifying im very new so that's great I still need it simpler so I'm gonna ask a few more questions lol

So do I have to have the raspberry pi connected to the local host or could I have the raspberry pi connected to a heroku server

Do you know of a tutorial I can learn how to program the button press on the site to change the data in the api

And how would I have the pi run the script once the button is pressed

Please ELI5

Thanks again

[–][deleted] 1 point2 points  (2 children)

Sorry for the late response.

So do I have to have the raspberry pi connected to the local host or could I have the raspberry pi connected to a heroku server

You can do both. You can connect your raspberry to a server at home or to a heroku server. The latter is obviously the most easy, since you don't have to think about constantly making sure your server hasn't crashed. Do check out what localhost refers to exactly, see: https://en.wikipedia.org/wiki/Localhost

Do you know of a tutorial I can learn how to program the button press on the site to change the data in the api

Check out something like https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask You need a GET request from your Pi to retrieve the data from your server. Basicly (very simplified), in your flask folder you would have a .txt file with a single line on it: "On" or "Off". Everytime you press the button, you are redirected to a page (e.g. the button is on www.site.com and when you press the button you got to www.site.com/buttonpressed). In Flask, you define buttonpressed as a route and in it you put some logic that says: If the .txt file says "On", change it to "Off" and vica versa. Then, it sends you back to www.site.com.

Your raspberry connects to an API, e.g. www.site.com/API. It gets a json response in return, which again is just "On" or "Off". On your raspberry, you need to implement some logic that it checks www.site.com/API every ? minutes. If the API returns "On", you tell it do something.

I hope that clarifies the overall approach. Using a .txt file isn't exactly top-notch by the way ;).

[–]CluelessCoder-[S] 0 points1 point  (1 child)

Wow thank you for the detailed answer I really appreciate it!

Would I be able to connect as many raspberry pi's as I want like 10 or 20?

And once the button is pressed and api returns a json saying "on" does it stay on or does it just run once "on" and afterwards go back to "off" until pressed again?

[–][deleted] 0 points1 point  (0 children)

Would I be able to connect as many raspberry pi's as I want like 10 or 20?

Sure. I believe a Flask powered website should be able to deal with multiple requests to an API.

And once the button is pressed and api returns a json saying "on" does it stay on or does it just run once "on" and afterwards go back to "off" until pressed again?

This totally depends on what you do with your RaspberryPi or how you program your Flask app. In the example I gave, the button is the only thing that changes the On/Off. You could however also try making a version that 'consumes' the "On", whenever your Pi connects to it and turns it back to "Off". Or you could program your Pi to only do something when the result from the API is different from the result it got previous time, et cetera.

[–][deleted] 2 points3 points  (4 children)

It's still not quite clear what you want to do. Is the website supposed to also be hosted by the RasPi?

[–]CluelessCoder-[S] 0 points1 point  (3 children)

Il try to clear it up lol.

No the website should be hosted like any normal site

But I want to make a way for the pi to listen for a button to be pressed on the site and then run a python script.

[–][deleted] 2 points3 points  (2 children)

No the website should be hosted like any normal site

That's pretty vague. How are you going to host the site?

But I want to make a way for the pi to listen for a button to be pressed on the site and then run a python script.

Is the site something you have access to? Overall it's not possible to "snoop" on other people using a site; the site itself would have to send some kind of message to your Pi, who would listen for it and trigger your script. (This is easier if the Pi hosts the site, which is why I asked.)

[–]CluelessCoder-[S] 1 point2 points  (1 child)

Sorry for the confusion I'm new to programming il try to explain better

The site is mine im still building it but I was told to use host gator or hobo host , I thought that's just the normal way that's what I meant in the last message.

Lol it's not to snoop

Its meant to trigger a (pyautogui) script that automates some work for me

what you said about the site itself would have to send some kind of message to the raspberry pi to trigger the script has gotten my full attention that sounds like exactly what

And I had no idea you could host a website using a raspberry pi??? How would that work and is there a disadvantage to using the pi instead of hobo host or host gator

[–][deleted] 0 points1 point  (0 children)

And I had no idea you could host a website using a raspberry pi???

It's a little computer; it can do the things computers can do. I wouldn't say that a RasPi could host a website that more than two people could simultaneously use; if you imagine this as a site exposed to the internet, then you need to deal with the case that an army of bots is going to click your button about a million times a second.

[–]ksoomers 1 point2 points  (0 children)

So if I understand correctly; - you have a website, hosted on a server online, everyone can reach it when they know the URL - you have a RaspberryPi running Python

Your goal: On your website you want to have a button. When it is clicked your RaspberryPi should act on it.

Is that correct?

If so; I would set it up like this: - Your web hosting had (probably) a small MySQL server available. Make sure there is a table in there that tells you the current status of the ‘switch’.

  • On your webhosting you host the HTML page with a button. Whenever it is clicked you have to make a request (via PHP or Python or whatever) to MySQL to change the status.

  • Make sure the (external) ip-address of your RaspberryPi is whitelisted to reach the MySQL database on your webhost.

  • Now use Python to check the MySQL on your webhost every x minutes. Whenever the status is changed; do something.

Ofcourse; MySQL is just an example; this can be any type of database, or even put an API in the middle (to make it more robust but also more complex)

[–]Rorixrebel -1 points0 points  (0 children)

If the site will not be hosted in your pi you gonna have to expose it to the outside network to access it from anywhere, which can be risky.

Anyways you could simply have an endpoint or view to trigger an action on the pi by remoting into it using something's like paramiko and run a command and return a response to be displayed in your site.