void setup() {
Particle.begin();
}
void loop() {
Particle.print(getTemp());
delay(1000);
}
float getTemp() {
return analogRead(A0);
//subsequent processing omitted for simplicity's sake
}
|
Particle.print(getTemp()); char publishTime[40];
unsigned long now = millis();
// now is in milliseconds
unsigned nowSec = now/1000UL;
unsigned sec = nowSec%60;
unsigned min = (nowSec%3600)/60;
unsigned hours = (nowSec%86400)/3600;
sprintf(publishTime,"%u:%u:%u",hours,min,sec);
Particle.publish("Uptime",publishTime);
int analogPin = A0; // potentiometer wiper (middle terminal) connected to analog pin 3
int val = 0; // variable to store the value read
double Vref = 3.18;
double reading = 3.3;
char buf;
String s;
char publishTemp[40];
reading = analogRead(A0); // read the input pin and convert to C
Vout = reading*Vref/1024;
temp = (Vout - 1.25)/0.005;
s = String(temp);
s.toCharArray(publishTemp, 40);
Particle.publish("TempC",publishTemp);
String msg = String(analogRead(A0));
Particle.publish("Status", msg.c_str());
/*
Calculate temperature using 10K thermistor attached to an Adafruit ADS1015 Analog-to-Digital Converter on the I2C communication bus
R1 is a 91k metal-film resistor, chosen because a) it's a metal-film resistor I had lying around, and b) combined with a 10k thermistor,
for the temperature range I'm interested in (plus a little room for the unexpected, i.e. down to -28C/-19F), it gives voltages at
the analog pin of up to 2.048V. This exactly matches the two times gain setting of the ADS1015 (measuring from 0 to 2.048V) and so I get
the benefit of 12-bit sensitivity over the full temperature range I'm interested in.
*/
#include <Wire.h>
#include <Adafruit_ADS1015.h>
#define RESISTOR_FIXED 91000 //R1 in ohms
#define VCC 3.34
Adafruit_ADS1015 ADS1015;
void setup() {
ADS1015.begin();
ADS1015.setGain(GAIN_TWO); //gain two: voltage = 1 mV per bit; good resolution for the values I'm looking at - up to 2.048V at A0
Particle.begin();
}
void loop() {
Particle.print(getTemp());
delay(1000);
}
float getTemp() { //average four analog readings from thermistor and returns temp in Celsius
float f = 0;
for (uint8_t i = 0; i < 4; i++) {
f += ADS1015.readADC_SingleEnded(0); //read analog value at A0
delay(1);
}
f /= 4000; //average 4 readings and divide by 1000 for voltage at A0
f = (RESISTOR_FIXED * f) / (VCC - f); //calculate resistance at thermistor (R2)
f = log(f); //Steinhart-Hart equation using coefficients for the 10K thermistor
f = 1 / (0.00112531 + 0.000234712 * f + 0.0000000856635 * f * f * f);
return f -= 273.15; //convert Kelvin to Celsius
}