Interact with python script running infinitive loop from web - python

I have a python script on my raspberry-pi continuously (every 5 seconds) running a loop to control the temperature of a pot with some electronics through GPIO.
I monitor temperature on a web page by having the python script write the temperature to a text file witch I request from java script and HTTP on a web page.
I would like to pass a parameter to the python script to make changes to the controlling, like change the target temperature.
What would be the better way to do this?
I'm working on a solution, where the python script is looking for parameters in a text file and then have a second python script write changes to this file. This second python script would be run by a http request from the web page.
Is this a way to go? Or am I missing a more direct way to do this.
This must be done many time before and described on the web, but I find nothing. Maybe I don't have the right terms to describe the problem.
Any hints is appreciated.
Best regards Kresten

You have to write somewhere your configuration for looping script. So file or database are possible choices but I would say that a formatted file (ini, yaml, …) is the way to go if you have a little number of parameters.

not sure about raspberry-pi but I see these solutions:
os signal (doc here
socket see Python socket server/client programming

Related

how to automated my selenium web scrapper?

I was able to write a script to scrape using selenium, right now I'm trying to automate it so it can work periodically on a server so I don't bother myself by running it from my local, I did a lot of googling but I got no clue of how I can do that, can anyone simplify things for me ..
In order to run a python script on a linux server periodically you can make a cronjob, since you already have a python script which most probably fetches or scrapes data and saves it in a file. You can make a cronjob and set the exact time it has to run, say for instance after every 2 hours you can do it using something like this,
crontab -e
this will open a editor in your terminal, at the bottom of the text just write timing and the command to be executed.
* * * * */path/to/your/code.py
from this link you can find out how to fill out the stars https://crontab.guru/#*_*_*_*_1
if you need anymore help with using cronjobs take a look at this https://www.geeksforgeeks.org/scheduling-python-scripts-on-linux/
You can simply redo your script on pythonanywhere and schedule it as a task and choose the frequency that you want the script to be executed. The current frequency options available include; script runs always, hourly or daily.
i dont know if it'll really work but
while True:
whole
code
here
time.sleep(period required)

Best way to create a script to automate a simple process using selenium?

Ok, so I'm a total noob with aspirations of learning to code. I've read about a guy who, for example, wrote a script which, if he was at work past a certain time, would automatically send a text to his wife stating he would be late. I want to do something sorta similar.
What I want in essence is a script that will log in to a website at a certain time of day, check if a box/text is green/yes or red/no, and send a text or notification to my phone informing me of the result each day.
The progress I've made so far is installing Python, installing PyCharm and done some research about tools I could use toward achieving my goal. Selenium seems like it would be capable of logging into the website, but I've no idea how to go about setting up a conditional statement to check the result, nor how I could set it up to send a text/notification to my phone. Also, if there is a more appropriate tool I should look into rather than Selenium and Python, I'm not attached to the idea of using these specific tools.
Finally, I realize that this may end up being too complicated for a first project, so I'd be up for hiring a freelancer to set this up. Equally, if this is something that could feasibly be written by someone with very little knowledge of coding such as myself, I'd really appreciate some direction from an expert!
Thanks for any input!
You are on the right track with selenium for web form automation. Sending notification however would require something else as was pointed out, and if you're on windows you can use windows task scheduler to automate, to performed only on certain time of day etc.
To make things more simplified, you can also look up general purpose automation programs that might support all these features together. For example, JRVSInputs uses selenium for web auto-fills https://jrvs.in/forums/viewtopic.php?t=182 and have features to send email or windows notifications. It can convert all its scripts into a neat batch file, you can then automate this batch file in the task scheduler.

Is there a python module to indicate if the .py is running?

I am new to python.
I created a simple Selenium bot that basically runs on a while loop.
However, it runs on an external machine that I do not have access at all times.
It runs into unexpected crashes sometimes, and it takes hours and sometimes days until I check it again only to find a crashed bot. With that said, I'd like to know if a monitoring module or program I can implement into my bot, so I can easily check if the program is running or not.
Some have suggested a WebSocket heartbeat, however, I do not know, after extensive research, how I would go about implementing such an intricate system. Thank you for taking the time to read my question.
Any answers appreciated!
You can just put a few lines of code at the appropriate point in your script (after the Selenium bot completes its work successfully) to write the current date and time into a file.
A second script can be scheduled just to read that file and ring an alarm bell (e.g. send you an email) if it is X hours since the timestamp was last updated.
Alternatively you could write a timestamp to a database table or use whatever other shared systems you have.

How to create a website that acts as GUI for a python script

We (my team and I) are developing a prototype sensor that uses a raspberry to preprocess sensor readings.
we have a working python script and made a simple GUI using GUIzero to test everything, but this needs a screen to be connected, and that is not possible once the setup is finished and deployed in a field for example.
We now have the raspberry acting as a wifi-hotspot, and after connecting to the local network, we can access the RBPi using VNC-viewer, and interact with the simple (guizero-)GUI. This is fine for continuing testing and developing. But once we will distribute the sensor to some test-users, the VNC-solution is not optimal, as it allows too much snooping around on the RBPi.
To solve this, we were thinking that a nice solution would be to somehow link our python script with a web page, hosted on the RBPi. A user could then walk upto the sensor, connect to the local wireless network, type in an IP in a browser and the page that loads would then allow starting/stopping the sensor, downloading measured data, managing archiving data, ...
Googling points in many directions (Django, flask, ...) and I'm too much of a beginner to choose the path to take (and understand (dis)advantages of all these frameworks/libs/...)
Can someone point me in the correct direction? (we know more python than we know html or PHP or..., so if the solution could be friendly in that sense, that's a plus)
If you are familiar with Python, I would advise you to set up a Django application on your RasPi (their beginner tutorial covers everything you need and the whole framework is documented really well). From there you could go two ways:
Either create a single view (basically a function that gets called when a certain URL is called) that renders some HTML with buttons you can connect to some Python code on your system
Or you could create one view per function (e.g. /api/start_sensor, /api/download_data) and connect these API-calls to a webview.
The latter variant would also allow controlling the sensor programmatically via network.

Best way to run a python program from user input via a website

So I'm trying to make a website that allows users to write and execute python programs via a text area in the browser. My front end code is written in react and I was thinking about setting up a server to basically save the input as a file and use the exec function to execute it. After doing a lot of reading I now realize how unsafe this is. Is there a standard way of doing this? I'm very flexible as to how it's implemented. Thanks!

Categories

Resources