Skip to content

Instantly share code, notes, and snippets.

@azdle
Created April 3, 2014 00:43
Show Gist options
  • Save azdle/9946247 to your computer and use it in GitHub Desktop.
Save azdle/9946247 to your computer and use it in GitHub Desktop.
Monitoring Power with Exosite using an Arduino Yun
//*****************************************************************************
//
// wifi_read_write_string - A Simple read/write to the Exosite Cloud API
// for the Arduino WiFi shield using the
// String library
//
// Copyright (c) 2013 Patrick Barrett - All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Exosite LLC nor the names of its contributors may
// be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
//*****************************************************************************
#include <SPI.h>
#include <YunClient.h>
#include <Exosite.h>
#include <Console.h>
/*==============================================================================
* Configuration Variables
*
* Change these variables to your own settings.
*=============================================================================*/
String cikData = "0000000000000000000000000000000000000000"; // <-- Fill in your CIK here! (https://portals.exosite.com -> Add Device)
// Use these variables to customize what datasources are read and written to.
String readString = "";
String writeStringUptime = "uptime=";
String writeStringAdc1 = "&adc1=";
String writeStringAdc2 = "&adc2=";
String returnString;
#define SAMPLES 100
long unsigned last_send = 0;
float adc1_val = 0;
float adc2_val = 0;
int adc1_vals[SAMPLES];
int adc2_vals[SAMPLES];
int idx = 0;
int count = 0;
/*==============================================================================
* End of Configuration Variables
*=============================================================================*/
class YunClient client;
Exosite exosite(cikData, &client);
/*==============================================================================
* setup
*
* Arduino setup function.
*=============================================================================*/
void setup(){
pinMode(13, OUTPUT);
Bridge.begin();
Console.begin();
// Not Debugging Anymore, Just Boot!
/*while (!Console){ // wait for Console port to connect.
digitalWrite(13, HIGH);
delay(250);
digitalWrite(13, LOW);
delay(250);
}*/
Console.println("Boot");
last_send = millis();
}
/*==============================================================================
* loop
*
* Arduino loop function.
*=============================================================================*/
void loop(){
last_send += 5000;
adc1_val = 0;
adc2_val = 0;
idx = 0;
count = 0;
while(last_send > millis()){
adc1_vals[idx] = analogRead(0);
adc2_vals[idx] = analogRead(1);
count++;
idx++;
if(idx >= SAMPLES)
idx = 0;
delay(10);
}
Console.print("Measurments: ");
Console.println(count);
for(int i = 0; i < SAMPLES && i < count; i++){
adc1_val += adc1_vals[i];
adc2_val += adc2_vals[i];
}
Console.println("1");
adc1_val = adc1_val / min(SAMPLES, count);
adc2_val = adc2_val / min(SAMPLES, count);
Console.println("2");
// Activity LED On
digitalWrite(13, HIGH);
Console.println("3");
//Write to "uptime" and read from "uptime" and "command" datasources.
Console.println(writeStringUptime+String(millis())+writeStringAdc1+String(adc1_val)+writeStringAdc2+String(adc2_val));
Console.print("Connecting to Exosite...");
if ( exosite.writeRead(writeStringUptime+String(millis())+writeStringAdc1+String(adc1_val)+writeStringAdc2+String(adc2_val), readString, returnString)){
Console.println("OK");
}else{
Console.println("Error");
}
// Activity LED Off
digitalWrite(13, LOW);
//delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment