#include "WiFiEsp.h" #include #define rxPin 3 #define txPin 2 SoftwareSerial esp01(txPin, rxPin); // SoftwareSerial NAME(TX, RX); const char ssid_AP[] = "esp01_AP"; // your network SSID (name) const char pass_AP[] = "1234test"; // your network password #define ledPin 13 #define digiPin 7 WiFiEspServer server(80); void setup() { pinMode(ledPin, OUTPUT); pinMode(digiPin, OUTPUT); digitalWrite(ledPin, LOW); digitalWrite(digiPin, LOW); Serial.begin(9600); // initialize serial for debugging esp01.begin(9600); //와이파이 시리얼 WiFi.init(&esp01); // initialize ESP module // IPAddress localIp(192, 168, 111, 111); // softAP 접속 IP 설정 // WiFi.configAP(localIp); WiFi.beginAP(ssid_AP, 10, pass_AP, ENC_TYPE_WPA2_PSK); // 10:채널, IPAddress ap = WiFi.localIP(); Serial.print(F("AP Address: ")); Serial.println(ap); server.begin(); } const char HTTP_HEAD[] PROGMEM = "" "" ""; const char HTTP_STYLE[] PROGMEM = ""; const char HTTP_HEAD_END[] PROGMEM = "
" "

ESP01 Digital Pin Control

"; const char BUTTON_TYPE[] PROGMEM = "

" "

"; const char BUTTON_A_ON[] PROGMEM = "

Button A

"; const char BUTTON_A_OFF[] PROGMEM = "

Button A

"; const char BUTTON_B_ON[] PROGMEM = "

Button B

"; const char BUTTON_B_OFF[] PROGMEM = "

Button B

"; const char HTTP_END[] PROGMEM = "
"; bool button_a = LOW; // off bool button_b = LOW; // off void loop() { WiFiEspClient client = server.available(); // listen for incoming clients if (client) { // if you get a client, while (client.connected()) { // loop while the client's connected if (client.available()) { // if there's bytes to read from the client, String income_AP = client.readStringUntil('\n'); if (income_AP.indexOf(F("A/1")) != -1) { Serial.println(F("button_A on")); button_a = HIGH; digitalWrite(ledPin, button_a); } else if (income_AP.indexOf(F("A/0")) != -1) { Serial.println(F("button_A off")); button_a = LOW; digitalWrite(ledPin, button_a); } else if (income_AP.indexOf(F("B/1")) != -1) { Serial.println(F("button_B on")); button_b = HIGH; digitalWrite(digiPin, button_b); } else if (income_AP.indexOf(F("B/0")) != -1) { Serial.println(F("button_B off")); button_b = LOW; digitalWrite(digiPin, button_b); } client.flush(); client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-type:text/html")); client.println(F("Connection: close")); client.println(); String page; page = (const __FlashStringHelper *)HTTP_HEAD; page += (const __FlashStringHelper *)HTTP_STYLE; page += (const __FlashStringHelper *)HTTP_HEAD_END; page += (const __FlashStringHelper *)BUTTON_TYPE; if (button_a == HIGH) { page += (const __FlashStringHelper *)BUTTON_A_ON; } else { page += (const __FlashStringHelper *)BUTTON_A_OFF; } if (button_b == HIGH) { page += (const __FlashStringHelper *)BUTTON_B_ON; } else { page += (const __FlashStringHelper *)BUTTON_B_OFF; } page += (const __FlashStringHelper *)HTTP_END; client.print(page); client.println(); delay(1); break; } } client.stop(); Serial.println(F("Client disconnected")); } }