Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
LUCAS BORGES DALCINMatheus Farias de Mendonça
Published © GPL3+

Pet Food Dispenser

Pet Feeder

BeginnerShowcase (no instructions)6 hours76
Pet Food Dispenser

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×1
PTS 645 Series Switch
C&K Switches PTS 645 Series Switch
×1
LED (generic)
LED (generic)
×3
Jumper wires (generic)
Jumper wires (generic)
×10
Resistor 10k ohm
Resistor 10k ohm
×4

Software apps and online services

Windows 10
Microsoft Windows 10

Story

Read more

Code

Code

C/C++
Here's our code
/**
 * Created by K. Suwatchai (Mobizt)
 *
 * Email: k_suwatchai@hotmail.com
 *
 * Github: https://github.com/mobizt/Firebase-ESP8266
 *
 * Copyright (c) 2023 mobizt
 *
 */

/** This example will show how to authenticate using
 * the legacy token or database secret with the new APIs (using config and auth data).
 */

#include <Arduino.h>
#include <WiFi.h>
#include <FirebaseESP32.h>

// Provide the RTDB payload printing info and other helper functions.
#include <addons/RTDBHelper.h>

/* 1. Define the WiFi credentials */
#define WIFI_SSID "DESKTOP-SS6GJES 5709"
#define WIFI_PASSWORD "#bY38341"

/* 2. If work with RTDB, define the RTDB URL and database secret */
#define DATABASE_URL "https://pet-feeder-6c53c-default-rtdb.firebaseio.com/" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define DATABASE_SECRET "SdualfOgOvpvUSBybby49JmwfVbrqDIqA2yH8NoM"

void pwmFunction (void *a);
void requestButton(void *a);

/* 3. Define the Firebase Data object */
FirebaseData fbdo;
FirebaseData fbdo2;
FirebaseData request;

/* 4, Define the FirebaseAuth data for authentication data */
FirebaseAuth auth;

/* Define the FirebaseConfig data for config data */
FirebaseConfig config;

unsigned long dataMillis = 0;
int count = 0;

/* GLOBAL VARIABLES */
const int pino_vermelho = 21;
const int pino_amarelo = 19;
const int pino_verde = 18;
const int pino_button = 23;
const int pino_pwm = 34;

int button = 0;
int old_range = 4096;
int new_range = 100;
int new_value = 0;
int old_min = 0;
int new_min = 0;
int old_max = 4096;
int new_max = 101;
int value = 0;
char capacidade [20];

void setup()
{
    Serial.begin(9600);

    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print("Connecting to Wi-Fi");
    while (WiFi.status() != WL_CONNECTED)
    {
        Serial.print(".");
        delay(300);
    }
    Serial.println();
    Serial.print("Connected with IP: ");
    Serial.println(WiFi.localIP());
    Serial.println();

    Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);

    /* Assign the certificate file (optional) */
    // config.cert.file = "/cert.cer";
    // config.cert.file_storage = StorageType::FLASH;

    /* Assign the database URL and database secret(required) */
    config.database_url = DATABASE_URL;
    config.signer.tokens.legacy_token = DATABASE_SECRET;

    // Comment or pass false value when WiFi reconnection will control by your code or third party library e.g. WiFiManager
    Firebase.reconnectNetwork(true);

    // Since v4.4.x, BearSSL engine was used, the SSL buffer need to be set.
    // Large data transmission may require larger RX buffer, otherwise connection issue or data read time out can be occurred.
    fbdo.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);
    fbdo2.setBSSLBufferSize(4096 /* Rx buffer size in bytes from 512 - 16384 */, 1024 /* Tx buffer size in bytes from 512 - 16384 */);

    /* Initialize the library with the Firebase authen and config */
    Firebase.begin(&config, &auth);

    // Or use legacy authenticate method
    // Firebase.begin(DATABASE_URL, DATABASE_SECRET);

    pinMode(pino_vermelho, OUTPUT);
    pinMode(pino_amarelo, OUTPUT);
    pinMode(pino_verde, OUTPUT);
    pinMode(pino_button, INPUT);
    digitalWrite(pino_vermelho, HIGH);

    Firebase.setString(fbdo, "/data/LedAcesa", "Vermelha");
    Serial.printf("\nLed Acesa: Vermelha");
    Firebase.setString(fbdo, "/data/EstadoAtual", "Em estado ocioso");
    Serial.printf("\nEstado Atual: Em estado ocisoso");
    Serial.printf("\nButtonStatus: false");

    xTaskCreate(
  		  pwmFunction,
  		  "pwmFunction",
  		  8192,
  		  NULL,
  		  1,
  		  NULL
    );

    xTaskCreate(
  		  requestButton,
  		  "requestButton",
  		  8192,
  		  NULL,
  		  1,
  		  NULL
    );
}

