pi9
Posts: 145
Joined: Wed Jun 10, 2015 3:32 pm

Can you call a url from a python script?

Sat Feb 08, 2020 2:21 am

I built a few curtain openers with arduino based controllers. I access the units using a built in webpage that i created to open and close the curtain. To open the curtain http://ip_address/open and to close (surprisingly) http://ip_address/close :)
Now I want to take it to the next level, I need to automate the openings and closings based on time of day or even outside light. The controllers don't have built in time (and I am not really interested in adding one). I am thinking that I should create a crontab entry that calls a python script that would somehow call the appropriate url for the activity.

Of course the side effect of that is that it would be hard coded, so I am open for other ideas as long as they are relatively simple.

robbes
Posts: 148
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada

Re: Can you call a url from a python script?

Sat Feb 08, 2020 6:39 am


User avatar
DougieLawson
Posts: 39589
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Can you call a url from a python script?

Sat Feb 08, 2020 9:22 am

Change the Arduinos to use the pub/sub client. Change the Raspberry to run Mosquito or HiveMQ. Change the web app to use MQTT websockets.

That gives you an MQTT broker that's designed for this IoT stuff.

It's bidirectional and an order of magnitude easier than GET/POST HTTP processing.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

hippy
Posts: 8084
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Can you call a url from a python script?

Sat Feb 08, 2020 2:43 pm

pi9 wrote:
Sat Feb 08, 2020 2:21 am
I am thinking that I should create a crontab entry that calls a python script that would somehow call the appropriate url for the activity.

Of course the side effect of that is that it would be hard coded, so I am open for other ideas as long as they are relatively simple.
Calling urllib from Python is easy enough to do. You could equally have a shell script which calls wget or curl -

Code: Select all

import urllib

HOST = "192.168.0.123"

def WGet(filename):
  socket = urllib.urlopen("http://" + HOST + "/" + filename)
  pageSource = socket.read()
  socket.close()
  return pageSource

def Open():
  WGet("open")

def Close():
  WGet("close")
  
As to avoiding hard coding, I believe you should be able to specify a name in your URL and have your 'hosts' file or DNS pointing to the target IP address. You would only need to change that one thing regardless of what the IP Address is.

Alternatively your Python file can open and read a file which has the IP address to use in it. I added my own /device.txt file which sits alongside /config.txt for my own per-Pi configuration settings to serve exactly that purpose.

pi9
Posts: 145
Joined: Wed Jun 10, 2015 3:32 pm

Re: Can you call a url from a python script?

Sat Feb 08, 2020 4:51 pm

Thanks for all the suggestions. I will try them hopefully soon. One thing to clarify is what I meant by hard coding. My present plan is to put the scheduled curtain openings in the crontab. Of course (at least to my understanding) is I will have to edit the crontab from linux. I would prefer to give my self the option of making those schedule changes from a configuration php page on the pi. I am not sure how this can be done or at least i don't yet have a thought on how to do it. I have a dedicated pi3 just for those IoT projects. Collecting sensor data mostly and now I would like to use it to control things. The database is getting big, but still ok i think. I have two years of 5 minutes increments of collected data.

pfletch101
Posts: 629
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Can you call a url from a python script?

Sat Feb 08, 2020 10:05 pm

pi9 wrote:
Sat Feb 08, 2020 4:51 pm
Thanks for all the suggestions. I will try them hopefully soon. One thing to clarify is what I meant by hard coding. My present plan is to put the scheduled curtain openings in the crontab. Of course (at least to my understanding) is I will have to edit the crontab from linux. I would prefer to give my self the option of making those schedule changes from a configuration php page on the pi. I am not sure how this can be done or at least i don't yet have a thought on how to do it. I have a dedicated pi3 just for those IoT projects. Collecting sensor data mostly and now I would like to use it to control things. The database is getting big, but still ok i think. I have two years of 5 minutes increments of collected data.
I am doing the same sort of thing, but controlling WeMo switches rather than curtain actuators. I use pywemo for the lower level WeMo interface, but the control system uses a Flask application to create and change entries in a sqlite database corresponding to the desired on and off times for each switch and a separate server application which reads the database entries and sends out the necessary commands. I have this working at home and at a remote location, where I can browse in and change settings as I wish. The Pis also collect and store sensor and weather data, and (for the home one) communicates with a Windows application which displays this.

pi9
Posts: 145
Joined: Wed Jun 10, 2015 3:32 pm

Re: Can you call a url from a python script?

Sat Feb 08, 2020 11:33 pm

Originally when I started those projects I was using those fancy websites which some offer apps with, but once you get to a certain data limit you have to start a paid account. That is why I switched to the pi. The interface I created is better than the website i started with and I have a lot more flexibility and no data limit.

pi9
Posts: 145
Joined: Wed Jun 10, 2015 3:32 pm

Re: Can you call a url from a python script?

Sun Feb 09, 2020 6:25 pm

Thanks hippy. Your code worked and the curtains now open and close on schedule. Thinking about it I will write a python script that queries the SQL server every minute to check for the programming for all devices and execute accordingly.

User avatar
Paul Webster
Posts: 822
Joined: Sat Jul 30, 2011 4:49 am
Location: London, UK
Contact: Twitter

Re: Can you call a url from a python script?

Sun Feb 09, 2020 7:58 pm

I have something like this in my crontab to turn on lights around early morning (if still dark) and on again near sunset. It also turns them off 1 hour after sun rise.
I turn them off at fixed times via other crontab lines.

Code: Select all

30 6 * * 1-5 /home/pi/lights.sh NIGHT HEX242430 NIGHT ON
0 8 * * 1-5 /home/pi/sunwait/sunwait wait rise offset 01:00 50N 0W ; /home/pi/lights.sh OFF
0 15 * * * /home/pi/sunwait/sunwait wait set offset 5 51.5084N 0.2746W ; /home/pi/lights.sh HEX454500 ON
My lights.sh script also calls sunwait to see if it is still night time. The calls to sunwait use my (edited) latitude and longitude to calculate then the sun rises/sets.

pi9
Posts: 145
Joined: Wed Jun 10, 2015 3:32 pm

Re: Can you call a url from a python script?

Mon Feb 10, 2020 1:14 am

Nice. So it is all based on calculations? In my case I'd rather base it on a sensor because the light in my backyard might be different for whatever reason.

User avatar
Paul Webster
Posts: 822
Joined: Sat Jul 30, 2011 4:49 am
Location: London, UK
Contact: Twitter

Re: Can you call a url from a python script?

Mon Feb 10, 2020 2:44 pm

Yes - mine is based on calculations of sunrise/sunset.

Return to “Python”