Sourcecode pakan lele otomatis
#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);
}
Comments
Post a Comment