#include #include #include "RTClib.h" #include #include #include #include #include #include #include //pins: const int HX711_dout = 6; //mcu > HX711 dout pin const int HX711_sck = 5; //mcu > HX711 sck pin //HX711 constructor: HX711_ADC LoadCell(HX711_dout, HX711_sck); RTC_DS1307 rtc; unsigned long t = 0; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; const char* ssid = "iPhone"; const char* password = "ati@1987"; WebServer server(80); Pwm pwm = Pwm(); const int extruder = 2; const int propeller = 3; float weight = 0.00; String dispenseBtn = "Start Dispensing"; String dispenseBtn_link = "/dispense/1"; String dispenseBtn_color = "007BFF"; void handleRoot() { char temp[5500]; snprintf(temp, 5500, "\ \ \ \ \ Fish Feeder Interface\ \ \ \ \
\
FFM
\
\ Current Feed Weight: %02fkg\
\
\ 70 %\ %s\
\
\ \ \
\
\ \ \
\
\ \ \
\
\ \ \
\ \
\
\ \ ", dispenseBtn_color.c_str(), weight/1000, int(weight/1000), dispenseBtn_link.c_str(),dispenseBtn.c_str() ); server.send(200, "text/html", temp); digitalWrite(propeller, 0); } void handleNotFound() { digitalWrite(propeller, 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; } server.send(404, "text/plain", message); digitalWrite(propeller, 0); } void rtc_init(){ if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1) delay(10); } if (! rtc.isrunning()) { Serial.println("RTC is NOT running, let's set the time!"); // When time needs to be set on a new device, or after a power loss, the // 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)); } } void wifi_ota_init(){ WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } ArduinoOTA .onStart([]() { String type; if (ArduinoOTA.getCommand() == U_FLASH) type = "sketch"; else // U_SPIFFS type = "filesystem"; // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() Serial.println("Start updating " + type); }) .onEnd([]() { Serial.println("\nEnd"); }) .onProgress([](unsigned int progress, unsigned int total) { Serial.printf("Progress: %u%%\r", (progress / (total / 100))); }) .onError([](ota_error_t error) { Serial.printf("Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); else if (error == OTA_END_ERROR) Serial.println("End Failed"); }); ArduinoOTA.begin(); Serial.println("Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (MDNS.begin("esp32")) { Serial.println("MDNS responder started"); } } void load_cell_init(){ LoadCell.begin(); unsigned long stabilizingtime = 2000; // preciscion right after power-up can be improved by adding a few seconds of stabilizing time boolean _tare = true; //set this to false if you don't want tare to be performed in the next step LoadCell.start(stabilizingtime, _tare); if (LoadCell.getTareTimeoutFlag() || LoadCell.getSignalTimeoutFlag()) { Serial.println("Timeout, check MCU>HX711 wiring and pin designations"); while (1); } else { LoadCell.setCalFactor(1.0); // user set calibration value (float), initial value 1.0 may be used for this sketch Serial.println("Startup is complete"); } } void setup() { pwm.write(propeller, 0); pwm.write(extruder, 0); Serial.begin(9600); Serial.println(); Serial.println("Starting..."); wifi_ota_init(); load_cell_init(); server.on("/", handleRoot); server.on("/test.svg", drawGraph); server.on("/dispense/1", []() { pwm.write(propeller, 255); pwm.write(extruder, 255); dispenseBtn_link = "/dispense/0"; dispenseBtn = "Stop Dispensing"; dispenseBtn_color = "6A1C0F"; handleRoot(); }); server.on("/dispense/0", []() { pwm.write(propeller, 0); pwm.write(extruder, 0); dispenseBtn_link = "/dispense/1"; dispenseBtn = "Start Dispensing"; dispenseBtn_color = "007BFF"; handleRoot(); }); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop() { ArduinoOTA.handle(); server.handleClient(); delay(2);//allow the cpu to switch to other tasks // check for new data/start next conversion: if (LoadCell.update()){ weight = abs(LoadCell.getData()/10); Serial.print("Load_cell output val: "); Serial.print(weight); Serial.print("g "); Serial.println(" "); } } void rtc_ds1307(){ DateTime now = rtc.now(); Serial.print(now.year(), DEC); Serial.print('/'); Serial.print(now.month(), DEC); Serial.print('/'); Serial.print(now.day(), DEC); Serial.print(" ("); Serial.print(daysOfTheWeek[now.dayOfTheWeek()]); Serial.print(") "); Serial.print(now.hour(), DEC); Serial.print(':'); Serial.print(now.minute(), DEC); Serial.print(':'); Serial.print(now.second(), DEC); Serial.println(); } void drawGraph() { String logo = ""; char temp[100]; logo = ""; server.send(200, "image/svg+xml", logo); }