ESP8266 – DS18B20

Table of Contents

Sources

Relevant Specs

				
					Communicates over one-wire bus
Power supply range: 3.0V to 5.5V
Operating temperature range: -55ºC to +125ºC
Accuracy +/-0.5 ºC (between the range -10ºC to 85ºC)
				
			

Parts Required

				
					ESP8266
DS18B20
4.7k Ohm Resistor
Breadboard
Jumper wires
				
			

Wiring

				
					COLOR       JOB     WIRING
RED     =   VCC     => 3.3V
YELLOW  =   DATA    D2 => RES4.7 => 3.3V
GREY    =   GND     => GND
				
			

Example Code

				
					/*********
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com  
*********/

#include <OneWire.h>
#include <DallasTemperature.h>

// GPIO where the DS18B20 is connected to
const int oneWireBus = 4;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

void setup() {
  // Start the Serial Monitor
  Serial.begin(115200);
  // Start the DS18B20 sensor
  sensors.begin();
}

void loop() {
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  Serial.print(temperatureC);
  Serial.println("ºC");
  Serial.print(temperatureF);
  Serial.println("ºF");
  delay(5000);
}
				
			

My Code

				
					//    _  ___          __   _     _
//   | || \ \        / /  | |   (_)
//   | || |\ \  /\  / /__ | |__  _
//   |__   _\ \/  \/ / _ \| '_ \| |
//      | |  \  /\  / (_) | |_) | |
//      |_|   \/  \/ \___/|_.__/|_|.com
//
//   inverterBatteryTemp
//   ESP8266 - OneWireBus - MQTT(noCallback)
//
//  /*TODO*/
//  UPTIME
//  MQTT SUB FOR UPTIME AND ALIVE request
//  LED_BUILTIN if active(now it's always on...)
//

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define oneWireBus_1 D2

const char* ssid     = "";
const char* password = "";
const char* mqtt_user = "";
const char* mqtt_password = "";
const char* mqtt_server = "";
const unsigned int mqtt_port = 1883;

char* mqtt_awaketopic = "inverterBatteryTemp-awake";
char* mqtt_awakemsg = "awake-inverterBatteryTemp";
char* mqtt_alivetopic = "inverterBatteryTemp-alive";
char* mqtt_alivemsg = "inverterBatteryTemp-%ld";
unsigned int mqtt_aliveInterval = 1800; //0.5h

char* mqtt_oneWireBus_1_topic = "inverterBatteryTemp/storage/oneWireBus1";

unsigned int mqtt_oneWireBus_message_interval = 30000; // ms
unsigned long mqtt_oneWireBus_lastsent_message = 0;

unsigned int analogReadInterval = 5000; // ms
unsigned long lastAnalogReadInterval = 0;

int value_C_oneWireBus_1 = 0;
int value_F_oneWireBus_1 = 0;

OneWire oneWire(oneWireBus_1);
DallasTemperature sensors(&oneWire);

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;

void setup_wifi() {
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ClosetHandler-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
      Serial.println("connected");
      client.publish(mqtt_awaketopic, mqtt_awakemsg);
      Serial.print("Awake message on: ");
      Serial.print(mqtt_awaketopic);
      Serial.print(" with message: ");
      Serial.println(mqtt_awakemsg);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);

  delay(10);
  Serial.println(F("__________________________________________"));
  Serial.println(F(""));
  Serial.println(F("  _  _ __   v0.1   __     _      _ "));
  Serial.println(F(" | || |\\ \\        / /    | |    (_)"));
  Serial.println(F(" | || |_\\ \\  /\\  / /___  | |__   _ "));
  Serial.println(F(" |__   _|\\ \\/  \\/ // _ \\ | '_ \\ | |"));
  Serial.println(F("    | |   \\  /\\  /| (_) || |_) || |"));
  Serial.println(F("    |_|    \\/  \\/  \\___/ |_.__/ |_|"));
  Serial.println(F(""));
  Serial.println(F("__________________________________________"));
  Serial.println(F(""));

  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);

  sensors.begin();

  Serial.println(F("__________________________________________"));
  Serial.println(F(""));
  Serial.println(F("inverterBatteryTemp - Initialized"));
  Serial.println(F("__________________________________________"));
  Serial.println(F(""));
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg > (mqtt_aliveInterval * 1000)) {
    lastMsg = now;
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, mqtt_alivemsg, value);
    Serial.print("Publish alive message: ");
    Serial.println(msg);
    client.publish(mqtt_alivetopic, msg);
  }

  /* AnalogRead */
  now = millis();
  if (now - lastAnalogReadInterval > analogReadInterval) {
    lastAnalogReadInterval = now;

    // oneWireBus 1
    sensors.requestTemperatures();
    value_C_oneWireBus_1 = sensors.getTempCByIndex(0);
    value_F_oneWireBus_1 = sensors.getTempFByIndex(0);
    Serial.print(value_C_oneWireBus_1);
    Serial.println("ºC");
    Serial.print(value_F_oneWireBus_1);
    Serial.println("ºF");

    unsigned long analogReadTime = millis() - now;
    Serial.print("ANALOG READ TIME: ");
    Serial.println(analogReadTime);

    /* PUBLISH MSGS */
    // oneWireBus 1
    if (now - mqtt_oneWireBus_lastsent_message > mqtt_oneWireBus_message_interval) {
      mqtt_oneWireBus_lastsent_message = now;


      snprintf (msg, MSG_BUFFER_SIZE, "%ld", value_C_oneWireBus_1);
      client.publish(mqtt_oneWireBus_1_topic, msg);
    }
  }
}
				
			

Image

Leave a Comment