#include "OneWire.h"
// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature.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(D0 );
// 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;
bool readytodrink = false;
bool gettingcold = false;
int buttonPin = D1;
bool sensetemp = false;
long framerate = 0;
void setup()
{
// Register a Particle variable here
/*Particle.variable("temperature", &temperature, DOUBLE);*/
Particle.variable("temperatureF", &temperatureF, DOUBLE);
Particle.variable("readytodrink", readytodrink );
Particle.variable("gettingcold", gettingcold );
pinMode( buttonPin , INPUT_PULLUP);
// setup the library
dallas.begin();
}
void loop()
{
framerate++;
int buttonState = digitalRead( buttonPin );
if( buttonState == LOW ) {
sensetemp = true;
}
// Request temperature conversion
if (sensetemp == true && framerate%3000 == 0) {
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;
if (temperatureF < 155 && temperatureF > 145 && !readytodrink) {
bool success;
success = Particle.publish("readytodrink");
if (success) {
readytodrink = true;
}
}
if (temperatureF < 122 && !gettingcold) {
bool success;
success = Particle.publish("gettingcold");
if (success) {
gettingcold = true;
}
}
/*if (temperatureF < 122) {
gettingcold = true;
}*/
}
/*delay(5000);*/
}
/*
The API request will look something like this:
GET /v1/devices/{DEVICE_ID}/temperature
# EXAMPLE REQUEST IN TERMINAL
# Device ID is 0123456789abcdef
# Your access token is 123412341234
curl -G https://api.particle.io/v1/devices/0123456789abcdef/temperature \
-d access_token=123412341234
*/
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .