Skip to content

Instantly share code, notes, and snippets.

@gmag11
Last active May 5, 2019 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmag11/bcbef1dc18ff6e5b59ce38801d0119df to your computer and use it in GitHub Desktop.
Save gmag11/bcbef1dc18ff6e5b59ce38801d0119df to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port D2 on NodeMCU
#define ONE_WIRE_BUS D2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup(void)
{
// start serial port
Serial.begin(115200);
delay (1000);
Serial.println("Dallas Temperature Control Library - Async Demo");
Serial.println("\nDemo shows the difference in length of the call\n\n");
// Start up the library
sensors.begin();
uint8_t address[8];
if (!sensors.getAddress (address, 0)) {
Serial.printf ("No device\n");
return;
}
sensors.setWaitForConversion (false);
}
void getTemp () {
time_t start = millis ();
sensors.requestTemperatures ();
while (!sensors.isConversionComplete ()) {
delay (1);
}
float temp = sensors.getTempCByIndex (0);
time_t totalTime = millis () - start;
if (temp == DEVICE_DISCONNECTED_C) {
Serial.printf ("Error: Could not read temperature data\n");
}
else {
Serial.printf ("Resolution is %d bit. Temp is %f. Measured in %d ms\n", sensors.getResolution (), temp, totalTime);
}
}
void loop(void)
{
sensors.setResolution (9);
getTemp ();
sensors.setResolution (10);
getTemp ();
sensors.setResolution (11);
getTemp ();
sensors.setResolution (12);
getTemp ();
delay (5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment