Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

First Feed - Python
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

First Feed - Python

by txbobs on Sat Feb 13, 2016 12:59 am

I am playing around with my first feed and thought I would do a ping and log the date/time if the ping failed.

Code: Select all | TOGGLE FULL SIZE
response=os.system("ping -c1 -w100 " + hostname2)

n = datetime.datetime.now()

if response == 0:
  print hostname2, 'is up.', n
else:
  print hostname2, 'is down!', n
  aio.send('Foo', n)


When I run this, n contains the date and time and prints ok but I get the following error when trying to send it. Any ideas would be appreciated.

pi@raspberrypi:~ $ python adaiotest.py
ping: unknown host texas.rr.com2
texas.rr.com2 is down! 2016-02-12 22:54:26.662560
Traceback (most recent call last):
File "adaiotest.py", line 20, in <module>
aio.send('Foo', n)
File "build/bdist.linux-armv6l/egg/Adafruit_IO/client.py", line 90, in send
File "build/bdist.linux-armv6l/egg/Adafruit_IO/client.py", line 71, in _post
File "/usr/lib/python2.7/json/__init__.py", line 243, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2016, 2, 12, 22, 54, 26, 662560) is not JSON serializable

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Re: First Feed - Python

by txbobs on Sat Feb 13, 2016 1:16 am

Well I don't understand why it was an error but If use str(), I can get it to work.

Code: Select all | TOGGLE FULL SIZE
n = datetime.datetime.now()
timestamp = str(n)
if response == 0:
  print hostname2, 'is up.', n
else:
  print hostname2, 'is down!', n
  aio.send('Foo', timestamp)


Thanks.

txbobs
 
Posts: 22
Joined: Wed Jan 30, 2013 3:11 pm

Please be positive and constructive with your questions and comments.