Newbie Using http Agent code

#1

I have some agent code that I can’t get to http post to Adafruit IO. Seems simple enough but I must be missing something in the sendasync section. I think my issue is not understanding the “processResponse” done callback function.

I have spent time reviewing the sendasync documentation and thought is was on the right track.

Thanks for your assistance,

Neal

// Adafruit IO
function reporttemp(celcius)
{
    server.log(format("Temperature: %3.2f degrees C", celcius));
    local header = {
       "Content-Type" : "“application/x-www-form-urlencoded”}",
       "X-AIO-Key" : "key goes here"
        
    };
     local body = "value = 45";
    local request = http.post( "https://io.adafruit.com/api/v2/test/feeds/foo", header,body);
    
    # This is where I am getting an error
    local response = request.sendasync(processResponse);


    function processResponse(response){
    server.log("Response: " + response.body);
    }
}   



device.on("reporttemp",reporttemp);

Here is the log:

|2019-04-21T13:34:39.364 +00:00|[Status]|Agent restarted: reload.|
|---|---|---|
|2019-04-21T13:34:39.890 +00:00|[Status]|Downloading new code; 2.01% program storage used|
|2019-04-21T13:34:41.002 +00:00|[Agent]|Temperature: 24.00 degrees C|
|2019-04-21T13:34:41.002 +00:00|[Agent]|ERROR: the index 'processResponse' does not exist|
|2019-04-21T13:34:41.002 +00:00|[Agent]|ERROR:   in reporttemp agent_code:14|
#2

Looking at the documentation here:

I would say that you need to move the processResponse function outside of the reporttemp function …
Like this:

// Adafruit IO

function processResponse(response){
server.log("Response: " + response.body);
}
	
function reporttemp(celcius){
    server.log(format("Temperature: %3.2f degrees C", celcius));
    local header = {
       "Content-Type" : "“application/x-www-form-urlencoded”}",
       "X-AIO-Key" : "key goes here"
        
    };
     local body = "value = 45";
    local request = http.post( "https://io.adafruit.com/api/v2/test/feeds/foo", header,body);
	
    // Send the message
    request.sendasync(processResponse);
}   

device.on("reporttemp",reporttemp);
#3

A function can be local, but you need to define it before you reference it - here it’s being referenced before it has been defined.