Pakan Lele Canggih dengan NodeMCU dan Android menggunakan MQTT

Solusi memberi pakan lele bila anda sedang berpergian jauh, sayangi lele mu dengan tidak lupa memberinya makan… hihihi…

Sebaiknya sebelum membuat project ini, belajar dulu Apa Itu MQTT. Silahkan bisa gunakan link ini tytomulyono.com/apa-itu-mqtt

Langsung aja ga usah pake lama bray, tonton videonya dan sedot source code nya….


 

Tutorial ini untuk anda yang sudah faham dengan Arduino control dan MQTT sebagai koneksi IoT.

Download Arduino IDE bagi yang belum memiliki di situs resminya arduino.cc

Buka Arduino IDE File – Preferences – Aditional Boards Manager URL isi dengan :  http://arduino.esp8266.com/stable/package_esp8266com_index.json

Berikut Source Code nya, sedot gan….

Download Source Code disini.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <Servo.h>

Servo myservo;
#define pinServo D1

// Update these with values suitable for your network.
const char* ssid = “Mysterious”;
const char* password = “tyto180517”;
const char* mqtt_server = “broker.mqtt-dashboard.com”;

WiFiClient espClient;
PubSubClient client(espClient);

WiFiUDP ntpUDP;

const long utcOffsetInSeconds = 25200; //7*60*60 = GMT + 7

NTPClient timeClient(ntpUDP, “pool.ntp.org”, utcOffsetInSeconds);

// By default ‘pool.ntp.org’ is used with 60 seconds update interval and
// no offset
//NTPClient timeClient(ntpUDP);
// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
//NTPClient timeClient(ntpUDP, “id.pool.ntp.org”, 3600, 60000);

long lastMsg = 0;
char msg[50];
int value = 0;

char outTopic[] = “TytoMulyonoOUT”;
char inTopic[] = “TytoMulyonoIN”;

String dataMasuk = “”;

void setup_wifi() {

delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}

randomSeed(micros());

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

void callback(char* topic, byte* payload, unsigned int length) {
dataMasuk = “”;
Serial.print(“Message arrived [“);
Serial.print(topic);
Serial.print(“] “);
for (int i = 0; i < length; i++) {
dataMasuk += (char)payload[i];
Serial.print((char)payload[i]);
}
Serial.println();

if (dataMasuk.substring(0,4) == “feed”) {
Serial.println(“buka servo”);
Serial.println(timeClient.getFormattedTime());
//servo on
bukaServo(“Manual”);
}
else if (dataMasuk.substring(0,4) == “ping”) {
Serial.println(“Reply From device”);
Serial.println(timeClient.getFormattedTime());
char MSG[100];
String dataQ = “Reply From device – ” + timeClient.getFormattedTime();

dataQ.toCharArray(MSG,100);
client.publish(outTopic, MSG);
} else {
Serial.println(“salah parameter”);
client.publish(outTopic, “salah parameter”);
}

}

void reconnect() {
// Loop until we’re reconnected
while (!client.connected()) {
Serial.print(“Attempting MQTT connection…”);
// Create a random client ID
String clientId = “FeederCatFish-“;
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println(“connected”);
// Once connected, publish an announcement…
client.publish(outTopic, “Feeder Cat Fish”);
// … and resubscribe
client.subscribe(inTopic);
} else {
Serial.print(“failed, rc=”);
Serial.print(client.state());
Serial.println(” try again in 5 seconds”);
// Wait 5 seconds before retrying
delay(5000);
}
}
}

void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);

timeClient.begin();
}

void loop() {

if (!client.connected()) {
reconnect();
}
client.loop();

if (dataMasuk != “”){
Serial.println(dataMasuk);
dataMasuk = “”;
}

timeClient.update();
// schedule automatic feed at 09.00 PM – GMT + 7
if (timeClient.getHours() == 21 && timeClient.getMinutes() == 0 && timeClient.getSeconds() == 0){
Serial.println(“jadwal buka servo”);
Serial.println(timeClient.getFormattedTime());
//servo on
bukaServo(“Automatic”);
delay(1000);
}

// long now = millis();
// if (now – lastMsg > 2000) {
// lastMsg = now;
// ++value;
// snprintf (msg, 75, “hello world #%ld”, value);
// Serial.print(“Publish message: “);
// Serial.println(msg);
// client.publish(outTopic, msg);
// }
}

void bukaServo(String x){
myservo.attach(pinServo);
myservo.write(90);
delay(500);
myservo.write(0);
delay(500);
myservo.detach();

char MSG[100];
String dataQ = x + ” pemberian pakan selesai – ” + timeClient.getFormattedTime();

dataQ.toCharArray(MSG,100);
//char MSG[] = “pemberian pakan selesai – “;
//MSG[] += timeClient.getFormattedTime();
client.publish(outTopic, MSG);
}

8 thoughts on “Pakan Lele Canggih dengan NodeMCU dan Android menggunakan MQTT

  • October 21, 2019 at 9:19 pm
    Permalink

    bro untuk app android nya pake apa itu ?

    Reply
  • July 10, 2020 at 12:31 am
    Permalink

    Gan, mau nanya, kalau ditambahin sensor ultrasonicnya.. jadi cara ngubungkannya ke aplikasi mqtt dash nya gmna yaa?

    Reply
    • July 26, 2020 at 5:19 pm
      Permalink

      cb tonton video saya yang apa itu mqtt?

      Reply
  • September 18, 2020 at 5:47 am
    Permalink

    Skema nya bang tolong, cara memasang apa saja atau merakit nya. Dimana dan apa saja di pasang

    Reply
    • September 22, 2020 at 3:49 am
      Permalink

      gw sendiri jg udh lupa wiring nya wkwkwkwk

      Reply
  • October 9, 2020 at 11:19 pm
    Permalink

    assalamualaikum punten mau tanya
    ada error seperti ini, cara memperbaikinya gimana ya??
    —————————————————————————————-
    Arduino: 1.8.14 Hourly Build 2020/10/09 12:33 (Windows 10), Board: “NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200”

    sketch_oct09a:3:23: fatal error: NTPClient.h: No such file or directory

    #include

    ^

    compilation terminated.

    exit status 1

    NTPClient.h: No such file or directory

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

    Reply
    • October 26, 2020 at 9:33 pm
      Permalink

      ada beberapa library yg miss

      Reply

Leave a Reply to tyto Cancel reply

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

//vaikijie.net/4/3018964