Hi, I am completely new here and would like to ask a question regarding a project.
I have 2 imps plus shields and on one I have an IR sensor, and the other will (hopefully) trigger a motor.
Got a friend to help me code, but still cannot get the right message to post on the receiving imp.
So far it just posts httppost, where I would ideally like it to post sensor values.
Many thanks in advance!
Here are the agent codes:
Sending imp(sensor):
`_last_sensor_value <- “”;
// Log the URLs we need
server.log("To poll sensor value: " + http.agenturl() + “?sensor”);
function sendSensorValue() {
local url = “https://agent.electricimp.com/xxxxxxxxxxxx”;
local header = {“Content-Type”: “application/x-www-form-urlencoded”};
local string = http.urlencode( {value = _last_sensor_value});
local request = http.post(url, header, string);
local response = request.sendsync();
server.log(response.statuscode);
return response;
}
function requestHandler(request, response) {
try {
local value = “ping”
// check if the user sent sensor as a query parameter
if (“sensor” in request.query) {
value = _last_sensor_value;
sendSensorValue();
}
// send a response back saying everything was OK.
response.send(200, value);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);
device.on(“sensorvalue”, function(value) {
server.log("received sensor value: " + value);
_last_sensor_value = value;
});`
receiving imp:
`// Log the URLs we need
server.log("Turn LED On: " + http.agenturl() + “?value=3000”);
server.log("Turn LED Off: " + http.agenturl() + “?value=0”);
function requestHandler(request, response) {
try {
server.log(“httppost”);
local sensorvalue = -1;
// check if the user sent led as a query parameter
if ("value" in request.query) {
local ledState = 0;
sensorvalue = request.query.value.tointeger();
server.log("received sensor value: " + sensorvalue);
if(sensorvalue >= 20000) {
ledState = 1;
} else {
ledState = 0;
}
// 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, sensorvalue);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
// register the HTTP handler
http.onrequest(requestHandler);`