studioimaginaire / phue

A Python library for the Philips Hue system
Other
1.52k stars 267 forks source link

ConnectionResetError: [Errno 54] Connection reset by peer #182

Closed Jonathanpou closed 3 years ago

Jonathanpou commented 3 years ago

Hi im trying to get my light to change colour based on a stock going up or down, but after 30 min or so phue gives me an error

  File "/Users/jonathanpoulsen/Documents/Repos/Home projects/phue-stonk-indicator/Stock_indicator.py", line 42, in <module>
    set_light(red)
  File "/Users/jonathanpoulsen/Documents/Repos/Home projects/phue-stonk-indicator/Stock_indicator.py", line 15, in set_light
    b.set_light(4, 'bri', 254)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/phue.py", line 885, in set_light
    result.append(self.request('PUT', '/api/' + self.username + '/lights/' + str(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/phue.py", line 662, in request
    result = connection.getresponse()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 1347, in getresponse
    response.begin()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/http/client.py", line 268, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/socket.py", line 669, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 54] Connection reset by peer```

My script
``` python
from phue import Bridge
from yahoo_fin import stock_info as si
from yahoo_fin.stock_info import get_market_status
import time

b = Bridge('192.168.8.110') # Bridge IP

b.connect()

red = [0.7350000508904126, 0.24174193816705097]
green = [0.11500021676131911, 0.8155333283841992]

def set_light(color):
        b.set_light(4, 'bri', 254)
        b.set_light(4, 'xy', color)

#Variable to change whitch stock it's monitoring
stock = "GME"
status = get_market_status()

last_price = 0
last_state = None

while status == "REGULAR":
        #get price og the stock in real time
    price = si.get_live_price(stock)
    #reducing the decimals from like a 100 to max 3
    redu = str(round(price, 3))
    state = None
        #printing the formated stock price 
    print(stock + ": {} ".format(redu))
    #waits 10 sec before printing the next price not to strees the connection to Yahoo Finance
    time.sleep(10)

#Light controller
    if price >= last_price:
        state = True
        set_light(green)
    else:
        state = False
        set_light(red)

    last_price = price
    last_state = state
natcl commented 3 years ago

Hello, At first glance this doesn't seem to be an issue with the library but with the Bridge itself disconnecting the client. Is it possible your network is not stable ?

Jonathanpou commented 3 years ago

Well now that you mention it, i don't have the most reliable network. But thanks alot for the help guiding my towards a fix :)