Fishtank Controller (part II)

The fishtank controller project was very successful <read about it here> and has been in operation for more than a year.   Since putting this system together I’ve become a huge fan of the ESP8266 programmable micro-chips.  An ESP8266 MCU can easily replace the functionality of the Raspberry Pi on the Fishtank Controller project.    This would free up a $39 Raspberry Pi replaced by a $3 ESP8266.   The next phase of this project is to configure the ESP to recognize MQTT messages.   These messages would control the charges on the GPIO pins which woud ultimately control the 120V relays.

More to come as this phase progresses.

Update 03/06/2018:

It’s finished.   I’ve replaced the Raspberry Pi with a much cheaper ESP8266. The ESP performs the same function as the Raspberry Pi at a fraction of the cost.    I’m still using MQTT functionality to trigger the ESP to flip a 5 volt relay on or off.

Below is a screenshot of the model ESP I used for this project:

Below is a screenshot of the controller developed with Node-Red on another Raspberry Pi.   Each of the 3 green icons are slider buttons turning things on or off.

Below is the Arduino code uploaded to the ESP.     Feel free to copy/paste the code into your Arduino IDE.    Simply replace the asterisks with your information.    You’ll may also with to choose different pin numbers depending on your needs and ESP model.

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

const char* ssid = "****************";
const char* password =  "*************";
const char* mqttServer = "192.168.1.***";
const int mqttPort = ****;
const char* mqttUser = "***********";
const char* mqttPassword = "**********";

int plug1Pin = D1;
int plug2Pin = D2;
int plug3Pin = D5;
int plug4Pin = D6;

WiFiClient espClient_FT;
PubSubClient client(espClient_FT);

void setup() {
  WiFi.begin(ssid, password);
  pinMode(plug1Pin, OUTPUT);
  pinMode(plug2Pin, OUTPUT);
  pinMode(plug3Pin, OUTPUT);
  pinMode(plug4Pin, OUTPUT);
  digitalWrite(plug1Pin, HIGH);
  digitalWrite(plug2Pin, HIGH);
  digitalWrite(plug3Pin, HIGH);
  digitalWrite(plug4Pin, HIGH);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.println("Connecting to WiFi..");
  Serial.println("Connected to the WiFi network");

  client.setServer(mqttServer, mqttPort);

  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");
    if (client.connect("ESP8266Client_fishtank", mqttUser, mqttPassword )) {
    } else {
      Serial.print("failed with state ");

void callback(char* topic, byte* payload, unsigned int length) {
  String req;
  Serial.print("Message arrived in topic: ");
  for (int i = 0; i < length; i++) {
    req += ((char)payload[i]);

  if (req == "/light/0") {
    digitalWrite(plug1Pin, LOW);
  } else {
    if (req == "/light/1") {
      digitalWrite(plug1Pin, HIGH);
    } else {
      if (req == "/pump/0") {
        digitalWrite(plug2Pin, LOW);
      } else {
        if (req == "/pump/1") {
          digitalWrite(plug2Pin, HIGH);
        } else {
          if (req == "/heater/0") {
            digitalWrite(plug3Pin, LOW);
          } else {
            if (req == "/heater/1") {
              digitalWrite(plug3Pin, HIGH);

void loop() {

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.