I am trying to configure an account with a JSON file, but it is giving an AttributeError. I have checked the location of the JSON it is importing. I have been fiddling with this for a few days now and I cannot seem to find the source of this issue. The __init__.py file
in JSON has a dumps function defined in it. I am new to python, so I would like to apologize in advance if there is a mistake to this that I overlooked or if there is a formatting issue. Thank you.
Input:
import sys
import json
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MAX31855.MAX31855 as MAX31855
__Author__ = 'Zach'
CLK = 11
DO = 25
sensor1 = MAX31855.MAX31855(CLK, 13, DO)
sensor2 = MAX31855.MAX31855(CLK, 29, DO)
sensor3 = MAX31855.MAX31855(CLK, 05, DO)
sensor4 = MAX31855.MAX31855(CLK, 06, DO)
sensor5 = MAX31855.MAX31855(CLK, 20, DO)
sensor6 = MAX31855.MAX31855(CLK, 21, DO)
sensor7 = MAX31855.MAX31855(CLK, 12, DO)
sensor8 = MAX31855.MAX31855(CLK, 16, DO)
try:
config_data = open('/home/pi/TempMonitor/picloud_config.json').read()
config = json.loads(config_data)
except:
print 'Failed to load config data from picloud_config.json. Exiting.'
raise
host = config['aggregate_host']
room = config['room']
location = config['location']
api_key = config['api_key']
app_key = config['app_key']
dc = config['dc']
options = {
'api_key': api_key,
'app_key': app_key
}
tags = [
'dc:' + dc,
'room:' + room,
'location:' + location
]
from datadog import initialize
from datadog import api
initialize(**options)
print('Press Ctrl-C to quit.')
print json
temp1 = float(sensor1.readTempC())
print('T1: {0:0.3F}*C'.format(temp1))
api.Metric.send(metric='sls.p7.temperature1', points=temp1, host=host, tags=tags)
temp2 = float(sensor2.readTempC())
print('T2: {0:0.3F}*C'.format(temp2))
api.Metric.send(metric='sls.p7.temperature2', points=temp2, host=host, tags=tags)
temp3 = float(sensor3.readTempC())
print('T3: {0:0.3F}*C'.format(temp3))
api.Metric.send(metric='sls.p7.temperature3', points=temp3, host=host, tags=tags)
temp4 = float(sensor4.readTempC())
print('T4: {0:0.3F}*C'.format(temp4))
api.Metric.send(metric='sls.p7.temperature4', points=temp4, host=host, tags=tags)
temp5 = float(sensor5.readTempC())
print('T5: {0:0.3F}*C'.format(temp5))
api.Metric.send(metric='sls.p7.temperature5', points=temp5, host=host, tags=tags)
temp6 = float(sensor6.readTempC())
print('T6: {0:0.3F}*C'.format(temp6))
api.Metric.send(metric='sls.p7.temperature6', points=temp6, host=host, tags=tags)
temp7 = float(sensor7.readTempC())
print('T7: {0:0.3F}*C'.format(temp7))
api.Metric.send(metric='sls.p7.temperature7', points=temp7, host=host, tags=tags)
temp8 = float(sensor8.readTempC())
print('T8: {0:0.3F}*C'.format(temp8))
api.Metric.send(metric='sls.p7.temperature8', points=temp8, host=host, tags=tags)
Output:
<module 'json' from '/usr/lib/python2.7/json/__init__.py'>
T1: NAN*C
Traceback (most recent call last):
File "gather_temps.py", line 56, in <module>
api.Metric.send(metric='sls.p7.temperature1', points=temp1, host=host, tags=tags)
File "/usr/local/lib/python2.7/dist-packages/datadog/api/metrics.py", line 120, in send
return super(Metric, cls).send(attach_host_name=True, **metrics_dict)
File "/usr/local/lib/python2.7/dist-packages/datadog/api/resources.py", line 68, in send
attach_host_name=attach_host_name)
File "/usr/local/lib/python2.7/dist-packages/datadog/api/api_client.py", line 112, in submit
body = json.dumps(body)
AttributeError: 'module' object has no attribute 'dumps'
So the issue was that the Datadog directory had a few files that were going to the wrong folders. After some uninstalling and reinstalling of directories. I got it to work just fine.