From d2c01298743c2c35573af197fa498010f54bd532 Mon Sep 17 00:00:00 2001 From: bigl9800 Date: Fri, 10 Mar 2017 21:45:35 +0100 Subject: [PATCH 1/2] try-block to prevent socket errors Added try block within while-loop to prevent the code from stopping if the internet connection resets and the stream connection breaks in due course. --- plotly-raspi-stream.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plotly-raspi-stream.py b/plotly-raspi-stream.py index 98b9cbe..4f3fea4 100644 --- a/plotly-raspi-stream.py +++ b/plotly-raspi-stream.py @@ -37,7 +37,12 @@ #the main sensor reading loop while True: sensor_data = readadc.readadc(sensor_pin, readadc.PINS.SPICLK, readadc.PINS.SPIMOSI, readadc.PINS.SPIMISO, readadc.PINS.SPICS) - stream.write({'x': i, 'y': sensor_data}) + # try section prevents stopping due to internet reconnection + try: + stream.write({'x': i, 'y': sensor_data}) + except socket.error: + stream.open + stream.write({'x': i, 'y': sensor_data}) i += 1 # delay between stream posts time.sleep(0.25) From 7f5e23a5a367f2e2c6b356c1cfc8d23e93aabb8a Mon Sep 17 00:00:00 2001 From: bigl9800 Date: Mon, 13 Mar 2017 21:49:51 +0100 Subject: [PATCH 2/2] try section prevents stopping due to internet reconnection --- plotly-raspi-stream.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plotly-raspi-stream.py b/plotly-raspi-stream.py index 4f3fea4..7a66704 100644 --- a/plotly-raspi-stream.py +++ b/plotly-raspi-stream.py @@ -2,6 +2,7 @@ from plotly.graph_objs import Scatter, Layout, Figure import time import readadc +import socket username = 'your_plotly_username' api_key = 'your_api_key' @@ -41,7 +42,7 @@ try: stream.write({'x': i, 'y': sensor_data}) except socket.error: - stream.open + stream.open() stream.write({'x': i, 'y': sensor_data}) i += 1 # delay between stream posts