#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
//static IP config
IPAddress staticIP(192, 168, 1, 1); //Node static IP
IPAddress gateway(192, 168, 0, 174); //IP on router
IPAddress subnet(255, 255, 255, 0); //subnet mask
IPAddress dns(8,8,8,8); //DNS
const char* deviceName = "Blindz Controller";
// WiFi settings
const char* ssid = "SSID Here"; //network ID here
const char* password = "Passwordhere"; //network password here
ESP8266WebServer server(80); //Server port 80
//int motor_mode = 0;
// motor settings
#define MOTOR_PIN1 4 //pin 1 of Blindz motor (D2)
#define MOTOR_PIN2 5 //pin 2 of Blindz motor (D1)
#define MOTOR_SPEED_DOWN 800 //speed going down (0-1023)
#define MOTOR_SPEED_UP 1023 //speed for going back up going up requires a higher speed
int blindz_mode = 0; // set blindz drive mode (0 = stop)
// initialize
void setup() {
Serial.begin(115200); //Buad rate 115200 for debugging/serial monitor
Serial.println("Blindz Server");
pinMode(MOTOR_PIN1, OUTPUT);
pinMode(MOTOR_PIN2, OUTPUT);
// blinds_mode(); //stop blinds
WiFi.begin(ssid, password); //Connects to router
Serial.println("");
WiFi.disconnect(); //prevents connecting to wifi on previous config
WiFi.hostname(deviceName);
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); //WiFi station mode
while (WiFi.status() != WL_CONNECTED){ //wait for connection
delay(500);
Serial.print(".");
}
Serial.println(" ");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP assigned to node
Serial.print("Blindz are live");
}
// setup web server to handle specific HTTP requests
server.on("/", HTTP_GET, handle_OnConnect);
server.on("/Up", HTTP_GET, handle_up);
server.on("/Down", HTTP_GET, handle_down);
server.on("/Stop", HTTP_GET, handle_stop);
server.on("/Open", HTTP_GET, handle_open);
server.on("/Close", HTTP_GET, handle_close);
server.onNotFound(handle_NotFound);
//start server
server.begin();
Serial.println("NodeMCU web server started.");
}
// handle HTTP requests and control blindz
void loop() {
server.handleClient();
blindz_control();
}
// HTTP request: on connect
void handle_OnConnect() {
blindz_mode = 0;
Serial.println("Client connected");
server.send(200, "text/html", SendHTML());
}
// HTTP request: stop blinds
void handle_stop() {
blindz_mode = 0;
Serial.println("Stopped");
server.send(200, "text/html", SendHTML());
}
// HTTP request: blinds go up
void handle_up() {
blindz_mode = 1;
Serial.println("Going Up...");
server.send(200, "text/html", SendHTML());
}
// HTTP request: blinds go down
void handle_down() {
blindz_mode = 2;
Serial.println("Going Down...");
server.send(200, "text/html", SendHTML());
}
// HTTP request: blinds fully close
void handle_close() {
blindz_mode = 3;
Serial.println("Time for Bed");
server.send(200, "text/html", SendHTML());
}
// HTTP request: blinds fully open
void handle_open() {
blindz_mode = 4;
Serial.println("Rise and Shine");
server.send(200, "text/html", SendHTML());
}
// HTTP request: other
void handle_NotFound() {
blindz_mode = 0;
Serial.println("Page error");
server.send(404, "text/plain", "Not found");
}
// control blinds movement
void blindz_control() {
switch (blindz_mode) {
case 0: // stop blindz
digitalWrite(MOTOR_PIN1, LOW);
digitalWrite(MOTOR_PIN2, LOW);
break;
case 1: // go up
analogWrite(MOTOR_PIN1, MOTOR_SPEED_UP);
digitalWrite(MOTOR_PIN2, LOW);
break;
case 2: // go down
digitalWrite(MOTOR_PIN1, LOW);
analogWrite(MOTOR_PIN2, MOTOR_SPEED_DOWN);
break;
//add case for fully open and fully close
}
}
// output HTML web page for user
String SendHTML() {
String html = "<!DOCTYPE html>\n";
html += "<html>\n";
html += "<head>\n";
html += "<title>Das Blindz</title>\n";
html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n";
html += "</head>\n";
html += "<body>\n";
html += "<div align=\"center\">\n";
html += "<h1>Das Blindz</h1>\n";
html += "
\n";
html += "<form method=\"GET\">\n";
html += "<input type=\"button\" value=\"Up\" onclick=\"window.location.href='/Up'\">\n";
html += "
\n";
html += "<input type=\"button\" value=\"Down\" onclick=\"window.location.href='/Down'\">\n";
html += "
\n";
html += "<input type=\"button\" value=\"Stop\" onclick=\"window.location.href='/Stop'\">\n";
html += "<input type=\"button\" value=\"Open\" onclick=\"window.location.href='/Open'\">\n";
html += "<input type=\"button\" value=\"Close\" onclick=\"window.location.href='/Close'\">\n";
html += "</form>\n";
html += "</div>\n";
html += "</body>\n";
html += "</html>\n";
return html;
}
// setup web server to handle specific HTTP requests
server.on("/", HTTP_GET, handle_OnConnect);
server.on("/Up", HTTP_GET, handle_up);
server.on("/Down", HTTP_GET, handle_down);
server.on("/Stop", HTTP_GET, handle_stop);
server.on("/Open", HTTP_GET, handle_open);
server.on("/Close", HTTP_GET, handle_close);
server.onNotFound(handle_NotFound);
//start server
server.begin();
Serial.println("NodeMCU web server started.");
None of this code is in a function
For future reference, if you're going to quote error messages, please do so accurately, using cut-and-paste.
Duplicate topic deleted
@NeedHelpWithThis - in future do not start a second topic on the same subject