Project tutorial
Aquarium Assistant

Aquarium Assistant © MIT

Control your fish tank, feed your fish, and more from anywhere in the world with the help of WIZ750SR and Google Assistant.

  • 1,631 views
  • 0 comments
  • 8 respects

Components and supplies

Apps and online services

About this project

Intro

I have always wanted to control my fish tank with a simple low cost and power usage hardware supporting voice command from wherever I am whether I am travailing or at work instead of forgetting to feed my fish every other day and the WIZ750SR seemed a perfect solution.

In this tutorial, we will learn how to control the fish tank lights and feeding the fish with voice commands through Google Assistant with Adafruit I/O MQTT & IFTTT protocols.

Demo

Aquarium lights

Feeding the fish

Closer look on the fish feeding

1: Set Up Adafruit MQTT

First of all, we need an Adafruit account after loging in create a new feed.

  • Go to Feeds > Actions > Create a New Feed
  • Enter a name and description and click create
  • Then Go to Dashboards > Actions > Create a Dashboard
  • Also enter a name and description and click create
  • Go to Dashboards > the one you created and click on the blue add icon to create a new block and choose slider
  • Edit the block settings so the slider values are from 0-4 (2 lights on/off and 1 for feeder)
Copy your credentials (username and password) of io.adafruit to the mbed.org code in the next step
  • press the yellow key to get these data

2: Set Up Mbed

The project is provided below, but I will explain the main parts briefly.

The code on Mbed os is simply Uding the MQTT library ( if you don't know what is MQTT it's messaging protocol where one device subscribe to a certain topic and another publish to all subscribers )

  • inside the main function

1- we subsribe to the topic "******/feeds/welcome-feed" from adafruit

2- connect to the host name with the username and password from last step

char* topic1 = "******/feeds/welcome-feed"; //******  type your username on IFTT instead of the stars 
char* hostname = "io.adafruit.com";//your MQTT broker host
   int port = 1883; // broker port number
data.clientID.cstring = MQTTClientID;
   data.username.cstring = "*******";   //******  type your username on IFTT instead of the stars
   data.password.cstring = "************************";   //******  type your Password on IFTT instead of the stars
   if ((rc = client.connect(data)) != 0)
       printf("rc from MQTT connect is %d\n", rc);
   if ((rc = client.subscribe(topic1, MQTT::QOS0, messageArrived)) != 0) {
       out.printf("rc from MQTT subscribe is %d\n", rc);
   }
  • In the messageArrived function we check the message received from the Adafruit io and based on the message we send another through serial communication to the Arduino
void messageArrived(MQTT::MessageData& md)
{
   MQTT::Message &message = md.message;
   out.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
   out.printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
   s.printf("%.*s\n", message.payloadlen, (char*)message.payload);//To send serial data to arduino
   int    i = atoi((char*)message.payload);
   switch(i) {
       case 0:   //start servo to feed the fish
           out.printf("Feeding");
           s.printf("Feed");
           break;
       case 1:    //turn lights on
           out.printf("Lights on");
           s.printf("ON");
           break;
       case 2:    //turn lights off
           out.printf("Lights off");
           s.printf("OFF");
           break;
       case 3:    //turn Dancing led strip on
           out.printf("Led Flashing");
           s.printf("Dance");
           break;
       case 4:    //turn Dancing led strip off
           out.printf("Led Not Flashing");
           s.printf("NoDance");
           break;
   }
}

click Compile and download the bin file to the Wiz750sr through the w7500 Isp tool make sure you tick "Erase all code Block" in order not to re-assign your mac address choose the com port and browse the binary file directory then click ISP start

3: Set Up Arduino Nano

After connecting the arduino as the schematic and uploading the code it should now after recieving the message turn on/off the relay by digitalWrite function

Note: I have reversed the relay output unintentionally so it's in my case normally closed in other word digitalWrite LOW make it turn on and vise versa nothing important but incase you find it strange

