#include #include // Define pins for rotary encoder and button #define pinCLK 32 #define pinDT 35 #define pinSW 34 #define pinLED 23 // Network credentials const char* ssid = "your_ssid"; const char* password = "your_password"; // Create a web server on port 80 WebServer server(80); // Variables to track the count and last action int rotaryCount = 0; // Variable to keep track of the rotary encoder count int previousStateSW; // Variable to store the previous state of the SW pin int previousStateCLK; // Variable to store the previous state of the CLK pin String last_action = ""; // HTML content const char index_html[] PROGMEM = R"rawliteral( ESP32 Rotary Encoder

ESP32 Rotary Encoder

Count:
Last Action:
)rawliteral"; void setup() { Serial.begin(115200); // Initialize serial communication at 115200 baud rate pinMode(pinSW, INPUT_PULLUP); // Set SW pin as input with internal pull-up resistor pinMode(pinDT, INPUT); // Set DT pin as input pinMode(pinCLK, INPUT); // Set CLK pin as input pinMode(pinLED, OUTPUT); // Set LED pin as output previousStateSW = digitalRead(pinSW); // Read initial state of the SW pin previousStateCLK = digitalRead(pinCLK); // Read initial state of the CLK pin // Connect to Wi-Fi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); // Print the IP address Serial.print("IP address: "); Serial.println(WiFi.localIP()); // Define server routes server.on("/", []() { server.send_P(200, "text/html", index_html); }); server.on("/data", []() { String json = "{\"rotaryCount\":" + String(rotaryCount) + ", \"last_action\":\"" + last_action + "\"}"; server.send(200, "application/json", json); }); server.begin(); Serial.println("HTTP server started"); } void loop() { server.handleClient(); int actualStateCLK = digitalRead(pinCLK); // Read current state of the CLK pin int actualStateDT = digitalRead(pinDT); // Read current state of the DT pin int actualStateSW = digitalRead(pinSW); // Read current state of the SW pin // Push Button verification if (actualStateSW != previousStateSW) { // Check if SW state has changed previousStateSW = actualStateSW; // Update the previous SW state delay(50); // Debounce delay if (actualStateSW == LOW) { Serial.println(F("SW Button pushed")); // Print message when button is pressed last_action = "SW Button pushed"; } else { Serial.println(F("SW Button released")); // Print message when button is released last_action = "SW Button released"; } } // Rotary encoder verification if (actualStateCLK != previousStateCLK) { delay(10); // Debounce delay if (digitalRead(pinCLK) == actualStateCLK) { // Check if the CLK signal has settled previousStateCLK = actualStateCLK; if (actualStateCLK == LOW) { if (actualStateCLK != actualStateDT) { // Comparing CLK and DT, if they're different, direction is counter-clockwise rotaryCount--; // Decrement counter Serial.println(F("counter-clockwise")); // Display value on Serial Monitor last_action = "Counter-clockwise"; } else { // When CLK and DT are similar, direction is clockwise rotaryCount++; // Increment counter Serial.println(F("clockwise")); // Display value on Serial Monitor last_action = "Clockwise"; } Serial.println(rotaryCount); // Ensure count stays within bounds (1-5 for PWM) if (rotaryCount < 1) { rotaryCount = 1; } else if (rotaryCount > 5) { rotaryCount = 5; } // Map rotaryCount to PWM range (0-255) int pwmValue = map(rotaryCount, 1, 5, 0, 128); analogWrite(pinLED, pwmValue); // Set LED brightness } } } }