Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  
  3.  Arduino GroveStreams Stream Feed via Ethernet
  4.  
  5.  The GroveStreams client sketch is designed for the Arduino YUN.
  6.  A full "how to" guide for this sketh can be found at https://www.grovestreams.com/developers/getting_started_arduino_yun.html
  7.  This sketch updates several stream feeds with an analog input reading,
  8.  from a temperature probe, via the GroveStreams API: https://www.grovestreams.com/developers/api.html
  9.  Use the Serial Monitor on the Arduino IDE to see verbose network feedback
  10.  and the GroveStreams connectivity status.
  11.  
  12.  License:
  13.   Copyright 2015 GroveStreams LLC.
  14.   Licensed under the Apache License, Version 2.0 (the "License");
  15.   you may not use this file except in compliance with the License.
  16.   You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
  17.  
  18.   Unless required by applicable law or agreed to in writing, software
  19.   distributed under the License is distributed on an "AS IS" BASIS,
  20.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21.   See the License for the specific language governing permissions and
  22.   limitations under the License.
  23.  
  24.  GroveStreams Setup:
  25.  
  26.  * Sign Up for Free User Account - https://www.grovestreams.com
  27.  * Create a GroveStreams organization while selecting the Arduino blueprint
  28.  * Enter a unique MAC Address for this network in this sketch under "Local Network Settings"
  29.  *    Navigate to http://arduino.local/ to find your YUN's MAC address
  30.  * Enter your GroveStreams secret api key under "GroveStreams Settings" in this sketch  
  31.  *    (The api key can be retrieved from within a GroveStreams organization: click the Api Keys toolbar button,
  32.  *     select your Api Key, and click View Secret Key. Paste the Secret Key below)
  33.  
  34.  Arduino Requirements:
  35.  
  36.  * Arduino YUN
  37.  * Arduino 1.5.8 IDE
  38.  
  39. */
  40.  
  41. #include <Bridge.h>
  42. #include <Process.h>
  43.  
  44.  
  45. // Local Network Settings
  46. char mac[] = "90:A2:DA:F5:01:C7"; // Change this!!! Must be unique on local network.
  47.                                   // Browse to your YUN to find it: http://arduino.local/
  48.  
  49. // GroveStreams Settings
  50. char gsApiKey[] = "YOUR_SECRET_API_KEY_HERE";   //Change This!!!
  51. char gsComponentName[] = "Temperature";        //Optionally change. Set this to give your component a name when it initially registers.
  52.  
  53. char gsComponentTemplateId[] = "temp";  //Don't change. Tells GS what template to use when the feed initially arrives and a new component needs to be created.
  54.                                         // The blueprint is expecting "temp".
  55.  
  56. //GroveStreams Stream IDs. Stream IDs tell GroveStreams which component streams the values will be assigned to.
  57. //Don't change these unless you edit your GroveStreams component definition and change the stream IDs to match these.
  58. char gsStreamId1[] = "s1";   //Don't change. Temp C.
  59. char gsStreamId2[] = "s2";   //Don't change. Temp F.
  60.  
  61. // Other Settings
  62. const unsigned long updateFrequency = 20000UL;    // Update frequency in milliseconds (20000 = 20 seconds). Change this to change your sample frequency.
  63.  
  64. const int temperaturePin = 0;          // You might need to change depending on the Pin you are using. The Temperature pin number.    
  65. char samples[40];                      // Change this buffer size only if you increase or decrease the size of samples being uploaded.
  66.  
  67. unsigned long lastUploadTime = 0; //Don't change. Used to determine if samples need to be uploaded.
  68.  
  69.  
  70. void setup()
  71. {
  72.  
  73.   Bridge.begin();
  74.    
  75.   Serial.begin(9600);
  76.   while(!Serial);
  77.  
  78.   //Wait for the connection to finish stopping
  79.   delay(2500);
  80.  
  81.   //Set the mac and ip variables so that they can be used during sensor uploads later
  82.   Serial.print("MAC: ");
  83.   Serial.println(mac);
  84.   Serial.println();
  85.  
  86. }
  87.  
  88. void loop()
  89. {
  90.  
  91.   // Update sensor data to GroveStreams
  92.   if(millis() - lastUploadTime > updateFrequency)
  93.   {
  94.     updateGroveStreams();
  95.   }
  96.  
  97. }
  98.  
  99. void updateGroveStreams()
  100. {
  101.   Serial.println("updateGroveStreams...");
  102.  
  103.   //Assemble the url that is used to pass the temperature readings to GroveStreams and call it
  104.   lastUploadTime = millis();
  105.  
  106.   //You may need to increase the size of urlBuf if any other char array sizes have increased
  107.   char urlBuf[180];
  108.   sprintf(urlBuf, "http://grovestreams.com/api/feed?compTmplId=%s&compId=%s&compName=%s&api_key=%s%s",
  109.                   gsComponentTemplateId, mac, gsComponentName, gsApiKey, getSamples());
  110.                  
  111.   char xHeadBuf[40];
  112.   sprintf(xHeadBuf, "X-Forwarded-For:%s\0", mac);
  113.                  
  114.   //Uncomment the following lines to debug your strings and to ensure their buffers are large enough
  115.   //Serial.println(strlen(xHeadBuf));
  116.   //Serial.println(xHeadBuf);
  117.   //Serial.println(urlBuf);
  118.   //Serial.println(strlen(urlBuf));
  119.   //Serial.println(getSamples());
  120.   //Serial.println(strlen(getSamples()));
  121.  
  122.   Process process;
  123.   process.begin("curl");
  124.   process.addParameter("-d");
  125.   process.addParameter("");
  126.   process.addParameter("-k");
  127.   process.addParameter("-X");
  128.   process.addParameter("PUT");
  129.  
  130.   //Headers
  131.   process.addParameter("-H");
  132.   process.addParameter(xHeadBuf);
  133.   process.addParameter("-H");
  134.   process.addParameter("Connection:close");                      
  135.   process.addParameter("-H");
  136.   process.addParameter("Content-Type:application/json");  
  137.  
  138.   //URL
  139.   process.addParameter(urlBuf);
  140.  
  141.   //Make the request
  142.   unsigned int result = process.run();
  143.  
  144.   //Display whatever is returned (usually an error message if one occurred)
  145.   while (process.available()>0) {
  146.     char c = process.read();
  147.     Serial.print(c);
  148.   }
  149.   Serial.flush();
  150.  
  151.   Serial.print("Feed PUT Result: ");
  152.   Serial.println(result);
  153. }
  154.  
  155. char* getSamples()
  156. {
  157.   //Get the temperature analog reading and convert it to a string
  158.   float voltage, degreesC, degreesF;
  159.  
  160.   voltage = (analogRead(temperaturePin) * 0.004882814);
  161.   degreesC = (voltage - 0.5) * 100.0;
  162.   degreesF = degreesC * (9.0/5.0) + 32.0;
  163.  
  164.   char tempC[15] = {0}; //Initialize buffer to nulls
  165.   dtostrf(degreesC, 12, 3, tempC); //Convert float to string
  166.  
  167.   char tempF[15] = {0}; //Initialize buffer to nulls
  168.   dtostrf(degreesF, 12, 3, tempF); //Convert float to string
  169.  
  170.   // Example: &s1=25.684&s2=78.231
  171.   sprintf(samples, "&%s=%s&%s=%s", gsStreamId1, trim(tempC), gsStreamId2, trim(tempF));
  172.  
  173.   return samples;
  174. }
  175.  
  176. char* trim(char* input)                                        
  177. {
  178.   //Trim leading and ending spaces
  179.   int i,j;
  180.   char *output=input;
  181.   for (i = 0, j = 0; i<strlen(input); i++,j++)          
  182.   {
  183.     if (input[i]!=' ')                          
  184.       output[j]=input[i];                    
  185.     else
  186.       j--;                                    
  187.   }
  188.   output[j]=0;
  189.  
  190.   return output;
  191. }