Page 1 of 1

urllib timeout or other solutions?

Posted: Sat Nov 02, 2013 10:31 pm
by Ecurb
Hi all,

So I have a bit of a problem in that my python sketch seems to stop working someitmes when the internet connection cuts out. Is there anyway to tell it to keep trying for a set period of time? I'm using the urllib to take some information from a website.

thank you,
Bruce

Re: urllib timeout or other solutions?

Posted: Sun Nov 03, 2013 10:33 am
by davef21370
Could you post your code?

Dave.

PS. Paste it between the Code tags so it looks like this

Code: Select all

mycode
etc

Re: urllib timeout or other solutions?

Posted: Mon Nov 04, 2013 10:28 pm
by Ecurb
here is the whole thing, please forgive the mess, it's my first time with python and I'm newish to coding in general.

Code: Select all

#!/usr/bin/python
import urllib
import time
import pygame
import json
import RPi.GPIO as GPIO
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import * 
#Set variables outside of loop
last_real_account_value = 100000
BTC_price_count = 89
lcd = Adafruit_CharLCD()
lcd.begin(20,4)
                
#setup the GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(04, GPIO.OUT)
GPIO.output(04, GPIO.LOW)
GPIO.setup(15, GPIO.OUT)
GPIO.output(15, GPIO.LOW)

#Functions below ----------------------------------------------------------

def BTCCHF_price():
        #uses the urlopen tool to download the data from bitcoincharts.com
        BTC_price = urllib.urlopen("http://api.bitcoincharts.com/v1/weighted_prices.json")
        #sets the downloaded value to a static variable
        static_BTC_price = json.load(BTC_price)
        #sets the new variable to the key CHF of the dictionary
        real_BTCCHF_price = static_BTC_price["CHF"]
        #returns the sub-key 24h value 
        return real_BTCCHF_price["24h"]

def exiter(dt):
        pyglet.app.exit()


#blink LED to indicate program is running
def heart_beat():
        GPIO.output(15, GPIO.HIGH)
        time.sleep(.1)
        GPIO.output(15, GPIO.LOW)

#End Functions ----------------------------------------------------------

try:
    while True:

        #creat a counter that when it gets to a number it calls the function to get the current price 
        BTC_price_count = BTC_price_count + 1
        #90 comes from 10 seconds times 90 = 900 = 15 minutes
        if (BTC_price_count == 90):
            holding_real_BTCCHF_price=(BTCCHF_price())
            BTC_price_count = 0
      
        #uses the urlopen tool to download the data from blockchain.info    
        read_account_value = urllib.urlopen("http://blockchain.info/q/addressbalance/1GtaPezXJnAcXhMy5Cq7QYuva463PYhRyR?confirmations=0")
        #read_account_value = urllib.urlopen("http://blockchain.info/q/addressbalance/1MARNUfCsk6qc8VxH2zTxjtbAAvNENESpR?confirmations=0")        
        #sets the downloaded value to a static variable
        real_account_value = (read_account_value.read())
        #store the converted float of the real_account value, divided by 100000000
        real_account_value = (float(real_account_value) / 100000000)
            
        #prints holding_real_BTCCHF_price
        #prints real_account_value
        #turns makes a new varable as a string

        lcd.setCursor(0,3)
        lcd.message("Price CHF/BTC:")
        lcd.message(holding_real_BTCCHF_price)
        
        str_real_account_value=(str(real_account_value))
        lcd.setCursor(0,0)
        lcd.message("Total: ")
        lcd.message(str_real_account_value)        
        lcd.message(" BTC")
        #seperator 
        lcd.setCursor(0,2)
        lcd.message("--------------------")
        #stores the result of multiplies the stored BTC price in CHF by the account value
        account_in_CHF=(float(holding_real_BTCCHF_price)*float(real_account_value))
        lcd.setCursor(0,1)
        lcd.message("Converted: ")
        lcd.message("%0.2f" %account_in_CHF)
        lcd.message(" CHF ")
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        heart_beat()
        time.sleep(1)
        #check to see if the account value has increased        
        if (real_account_value > last_real_account_value): 
            add_to_account_value = real_account_value - last_real_account_value
            lcd.clear()
            lcd.setCursor(0,1)
            lcd.message("%0.4f" %add_to_account_value)
            lcd.message(" BTC added!!!!")
            #play sound if account value increased
            '''     
            pygame.mixer.init()
            pygame.mixer.music.load("money1.wav")
            pygame.mixer.music.play()
            '''
            #blink LED when fed
            LED_show = 0
            while (LED_show < 40):
                    LED_show = LED_show + 1
                    GPIO.output(04, GPIO.HIGH)
                    time.sleep(.06)
                    GPIO.output(04, GPIO.LOW)
                    time.sleep(.06)
            add_to_account_value = 0

        #resets the account value so it detect new moneyz
        last_real_account_value = real_account_value

        
except:
    pass