Back to Parent

// This #include statement was automatically added by the Particle IDE.
#include <spark-dallas-temperature.h>

// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>

// -----------------
// Read temperature
// -----------------

// Data wire is plugged into port 0 on the Arduino
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire( D2 );

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dallas(&oneWire);

// Create a variable that will store the temperature value
double temperature = 0.0;
double temperatureF = 0.0;
int redLed = D3;
int bluLed = D4;

void setup()
{
    // Register a Particle variable here
    Particle.variable("temperature", &temperature, DOUBLE);
    Particle.variable("temperatureF", &temperatureF, DOUBLE);
    pinMode(redLed, OUTPUT);
    pinMode(bluLed, OUTPUT);
    digitalWrite(redLed, LOW);
    digitalWrite(bluLed, LOW);
    // setup the library
    dallas.begin();
}

void loop()
{
  // Request temperature conversion

  dallas.requestTemperatures();

  // get the temperature in Celcius
  float tempC = dallas.getTempCByIndex(0);
  // convert to double
  temperature = (double)tempC;

  // convert to Fahrenheit
  float tempF = DallasTemperature::toFahrenheit( tempC );
  // convert to double
  temperatureF = (double)tempF;
  
  blinkLed(temperatureF);

  delay(5000);

}

void blinkLed(float temp) {
    if ( temp >= 120 and temp <= 140) {
        Particle.publish("Roger Mr. Java", String(temp) + "Farenheit");
        digitalWrite(redLed, HIGH);
        digitalWrite(bluLed, LOW);
    } else if (temp < 120) {
        Particle.publish("Not making coffee", String(temp) + "Farenheit");
        digitalWrite(redLed, LOW);
        digitalWrite(bluLed, HIGH);
    }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0