// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
String domain = "CSProbot";
/* Setup IP for ESP32 */
IPAddress localip(192,168,3,20);
IPAddress gateway(192,168,3,1);
IPAddress subnet(255,255,255,0);
IPAddress IP;
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
/* Setup SSID and Password */
const char *ssid = "ESP32";
const char *pass = "0123456789";
//----------BUTTONS----------
#define BUTTON1_pin 5
#define BUTTON2_pin 4
/* Initial state */
int button1_state = HIGH;
int button2_state = HIGH;
int button1_press = LOW;
int button2_press = LOW;
unsigned long B1DebounceTime = 0;
unsigned long B2DebounceTime = 0;
unsigned long debounceTime = 50;
void initLCD();
void displayText(int column, int row, String str);
void transWiFi();
void setup() {
Serial.begin(115200);
/* Declare GPIO pins for buttons */
pinMode(BUTTON1_pin, INPUT_PULLUP);
pinMode(BUTTON2_pin, INPUT_PULLUP);
/* Initialize LCD screen */
initLCD();
/* Transmit WiFi */
transWiFi();
/* Get IP address */
IP = WiFi.softAPIP();
Serial.println("IP:" + IP.toString());
/* Press button 1 to see SSID and Password */
/* Press button 2 to see IP address and domain */
}
void loop() {
unsigned long currentMillis = millis();
bool button1Read = digitalRead(BUTTON1_pin);
bool button2Read = digitalRead(BUTTON2_pin);
/* check button 1 */
if (button1Read != button1_state){
B1DebounceTime = currentMillis;
}
if ((currentMillis - B1DebounceTime) >= debounceTime){
if (!(digitalRead(BUTTON1_pin))){
lcd.clear();
displayText(0,0,"SSID:");
displayText(6,0,String(ssid));
displayText(0,1,"PASS:");
displayText(6,1,String(pass));
delay(500);
}
}
/* check button 2 */
if (button2Read != button2_state){
B2DebounceTime = currentMillis;
}
if ((currentMillis - B2DebounceTime) >= debounceTime){
if (!(digitalRead(BUTTON2_pin))){
lcd.clear();
displayText(0,0,"IP:");
displayText(4,0,IP.toString());
displayText(0,1,"Host:");
displayText(6,1,String(domain));
delay(500);
}
}
}
void transWiFi(){
WiFi.mode(WIFI_AP);
WiFi.softAP(ssid, pass);
WiFi.softAPConfig(localip, gateway, subnet);
/*
* *WiFi.softAP(const char *ssid, const char *pass, int channel, int ssid_hidden, int max_connection);
* ssid: name for the access point
* pass: password for the ssid that you want the access point to be open
* channel: wifi channel number (1-13, in some cases, channel 12 & 13 'll not be available
* ssid_hidden: 0 = broadcast SSID, 1 = hide SSID
* max_connection: maximum simultaneous connected clients (1-4)
*/
/* Print on serial screen */
Serial.println("ACCESS POINT");
Serial.println("SSID:" + String(ssid));
Serial.println("PASS:" + String(pass));
}
//--------------------------------------------------
void initLCD(){
lcd.init();
lcd.clear(); // Xóa màn hình
lcd.backlight(); // Bật đèn nền
lcd.setCursor(0,0); // Đưa con trỏ về vị trí hàng 0, cột 0
}
//--------------------------------------------------
void displayText(int column, int row, String str){
lcd.setCursor(column, row);
lcd.print(str);
}