ESP32 wifi - Arduino IDE


/*
 *   blue    7      8      9      /
 *   purple  4      5      6      *
 *   grey    1      2      3      +
 *   white   c      0      =      -
 *          green  yellow  orange red     (input lines)
 *          
 *          10K  pulldown resitors needed on the input lines
 *          
 */
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>

LiquidCrystal_I2C lcd(0x27,16,2); 
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;

// Auxiliar variables to store the current output state
String switch1State = "off";
String switch2State = "off";

// Assign output variables to GPIO pins
#define switch1pin 26
#define switch2pin 27
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;


int pinout[] = {17,5,18,19};
int pinin[] = {15,2,4,16};
char buttons[4][4] = { {65,78,97,110}, {48,33,48,33}, {45,46,64,95}, {60,62,91,93}}; 
//                       A N a n,  0 ! 0 !, - . @ _ , space back clear enter 
char sequence[4][4]; //    ={{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}}; 
char limits[4][4] = {{12,12,12,12}, {9,14,9,14}, {0,0,0,0}, {0,0,0,0}}; 
byte moveCursor = 0;
byte tmpstr_index = 0;
bool first = true;
bool get_input = true;
char tmpstr[16];
char ssid[16];
char pwd[16];

void init_() {
  moveCursor = 0;
  tmpstr_index = 0;

  lcd.setCursor(moveCursor, 1);   //second row first position
  lcd.print("                 ");
    
  for (int i=0; i< 16; i++) {
    tmpstr[i] = '\0';
  }
  reset_limits();
}

void set_parameters() {
  if (first) {
            lcd.clear();
            lcd.setCursor(0, 0);
            strcpy(ssid, tmpstr);
            lcd.print(ssid);  
            delay(4000);
            lcd.setCursor(0, 0);
            lcd.setCursor(0,0);
            lcd.print("Enter password  ");
            first = false;
  } else {
            get_input = false;
            lcd.clear();
            lcd.setCursor(0, 0);
            strcpy(pwd, tmpstr);
            lcd.print(pwd);  
            delay(4000);
            lcd.setCursor(0, 0);
            lcd.setCursor(0,0);
            lcd.print("WiFi param set  ");
                
  }
}

void keypress(int j) {
    for (int i=0; i < 4; i++) {
      if (digitalRead(pinin[i]) == HIGH) {
          if (buttons[j][i] == 60) {  // move cursor or space
            moveCursor++;
            tmpstr_index++;
            reset_limits();
          } else if (buttons[j][i] == 62) { // backspace          
            moveCursor--;
            if (moveCursor < 0) moveCursor = 0;
            tmpstr_index--;
            if (tmpstr_index < 0) tmpstr_index = 0;
          } else if (buttons[j][i] == 91) {  //clear
            init_();            
          } else if (buttons[j][i] == 93) {  //enter !!!!
            set_parameters();
            init_();    
          } else {   // non specific character
            lcd.setCursor(moveCursor, 1);
            lcd.print((char)(buttons[j][i] + sequence[j][i]));     
            tmpstr[tmpstr_index] = (char)(buttons[j][i] + sequence[j][i]);   
            sequence[j][i] = sequence[j][i] + 1;
            check_limits();
          }
          while (digitalRead(pinin[i]) == HIGH) {  // wait until end keypress
            delay(20);
          }
    }
  }
}

// make the characters loop
void check_limits() {
    for (int j=0; j< 4; j++) {
      for (int i=0; i< 4; i++) {
        if (sequence[j][i] > limits[j][i]) {
          sequence[j][i] = 0;
        }
      }
  }  
}

void reset_limits() {
 for (int j=0; j< 4; j++) {
      for (int i=0; i< 4; i++) {
          sequence[j][i] = 0;
      }
  }  
}

void setup() {

  for (int i=0; i <4; i++) {
    pinMode(pinout[i], OUTPUT);
    pinMode(pinin[i], INPUT);
  }
  pinMode(switch1pin, OUTPUT);
  pinMode(switch2pin, OUTPUT);
  digitalWrite(switch1pin, LOW);
  digitalWrite(switch2pin, LOW);

  lcd.init();                                       
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Enter SSID");
  delay(200);

  init_();

  while (get_input) {  // boolean
    for (int i=0; i < 4; i++) {
      digitalWrite(pinout[i], HIGH); // multiplexing
      keypress(i);                   // check keypress
      digitalWrite(pinout[i], LOW);
      delay(20);
    }
  }

  WiFi.begin(ssid, pwd);
  lcd.setCursor(0,1);
  while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      lcd.print(".");
  }
  lcd.setCursor(0,1);
  lcd.print(WiFi.localIP()); 

  server.begin();
}

void loop(){

WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    currentTime = millis();
    previousTime = currentTime;
    String currentLine = "";                // make a String to hold incoming data 
    while (client.connected() && currentTime - previousTime <= timeoutTime) {  
      currentTime = millis();
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
    //    Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // turns the GPIOs on and off
            if (header.indexOf("GET /switch1_on") >= 0) {
           //   Serial.println("GPIO 26 on");
           
              switch1State = "on";
              digitalWrite(switch1pin, HIGH);
            } else if (header.indexOf("GET /switch1_off") >= 0) {
             // Serial.println("GPIO 26 off");
              
              switch1State = "off";
              digitalWrite(switch1pin, LOW);
            } else if (header.indexOf("GET /switch2_on") >= 0) {
             // Serial.println("GPIO 27 on");
             
              switch2State = "on";
              digitalWrite(switch2pin, HIGH);
            } else if (header.indexOf("GET /switch2_off") >= 0) {
              // lcd.println("GPIO 27 off");
     
              switch2State = "off";
              digitalWrite(switch2pin, LOW);
            }

            if (switch1State == "off") {
              client.print("Switch1 off ");
              lcd.setCursor(0, 0);
              lcd.print("Switch1 off     ");
              
            } else {
              client.print("Switch1 on ");
              lcd.setCursor(0, 0);
              lcd.print("Switch1 on      ");              
            }
             if (switch2State == "off") {
              client.println("Switch2 off");
              lcd.setCursor(0, 1);
              lcd.print("Switch2 off     ");
            } else {
              client.println("Switch2 on");
              lcd.setCursor(0, 1);
              lcd.print("Switch2 on      ");
            }           
            // Display the HTML web page
            client.println("<DOCTYPE html>lthtml>
            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 
            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>lthead>
            
            // 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 " + switch1State + "</p>");
            // If the switch1State is off, it displays the ON button       
            if (switch1State=="off") {
              client.println("<p><a href=\"/switch1_on\"><button 
                                   class=\"button\">ON<button></a></p>
            } else {
              client.println("<p><a href=\"/switch1_off\"<button 
                                   class=\"button button2\">OFF<button></a></p>
            } 
               
            // Display current state, and ON/OFF buttons for GPIO 27  
            client.println("<p>GPIO 27 - State " + switch2State + "<p>
            // If the switch2State is off, it displays the ON button       
            if (switch2State=="off") {
              client.println("<p>a href=\"/switch2_on\">ltbutton 
                                         class=\"button\">ON<button></a></p>");
            } else {
              client.println("<p><a href=\"/switch2_off\">ltbutton 
                                      class=\"button button2\">OFF<button></a></p>");
            }
            client.println("<body><html>
            
            // The HTTP response ends with another blank line
            client.println();
           
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
  
  }
}  // end loop