I’m using my raspberry pi to stream temperature data to plotly which is working fine so far. However my internet connection is very unstable and drops out every few hours. I cant figure out how to get the pi to reconnect to the graph whenever i lose the internet connection. The API documentation doesn’t really specify the code required to reconnect without erasing and restarting the entire graph. Currently i have to manually restart the program every time i disconnect, but this causes all of my previous data to be erased.
Here’s my current code:
py.sign_in(plotly_user_config[“plotly_username”], plotly_user_config[“plotly_api_key”])
stream_id1 = dict(token=token1, maxpoints=10000)
stream_id2 = dict(token=token2, maxpoints=10000)
trace1 = go.Scatter(x=[], y=[], stream=stream_id1, name=‘trace1’)
trace2 = go.Scatter(x=[], y=[], stream=stream_id2, yaxis=‘y2’, name=‘trace2’, marker=dict(color=‘rgb(148, 103, 189)’))
data = [trace1, trace2]
layout = go.Layout(
title=‘Temperature Monitor’,
yaxis=dict(
title=‘Inside’,
range=[0,35]
),
yaxis2=dict(
title=‘Outside’,
titlefont=dict(
color=‘rgb(148, 103, 189)’
),
tickfont=dict(
color=‘rgb(148, 103, 189)’
),
overlaying=‘y’,
side=‘left’,
range=[0,35]
)
)
fig = go.Figure(data=data, layout=layout)
plot_url = py.plot(fig, filename=‘Temperature_Monitor’)
stream = []
stream.append(py.Stream(stream_id=token1))
stream.append(py.Stream(stream_id=token2))
stream[0].open()
stream[1].open()
sensors = []
i = 0
for sensor in W1ThermSensor.get_available_sensors():
sensors.append(sensor)
i = i+1
the main sensor reading and plotting loop
while True:
currtime = datetime.datetime.now()
for count in range(0,i):
# temperature in celsius
temp_C = sensors[count].get_temperature()
# convert celsius to fahrenheit
temp_F = sensors[count].get_temperature(W1ThermSensor.DEGREES_F)
# show only one decimal place for temperature
temp_C = “%.1f” % temp_C
temp_F = “%.1f” % temp_F
# write the data to plotly
stream[count].write({‘x’: currtime, ‘y’: temp_C})
time.sleep(30)