Hello, can you please check this code for me, what is wrong, I tried for a few day now. Thank you.
#include "WiFi.h"
#include <U8x8lib.h>
// the OLED used
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16);
long lastScanMillis=0;
long currentMillis=1;
const char* displaystatus[]={"On","Off","Blink Blink","Boom Boom","FadeED"};
int dsplay=1;
int ledcFreq=5000;
int resolution =10;
int kzyStatus=0;
int fadStatus=0;
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
const char* ssid = "NONSERVICES";
const char* password = "495147485AZ";
int value = LOW;
int channel=0;
int ledPin = 2; // GPIO2
int freq;
WiFiServer server(80);
void setup()
{
ledcSetup(channel, ledcFreq, resolution);
ledcAttachPin(LED_BUILTIN, channel);
u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// Connect to WiFi network
u8x8.clearDisplay();
u8x8.drawString(0, 0, "Connecting to ");
u8x8.drawString(0,1,ssid);
WiFi.disconnect(true);
WiFi.begin(ssid, password);
u8x8.setCursor(0,4);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
u8x8.print(".");
}
u8x8.drawString(0,2,"WiFi connected");
delay(3000);
// Start the server
u8x8.clearDisplay();
u8x8.drawString(0,0,"Server started");
// Print the IP address
u8x8.drawString(0,1,"Connect to : ");
u8x8.drawString(0,2,"https://");
u8x8.setCursor(0,4);
u8x8.print(WiFi.localIP().toString());
server.begin();
delay(3000);
}
void loop() {
// Check if a client has connected
u8x8.drawString(0,5,"Wait");
WiFiClient client = server.available();
if (!client) {
currentMillis = millis();
if(((currentMillis-lastScanMillis)>freq)&&(kzyStatus)){
lastScanMillis=currentMillis;
if(value){
digitalWrite(ledPin, LOW);
value = LOW;
}
else{
digitalWrite(ledPin, HIGH);
value = HIGH;
}
}
if(((currentMillis-lastScanMillis)>freq)&&(fadStatus)){
lastScanMillis=currentMillis;
ledcWrite(channel, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 1023) {
fadeAmount = -fadeAmount;
}
}
return;
}
// Wait until the client sends some data
do{
delay(10);
} while(!client.available());
delay(10);
u8x8.drawString(0,6,"client comming");
// Read the first line of the request
String request = client.readStringUntil('\n');
client.flush();
// Match the request
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
dsplay=0;
kzyStatus=0;
fadStatus=0;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
dsplay=1;
kzyStatus=0;
fadStatus=0;
}
if (request.indexOf("/LED=KZY") != -1) {
dsplay=2;
kzyStatus=1;
fadStatus=0;
freq=300;
}
if (request.indexOf("/BOOM-me") != -1) {
dsplay=3;
kzyStatus=1;
fadStatus=0;
freq=60;
}
if (request.indexOf("/Fade-ON") != -1) {
dsplay=4;
kzyStatus=0;
fadStatus=1;
freq=10;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
//client.println(""); // do not forget this one
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State ""</p>");
// If the output26State is off, it displays the ON button
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
delay(10);
u8x8.drawString(0,6," ");
}