Hey team. I built a "Doggie hotel" that controls a couple of servos. One to open a gate and another the dispenses food. I'm using a simple webserver to control both of these with the addition of a button to dispense foot and it was working great.... but as of late it doesn't seem to keep looping. I lose the functionality of the webserver and even the button quits working. Most the time doing a reset fixes it but that is such a pain. I was hoping someone could look over my code and let me know if I have something that could be causing me issues. I'm using a ESP8266 board. This is hooked to a breadboard and powered on the VIN (9volts). In addition the servos are getting their power from the same power supply.
Here is my code:
#include <ESP8266WiFi.h>
#include <Servo.h>
Servo feederservo; // create servo object to control a servo for the feeder
Servo gateservo; // create servo object to control a servo for the gate
int pos = 0; // variable to store the servo position
#define WIFI_SSID "WiFi Network"
#define WIFI_PASS "1234567890"
WiFiServer server(80);
void wifiSetup() {
// Set WIFI module to STA mode
WiFi.mode(WIFI_STA);
// Connect
Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASS);
// Wait
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println();
// Connected!
Serial.printf("[WIFI] STATION Mode, SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup(){
//start serial connection
Serial.begin(115200);
delay(10);
pinMode(D3, INPUT_PULLUP); //Gpio-0(D3) Button input - Feed Crunchers
feederservo.attach(D4); //Gpio-2(D4) of nodemcu with pwm pin of servo motor
gateservo.attach(D1); //Gpio-5(D1) of nodemcu with pwm pin of servo motor
wifiSetup();
server.begin();
Serial.println("Server started");
feederservo.write(180);
gateservo.write(180);
}
void loop() {
int sensorVal = digitalRead(0);
if (sensorVal == LOW) {
digitalWrite(1, HIGH);
feederservo.write(180);
delay(300);
feederservo.write(0);
delay(900);
feederservo.write(180);
delay(300);
}
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("new client");
while(!client.available()){
delay(1);
}
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
int value = 0;
if (request.indexOf("/Req=0") != -1) {
digitalWrite(1, HIGH);
feederservo.write(180);
delay(300);
feederservo.write(0);
delay(900);
feederservo.write(180);
delay(300);
}
if (request.indexOf("/Req=1") != -1) {
digitalWrite(1, HIGH);
gateservo.write(180);
delay(300);
gateservo.write(90);
delay(3000);
gateservo.write(180);
delay(300);
}
else {
digitalWrite(1, LOW);
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("");
client.println("");
client.println("
Cocoa Hotel
");
client.println("
");
client.println("<a href="/Req=0"">Send the Crunchers");
client.println("<a href="/Req=1"">Free Cocoa");
client.println("");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}