IOT Printer Problem: Tweets Printing Repeatedly

Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/
Locked
User avatar
gastonw
 
Posts: 11
Joined: Thu Nov 10, 2011 11:23 am

IOT Printer Problem: Tweets Printing Repeatedly

Post by gastonw »

Hi,

I have an Raspberry Pi Internet of Things printer. It prints fine, except for one issue. It seems to get stuck printing the same set of Tweets over and over. I made the change suggested at http://forums.adafruit.com/viewtopic.ph ... 45#p243748 but I still get the same 3 tweets repeated over and over.

I put in some debugging statements in main.py to print lastID value after the call to interval that calls Twitter.py. I see this output:
598874698664046592
598963234142593025
598963354086940673
598963496433229824
598963650532134912
Traceback (most recent call last):
File "twitter.py", line 89, in <module>
urllib.quote(consumer_key) + ':' + urllib.quote(consumer_secret))}
File "twitter.py", line 63, in issueRequestAndDecodeResponse
connection.request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 962, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 996, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 958, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 818, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 780, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 1161, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

598874698664046592
598963989402484736

Notice that after the error occurs, Twitter seems to seems to reset back to the value of max_id marked in bold.

I'm not sure what to do. I'm thinking about adding a floor variable and checking to make sure the last_id variable never drops back to a lower value.

I'd appreciate any suggestions on how to fix this. Many thanks.

User avatar
gastonw
 
Posts: 11
Joined: Thu Nov 10, 2011 11:23 am

Re: IOT Printer Problem: Tweets Printing Repeatedly

Post by gastonw »

After a little more digging and a few print statements, I figured out that the result back from Twitter.py was blank after an error. This causes the original set of tweets to repeat each time. It looks like Twiter sometimes refuses the connection and this causes the connect statement in Twitter.py around line 62 to throw an exception. The exception breaks the flow of execution, and Twitter.py returns an empty value to main.py and the lastid counter is wiped out.

After thinking about it, adding a floor variable to main.py would be an ugly hack. I think a better fix would be to add try logic around the connection statements and return the previous lastid value when it fails.

I'll try this change and see if it works. After I read up on Python try: and except: logic.

User avatar
gastonw
 
Posts: 11
Joined: Thu Nov 10, 2011 11:23 am

Re: IOT Printer Problem: Tweets Printing Repeatedly

Post by gastonw »

Hi,
Good news. Adding try and except in the issueRequestAndDecodeResponse(method, url, body, headers):
routine at about line 60 in twitter.py fixed my problem of the repeated sets of tweets.

What I did was change the code at around line 60 from this:

Code: Select all

# Initiate an HTTPS connection/request, uncompress and JSON-decode results
def issueRequestAndDecodeResponse(method, url, body, headers):
  connection = httplib.HTTPSConnection(host)
  connection.request(method, url, body, headers)
  response = connection.getresponse()
  if response.status != 200:
    # This is OK for command-line testing, otherwise
    # keep it commented out when using main.py
    # print('HTTP error: %d' % response.status)
    exit(-1)

  compressed = response.read()
  connection.close()
  return json.loads(zlib.decompress(compressed, 16+zlib.MAX_WBITS))
to this:

Code: Select all

# Initiate an HTTPS connection/request, uncompress and JSON-decode results
def issueRequestAndDecodeResponse(method, url, body, headers):
  # grw - May 15, 2015
  # Added try block to return the lastid
  # value when conection throws an exception.
  # This will prevent repeated printing of last
  #  set of tweets when Twitter refuses the connection.
  try:
    connection = httplib.HTTPSConnection(host)
    connection.request(method, url, body, headers)
    response = connection.getresponse()
  except:
    # printer.println('--Connection Error--')  # debugging
    # printer.println(lastId) # debugging
    print(lastId) #Piped back to calling process
    exit(0)

  if response.status != 200:
    # This is OK for command-line testing, otherwise
    # keep it commented out when using main.py
    # print('HTTP error: %d' % response.status)
    # exit(-1)
    print(lastId) #Piped back to calling process
    exit(0)

  compressed = response.read()
  connection.close()
  return json.loads(zlib.decompress(compressed, 16+zlib.MAX_WBITS))
Everything seems to be working fine after that change. Uncommenting the two debug lines will print a message each time the connection is refused by Twitter. But that can waste a lot of paper.

I hope this information is helpful to someone. Good luck!

Locked
Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Return to “Adafruit Raspberry Pi® accessories”