Skip to content

Instantly share code, notes, and snippets.

@kakopappa
Created August 21, 2024 01:18
Show Gist options
  • Save kakopappa/626952578a3a04bd8fe414df521ec601 to your computer and use it in GitHub Desktop.
Save kakopappa/626952578a3a04bd8fe414df521ec601 to your computer and use it in GitHub Desktop.
cat-feeder-esp32.ino
#include "freeRTOS\freeRTOS.h"
#include "freeRTOS\task.h"
#include <ESP32Servo.h>
#define ENABLE_DEBUG
#ifdef ENABLE_DEBUG
#define DEBUG_ESP_PORT Serial
#define NODEBUG_WEBSOCKETS
#define NDEBUG
#endif
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32) || defined(ARDUINO_ARCH_RP2040)
#include <WiFi.h>
#endif
#include "SinricPro.h"
#include "SinricProSwitch.h"
#define WIFI_SSID ""
#define WIFI_PASS ""
#define APP_KEY ""
#define APP_SECRET ""
#define SWITCH_ID_1 ""
#define SERVO_PIN 17
#define BUZZER_PIN 16
#define BAUD_RATE 115200 // Change baudrate to your need
Servo myservo;
void setupWiFi() {
Serial.printf("\r\n[Wifi]: Connecting");
#if defined(ESP8266)
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.setAutoReconnect(true);
#elif defined(ESP32)
WiFi.setSleep(false);
WiFi.setAutoReconnect(true);
#endif
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
Serial.printf(".");
delay(250);
}
Serial.printf("connected!\r\n[WiFi]: IP-Address is %s\r\n", WiFi.localIP().toString().c_str());
}
void setupBuzzer() {
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
}
void setupServo() {
myservo.attach(SERVO_PIN);
myservo.write(90); // Initialize the servo to 90 degrees (stopped)
}
// Beeper task function
void beeperTask(void *parameter) {
digitalWrite(BUZZER_PIN, HIGH);
vTaskDelay(800 / portTICK_PERIOD_MS);
digitalWrite(BUZZER_PIN, LOW);
vTaskDelay(100 / portTICK_PERIOD_MS);
digitalWrite(BUZZER_PIN, HIGH);
vTaskDelay(800 / portTICK_PERIOD_MS);
digitalWrite(BUZZER_PIN, LOW);
vTaskDelay(100 / portTICK_PERIOD_MS);
digitalWrite(BUZZER_PIN, HIGH);
vTaskDelay(800 / portTICK_PERIOD_MS);
digitalWrite(BUZZER_PIN, LOW);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
// Servo task function
void servoTask(void *parameter) {
myservo.write(40); // Start rotating the servo
// Delay for 4 seconds
vTaskDelay(4000 / portTICK_PERIOD_MS);
// 4 seconds elapsed, move servo to the neutral position (stopped)
myservo.write(90);
}
void startCatFeederTasks() {
// Create tasks
xTaskCreate(
beeperTask, // Task function
"BeeperTask", // Task name
2048, // Stack size (bytes)
NULL, // Task parameters
1, // Task priority
NULL // Task handle
);
xTaskCreate(
servoTask, // Task function
"ServoTask", // Task name
2048, // Stack size (bytes)
NULL, // Task parameters
1, // Task priority
NULL // Task handle
);
Serial.println("Tasks started!");
}
// Callback function that is called when the switch is toggled in the Sinric Pro app
bool onPowerState(const String &deviceId, bool &state) {
Serial.printf("Device %s turned %s\r\n", deviceId.c_str(), state ? "ON" : "OFF");
startCatFeederTasks();
return true; // Return true to confirm the command was processed successfully
}
// setup function for SinricPro
void setupSinricPro() {
SinricProSwitch &mySwitch1 = SinricPro[SWITCH_ID_1];
mySwitch1.onPowerState(onPowerState);
// setup SinricPro
SinricPro.onConnected([]() {
Serial.printf("Connected to SinricPro\r\n");
});
SinricPro.onDisconnected([]() {
Serial.printf("Disconnected from SinricPro\r\n");
});
SinricPro.begin(APP_KEY, APP_SECRET);
}
// main setup function
void setup() {
Serial.begin(BAUD_RATE);
Serial.printf("\r\n\r\n");
setupWiFi();
setupServo();
setupBuzzer();
setupSinricPro();
}
void loop() {
SinricPro.handle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment