#include <WiFi.h>
#include <ESP32Servo.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ThingsBoard.h>
#include <RTClib.h>
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// See https://thingsboard.io/docs/getting-started-guides/helloworld/
// to understand how to obtain an access token
#define TOKEN "DctUYKRTm8NN4CaJNFYv"
#define THINGSBOARD_SERVER "labiot.my.id"
// Baud rate for debug serial
#define SERIAL_DEBUG_BAUD 115200
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define buzzerPIN 4
#define servoPin 5
#define pushBtn 27
#define Trig 13
#define Echo 12
float jarak = 0;
float tinggi_pakan = 0;
float volume_pakan = 0;
const char* status_pakan = "";
const char* isTimeFeed = "";
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
Servo myservo;
int val=0;
int a = true;
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect() {
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void baca_jarak()
{
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
int duration = pulseIn(Echo, HIGH);
jarak = duration * 0.034 / 2;
tinggi_pakan = 400 - jarak;
//Volume Kotak Pakan
volume_pakan = 2*4*tinggi_pakan;
//Serial.print("Jarak: ");
//Serial.print(jarak);
//Serial.println(" CM");
}
void setup() {
// initialize serial for debugging
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
} else {
Serial.println("RTC started successfully");
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(2, 2);
// Display static text
display.println("starting...");
display.display();
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(buzzerPIN, OUTPUT);
pinMode(pushBtn, INPUT_PULLUP);
myservo.attach(servoPin);
}
void doScreenStuff (TimeSpan now, float jarak, String status_pakan) {
display.clearDisplay();
display.setCursor(0,2);
display.print(" Time: ");
display.print(now.hours());
display.print(":");
display.print(now.minutes());
display.print(":");
display.println(now.seconds());
display.print(" Jarak: ");
display.println(jarak);
display.print(" Status: ");
display.println(status_pakan);
display.print(" Ket: ");
display.print(tinggi_pakan);
display.println(" cm");
display.print(" Jumlah: ");
display.print(volume_pakan);
display.println(" cm3");
display.display();
}
void loop() {
delay(2000);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
Serial.println("Failed to connect");
return;
}
}
DateTime now = rtc.now();
TimeSpan time = TimeSpan(0, now.hour(), now.minute(), now.second());
baca_jarak();
Serial.println("Sending data...");
if(volume_pakan < 1){
status_pakan = "Pakan habis!";
tone(buzzerPIN, 1000);
delay(200);
tone(buzzerPIN, 800);
delay(200);
noTone(buzzerPIN);
tb.sendTelemetryString("Status Pakan", status_pakan);
}else{
status_pakan = "Pakan ada!";
tb.sendTelemetryString("Status Pakan", status_pakan);
}
tb.sendTelemetryFloat("Tinggi Pakan", tinggi_pakan);
tb.sendTelemetryFloat("Jumlah Pakan", volume_pakan);
doScreenStuff(time,jarak,status_pakan);
Serial.print("Value a : ");
Serial.println(a);
Serial.print("Value val : ");
Serial.println(val);
if(now.second() == 5){
if(a == true){
val = val+90;
myservo.write(val);
isTimeFeed = "Makan Pagi!";
tb.sendTelemetryString("isTimeFeed", isTimeFeed);
tone(buzzerPIN, 1000);
delay(200);
tone(buzzerPIN, 800);
delay(200);
noTone(buzzerPIN);
delay(2500);
myservo.write(0);
}
if(val == 90){
a = false;
}
}else{
myservo.write(0);
if(val == 90){
val = 0;
a = true;
}
}
if(now.second() == 15){
if(a == true){
val = val+90;
myservo.write(val);
isTimeFeed = "Makan Siang!";
tb.sendTelemetryString("isTimeFeed", isTimeFeed);
tone(buzzerPIN, 1000);
delay(200);
tone(buzzerPIN, 800);
delay(200);
noTone(buzzerPIN);
delay(2500);
myservo.write(0);
}
if(val == 90){
a = false;
}
}else{
myservo.write(0);
if(val == 90){
val = 0;
a = true;
}
}
if(now.second() == 45){
if(a == true){
val = val+90;
myservo.write(val);
isTimeFeed = "Makan Malam!";
tb.sendTelemetryString("isTimeFeed", isTimeFeed);
tone(buzzerPIN, 1000);
delay(200);
tone(buzzerPIN, 800);
delay(200);
noTone(buzzerPIN);
delay(2500);
myservo.write(0);
}
if(val == 90){
a = false;
}
}else{
myservo.write(0);
if(val == 90){
val = 0;
a = true;
}
}
if(digitalRead(pushBtn) == LOW){
if(a == true){
val = val+90;
myservo.write(val);
isTimeFeed = "Makan jam 5 sore!";
tb.sendTelemetryString("isTimeFeed", isTimeFeed);
tone(buzzerPIN, 1000);
delay(200);
tone(buzzerPIN, 800);
delay(200);
noTone(buzzerPIN);
delay(2000);
myservo.write(0);
}
if(val == 90){
a = false;
}
}else{
myservo.write(0);
if(val == 90){
val = 0;
a = true;
}
}
tb.loop();
}