[SOLVED] Raspberry pi stops sending data after 15~30 minutes

Hi.

I am a beginner to this field and currently designing a digital alcohol sensor project. To briefly explain, if a sensor does not detect any alcohol, the data value is 0 but when it does the value becomes 1.

I am using Raspberry pi zero wireless and python.

My problem is, the sensor works perfectly with Ubidots for the first 15~30 minutes. However, after certain amount of time has passed, the connection abruptly fails and stops sending data.

Here’s my code(GPIO no.7 is where I plugged the sensor in) :

from ubidots import ApiClient
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

try:

   api = ApiClient(token="wn28jJZVysdZOL6WM2DbnBVi4bKiOM")
   variable = api.get_variable("594e66f27625423d76e57f83")
   print("connectd to ubidots")

except:

   print ("connection failure")

digital = 0
while(1):
presence = GPIO.input(7)

    if (presence==0):
            variable.save_value({"value":digital})
    if (presence):
            digital = 1
            variable.save_value({"value":digital})

Also, when the device gets disconnected with Ubidots, this is what comes out on the Terminal.

Traceback (most recent call last):
File “ubi.py”, line 24, in
variable.save_value({“value”:digital})
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 148, in wrapped_f
return fn(self, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 306, in save_value
return self.bridge.post(‘variables/’ + self.id + ‘/values’, data).json()
File “/usr/lib/python2.7/dist-packages/requests/models.py”, line 793, in json
return json.loads(self.text, **kwargs)
File “/usr/lib/python2.7/json/init.py”, line 338, in loads
return _default_decoder.decode(s)
File “/usr/lib/python2.7/json/decoder.py”, line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python2.7/json/decoder.py”, line 384, in raw_decode
raise ValueError(“No JSON object could be decoded”)
ValueError: No JSON object could be decoded

Thank you!

Hello, I think that you should try to make the connection inside your loop, in that way you avoid the connection lost erros,

Regards

I’d recommend using a try-except inside the loop to wrap the save.value function. This way you make sure that, if the connection is lost or something happens, the program will continue and the loop won’t break.

1 Like

Sorry for the late response but seems like the problem has been solved! Thank you!

Glad to know that you could solve the issue.

Regards

I am having a similar problem, my code stops with this report. Any idea why?

Traceback (most recent call last):
File “ubi_onoff_lib_ADC.py”, line 86, in
ADC2_rep = ADC2.save_value({‘value’: volt2})
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 148, in wrapped_f
return fn(self, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 306, in save_value
return self.bridge.post(‘variables/’ + self.id + ‘/values’, data).json()
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 108, in wrapped_f
response = fn(self, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 88, in wrapped_f
response = fn(self, *args, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/ubidots/apiclient.py”, line 204, in post
response = requests.post(self.base_url + path, data=data, headers=headers, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/api.py”, line 112, in post
return request(‘post’, url, data=data, json=json, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/api.py”, line 58, in request
return session.request(method=method, url=url, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/sessions.py”, line 508, in request
resp = self.send(prep, **send_kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/sessions.py”, line 618, in send
r = adapter.send(request, **kwargs)
File “/usr/local/lib/python2.7/dist-packages/requests/adapters.py”, line 508, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host=‘things.ubidots.com’, port=80): Max retries exceeded with url: /api/v1.6/variables/59bbbf
00c03f970e68701b3c/values (Caused by NewConnectionError(’<urllib3.connection.HTTPConnection object at 0xb60433b0>: Failed to establish a new connectio
n: [Errno -3] Temporary failure in name resolution’,))

Hi there, from this piece of code answer:

you are experiencing a DNS issue, please check your local network settings. Also, you can include your send data routine inside a try-except to avoid that your code stops if the DNS is not solved properly by your device.

Regards