| Electronic Components Datasheet Search |
|
DEV-12887 Datasheet(PDF) 35 Page - SparkFun Electronics |
|
|
|||||||||||||||||||||||||||||
DEV-12887 Datasheet(HTML) 35 Page - SparkFun Electronics |
|
35 / 40 page ![]() s/api/http/jsonencode/ local jvars = http.jsonencode(pinTable); // Attach a header to our response. // "AccessControlAllowOrigin: *" allows crossorigin re source sharing // https://electricimp.com/docs/api/httpresponse/header/ response.header("AccessControlAllowOrigin", "*"); // Send out our response. // 200 is the "OK" http status code // jvars is our response string. The JSON table we constru cted earlier. // https://electricimp.com/docs/api/httpresponse/send/ response.send(200,jvars); } // device.on("impValues") will be called whenever an "impValue s" request is sent // from the device side. This simple function simply fills up our global variables // with the equivalent vars received from the imp. device.on("impValues", function(iv) { _pin1 = iv.pin1; _pin2 = iv.pin2; _pin5 = iv.pin5; _pin7 = iv.pin7; _pin8 = iv.pin8; _pin9 = iv.pin9; _voltage = iv.voltage; }); /////////// // Setup // /////////// // http.onrequest(function) sets up a function handler to cal l when an http // request is received. Whenever we receive an http request ca ll respondImpValues // https://electricimp.com/docs/api/http/onrequest/ http.onrequest(respondImpValues); There are two things to point out here. The first is the device.on function. This function sets up a handler to be called when the imp sends a defined string. In this case, we’re looking for the imp sending “impValues”. The data associated with this string is a table, full of all of the imp’s pin readings. That function is called every time the imp sends that specific string to the agent. The real magic happens in the respondImpValues() function. The agent sets this function as the handler for any HTTP request (using the http.onrequest function). This function constructs a JSON string of data, in the form of “"key”:“value”, “key”:“value”, …,“, which the requesting HTTP client should be able to parse and understand. Check the comments in the code for more information on the agent code. For now, Build and Run the code. Don’t expect it to do anything of interest until we add the last bit of magic… The Web App – AJAX, jQuery, JSON Oh My! The final piece to this puzzle is constructing a web page that can both send an HTTP request and deal with the response from the agent. Page 35 of 40 |
|
Link URL |
| Does ALLDATASHEET help your business so far? [ DONATE ] |
About Alldatasheet | Advertisement | Contact us | Privacy Policy | Link to Datasheet | Link Exchange | Manufacturer List All Rights Reserved©Alldatasheet.com |
| Russian : Alldatasheetru.com | Korean : Alldatasheet.co.kr | Spanish : Alldatasheet.es | French : Alldatasheet.fr | Italian : Alldatasheetit.com Portuguese : Alldatasheetpt.com | Polish : Alldatasheet.pl | Vietnamese : Alldatasheet.vn Indian : Alldatasheet.in | Mexican : Alldatasheet.com.mx | British : Alldatasheet.co.uk | New Zealand : Alldatasheet.co.nz |
|
Family Site : ic2ic.com |
icmetro.com |