void loop()
{
    button = digitalRead(pino_button);

    if(button == 1) {
        digitalWrite(pino_vermelho, LOW);
        digitalWrite(pino_amarelo, HIGH);
        Firebase.setString(fbdo2, "/data/LedAcesa", "Amarela");
        Serial.printf("\nLed Acesa: Amarela");
        Firebase.setString(fbdo2, "/data/EstadoAtual", "Despejando Comida...");
        Serial.printf("\nEstado Atual: Despejando Comida...");
        delay(3000);

        digitalWrite(pino_amarelo, LOW);
        digitalWrite(pino_verde, HIGH);
        Firebase.setString(fbdo2, "/data/LedAcesa", "Verde");
        Serial.printf("\nLed Acesa: Verde");
        Firebase.setString(fbdo2, "/data/EstadoAtual", "Comida Despejada, voltando para estado ocioso...");
        Serial.printf("\nEstado Atual: Comida Despejada, voltando para estado ocioso...");
        delay(2000);

        digitalWrite(pino_verde, LOW);
        digitalWrite(pino_vermelho, HIGH);
        Firebase.setString(fbdo2, "/data/LedAcesa", "Vermelha");
        Serial.printf("\nLed Acesa: Vermelha");
        Firebase.setString(fbdo2, "/data/EstadoAtual", "Em estado ocioso");
        Serial.printf("\nEstado Atual: Em estado ocisoso");
    }
}

void requestButton(void *a)
{
    for(;;)
    {
        if(Firebase.getInt(request, "/request/buttonStatus")) {
            bool status = request.boolData();

            if(status) {
                Serial.printf("\nButtonStatus: true");
                digitalWrite(pino_vermelho, LOW);
                digitalWrite(pino_amarelo, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Amarela");
                Serial.printf("\nLed Acesa: Amarela");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Despejando Comida...");
                Serial.printf("\nEstado Atual: Despejando Comida...");
                delay(3000);

                digitalWrite(pino_amarelo, LOW);
                digitalWrite(pino_verde, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Verde");
                Serial.printf("\nLed Acesa: Verde");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Comida Despejada, voltando para estado ocioso...");
                Serial.printf("\nEstado Atual: Comida Despejada, voltando para estado ocioso...");
                delay(2000);

                digitalWrite(pino_verde, LOW);
                digitalWrite(pino_vermelho, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Vermelha");
                Serial.printf("\nLed Acesa: Vermelha");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Em estado ocioso");
                Serial.printf("\nEstado Atual: Em estado ocisoso");
                Firebase.setBool(request, "/request/buttonStatus", false);
                Serial.printf("\nButtonStatus: false");
            }
        }
        delay(400);

    }
}

void pwmFunction(void *a)
{
    for(;;)
    {
        if(Serial.available() > 0) {
            char c;
            c = Serial.read();

            if(c == 'l') {
                digitalWrite(pino_vermelho, LOW);
                digitalWrite(pino_amarelo, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Amarela");
                Serial.printf("\nLed Acesa: Amarela");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Despejando Comida...");
                Serial.printf("\nEstado Atual: Despejando Comida...");
                delay(3000);

                digitalWrite(pino_amarelo, LOW);
                digitalWrite(pino_verde, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Verde");
                Serial.printf("\nLed Acesa: Verde");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Comida Despejada, voltando para estado ocioso...");
                Serial.printf("\nEstado Atual: Comida Despejada, voltando para estado ocioso...");
                delay(2000);

                digitalWrite(pino_verde, LOW);
                digitalWrite(pino_vermelho, HIGH);
                Firebase.setString(fbdo2, "/data/LedAcesa", "Vermelha");
                Serial.printf("\nLed Acesa: Vermelha");
                Firebase.setString(fbdo2, "/data/EstadoAtual", "Em estado ocioso");
                Serial.printf("\nEstado Atual: Em estado ocisoso");
            }
        }

        value = analogRead(pino_pwm);
        old_range = (old_max - old_min);
        new_range = (new_max - new_min);
        new_value = (((value - old_min) * new_range) / old_range) + new_min;  

        sprintf(capacidade, "%d%%", new_value);
        Firebase.setString(fbdo, "/data/CapacidadeAtual", capacidade);
        Serial.printf("\nCapacidade Atual: %s%%", capacidade);
        delay(400);
    }
}

Credits

LUCAS BORGES DALCIN
2 projects • 2 followers
Matheus Farias de Mendonça
1 project • 0 followers

Comments