You need to send the value of i to the agent, which will store the current value in a variable. So instead of server.log(i) use agent.send(“current.temp”, i)
You now get your agent to save this temperature:
function save_temp(temperature_from_imp) { current_temperature = temperature_from_imp }
and
current_temperature <- 0 device.on("current.temp", save_temp)
The device.on() tine tells the agent to watch for the incoming temperature message “current.temp” and to call the function save_temp() when it receives this message.
Now set up the agent with an http handler function (see this guide: electricimp.com/docs/gettingstarted/3-agents/
The http handler detects the request from the browser (“getvalue”) and sends back the current temperature value:
`function request_handler(request, response)
{
try
{
// Check for app test
if ("getvalue" in request.query)
{
response.send(200, current_temperature.tostring)
return
}
}
catch(ex)
{
response.send(500, ("Agent error: " + ex))
}
}`
and
http.onrequest(request_handler)
Next, you need to get your web page to send a request to your agent URL (displayed at the top of the agent code in the IDE, eg. https://agent.electricimp.com/12345678abcde) for the current value:
https://agent.electricimp.com/12345678abcde?getvalue
You will need some suitable web code to get the response sent by the agent. You can put the line https://agent.electricimp.com/12345678abcde?getvalue in the address field of your browser, and the response from agent will appear on the page. But if you need something more interactive, you’ll need to write or obtain JavaScript code to send the temperature request to the agent (as above) and display the response.