#include #include #include #ifndef APSSID #define APSSID "Cat Ball" #define APPSK "churchill" #endif const char *ssid = APSSID; const char *password = APPSK; ESP8266WebServer server(80); const int ledPin = 2; const int motorPin = 1; const int sensorPin = 3; bool motorStatus = true; bool manualControl = false; const int onTime = 1000*2; const int offTime = 1000*60; const int liveTime = 1000*60*5; unsigned long ticker = millis(); unsigned long touchedAt = millis() - liveTime; String createHtml() { String res = "\n"; res += "\n"; res += "Cat Ball\n"; res += "\n"; res += "\n"; res += "\n"; res += "

Cat Ball

\n"; if (manualControl) { res += "Disable manual mode\n"; res += "Turn motor on\n"; res += "Turn motor off\n"; } else { res += "Enable manual mode\n"; } res += "\n"; res += "\n"; return res; } void handleRoot() { server.send(200, "text/html", createHtml()); } void manual() { manualControl = !manualControl; motorStatus = false; server.send(200, "text/html", createHtml()); } void motorOn() { motorStatus = true; server.send(200, "text/html", createHtml()); } void motorOff() { motorStatus = false; server.send(200, "text/html", createHtml()); } void setup() { pinMode(ledPin, OUTPUT); pinMode(motorPin, OUTPUT); pinMode(sensorPin, INPUT); WiFi.softAP(ssid, password); server.on("/", handleRoot); server.on("/manual", manual); server.on("/motoron", motorOn); server.on("/motoroff", motorOff); server.begin(); } void loop() { server.handleClient(); if (manualControl) { digitalWrite(motorPin, motorStatus); } else { if (millis()-touchedAt < liveTime) { // If touched less than 5 minutes ago then act alive if (motorStatus) { if (millis()-ticker > onTime) { ticker = millis(); motorStatus = false; digitalWrite(motorPin, false); } } else { if (millis()-ticker > offTime) { ticker = millis(); motorStatus = true; digitalWrite(motorPin, motorStatus); } } } else { motorStatus = false; digitalWrite(motorPin, false); digitalWrite(ledPin, true); if (digitalRead(sensorPin)) { touchedAt = millis(); digitalWrite(ledPin, false); } } } }