Hi,
I’m trying to read the temperature from the device and send it to a server which accepts the details in a JSON and requires authorization which I’m hard coding here just for testing. So here is the code:
// Agent Code (sending temperature)
function postReading(reading) { // reading is the data (squirrel table) passed from the device
// Define a table with the JASON info
local AM_Data = [
{
metricCode="testMetric1",
values=[
{
metricValue= reading.temp,
detectionTime="2015-11-05T13=15=30Z"
},
{
metricValue= reading.temp,
detectionTime="2015-11-05T13=15=30Z"
}
]
},
{
metricCode="testMetric2",
values=[
{
metricValue= "test",
detectionTime="2015-11-05T13=15=30Z"
}
]
}
]
// encode data into JSON and log
local body = http.jsonencode(AM_Data);
server.log(body);
local url = "http://mytest.com:8580/agentapi/assetmetrics";
local headers = {"Content-Type": "application/json", "Authorization": "Basic ZGYwNGFjNWEtNGQ4Zi00NjllLTg4MjEtYTU3YTAxNzRiZWRkOg=="};
local request = http.post(url, headers, body);
local response = request.sendsync();
server.log(response.statuscode);
}
device.on(“reading”, postReading);
So running this I get :
2015-12-24 13:57:25 UTC-5 [Status] Agent restarted: reload.
2015-12-24 13:57:25 UTC-5 [Status] Device connected
2015-12-24 13:57:25 UTC-5 [Device] Got temperature: 26.5 deg C
2015-12-24 13:57:25 UTC-5 [Agent] [ { “values”: [ { “metricValue”: 26.456, “detectionTime”: “2015-11-05T13=15=30Z” }, { “metricValue”: 26.456, “detectionTime”: “2015-11-05T13=15=30Z” } ], “metricCode”: “testMetric1” }, { “values”: [ { “metricValue”: “test”, “detectionTime”: “2015-11-05T13=15=30Z” } ], “metricCode”: “testMetric2” } ]
2015-12-24 13:58:29 UTC-5 [Agent] 7
and it doesn’t add any records to the database.
I know 7 means CURLE_COULDNT_CONNECT (7) : Failed to connect() to host or proxy.
But I’m not sure why. Using the same URL, headers and JSON on postman it works fine.
I would appreciate any ideas that might help.
Thanks.