Page 1 of 1

Detection of internet connection by a Python Script

Posted: Sun Feb 02, 2014 5:31 pm
by PeteThePen
Hi Folks

I am hanging a bundle of DS18B20 temperature sensors off my Pi to monitor the strange activities of our solar thermal panel control box. I have now got my Python skills up to the stage of being able to query the sensors, check if the reading is valid, re-do if not and then write the result to an SQLite database. The program will run with a while True loop (OK there may be a better way but I have yet to find it).

All the kit will be in the airing cupboard and I will be checking on it using X from my normal machine (Works a treat!). However, I gave up on the RTC module as it never seemed to keep the time, and am relying on the system picking up time from the internet through the wifi dongle. Once the system is booted and then connected to the internet, time seems to be automatically updated. However, there is an appreciable delay in that happening. Thinking of power cuts and the like when the Pi will re-start without intervention, I would like it to hold off starting my data collection program until it has picked up the connection to the internet, and the correct time.

Does anybody have any better suggestions than adding time.sleep(180) to the start of my script?

Regards

Pete

Re: Detection of internet connection by a Python Script

Posted: Sun Feb 02, 2014 7:35 pm
by hcunningham
you might run ifconfig and see if there is an IP address... for an example grep see http://pi.gate.ac.uk/pages/blinkip.html (not doing exactly what you want, but might give an idea)

no doubt there's a pythonesque way to do it too which might be more elegant :-)

hth
h

http://pi.gate.ac.uk/

Re: Detection of internet connection by a Python Script

Posted: Sun Feb 02, 2014 7:54 pm
by sdjf
You might run a daemon that loops every nn seconds, and checks to see if the date is a current one. I use unix seconds for that, but you could use the year. Then, once the date is a current one, issue the command that starts your application(s).

In bash, I start with a test of the following, you might do something with the equivalent of "until" in python, and once the condition is reached, start your stuff.

`date +%s` -lt 1358500000

You could test for a current date, it will be 1969 or 1970 until it updates, that would be:

`date +%Y` -lt 2014

Or whatever test you want to use. I have code looking for a current date at the start of certain scripts, an alternative would be to simply put a test in the start of you python script, and loop until the date is a current one.

Re: Detection of internet connection by a Python Script

Posted: Sun Feb 02, 2014 10:03 pm
by PeteThePen
Hi Folks

Thanks for the speedy replies.

I like both the ideas suggested. However, a small play around has underlined the fact that still have a huge amount to learn. So the idea is clear but the implementation is beyond me at the moment.

Regards

Pete

Re: Detection of internet connection by a Python Script

Posted: Mon Feb 03, 2014 8:59 am
by MaxK1
What RTC module are you using? A simple DS1307 based circuit _should_ be accurate enough (Within a few seconds a month)
Otherwise, Adafruit has a more accurate Chronodot. If you need more accuracy than that, you could look into a GPS module get the time from that.

The internet connection could be tested by issuing a single ping to 8.8.8.8 and/or 4.4.4.4 periodically (ping -c1 x.x.x.x and see if the exit code is zero) for something quick 'n dirty....

Re: Detection of internet connection by a Python Script

Posted: Mon Feb 03, 2014 10:53 pm
by PeteThePen
Hi Again

Thanks for the response. The RTC is (I think) by AB Electronics, but that is not really a problem.. It is probably user error with the battery. I am planning a reading of the sensors every ten minutes. Going for a Ping is, at the moment, far too advanced for me to learn. I am fighting with Python for the time being and playing with the Date function. The plan is simply to write the date in the form YYYYMMDD to a file after the sensors have been read. So, this file will be updated every ten minutes. On re-start after (say) a power cut the main script simply checks the system date against the date file and keeps on checking until the new date is newer than the file date.

Unfortunately, my function writing is not yet good enough. The following function when run once will output the date to the variable STORE, but next time it is run claims that STORE has not been defined. That is rubbish, and I suspect that there is an error elsewhere which must be wiping STORE after the first iteration. Here is the function with all the test bits still in place:

#Function to get today's date and convert to integer
def getdate () :
from datetime import date
nowdate = date.today()
new1 = nowdate.year
new2 = nowdate.month
new3 = nowdate.day
#Convert date now to a single string
new1 = str(new1)
new2 =str(new2)
new3 = str(new3)
new2 = new2.zfill(2) # This adds left hand zeros to single digit months
new3 = new3.zfill(2) # This adds left hand zeros to single digit days
store = str(new1 + new2 + new3)
# Change strings to integers
store = int(store)
print "Function used"
print store

You suggestions would be welcome.

Regards

Pete

Re: Detection of internet connection by a Python Script

Posted: Tue Feb 04, 2014 12:07 am
by DougieLawson
Try
ntpq -p
ntpdate

Those commands tell you whether your local clock is sync'd and which NTP servers you're sync'd to.

Physcially disconnect your network, wait ten minutes then test again with those commands.