5.1 Implementation
5.1.1 Arduino IDE
This project uses the arduino IDE application. This application
used to compile and to upload the program. The program can be seen
as below:
1. Libraries.
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <Servo.h>
#include <SPI.h>
#include <SD.h>
2. Setting the name and the password of WiFi.
const char* ssid = "redmiku";
const char* password = "123456789";
3. Setting the name of server (thingspeak and IP laptop/PC), setting
channel of IoT, and setting API of IoT.
unsigned long myChannelNumber = 156758;
const char * myWriteAPIKey = "R9VHNWY87JFE23QW";
const char* server = "api.thingspeak.com";
const char* server1 = "192.168.43.171"; //ip laptop
4. Declaring variable and pinout will be used.
const int chipSelect = D4; //pin of micro SD module
int trigAir =D0; // pin trigger of the HC-SR04
int echoAir =D1; // pin echo of HC-SR04
int tinggiAir; //used to save the water level
int tinggiEmber=21; // set the height of aquarium
int trigPin =D2; // pin trigger of the HC-SR04
int echoPin =D3; // pin echo of HC-SR04
float volume; // used to save the volume of fish feed can
int tinggiPenampung=15; // set the height of fish feed can
int tinggiPakan; // used to save the height of fish feed
int buzzer =D2; // pin of buzzer
5. Setting transfer rate to serial and setting sensor as input/output.
Serial.begin(115200);
6. Declaring micro servo variable and pin will be used. Setting the
angle of micro servo.
myservo1.attach(D8); //servo penampung
myservo1.write(0);
7. Connecting to WiFi.
Serial.println();
Serial.print("Connecting");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
8. Detecting micro sd memory.
if (!SD.begin(chipSelect))
{
Serial.println("Card does not exists");
// don't do anything more:
return;
}
Serial.println("card initialized.");
9. Detecting file jam.txt. The time.txt used to setting the clock.
File dataFile3 = SD.open("time.txt");
// if the file is available, write to it:
Serial.println(data1);
Serial.println(data3);
Serial.println(data4);
Serial.println(data5);
Serial.println(data6);
setTime(data1,data2,data3,data4,data5,data6); //setting the
clock
}
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening time.txt");
}
dataFile3.close();
10. Detecting alarm.txt. The alarm.txt used to schedule. The schedule
used to open and to close the door of fish feed can.
File dataFile = SD.open("alarm.txt");
// if the file is available, write to it:
if (dataFile) {
while (dataFile.available()) {
int dataalarm1 = dataFile.parseInt(); // ambil data ke-1
dan parsing ke int
int dataalarm2 = dataFile.parseInt(); // ambil data ke-2
dan parsing ke int
int dataalarm3 = dataFile.parseInt(); // ambil data ke-3
dan parsing ke int
int dataalarm1 = dataalarm1;
Serial.println(dataalarm1);
Serial.println(dataalarm2);
Serial.println(dataalarm3);
Serial.println(dataalarm4);
Serial.println(dataalarm5);
Serial.println(dataalarm6);
Alarm.alarmRepeat(dataalarm1,dataalarm2,dataalarm3,
Penampung); /*alarm tiap hari*/
Alarm.alarmRepeat(dataalarm4,dataalarm5,dataalarm6,
Penampung); /*alarm tiap hari*/
}
}
else {
Serial.println("error opening alarm.txt");
}
dataFile.close();
11. Setting count of micro servo when opened and closed.
void Penampung()
{
Serial.println("SERVO BUKAA");
myservo1.write(70);
delay(550); //DELAY 0.5 DETIK
delay(550);
12. The measurement of the volume of fish feed can and the water
level.
void loop() {
long duration, distance;
long durationAir, distanceAir;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
volume = 3.14*8.6*8.6*tinggiPakan;
digitalWrite(trigAir, LOW);
delayMicroseconds(2);
digitalWrite(trigAir, HIGH);
delayMicroseconds(10);
digitalWrite(trigAir, LOW);
durationAir = pulseIn(echoAir, HIGH);
distanceAir = (durationAir/2) / 29.1;
tinggiAir = tinggiEmber-distanceAir;
Serial.print("\n");
if (distance>=16){
distance=0;
tone(buzzer,1000);
}
if (distance >= 30 || distance <= 0){
Serial.println("Jarak diluar jangkauan!");
}
else {
Serial.print("jarak= ");
Serial.print(distance);
Serial.println(" cm");
Serial.print("Tinggi Pakan= ");
Serial.print(tinggiPakan);
Serial.print(" cm");
Serial.print("Volume= ");
Serial.print(volume);
Serial.println("cm3");
noTone(buzzer);
}
if (distanceAir > 21 || distanceAir <= 0){
distanceAir = 0;
Serial.println("Jarak diluar jangkauan!");
}
else {
Serial.print("jarak= ");
Serial.print(distanceAir);
Serial.println(" cm");
Serial.print("Tinggi Air= ");
Serial.print(tinggiAir);
Serial.println(" cm\n");
}
13. Setting the field will be sent to IoT server.
ThingSpeak.setField(1,volume);
14. Sending data (volume, date, month, year, hour, minute, second,
the water level) to a computer using HTTP protocol.
if (client.connect(server1, 80)) {
client.print("GET /tes/coba.php?"); // the address file to save in
txt file
client.print("volume=");
client.print(volume);
client.print("&&");
client.print("tanggal=");
client.print(day());
client.print("&&");
client.print("bulan=");
client.print(month());
client.print("&&");
client.print("tahun=");
client.print(year());
client.print("&&");
client.print("jam=");
client.print(hour());
client.print("&&");
client.print("menit=");
client.print(minute());
client.print("&&");
client.print("detik=");
client.print(second());
client.print("&&");
client.print("tinggiAir=");
client.print(tinggiAir);
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 192.168.43.171"); // IP address laptop
telling the server that we are over transmitting the message
// If Arduino can't connect to the server (your computer or web
page)
Serial.println("--> connection failed\n");
}
15. Sending data (volume and the water level) to IoT server.
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(15000); // ThingSpeak will only accept updates every 15
seconds.
5.1.2 PHP File
In this project PHP file use to get the data (volume, date, month,
year, hour, minute, second, the water level) from sensor. After that,
save the data to txt file.
<html>
<body>
<?php
if(isset($_GET["volume"]) && isset($_GET["tanggal"]) &&
isset($_GET["bulan"])&& isset($_GET["tahun"]) && isset($_GET["jam"])
&& isset($_GET["menit"]) && isset($_GET["detik"]) &&
$jam = $_GET["jam"];
$menit = $_GET["menit"];
$detik = $_GET["detik"];
$tinggiAir = $_GET["tinggiAir"];
$file = fopen("coba.txt","a"); //a untuk menambah data,
w untuk menulis baru
if($file)
{
fputs($file,$tanggal."-".$bulan."-".$tahun."\n");
fputs($file,$jam.":".$menit.":".$detik."\n");
fputs($file,"Volume Pakan=".$volume."\n");
fputs($file,"Tinggi Air=".$tinggiAir."\n\n");
}
fclose($file);
}
else{
echo "data tidak ada <br>";
}
echo "<b></b>Isi dari file coba.txt sekarang:</b><br>";
$file = fopen("coba.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
</body>
5.2 Testing
The figure below shows how the nodeMCU connected to the WiFi,
the nodeMCU detected the micro SD memory, the nodeMCU detected
the time.txt and the alarm.txt, the nodeMCU reading the HC-SR04
sensor in measuring the distance, the nodeMCU reading the volume,
the nodeMCU reading the water level, the volume and the water level
in txt file, the volume and the water level in IoT server.
1. Connecting to the WiFi.
When the nodeMCU connected to the WiFi, the nodeMCU
will get an IP address. The IP address will be print in serial monitor.
2. Detecting the micro SD Memory.
When the micro SD memory detected, the serial monitor will
be print “card initialized”.
Figure 9: Detecting Micro SD Memory
3. Detecting the time.txt and the alarm.txt.
The time.txt used to set the time in nodeMCU. The alarm.txt
used to set the schedule to open and to close the door of fish feed
can. When the time.txt and the alarm.txt detected, the serial monitor
will be print the contain of time.txt and the contain of the alarm.txt.
4. Setting the time.
After detected the time.txt in micro SD memory, the time will
be set. The time will be loop in this program.
5. Measuring the distance using HC-SR04 sensor.
The HC-SR04 sensor will read the distance of fish feed can
and read the distance of the water level. The distance will be print in
serial monitor (jarak). The height of fish feed will be print too in
serial monitor. To determine the height of fish feed, this program
setting the height of canned fish feed beforehand. After that, the
height of canned fish feed minus the distance. To calculating
volume of fish feed can, this program using tube volume formula.
After that the volume will be print in serial monitor.
The distance and the water level will be print in serial
monitor. But to know the water level this program set the height of
aquarium. After that the height if aquarium minus the distance.
6. Saving the volume and the water level in txt file.
The volume and the water level will be saving in txt file via
XAMPP every 25 second.
Figure 13: Saving the volume and the water level in txt file
7. Saving the volume and the water level in IoT server (thingspeak).
The volume and the water level will be saving in IoT server
every 25 second.