First Iteration of Code
// This #include statement was automatically added by the Spark IDE.
#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;
int waterSensor = A0;
int water;
int waterHot = 100;
int waterCold = 97;
int ledPin = D2;
void setup()
{
// Register a Particle variable here
Particle.variable("temperature", &temperature, DOUBLE);
Particle.variable("temperatureF", &temperatureF, DOUBLE);
Particle.variable("water", &water, INT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, 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;
water = analogRead(waterSensor);
if (water < 100){
Particle.publish("Water-Detection","Your water is ready");
if (temperatureF < waterHot){
if (temperatureF > waterCold){
Particle.publish("Water-At-Optimal-Temp");
digitalWrite(ledPin, HIGH);
}else{
Particle.publish("Water-Too-Cold");
}
}else{
Particle.publish("Water-Too-Hot");
}
delay(10000);}
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. .