Page 134 - Programming the Photon Getting Started With the Internet of Things
P. 134
DallasTemperature sensors(&oneWire);
void setup() {
sensors.begin();
Spark.variable("tempc", &tempC, DOUBLE);
Spark.variable("tempf", &tempF, DOUBLE);
}
void loop() {
sensors.requestTemperatures();
tempC = sensors.getTempCByIndex(0);
tempF = tempC * 9.0 / 5.0 + 32.0;
}
The first thing you will notice is that the program uses two different libraries, indicated
by the #include statement at the top of the program. As we know from previous
experiments, libraries are used so that we don’t have to make our programming code
really complex, and this allows us to include functions from other programs without
having to understand the complexity. Because we are using a DS18B20 IC which uses a
one-wire serial communication, we need to import the OneWire library, which will handle
all the communication with the digital temperature sensor. The spark-dallas-
temperature library handles everything else to do with reading the temperature on the
Photon board—luckily someone has already created this for us. We add these libraries in
the same way as before by using the build IDE in your Web browser and navigating to the
Libraries section as shown in Figure 6.12. You can search for “spark dallas temperature”
and add this to your application.