Hello Particle community,
So for a while now I’ve been working on this little project where I’m using 4 separate Photons to pull the RPMs from several different label printing press unwind shafts. Now with standard arduino code and using their Yun board and such, this would generally be a breeze. However, obviously the Build-IDE is a little different in its syntax and library addition, so making a working, accurate tachometer has been an extremely frustrating experience. On the other end of the spectrum, I am using Google Sheets to display the data from these Photons (I have only interfaced one so far), and used code from a project I found on google that displays basic data from a temperature-humidity sensor. Now, I am using a Keyes Magnet-Ring sensor for each of these particle boards and it is the only thing attached to each board, so no other sensors or anything are being read or controlled by the board. I am currently at my wits end with both the particle code and the Google Sheets script editor code, and any help or input would be so greatly appreciated.
Here’s the Build-IDE code:
#include "FreqPeriodCounter.h"
const int simulated_fan_output_pin = A0;
const int counter_interrupt_pin = D4;
char resultstr[64];
FreqPeriodCounter counter(counter_interrupt_pin, micros, 0);
const int k_seconds_per_minute = 60;
const int k_pulses_per_revolution = 2;
const int k_hertz_to_RPM_conversion_factor = k_seconds_per_minute / k_pulses_per_revolution;
void setup(void)
{
Serial.begin(9600);
Serial.println("Computer fan RPM Demo");
pinMode(simulated_fan_output_pin, OUTPUT);
analogWrite(simulated_fan_output_pin, 128); // any number here should create a 500Hz frequency
pinMode(counter_interrupt_pin, INPUT);
attachInterrupt(counter_interrupt_pin, counter_interrupt_service_routine, CHANGE);
Particle.variable("result", resultstr, STRING);
}
void loop()
{
int per;
long her;
int rpm;
if(counter.ready()) {
per = counter.period;
her = counter.hertz();
rpm = her * k_hertz_to_RPM_conversion_factor;
rpm = rpm/60000;
sprintf(resultstr, "{\"per\":%d,\"her\":%d,\"rpm\":%d}", per, her, rpm);
}
}
void counter_interrupt_service_routine()
{
counter.poll();
}
Here is the Google Sheets Script Editor code:
function collectData() {
var sheet = SpreadsheetApp.getActiveSheet();
var response = UrlFetchApp.fetch("https://api.spark.io/v1/devices/(Device_ID)/result?access_token=(Access_Token)");
try {
var response = JSON.parse(response.getContentText());
var result = unescape(response.result);
try {
var p = JSON.parse(result);
var d = new Date();
sheet.appendRow([d, p.per, p.her, p.rpm]);
} catch(e)
{
Logger.log("Unable to do second parse");
}
} catch(e)
{
Logger.log("Unable to returned JSON");
}
}