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);
}

Leave a Reply

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

//nainoucoothulo.net/4/3018964