else if (res == "ON") {
     Serial.println("Lights ON");
     //realy 1 on
     digitalWrite(10, LOW);   // turn the Relay on
     delay(1000);                       // wait for a second
   }
   else if (res == "OFF") {
     Serial.println("Lights OFF");
     //realy 1 off
     digitalWrite(10, HIGH);   // turn the Relay on 
     delay(1000);                       // wait for a second
   }
   else if (res == "Dance") {
     Serial.println("Dancing Lights ON");
     //realy 2 on
     digitalWrite(11, LOW);   // turn the Relay on 
     delay(1000);                       // wait for a second
   }
   else if (res == "NoDance") {
     Serial.println("Dancing lights OFF");
     //realy 2 off
     digitalWrite(11, HIGH);   // turn the Relay on 
     delay(1000);                       // wait for a second
   }
  • The other function is rotateServo(3) rotate the motor 3 times to ensure food has fallen in the tank
if (mySerial.available()) {
   String res = mySerial.readString();
   if (res == "Feed") {
     Serial.println("Feeding ur fish NOW");
     rotateServo(3);
   } 

4: Set Up IFTT

  • Sign up and login to IFTT platform then create a new applet as follows
  • Go to this link > select ‘+ this’ and Search for ‘Google Assistant’ then choose a trigger select ‘Say a simple phrase’

Fill in what you want to say and what should be the reply , choose 'English' language Then click on ‘Create trigger’

Now select ‘+that’

In choose action service page, search for ‘Adafruit’, connect to your adafruit account and authorize it

Choose action ‘Send data to Adafruit IO’ and Fill in your Feed name , Data to save , choose a number within the slider's value 0-4 , Then ‘Create action’ then click ‘Finish’ and Make sure your applet is turned ON.

Follow these steps again for the other actions (turning lights off , Feeding the Fish) with differnet slider value Now Let's Test that everything is working

5: Testing Our MQTT Broker

mqttfx is an MQTT Client you can use to test in case the Publish-Subscribe protocol is new to you by opening 2 istanstence of the program and write the same topic with same broker address as "iot.eclipse.org", port "1883" but of course different client IDs and connect write anything to in the publish and send it the subscriber instance should receive it successfully try it again with adafruit data to confirm everything is correct Finally test using your android phone try asking Google Assistant to feed your fish and you should see the number you have written for this action in the subscriber's messages

6: Set Up Fish Tank Servo Motor, Relay for Lights

After making sure everything is working well on the software end let get our hardware Ready

Mount the servo to a plastic cup that will be our fish feeder and connect the Relay to the output as in the picture , I have made this connection in-order not to damage the original cables of the Led Light strips and Aquarium Lamp.

Connecting the relay module

A more detailed schematic of the connection

7: Testing the Voice Control with Google Assistant

Testing the relay

Testing the feeder servo

Working Project

Link to the working project (as many MQTT libraries were causing problem compiling till I found this one working so I published it to easier than just main.c file).

https://os.mbed.com/users/Fady/code/Aquarium_Assistant/

Hope you liked the project! If you need any help creating yours, don't hesitate to ask here in the comments and I will be more than happy to help! Thanks!

Code

w750sr C/C++
Wiznet w750sr mbed.org main.c file
#include "mbed.h"
#include "MQTTEthernet.h"
#include "MQTTClient.h"
#include <sstream>
#include <string>

Serial s(PA_13,PA_14); // Communication with the Arduino Board

Serial out(USBTX,USBRX);  // Debuging through USB

void messageArrived(MQTT::MessageData& md)
{
    MQTT::Message &message = md.message;
    out.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id);
    out.printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
    s.printf("%.*s\n", message.payloadlen, (char*)message.payload);//To send serial data to arduino
    int    i = atoi((char*)message.payload);
    switch(i) {
        case 0:   //start servo to feed the fish
            out.printf("Feeding");
            s.printf("Feed");
            break;
        case 1:    //turn lights on
            out.printf("Lights on");
            s.printf("ON");

            break;
        case 2:    //turn lights off
            out.printf("Lights off");
            s.printf("OFF");

            break;
        case 3:    //turn Dancing led strip on
            out.printf("Led Flashing");
            s.printf("Dance");

            break;
        case 4:    //turn Dancing led strip off
            out.printf("Led Not Flashing");
            s.printf("NoDance");

            break;

    }
}

