Skip to content

Instantly share code, notes, and snippets.

@electricimp
Last active February 20, 2016 22:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save electricimp/5905365 to your computer and use it in GitHub Desktop.
Save electricimp/5905365 to your computer and use it in GitHub Desktop.
Basic example to demonstrate how to encode json data and POST it to an arbitrary web service.
device.on("senddata", function(data) {
// Set URL to your web service
local url = "https://www.mywebservice/object";
// Set Content-Type header to json
local headers = { "Content-Type": "application/json" };
// encode data and log
local body = http.jsonencode(data);
server.log(body);
// send data to your web service
http.post(url, headers, body).sendsync();
});
// placeholder to ignore digital input change
function nullfunction() {}
// configure imp
hardware.pin1.configure(DIGITAL_IN, nullfunction);
hardware.pin2.configure(ANALOG_IN);
function loop() {
// read values
local pin1Value = hardware.pin1.read();
local pin2Value = hardware.pin2.read();
// create table and send to agent
local data = { "pin1": pin1Value, "pin2": pin2Value }
agent.send("senddata", data);
// recursively call self after 15 seconds
imp.wakeup(15.0, loop);
}
// start the loop
loop();
@pcon
Copy link

pcon commented Jan 2, 2014

There is a typo on line 13 of httpjsonencode.agent.nut: headerss should read headers

@pcon
Copy link

pcon commented Jan 2, 2014

Also, line 6 should end in }; instead of );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment