Monitoring aquarium temperature using DS18B20

I have a aquarium with two parrot fishes and few other species. Parrot fishes live happily within 25 to 32 degree Celsius. If temperature goes out of limits then it effects metabolism and behavior of fishes. Even though aquarium heater has thermostat I thought to observe temperature and record it over time.

For this you need following components

1) NodeMCU board.
2) DS18B20 sensor, this is water proof temperature sensor.
3) 4.7k OHM resistor.
4) Some jumper cables.
5) a breadboard.

Connections

1) Connect 3.3V and Ground of NodeMCU to the Power input (red wire) and Ground (black wire) of sensor.
2) Connect D5 pin of NodeMCU with data (yellow wire) of sensor.
3) You will have to place a 4.7k OHM resistor between 3.3V and data wire of sensor to pullup data signal.

Here is the code you need to flash on controller

#include <ESP8266WiFi.h>
#include <Adafruit_MQTT.h>
#include <Adafruit_MQTT_Client.h>
#include <DallasTemperature.h>
#include <OneWire.h>

/********** temperature data at D5 pin on nodemcu **************/
#define ONE_WIRE_BUS D5     // D5 pin on NodeMCU

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

/******* WiFi Access Point ***********/

#define WLAN_SSID       "your_wifi_ssid"
#define WLAN_PASS       "your_wifi_password"

/******** Adafruit.io Setup ********/

#define MQTT_SERVER      "your_mqtt_broker"
#define MQTT_SERVERPORT  1883                   // default port of MQTT broker
#define MQTT_USERNAME    "your_mqtt_username"
#define MQTT_KEY         "your_mqtt_password"

/************ Global State **********/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_KEY);

/************ Feeds ***************/

Adafruit_MQTT_Publish aquatempdata = Adafruit_MQTT_Publish(&mqtt, "aquatemp"); //This is the topic/subject to which I am publishing temp data from DS18B20 sensor

/******** Sketch Code ***************/

void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);

  Serial.println(F("Temp data capture"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

String aquatemppub;

void loop() {

  MQTT_connect();

  // Read temp data and publish on MQTT broker
  delay(1000);
  sensors.requestTemperatures();                // request temperature
  Serial.println("Temperature is: ");
  float temp=sensors.getTempCByIndex(0);
  Serial.println(temp);
// Formatting message like temp, aquarium name for eg 20, Gurgaon Aquarium
  aquatemppub="";
  aquatemppub=String(temp,2);
  aquatemppub+=F(",Gurgaon Aquarium");
 
  // write data on serial port and publish on MQTT broker
 
  Serial.print(F("\nSending temp data "));
  Serial.println(aquatemppub);
  Serial.print("...");

    if (! aquatempdata.publish(aquatemppub.c_str())) { //c_str() is to convert String data to const char*, String datatype is not accepted in publish function
      Serial.println(F("MQTT tranmit Failed"));
    }
    else
    {
      Serial.println(F("MQTT tranmit OK!"));
    }

//delay(3600000);
delay(10);
//ESP.deepSleep(600000000);  //uncomment this to send nodemcu in deep sleep for 10 min
delay(2000); //comment this if you enable above deep sleep function
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

You can verify MQTT data transmission using any MQTT client. I have used MQTT dash android app to check it.

I will extend this to push readings into influxdb and monitor it into Grafana later.