Robert Allan
Published © GPL3+

Fishtank Controller Upgrade

This is an upgrade to my Fishtank controller project to integrate it with my home automation system.

IntermediateShowcase (no instructions)2 hours1,217
Fishtank Controller Upgrade

Things used in this project

Software apps and online services

Domoticz Home Automation System
Node-RED
Node-RED
Arduino IDE
Arduino IDE
MQTT
MQTT

Story

Read more

Schematics

PCB Schematic

Code

Fish Controller Version2

C/C++
Upgrade version to include receiving JSON messages and handling them
#include "ESPHelper.h"
#include <ArduinoJson.h>
#include <NtpClientLib.h>
#include <TimeLib.h>
#include "Chrono.h"

Chrono myChrono(Chrono::SECONDS);

#define TOPIC "home/fishtank/control"
#define STATUS_TOPIC "home/fishtank/status"
#define LIGHT_PIN 5
#define PUMP_PIN 4
#define BUBBLES_PIN 13
#define NETWORK_HOSTNAME "ESP12FishTank"
#define OTA_PASSWORD "YourOTAPassword" // Create your own unique password for OTA updates

char* CtrlTopic = TOPIC;
char* statusTopic = STATUS_TOPIC;
char* hostnameStr = NETWORK_HOSTNAME;
char* otaPassword = OTA_PASSWORD;

const int LightPin = LIGHT_PIN;
const int PumpPin = PUMP_PIN;
const int BubblesPin = BUBBLES_PIN;

bool LightSts = false;
bool PumpSts = false;
bool BubblesSts = false;

boolean syncEventTriggered = false; // True if a time even has been triggered
NTPSyncEvent_t ntpEvent; // Last triggered event

int8_t timeZone = 0;

int ActHr = 0;
int ActMin = 0;
int ActDay = 0;
int Selector = 0;

netInfo homeNet = {  .mqttHost = "***.***.***.***",     //Your MQQT Brokers IP Adress
          .mqttUser = "",   //can be blank depending on your broker config
          .mqttPass = "",   //can be blank depending on your broker config
          .mqttPort = 1883,         //default port for MQTT is 1883 - only chance if needed.
          .ssid = "WIFISSID", // Your wifi SSID 
          .pass = "WIFICode"};
ESPHelper myESP(&homeNet);

const size_t bufferSize = JSON_OBJECT_SIZE(7) + 80;
DynamicJsonBuffer jsonBuffer(300);

void callback(char* topic, byte* payload, unsigned int length) {
  // handle message arrived
  const size_t bufferSize = JSON_OBJECT_SIZE(7) + 80;
  DynamicJsonBuffer jsonBuffer(300);
  char inData[150];
  Serial.print("payload: ");
  for(int i =0; i<length; i++){
    Serial.print((char)payload[i]);
    inData[i] = (char)payload[i];
  }
  Serial.println();
  JsonObject& root = jsonBuffer.parseObject(inData);  
  int Battery = root["Battery"];
  int RSSI = root["RSSI"];
  char description = root["description"];
  char dtype = root["dtype"];
  char id = root["id"];
  int idx = root["idx"];
  Serial.println("idx: ");
  Serial.println(idx);
  char dt = root["dt"];
  float svalue1 = root["svalue1"];
  float svalue = root["svalue"];
  if (idx == 33){
    int CmdSelect = svalue1;
    if (CmdSelect==10){
        myESP.publish(statusTopic,"10",true);  
        LightSts = false;
    }
    else if (CmdSelect==20){
        myESP.publish(statusTopic,"20",true);
        LightSts = true;
    }
    else if (CmdSelect==30){
        myESP.publish(statusTopic,"30",true);
        BubblesSts = false;
    }
    else if (CmdSelect==40){
        myESP.publish(statusTopic,"40",true);
        BubblesSts = true;
    }
    else if (CmdSelect==50){
        myESP.publish(statusTopic,"50",true);
        PumpSts = false;
    }
    else if (CmdSelect==60){
        myESP.publish(statusTopic,"60",true);
        PumpSts = true;
    }
    else if (CmdSelect==70){
        Serial.println("Payload Received 70");
        myESP.publish(statusTopic,"70",true);
        Serial.print("1. Got NTP time : ");
        Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
        NTP.onNTPSyncEvent([](NTPSyncEvent_t event) {
        ntpEvent = event;
        if (ntpEvent) {
          Serial.print("Time Sync error: ");
          myESP.publish(statusTopic,"611",true);
          if (ntpEvent == noResponse)
            Serial.println("NTP server not reachable");
          else if (ntpEvent == invalidAddress)
            Serial.println("Invalid NTP server address");
        }
        else {
        delay(100);
        Serial.print("2. Got NTP time: ");
        Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
        myESP.publish(statusTopic,"661",true);
        }    
        syncEventTriggered = true;
        });
        delay(3000);
        String xActHr = String(ActHr);   
        String xActMin = String(ActMin);
        String xTime = String(xActHr + xActMin);
        char xTimex[5];
        xTime.toCharArray(xTimex,5);  
        Serial.print("Current Time: ");
        Serial.println(xTimex);
        myESP.publish(statusTopic,xTimex,true);
    }
  }  
}


