Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autofeeding(wifi control, stepper motor) #154

Open
P-inc opened this issue Oct 5, 2023 · 0 comments
Open

Autofeeding(wifi control, stepper motor) #154

P-inc opened this issue Oct 5, 2023 · 0 comments

Comments

@P-inc
Copy link

P-inc commented Oct 5, 2023

 #include <Stepper.h>
 #include <WiFi.h>
 #include <NTPClient.h>
 #include <WiFiUdp.h>
 #include <ESPAsyncWebServer.h> 
 #include <Arduino.h>

 const char* ssid = "xx; // Enter WiFi network name here
 const char* password = "xx"; // Enter WiFi password here

 const int stepsPerRevolution = 2037; // Number of steps in one revolution of the stepper motor

 Stepper stepper(stepsPerRevolution, 19, 18, 5, 17); // Create step motor object

 AsyncWebServerserver(80);

 // NTP settings
 WiFiUDP ntpUDP;
 NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 32400, 60000);

 const unsigned long feedingInterval = 60000;
 unsigned long lastFeedingTime = 0;

 struct FeedingSchedule {
    int hour;
    int minute;
 };

 FeedingSchedule feedingSchedule[] = {
   {8, 0}, // Example: 8 o'clock in the morning
    {12, 30}, // Example: Lunch 12:30
    {18, 0} // Example: 6pm
 };

 const int numberOfSchedules = sizeof(feedingSchedule) / sizeof(feedingSchedule[0]);

 // Set pay cycle and time
 unsigned long feedingPeriod = 60000; // Default value: 1 minute    
 int userSetHour = 12;
 int userSetMinute = 0;

 bool isFeeding = false; // Whether or not you are being paid

 void startFeeding() {
     isFeeding = true;
    for (int i = 0; i < stepsPerRevolution; i++) {
      stepper.step(1); // Move one step at a time (clockwise)
      delay(5); // Delay between each step
    }
    stopFeeding(); //Stop the stepper motor when pay is finished  
 }

 void stopFeeding() {
  isFeeding = false;
  // Stop the step motor or perform the desired action.
 }

 void setup() {
    Serial.begin(115200);
    delay(10);

  Serial.println();
  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  timeClient.begin();
  timeClient.setTimeOffset(32400);
  timeClient.update();

  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    String html = "<html><head><title>Auto Feeder</title></head><body>";
    html += "<h1>Auto Feeder</h1>";
    html += "<p>Current time: " + timeClient.getFormattedTime() + "</p>";

    html += "<p>Feeding every " + String(feedingPeriod / 1000) + " seconds</p>";

    html += "<form action='/set-period' method='POST'>";
    html += "<label for='period'>Feeding Period (seconds):</label>";
    html += "<input type='number' id='period' name='period' min='1' value='" + String(feedingPeriod / 1000) + "'>";
    html += "<input type='submit' value='Set Feeding Period'>";
    html += "</form>";

    html += "<form action='/set-time' method='POST'>";
    html += "<label for='hour'>Hour:</label>";
    html += "<input type='number' id='hour' name='hour' min='0' max='23' value='" + String(userSetHour) + "'>";
    html += "<label for='minute'>Minute:</label>";
    html += "<input type='number' id='minute' name='minute' min='0' max='59' value='" + String(userSetMinute) + "'>";
    html += "<input type='submit' value='Set Feeding Time'>";
    html += "</form>";

    html += "<form action='/start-feeding' method='POST'>";
    html += "<input type='submit' value='Start Feeding'>";
    html += "</form>";

    html += "<form action='/stop-feeding' method='POST'>";
    html += "<input type='submit' value='Stop Feeding'>";
    html += "</form>";

    html += "</body></html>";
    request->send(200, "text/html", html);
  });

   //Handler to handle payroll cycle settings
   server.on("/set-period", HTTP_POST, [](AsyncWebServerRequest *request) {
     if (request->hasParam("period", true)) {
       feedingPeriod = request->getParam("period", true)->value().toInt() * 1000; // Convert seconds to milliseconds
     }
     request->redirect("/");
   });

  //Handler to handle setting payroll hours
  server.on("/set-time", HTTP_POST, [](AsyncWebServerRequest *request) {
    if (request->hasParam("hour", true) && request->hasParam("minute", true)) {
      userSetHour = request->getParam("hour", true)->value().toInt();
      userSetMinute = request->getParam("minute", true)->value().toInt();
    }
     request->redirect("/");
  } );

  // The rest of the web interface code remains the same.

  server.begin();

}

void loop() {
   unsigned long currentTime = millis();
   if (isFeedingTime() && currentTime - lastFeedingTime >= feedingPeriod) {
    if (!isFeeding) {
      startFeeding();
    }
    lastFeedingTime = currentTime;
  }

   //Continue running the step motor to update the position
   stepper.step(0); // Add this part to set the motor to stop

   // NTP time update
   timeClient.update();
}

bool isFeedingTime() {


   struct tm timeinfo;
   if (!getLocalTime(&timeinfo)) {
     return false;
  }
   return timeinfo.tm_hour == userSetHour && timeinfo.tm_min == userSetMinute;
}

KakaoTalk_20231005_154154715

Like the picture above, it uploads but does not work.

@P-inc P-inc changed the title Autofeeding(wifi control, stepper motor) eree Autofeeding(wifi control, stepper motor) Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant