Last active
February 20, 2016 22:16
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a typo on line 13 of httpjsonencode.agent.nut:
headerss
should readheaders