void baud(int baudrate)
{

    s.baud(baudrate);
}




int main(void)
{

    char* topic1 = "******/feeds/welcome-feed";       //******  type your username on IFTT instead of the stars 


    printf("starting...\n");


    MQTTEthernet ipstack = MQTTEthernet();

    MQTT::Client<MQTTEthernet, Countdown> client = MQTT::Client<MQTTEthernet, Countdown>(ipstack);

    char* hostname = "io.adafruit.com";//your MQTT broker host
    int port = 1883; // broker port number

    int rc = ipstack.connect(hostname, port);

    out.printf("rc from TCP connect is %d\n", rc);

    if (rc != 0)
        out.printf("rc from TCP connect is %d\n", rc);

    char MQTTClientID[30];
    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
    data.MQTTVersion = 3;
    sprintf(MQTTClientID,"WIZwiki-%d",rand()%1000);

    data.clientID.cstring = MQTTClientID;
    data.username.cstring = "*******";   //******  type your username on IFTT instead of the stars
    data.password.cstring = "************************";   //******  type your Password on IFTT instead of the stars
    if ((rc = client.connect(data)) != 0)
        printf("rc from MQTT connect is %d\n", rc);


    if ((rc = client.subscribe(topic1, MQTT::QOS0, messageArrived)) != 0) {
        out.printf("rc from MQTT subscribe is %d\n", rc);


    }

    while (true) {



        client.yield(6);

    }
}
Adruino CodeC/C++
Adruino Code to control lights , led strip and servo motor
#include <SoftwareSerial.h>
#include <Servo.h>

Servo myservo;
int pos = 0;

SoftwareSerial mySerial(8, 9); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  myservo.attach(A5); //analog pin 0
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Welcome to Aquaruim Assistant");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  // mySerial.println("Hello, world?");

  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);

}

void loop() { // run over and over
  if (mySerial.available()) {
    String res = mySerial.readString();
    if (res == "Feed") {
      Serial.println("Feeding ur fish NOW");
      rotateServo(3);
    }
    else if (res == "ON") {
      Serial.println("Lights ON");
      //realy 1 on
      digitalWrite(10, LOW);   // turn the Relay on
      delay(1000);                       // wait for a second

    }
    else if (res == "OFF") {
      Serial.println("Lights OFF");
      //realy 1 off
      digitalWrite(10, HIGH);   // turn the Relay on 
      delay(1000);                       // wait for a second

    }
    else if (res == "Dance") {
      Serial.println("Dancing Lights ON");
      //realy 2 on
      digitalWrite(11, LOW);   // turn the Relay on 
      delay(1000);                       // wait for a second

    }
    else if (res == "NoDance") {
      Serial.println("Dancing lights OFF");
      //realy 2 off
      digitalWrite(11, HIGH);   // turn the Relay on 
      delay(1000);                       // wait for a second

    }

  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}

void rotateServo(int rotations) {
  for (int i = 0; i < rotations; i++) {
    for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
      // in steps of 1 degree
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
  }
}

Schematics

Aquarium Assistant Connection
Schematic for connecting the Wiz750sr with arduino , Relay and servo
Unadas exsmqus09v

Comments

Similar projects you might like

Home Automation Using Zone Beacon via Android App

Project tutorial by SomeRandomTeam

  • 1,863 views
  • 2 comments
  • 14 respects

Making a Cheap Aquarium Parameters Controller

Project tutorial by Boisse Martin

  • 5,025 views
  • 3 comments
  • 16 respects

Aquarium Controlling and Monitoring Using Arduino + 1Sheeld

Project tutorial by Khaled Mohamed

  • 3,057 views
  • 1 comment
  • 15 respects

Home Automation Using Google Assistant

Project showcase by Pawan Kumar

  • 9,702 views
  • 12 comments
  • 41 respects

Personal Home Assistant

Project tutorial by 3 developers

  • 4,705 views
  • 0 comments
  • 29 respects

Candy Dispenser with Google Assistant

Project tutorial by Arduino “having11” Guy

  • 15,487 views
  • 1 comment
  • 51 respects
Add projectSign up / Login