merged Webserver.ino into Garage.ino
This commit is contained in:
+62
-21
@@ -1,33 +1,74 @@
|
|||||||
|
#include "ESP8266WiFi.h"
|
||||||
|
#include "ESP8266WebServer.h
|
||||||
|
|
||||||
|
// https://techtutorialsx.com/2016/10/03/esp8266-setting-a-simple-http-webserver/
|
||||||
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
const int remotePin = D1;
|
const int remotePin = D1;
|
||||||
const int buttonPin = D2;
|
const int buttonPin = D2;
|
||||||
const int motorPin = D3;
|
const int motorPin = D3;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(115200);
|
||||||
Serial.println("Start");
|
connectWifi();
|
||||||
pinMode(BUILTIN_LED, OUTPUT);
|
|
||||||
pinMode(remotePin, OUTPUT);
|
server.on("/", handleRootPath);
|
||||||
pinMode(buttonPin, INPUT);
|
server.begin();
|
||||||
pinMode(motorPin, OUTPUT);
|
Serial.println("Server listening");
|
||||||
|
|
||||||
|
pinMode(BUILTIN_LED, OUTPUT);
|
||||||
|
pinMode(remotePin, OUTPUT);
|
||||||
|
pinMode(buttonPin, INPUT);
|
||||||
|
pinMode(motorPin, OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int buttonState = 0;
|
int buttonState = 0;
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
buttonState = digitalRead(buttonPin);
|
if ((WiFi.status() == WL_CONNECTED)) {
|
||||||
//Serial.println(buttonState);
|
server.handleClient();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("WiFi not connected!");
|
||||||
|
connectWifi();
|
||||||
|
}
|
||||||
|
|
||||||
|
buttonState = digitalRead(buttonPin);
|
||||||
|
|
||||||
|
if (buttonState == HIGH) {
|
||||||
|
Serial.println("Button pressed");
|
||||||
|
digitalWrite(remotePin, HIGH);
|
||||||
|
digitalWrite(motorPin, HIGH);
|
||||||
|
digitalWrite(BUILTIN_LED, LOW);
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
digitalWrite(remotePin, LOW);
|
||||||
|
digitalWrite(motorPin, LOW);
|
||||||
|
digitalWrite(BUILTIN_LED, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleRootPath() {
|
||||||
|
server.send(200, "text/plain", "Hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
void connectWifi() {
|
||||||
|
Serial.print("Connecting to ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
Serial.print(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("WiFi connected.");
|
||||||
|
Serial.println("IP address: ");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (buttonState == HIGH) {
|
|
||||||
Serial.println("Button pressed");
|
|
||||||
digitalWrite(remotePin, HIGH);
|
|
||||||
digitalWrite(motorPin, HIGH);
|
|
||||||
digitalWrite(BUILTIN_LED, LOW);
|
|
||||||
delay(500);
|
|
||||||
} else {
|
|
||||||
digitalWrite(remotePin, LOW);
|
|
||||||
digitalWrite(motorPin, LOW);
|
|
||||||
digitalWrite(BUILTIN_LED, HIGH);
|
|
||||||
}
|
|
||||||
delay(10);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user