hi
im a total beginner with the coding stuff and im trying to create a simple code with agent to control a servo from certain position to another (min to max) using http req
i tried modifying the agent code for LED (from the examples page)
but im clearly doing it wrong cause i dont know what to add or take out of the code
below is the code i used (seems ok until i get Internal Server Error: the index ‘ServoState’ does not exist (line 15))
agent:
server.log("Turn servo On: " + http.agenturl() + “?servo=1”);
server.log("Turn servo Off: " + http.agenturl() + “?servo=0”);
function requestHandler(request, response) {
try {
if (“servo” in request.query) {
if (request.query.servo == “1” || request.query.servo == “0”) {
local servoState = request.query.servo.tointeger();
device.send(“servo”, ServoState);
}
}
response.send(200, “OK”);
} catch (ex) {
response.send(500, "Internal Server Error: " + ex);
}
}
http.onrequest(requestHandler);
device
const SERVO_MIN = 0.03;
const SERVO_MAX = 0.1;
servo <- hardware.pin7;
servo.configure(PWM_OUT, 0.02, SERVO_MIN);
function SetServo(ServoState) {
local scaledValue = ServoState * (SERVO_MAX-SERVO_MIN) + SERVO_MIN;
servo.write(scaledValue);
}
function SetServoDegrees(ServoState) {
local scaledValue = (ServoState + 81) / 161.0 * (SERVO_MAX-SERVO_MIN) + SERVO_MIN;
servo.write(scaledValue);
}
position <- 0;
function setServo(ServoState) {
server.log("Set servo: " + ServoState);
led.write(ServoState);
}
agent.on(“servo”, setServo);
this is the servo im using
I need some directions here…
thanks!!