Question
Arduino 8266 Hello, i'm working on a wifi garage door opener, that I would like to use servo, and mount a magnetic sensor on it
Arduino 8266
Hello, i'm working on a wifi garage door opener, that I would like to use servo, and mount a magnetic sensor on it to demonstrate a garage door as it open and closes. I want the servo to move 60 degrees when I hit the "open door" and move back when I hit "close Door" Can you please help me with the code ?
#include
const char* ssid = "Your SSID"; const char* password = " Your password ";
ESP8266WebServer server(80);
int switchPin = 0; int switchStateCur; int relayPin = 2;
int WiFiCon() { // Check if we have a WiFi connection, if we don't, connect. int xCnt = 0;
if (WiFi.status() != WL_CONNECTED){
Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);
WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED && xCnt < 50) { delay(500); Serial.print("."); xCnt ++; }
if (WiFi.status() != WL_CONNECTED){ Serial.println("WiFiCon=0"); return 0; //never connected } else { Serial.println("WiFiCon=1"); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); return 1; //1 is initial connection }
} else { Serial.println("WiFiCon=2"); return 2; //2 is already connected } }
String htmlServe(int doorAction) { String htmStr;
if (switchStateCur==1 && doorAction ==1){ digitalWrite(relayPin, 0); delay(400); digitalWrite(relayPin, 1); } else if (switchStateCur==0 && doorAction ==0){ digitalWrite(relayPin, 0); delay(400); digitalWrite(relayPin, 1); }
htmStr += " "; htmStr += " "; htmStr += "
";
if (switchStateCur==1){ htmStr += "Your Door is Open"; } else { htmStr += "The Door is Closed"; } htmStr +=" ";
htmStr += " "; htmStr += "
return htmStr; }
void setup(){
pinMode(switchPin, INPUT); pinMode(relayPin, OUTPUT); digitalWrite(relayPin, HIGH); Serial.begin(115200);
WiFiCon();
server.on("/odoor", [](){ server.send(200, "text/html", htmlServe(0)); });
server.on("/cdoor", [](){ server.send(200, "text/html", htmlServe(1)); });
server.on("/sdoor", [](){ server.send(200, "text/html", htmlServe(2)); });
server.begin();
}
void loop(){
switchStateCur = digitalRead(switchPin); server.handleClient();
delay(1000); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started