Hi so basically I two when separate functions which when on their own work as intended but I need them to work together but unsure as to how to combine them. Tried several ways but none worked.
First function is to return pin values to a web page, second is to listen for web page and activate pins depending on user parameters.
Code as follows:
// Log the URLs we need
server.log("Internal Lights OFF : " + http.agenturl() + “?led=0”);
server.log("Internal Lights ON : " + http.agenturl() + “?led=1”);
server.log("Internal LUX : " + http.agenturl() + “?led=2”);
server.log("External LUX : " + http.agenturl() + “?led=3”);
server.log("Heater : " + http.agenturl() + “?led=4”);
server.log("Internal Temp : " + http.agenturl() + “?led=5”);
server.log("External Temp : " + http.agenturl() + “?led=6”);
_pin2 <- “”;
_pin5 <- “”;
_pin8 <- “”;
_pin9 <- “”;
function respondImpValues(request,response){
local pinTable = {
"pin2": ""+_pin2+"",
"pin5": ""+_pin5+"", // e.g.: "pin5" : "48491"
"pin8": ""+_pin8+"",
"pin9": ""+_pin9+"",
}
local jvars = http.jsonencode(pinTable);
response.header("Access-Control-Allow-Origin", "*");
response.send(200,jvars);
}
device.on(“impValues”, function(iv) {
_pin2 = iv.pin2;
_pin5 = iv.pin5;
_pin8 = iv.pin8;
_pin9 = iv.pin9;
});
http.onrequest(respondImpValues);
function requestHandler(request, response)
{
response.header(“Access-Control-Allow-Origin”, “*”);
try
{
// check if the user sent led as a query parameter
if (“led” in request.query)
{
// if they did
if (request.query.led == “0” || request.query.led == “1” ||
request.query.led == “2” || request.query.led == “3” ||
request.query.led == “4” || request.query.led == “5” ||
request.query.led == “6”)
{
// convert the led query parameter to an integer
local ledState = request.query.led.tointeger();
// send "led" message to device, and send ledState as the data
device.send("led", ledState);
}
}
// send a response back saying everything was OK.
response.send(200, "OK");
}
catch (ex)
{
response.send(500, "Internal Server Error: " + ex);
}
}
http.onrequest(requestHandler);
How can I merge the two functions, any advice? The main barrier is the because of the response.send I believe.