Forgive me for my ignorance. I’m still learning here.
When I’m making http requests to my imp with the following code everything works fine. When I add another set of if statement commands for it to check through as the data is being processed. It bombs… Let me show you.
This is the agent code and it doesn’t change.
`http.onrequest(function(req, resp) {
try {
local path = req.path.tolower();
// if user sends response to /asyncdata/...
if (path.find("/asyncdata") == 0) {
// generate key, and store the response object
local responseKey = generateKey();
responses[responseKey] <- { resp = resp, t = time() };
// this is what we're asking the agent for
local requestedData = "unknown";
// Asking for Relay 1 state
if (path == "/asyncdata/r1" || path == "/asyncdata/r1/") requestedData = "r1";
// Asking for Relay 2 state
if (path == "/asyncdata/r2" || path == "/asyncdata/r2/") requestedData = "r2";
// Asking for Temp State in C
if (path == "/asyncdata/celsius" || path == "/asyncdata/celsius/") requestedData = "celsius";
//check button state
if (path == "/asyncdata/touch" || path == "/asyncdata/touch/") requestedData = "touch";
// request asyncdata from the device:
// k is the response key - we'll use this in the response
// r is what we're looking for from the device
// d is an object that will store the data
device.send("asyncdata", { k = responseKey, r = requestedData, d = null });
return;
}`
When I run this device code everything works fine. I can get data from r1, r2, celsius.
`//Route the request coming in to the proper function.
agent.on(“asyncdata”, function(data) {
if (data.r == "r1") {
data.d = { r1=getR1() };
}
if (data.r == "r2") {
data.d = { r2=getR2() };
}
if (data.r == "celsius")
{
data.d={ tempC=GetTemp_C() };
}
//if (data.r == "touch")
//{data.d={ touch=ReadTouchSensor() };
//}
else data.d = { error = "Unknown async request to device" };
agent.send("asyncdata", data);
});`
When I change the above code and uncomment the read touch sensor section. It automatically goes to else and throw the error. Below is the adjusted code.
`//Route the request coming in to the proper function.
agent.on(“asyncdata”, function(data) {
if (data.r == "r1") {
data.d = { r1=getR1() };
}
if (data.r == "r2") {
data.d = { r2=getR2() };
}
if (data.r == "celsius")
{
data.d={ tempC=GetTemp_C() };
}
if (data.r == "touch")
{data.d={ touch=ReadTouchSensor() };
}
else data.d = { error = "Unknown async request to device" };
agent.send("asyncdata", data);
});`
When it’s working correctly I get the following output:
tempC=22.2814
When it’s uncommented, this is what happens:
error=Unknown%20async%20request%20to%20device