daily pastebin goal
50%
SHARE
TWEET

Raspberry Pi High Tide Tracker

AverageMan Dec 30th, 2015 2,324 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #
  2. # Southend-on-Sea high tide display project by @AverageManVsPi
  3. # Blog of this project at http://www.averagemanvsraspberrypi.com/2015/12/raspberry-pi-high-tide-tracker.html
  4. # Code used from RasPi.TV 7-Seg tutorial and Kickstarter tracker tutorial (and kind of merged together and changed)
  5. #
  6.  
  7. import RPi.GPIO as GPIO
  8. import time
  9. import subprocess
  10. import os
  11. from urllib2 import Request, urlopen, URLError
  12.  
  13. # SET GPIO MODE
  14. GPIO.setmode(GPIO.BCM)
  15.  
  16. # GPIO PORTS FOR THE 7SEG PINS (I think the order is important, in case you change these)
  17. segments =  (17,9,13,5,11,27,19,6) # ALL HAVE RESISTORS
  18.  
  19. # SET THE SEGMNENT DISPLAY GPIO PORTS TO OUTPUTS
  20. for segment in segments:
  21.     GPIO.setup(segment, GPIO.OUT)
  22.     GPIO.output(segment, 0) # SET ALL SEGMENTS TO LOW
  23.  
  24. # GPIO PORTS FOR THE DIGIT 0-3 PINS
  25. digits = (3,22,10,26) # NO RESISTORS
  26.  
  27. # SET THE DIGIT PINS TO OUTPUTS
  28. for digit in digits:
  29.     GPIO.setup(digit, GPIO.OUT)
  30.     GPIO.output(digit, 1) # SET ALL SEGMENTS TO HIGH
  31.  
  32. num = {' ':(0,0,0,0,0,0,0),
  33.     '0':(1,1,1,1,1,1,0),
  34.     '1':(0,1,1,0,0,0,0),
  35.     '2':(1,1,0,1,1,0,1),
  36.     '3':(1,1,1,1,0,0,1),
  37.     '4':(0,1,1,0,0,1,1),
  38.     '5':(1,0,1,1,0,1,1),
  39.     '6':(1,0,1,1,1,1,1),
  40.     '7':(1,1,1,0,0,0,0),
  41.     '8':(1,1,1,1,1,1,1),
  42.     '9':(1,1,1,1,0,1,1)}
  43.    
  44. try:
  45.  
  46.     #======================================================================
  47.     # MAIN
  48.    
  49.     def mainprog():
  50.  
  51.         timelastchecked = 0
  52.  
  53.         errsquare() # Show a square on the display so that we know it has started the program properly
  54.         time.sleep(2) # Wait 2 seconds
  55.        
  56.         while True:
  57.             if time.time() >= timelastchecked: #If the current time is greater than or equal to the 'timelastchecked' string
  58.                
  59.                 print "-------- UPDATING TIDE TIME --------"
  60.                 someurl= 'https://www.tidetimes.org.uk/southend-on-sea-tide-times' # Set the URL for the Pi to look in to
  61.                 req = Request(someurl) # Request the URL
  62.                
  63.                 try:
  64.                     response = urlopen(req) # Open the URL
  65.                     print "Trying URL..."
  66.                    
  67.                 except URLError as e: # This section is for error handling if WiFi is down etc
  68.                
  69.                     if hasattr(e, 'reason'): # One reason (unsure what!)
  70.                         print 'We failed to reach a server.'
  71.                         print 'Reason: ', e.reason
  72.                        
  73.                         errsquare() # Show a square on the display to indicate an issue
  74.                         time.sleep(900) # Wait 15 minutes before trying again so as not to over do the url requests
  75.                        
  76.                     elif hasattr(e, 'code'): # Another reason (unsure what!)
  77.                         print 'The server couldn\'t fulfill the request.'
  78.                         print 'Error code: ', e.code
  79.                        
  80.                         errsquare() # Show a square on the display to indicate an issue
  81.                         time.sleep(900) # Wait 15 minutes before trying again so as not to over do the url requests
  82.                        
  83.                 else:
  84.                     print "URL open success!"
  85.                     nexthightide = response.readlines() #read the lines of the URL (web page source) and turn that into 'nexthightide'
  86.                     time.sleep(1) # Wait a second
  87.                    
  88.                     for line in nexthightide: # For statement
  89.                    
  90.                         if 'nxhi' in line: # If the Pi finds 'nxhi' in the line...
  91.                             line = line[0:37] # Chop 37 characters from the back of the line
  92.                             print "Starting line string is: ", line
  93.                             nhtpt1 = line[31:-4] # Take that line and cut away 31 characters from the front and 4 from the rear
  94.                             nhtpt2 = line[34:-1] # Take that line and cut away 34 characters from the front and 1 from the rear
  95.                             nexthightide = nhtpt1 + nhtpt2 # add those two cut strings together to make a 4-digit string
  96.                             print "First segment of time: ",nhtpt1
  97.                             print "Second segment of time: ",nhtpt2
  98.                             print "Time string: ", nexthightide
  99.                             timelastchecked = time.time()+3600 # wait 1 hour until next url check
  100.  
  101.             else:
  102.                 for digit in range(4):
  103.                     for loop in range(0,7):
  104.                         GPIO.output(segments[loop], num[nexthightide[digit]][loop])
  105.                     GPIO.output(digits[digit], 0)
  106.                     time.sleep(0.001)
  107.                     GPIO.output(digits[digit], 1)
  108.                
  109.     def errsquare():
  110.         # Set digits to low to activate digit
  111.         GPIO.output(3, 0)
  112.         GPIO.output(22, 0)
  113.         GPIO.output(10, 0)
  114.         GPIO.output(26, 0)
  115.        
  116.         # Set segment high to activate segment
  117.         GPIO.output(17, 1)
  118.         GPIO.output(5, 1)
  119.        
  120.     #Run main program:
  121.     mainprog()
  122.    
  123. #======================================================================
  124. # EXCEPT BLOCK
  125. # This will run if there is an error or we choose to exit the program
  126.  
  127. except KeyboardInterrupt: # USE THIS OPTION FOR DEBUGGING
  128.  
  129.     print "EXIT SCRIPT"
  130.     time.sleep(0.5)
  131.    
  132.     # Clean up GPIOs
  133.     print "PERFORMING GPIO CLEANUP"
  134.     time.sleep(0.5)
  135.     GPIO.cleanup() # Clean up the gpio pins ready for the next project.
  136.    
  137.     # Exit program
  138.     print "--- EXIT NOW ---"
  139.     time.sleep(0.5)
  140.     quit()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top