Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. // Electric Imp Quick Start Agent Code for GroveStreams
  2. // A full "how to" guide for this agent can be found at https://www.grovestreams.com/developers/getting_started_elec_imp_temp.html
  3. //
  4.  
  5. // License:
  6. //  Copyright 2014 GroveStreams LLC.
  7. //  Licensed under the Apache License, Version 2.0 (the "License");
  8. //  you may not use this file except in compliance with the License.
  9. //  You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. //  Unless required by applicable law or agreed to in writing, software
  12. //  distributed under the License is distributed on an "AS IS" BASIS,
  13. //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. //  See the License for the specific language governing permissions and
  15. //  limitations under the License.
  16. // GroveStreams Settings
  17. API_KEY <- "YOUR_SECRET_API_KEY_HERE";  //Change This!!! Your GS Organization Secret API Key
  18. COMP_NAME <- "Electric+Imp+Quick"      //Optionally change. Your GS Component Name (URL encoded. The "+" indicates spaces)
  19. RSSI_STREAM_ID <- "rssi";               //Optionally change. Your GS rssi stream ID
  20. VOLTAGE_STREAM_ID <- "voltage"          //Optionally change. Your GS voltage stream ID
  21. LIGHT_STREAM_ID <- "light"              //Optionally change. Your GS light stream ID
  22.  
  23. GroveStreams <- {};    // this makes a 'namespace'
  24. class GroveStreams.Client {
  25.  
  26.     constructor() {
  27.     }
  28.    
  29.  
  30.     function Put(data){
  31.  
  32.         local url = "http"+"://grovestreams.com/api/feed?compId=" + data.mac;  
  33.         url += "&compName=" + COMP_NAME + "+(" + data.mac + ")";
  34.         url += "&api_key=" + API_KEY;
  35.         url += "&" + RSSI_STREAM_ID + "=" + data.rssi;
  36.         url += "&" + LIGHT_STREAM_ID + "=" + data.light;
  37.         url += "&" + VOLTAGE_STREAM_ID + "=" + data.voltage;
  38.         //url += "&compTmplId=template1";  //Uncomment to auto register a new component based on the template with ID=template1
  39.         local headers = { "Connection":"close", "X-Forwarded-For" : data.mac };
  40.         local request = http.put(url, headers, "");
  41.  
  42.         local response = request.sendsync();
  43.         if(response.statuscode != 200) {
  44.             server.log("error sending message: " + response.body);
  45.             return null;
  46.         }
  47.        
  48.     }
  49. }
  50.  
  51. client <- GroveStreams.Client();
  52.  
  53. //********************END GroveStreams********************
  54.  
  55. device.on("GroveStreams", function(data) {
  56.     server.log("Sending data to GS");
  57.     client.Put(data);
  58. });