SHARE
TWEET

GroveStreams Electric Imp Agent

mikecmills2 Jun 17th, 2014 (edited) 1,937 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Electric Imp Agent for GroveStreams
  2. //
  3. // This Agent demonstrates how to upload Electric Imp data into a GroveStreams organization stream.
  4. // A full "how to" guide for this agent can be found at https://www.grovestreams.com/developers/getting_started_elec_imp_temp.html
  5. //
  6.  
  7. // License:
  8. //  Copyright 2014 GroveStreams LLC.
  9. //  Licensed under the Apache License, Version 2.0 (the "License");
  10. //  you may not use this file except in compliance with the License.
  11. //  You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. //  Unless required by applicable law or agreed to in writing, software
  14. //  distributed under the License is distributed on an "AS IS" BASIS,
  15. //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. //  See the License for the specific language governing permissions and
  17. //  limitations under the License.
  18.  
  19. // GroveStreams Setup:
  20. // Sign Up for Free User Account - https://www.grovestreams.com
  21. // Create a GroveStreams organization
  22. // Enter the GroveStreams api key under "GroveStreams Settings" in this agent  
  23. //    (The api key can be retrieved from a GroveStreams organization: click the Api Keys toolbar button,
  24. //     select your Api Key, and click View Secret Key. Paste the Secret Key below)
  25.  
  26. // GroveStreams Setting
  27. API_KEY <- "YOUR_SECRET_API_KEY_HERE";      //Change This!!! Your GS Organization Secret API Key
  28. COMP_NAME <- "Electric+Imp"         //Optionally change. Your GS Component Name (URL encoded. The "+" indicates spaces)
  29. TEMP_STREAM_ID <- "temperature";    //Optionally change. Your GS temperature stream ID
  30. VOLTAGE_STREAM_ID <- "voltage"      //Optionally change. Your GS voltage stream ID
  31.  
  32. GroveStreams <- {};    // this makes a 'namespace'
  33. class GroveStreams.Client {
  34.  
  35.         constructor() {
  36.         }
  37.        
  38.         /*****************************************
  39.          * method: PUT
  40.          *   This method will upload a sample into a GroveStreams organization
  41.          *   component and stream. It will automatically register a new component
  42.          *   with random sample time streams. Create a component template and reference its
  43.          *   ID within the URL below to have a component register with different
  44.          *   attributes and streams.
  45.          *   This method is utilizing the GS no JSON body feed API which can be found here:
  46.          *     https://www.grovestreams.com/developers/api.html#1
  47.          *   If you have more than one Electric Imp device feeding into GroveStreams, then you need to set
  48.          *    X-Forwarded-For to a unique value to avoid the GroveStreams 10 second
  49.          *    stream feed PUT limit from a single source (the Imp servers). It is being
  50.          *    set to the Imp device's mac address below.
  51.          * IN:
  52.          *   data : data from the device
  53.          *****************************************/
  54.         function Put(data){
  55.                 local url = "http://grovestreams.com/api/feed?compId=" + data.mac;
  56.                 url += "&compName=" + COMP_NAME + "+(" + data.mac + ")";
  57.                 url += "&api_key=" + API_KEY;
  58.                 url += "&" + TEMP_STREAM_ID + "=" + data.temp;
  59.                 url += "&" + VOLTAGE_STREAM_ID + "=" + data.voltage;
  60.                 //url += "&compTmplId=template1";  //Uncomment to auto register a new component based on the template with ID=template1
  61.                 local headers = { "Connection":"close", "X-Forwarded-For" : data.mac };
  62.                 local request = http.put(url, headers, "");
  63.  
  64.                 local response = request.sendsync();
  65.                 if(response.statuscode != 200) {
  66.                         server.log("error sending message: " + response.body);
  67.                         return null;
  68.                 }
  69.         }
  70. }
  71.  
  72. client <- GroveStreams.Client();
  73.  
  74. //********************END GroveStreams********************
  75.  
  76. device.on("GroveStreams", function(data) {
  77.     server.log("Sending temp: " + data.temp);
  78.     client.Put(data);
  79. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top