void setup() {
  Serial.begin(115200); //start the serial line
  delay(500);
  Serial.println("Starting Up, Please Wait...");
  myESP.OTA_enable();
  myESP.OTA_setPassword(otaPassword);
  myESP.OTA_setHostnameWithVersion(hostnameStr);
  myESP.OTA_begin();
  myESP.addSubscription(CtrlTopic);
  myESP.begin();
  myESP.setMQTTCallback(callback);
  NTP.begin("***.***.***.***", timeZone, true);
  NTP.setInterval(3600);
  Serial.print("Got NTP time: ");
  Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync())); 
  pinMode(LightPin, OUTPUT);
  digitalWrite(LightPin, LOW);
  pinMode(PumpPin, OUTPUT);
  digitalWrite(PumpPin, LOW);
  pinMode(BubblesPin, OUTPUT);
  digitalWrite(BubblesPin, LOW);
  myChrono.restart();  
  Serial.println("Initialization Finished.");
  delay(100);
  
}

void loop(){
  myESP.loop();  //run the loop() method as often as possible - this keeps the network services running
  time_t t = now();
  ActHr = hour();
  ActMin = minute();
  ActDay = weekday();
  if (myChrono.hasPassed(3,true)){
    if (ActHr == 21 && ActMin == 45){
      if (BubblesSts == false && ActDay != 7){
        BubblesSts = true;  
      }
      if (LightSts == true){
        LightSts = false;  
      }
      if (PumpSts == false){
        PumpSts = true;  
      }
    }
    if (ActHr == 06 && ActMin == 00){
      if (LightSts == false){
        LightSts = true;  
      }
      if (PumpSts == false){
        PumpSts = true;  
      }
    }
    if (ActHr == 07 && ActMin == 00){
      if (BubblesSts == true){
        BubblesSts = false;
      }  
    }
    switch (BubblesSts){
      case false:
        digitalWrite(BubblesPin, LOW);
        break;
      case true:
        digitalWrite(BubblesPin, HIGH);
        break;
    }
    switch (LightSts){
      case false:
        digitalWrite(LightPin, LOW);
        break;
      case true:
        digitalWrite(LightPin, HIGH);
        break;
    }
    switch (PumpSts){
      case false:
        digitalWrite(PumpPin, LOW);
        break;
      case true:
        digitalWrite(PumpPin, HIGH);     
        break;
    }
  }
  delay(10);
  yield();
}

Credits

Robert Allan
6 projects • 14 followers
Maker of things.... some get completed....
Contact

Comments

Please log in or sign up to comment.