Skip to content

Instantly share code, notes, and snippets.

@redaphid
Created October 6, 2016 07:15
Show Gist options
  • Save redaphid/adf1d38404d8af5f9d87b6b28a5ab86d to your computer and use it in GitHub Desktop.
Save redaphid/adf1d38404d8af5f9d87b6b28a5ab86d to your computer and use it in GitHub Desktop.
octoblu-particle integration
// Define the Pin the Temperature sensor is on
int tempPin = A0;
// Create a variable that will store the temperature value
double temperature = 0.0;
double temperatureF = 0.0;
double lastTempF = 0.0;
String temperatureString = "";
void setup()
{
// Register a Particle variable here
Particle.variable("temperature", &temperature, DOUBLE);
Particle.variable("temperatureF", &temperatureF, DOUBLE);
Particle.variable("tempString", &temperatureString, STRING);
// Connect the temperature sensor to A0 and configure it
// to be an input
pinMode(tempPin, INPUT);
}
void loop()
{
// Keep reading the sensor value so when we make an API
// call to read its value, we have the latest one
int reading = analogRead(tempPin);
// The returned value from the device is going to be in the range from 0 to 4095
// Calculate the voltage from the sensor reading
double voltage = (reading * 3.3) / 4095.0;
// Calculate the temperature and update our static variable
temperature = (voltage - 0.5) * 100;
// Now convert to Farenheight
temperatureF = ((temperature * 9.0) / 5.0) + 32.0;
temperatureString = String::format("%.2f", temperatureF);
if ( abs(temperatureF - lastTempF) > 2) {
Particle.publish("temperature", temperatureString, 60, PRIVATE);
delay(100);
}
lastTempF = temperatureF;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment