I just bought the arduino plugin and started getting into arduino, I have a couple of nodemcu boards setup with temp.
I got this sketch off the net, by itself it works fine. however, I need some help with this api sketch for an accelerometer I am unsure what/how exactly to send the data to homeseer.
/************************************************************ *
Arduino to Homeseer 3 Plugin API written by Enigma Theatre.
V1.0.0.146
* *
************************************************************ */
int FromHS[50];
boolean IsConnected = false;
//************************************************************
//**************Declare your variables here*******************
const boolean debugging = true; // whether to print debugging to serial output
const boolean showBuffer = false; // whether to dump details of ring buffer at each read
const int xPin = 3; // analog: X axis output from accelerometer
const int yPin = 1; // analog: Y axis output from accelerometer
const int zPin = 2; // analog: Z axis output from accelerometer
const int led = 13; // just to blink a heartbeat while running
const int totalAxes = 3; // for XYZ arrays: 0=x, 1=y, 2=z
const int baseSamples = 1000; // number of samples to average for establishing zero g base
const int bufferSize = 16; // number of samples for buffer of data for running average
const int loopBlink = 100; // number of trips through main loop to blink led
// array of pin numbers for each axis, so the constants above can be chnaged with impunity
const int pin [totalAxes] = {
xPin, yPin, zPin};
// base value for each axis - zero g offset (at rest when sketch starts)
int base [totalAxes];
// ring buffer for running average of data, one for each axis, each with <bufferSize> samples
int buffer [totalAxes] [bufferSize];
// index into ring buffer of next slot to use, for each axis
int next [totalAxes] = {
0,0,0};
// current values from each axis of accelerometer
int curVal [totalAxes];
// count of trips through main loop, modulo blink rate
int loops = 0;
//************************************************************ ****
void HSSetup() {
//************************
//Add YOUR SETUP HERE;
//************************
long sum [totalAxes]= { // accumulator for calculating base value of each axis
0,0,0 };
Serial.begin (9600);
Serial.println ("***");
// initialize all pins
pinMode (led, OUTPUT);
for (int axis=0; axis<totalAxes; axis++)
pinMode (pin [axis], INPUT); // not necessary for analog, really
// read all axes a bunch of times and average the data to establish zero g offset
// chip should be at rest during this time
for (int i=0; i<baseSamples; i++)
for (int axis=0; axis<totalAxes; axis++)
sum [axis] += analogRead (pin [axis]);
for (int axis=0; axis<totalAxes; axis++)
base [axis] = round (sum [axis] / baseSamples);
// and display them
Serial.print ("*** base: ");
for (int axis=0; axis<totalAxes; axis++) {
Serial.print (base [axis]);
Serial.print ("\t");
}
Serial.println ();
Serial.println ("***");
// initialize the ring buffer with these values so the averaging starts off right
for (int axis=0; axis<totalAxes; axis++)
for (int i=0; i<bufferSize; i++)
buffer [axis] [i] = base [axis];
// light up the led and wait til the user is ready to start (sends anything on serial)
// so that the base values don't immediately shoot off the top of the serial window
digitalWrite (led, HIGH);
while (!Serial.available())
/* wait for <RETURN> */ ;
digitalWrite (led, LOW);
}
void HSloop() {
//increment the loop counter and blink the led periodically
loops = (loops + 1) % loopBlink;
digitalWrite (led, loops == 0);
// get new data from each axis by calling a routine that returns
// the running average, instead of calling analogRead directly
for (int axis=0; axis<totalAxes; axis++) {
curVal [axis] = getVal (axis, showBuffer);
if (debugging) {
Serial.print (curVal [axis]);
Serial.print ("\t");
SendToHS(1,axis);
SendToHS(2,axis);
SendToHS(3,axis);
}
}
if (debugging)
Serial.println ();
// here we will do all of the real work with curVals
}
int getVal (int axis, boolean show) {
// returns the current value on <axis>, averaged across the previous <bufferSize> reads
// print details if <show> is true
long sum; // to hold the total for aaveraging all values in the buffer
// read the data into the next slot in the buffer and stall for a short time
// to make sure the ADC can cleanly finish multiplexing to another pin
buffer [axis] [next [axis]] = analogRead (pin [axis]);
delay (10); // probably not necessary given the stuff below
// display the buffer if requested
if (show) {
for (int i=0; i<bufferSize; i++) {
if (i == next [axis]) Serial.print ("*");
Serial.print (buffer [axis] [i]);
Serial.print (" ");
}
Serial.println ();
}
// bump up the index of the next available slot, wrapping around
next [axis] = (next [axis] + 1) % bufferSize;
// add up all the values and return the average,
// taking into account the offset for zero g base
sum = 0;
for (int i=0; i<bufferSize; i++)
sum += buffer [axis] [i];
return (round (sum / bufferSize) - base [axis]);
//************************
//Add YOUR CODE HERE;
//************************
/* To Send Data to Homeseer use SendToHS(Device,Value)
Eg.. SendToHS(1,200); where 1 is the API device in homeseer and 200 is the value to send
To Recieve data from Homeseer look up the FromHS array that is updated when the device value changes.
Eg.. FromHS[5] would be the data from API Output device 5
All code that is located just below this block will execute regardless of connection status!
You can include SendToHS() calls, however when there isn't an active connection, it will just return and continue.
If you only want code to execute when HomeSeer is connected, put it inside the if statement below.
*/
/*Execute regardless of connection status*/
if (IsConnected == true) {
/*Execute ONLY when HomeSeer is connected*/
}
}
I got this sketch off the net, by itself it works fine. however, I need some help with this api sketch for an accelerometer I am unsure what/how exactly to send the data to homeseer.
/************************************************************ *
Arduino to Homeseer 3 Plugin API written by Enigma Theatre.
V1.0.0.146
* *
************************************************************ */
int FromHS[50];
boolean IsConnected = false;
//************************************************************
//**************Declare your variables here*******************
const boolean debugging = true; // whether to print debugging to serial output
const boolean showBuffer = false; // whether to dump details of ring buffer at each read
const int xPin = 3; // analog: X axis output from accelerometer
const int yPin = 1; // analog: Y axis output from accelerometer
const int zPin = 2; // analog: Z axis output from accelerometer
const int led = 13; // just to blink a heartbeat while running
const int totalAxes = 3; // for XYZ arrays: 0=x, 1=y, 2=z
const int baseSamples = 1000; // number of samples to average for establishing zero g base
const int bufferSize = 16; // number of samples for buffer of data for running average
const int loopBlink = 100; // number of trips through main loop to blink led
// array of pin numbers for each axis, so the constants above can be chnaged with impunity
const int pin [totalAxes] = {
xPin, yPin, zPin};
// base value for each axis - zero g offset (at rest when sketch starts)
int base [totalAxes];
// ring buffer for running average of data, one for each axis, each with <bufferSize> samples
int buffer [totalAxes] [bufferSize];
// index into ring buffer of next slot to use, for each axis
int next [totalAxes] = {
0,0,0};
// current values from each axis of accelerometer
int curVal [totalAxes];
// count of trips through main loop, modulo blink rate
int loops = 0;
//************************************************************ ****
void HSSetup() {
//************************
//Add YOUR SETUP HERE;
//************************
long sum [totalAxes]= { // accumulator for calculating base value of each axis
0,0,0 };
Serial.begin (9600);
Serial.println ("***");
// initialize all pins
pinMode (led, OUTPUT);
for (int axis=0; axis<totalAxes; axis++)
pinMode (pin [axis], INPUT); // not necessary for analog, really
// read all axes a bunch of times and average the data to establish zero g offset
// chip should be at rest during this time
for (int i=0; i<baseSamples; i++)
for (int axis=0; axis<totalAxes; axis++)
sum [axis] += analogRead (pin [axis]);
for (int axis=0; axis<totalAxes; axis++)
base [axis] = round (sum [axis] / baseSamples);
// and display them
Serial.print ("*** base: ");
for (int axis=0; axis<totalAxes; axis++) {
Serial.print (base [axis]);
Serial.print ("\t");
}
Serial.println ();
Serial.println ("***");
// initialize the ring buffer with these values so the averaging starts off right
for (int axis=0; axis<totalAxes; axis++)
for (int i=0; i<bufferSize; i++)
buffer [axis] [i] = base [axis];
// light up the led and wait til the user is ready to start (sends anything on serial)
// so that the base values don't immediately shoot off the top of the serial window
digitalWrite (led, HIGH);
while (!Serial.available())
/* wait for <RETURN> */ ;
digitalWrite (led, LOW);
}
void HSloop() {
//increment the loop counter and blink the led periodically
loops = (loops + 1) % loopBlink;
digitalWrite (led, loops == 0);
// get new data from each axis by calling a routine that returns
// the running average, instead of calling analogRead directly
for (int axis=0; axis<totalAxes; axis++) {
curVal [axis] = getVal (axis, showBuffer);
if (debugging) {
Serial.print (curVal [axis]);
Serial.print ("\t");
SendToHS(1,axis);
SendToHS(2,axis);
SendToHS(3,axis);
}
}
if (debugging)
Serial.println ();
// here we will do all of the real work with curVals
}
int getVal (int axis, boolean show) {
// returns the current value on <axis>, averaged across the previous <bufferSize> reads
// print details if <show> is true
long sum; // to hold the total for aaveraging all values in the buffer
// read the data into the next slot in the buffer and stall for a short time
// to make sure the ADC can cleanly finish multiplexing to another pin
buffer [axis] [next [axis]] = analogRead (pin [axis]);
delay (10); // probably not necessary given the stuff below
// display the buffer if requested
if (show) {
for (int i=0; i<bufferSize; i++) {
if (i == next [axis]) Serial.print ("*");
Serial.print (buffer [axis] [i]);
Serial.print (" ");
}
Serial.println ();
}
// bump up the index of the next available slot, wrapping around
next [axis] = (next [axis] + 1) % bufferSize;
// add up all the values and return the average,
// taking into account the offset for zero g base
sum = 0;
for (int i=0; i<bufferSize; i++)
sum += buffer [axis] [i];
return (round (sum / bufferSize) - base [axis]);
//************************
//Add YOUR CODE HERE;
//************************
/* To Send Data to Homeseer use SendToHS(Device,Value)
Eg.. SendToHS(1,200); where 1 is the API device in homeseer and 200 is the value to send
To Recieve data from Homeseer look up the FromHS array that is updated when the device value changes.
Eg.. FromHS[5] would be the data from API Output device 5
All code that is located just below this block will execute regardless of connection status!
You can include SendToHS() calls, however when there isn't an active connection, it will just return and continue.
If you only want code to execute when HomeSeer is connected, put it inside the if statement below.
*/
/*Execute regardless of connection status*/
if (IsConnected == true) {
/*Execute ONLY when HomeSeer is connected*/
}
}
Comment