Skip to content

Instantly share code, notes, and snippets.

@ugnb
Last active August 17, 2016 18:30
Show Gist options
  • Save ugnb/26830bcd8810d11fd5ad0d384888197e to your computer and use it in GitHub Desktop.
Save ugnb/26830bcd8810d11fd5ad0d384888197e to your computer and use it in GitHub Desktop.
Using DallasTemperature lib for 1-wire DS18B20 thermometer with Arduino timer interrupt on ESP8266
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define TEMP_PIN D5
#define TEMP_SENSOR_PRECISION 8
#define TEMP_INTERRUPT_CYCLES ESP.getCycleCount() + 50000000
// Change this to your sensor address
#define TEMP_SENSOR_ADDR {0x28, 0x35, 0x33, 0xB3, 0x06, 0x00, 0x00, 0x1A}
OneWire temperatureSensorsBus(TEMP_PIN);
DallasTemperature temperatureSensorsManager(&temperatureSensorsBus);
DeviceAddress sensorAddress = TEMP_SENSOR_ADDR;
void sensorsSetup() {
temperatureSensorsManager.begin();
temperatureSensorsManager.setResolution(sensorAddress, TEMP_SENSOR_PRECISION);
// This line disables delay in the library
temperatureSensorsManager.setWaitForConversion(false);
}
float temperature = 0;
void ICACHE_RAM_ATTR checkSensors() {
temperatureSensorsManager.requestTemperatures();
temperature = temperatureSensorsManager.getTempC(sensorAddress);
timer0_write(TEMP_INTERRUPT_CYCLES);
}
void setup() {
Serial.begin(9600);
sensorsSetup();
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(checkSensors);
timer0_write(TEMP_INTERRUPT_CYCLES);
interrupts();
}
void loop() {
Serial.print("Temperature: ");
Serial.println(temperature);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment