トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

ESP32_softAP

WROOM32 で softAP

http://mukujii.sakura.ne.jp/esp2.html

/* WiFi-Control-Car(softAP) */
								
#include <WiFi.h>

const char ssid[] = "ESP32AP-WiFi";
const char pass[] = "esp32apwifi";
const IPAddress ip(192,168,130,130);
const IPAddress subnet(255,255,255,0);

const char html[] =
"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\
<style>input {margin:8px;width:80px;}\
div {font-size:16pt;color:red;text-align:center;width:400px;border:groove 40px orange;}</style>\
<title>WiFi_Car Controller</title></head>\
<body><div><p>Tank Controller</p>\
<form method='get'>\
<input type='submit' name='le' value='左' />\
<input type='submit' name='fo' value='前' />\
<input type='submit' name='ri' value='右' /><br>\
<input type='submit' name='st' value='停止' /><br>\
<input type='submit' name='bl' value='後左' />\
<input type='submit' name='ba' value='後ろ' />\
<input type='submit' name='br' value='後右' /><br><br>\
</form></div></body></html>";

void stop(){
   digitalWrite(0, LOW);
   digitalWrite(2, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
}

WiFiServer server(80);

void setup()
{
   Serial.begin(115200);

   WiFi.softAP(ssid,pass);
   delay(100);
   WiFi.softAPConfig(ip,ip,subnet);

   IPAddress myIP = WiFi.softAPIP();

   pinMode(0, OUTPUT);
   pinMode(2, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   delay(10);

   server.begin();

   Serial.print("SSID: ");
   Serial.println(ssid);
   Serial.print("AP IP address: ");
   Serial.println(myIP);
   Serial.println("Server start!");

}

void loop(){
   WiFiClient client = server.available();

   if (client) {
       String currentLine = "";
       Serial.println("New Client.");

       while (client.connected()) {
           if (client.available()) {
               char c = client.read();
               Serial.write(c);
               if (c == '\n') {
                   if (currentLine.length() == 0) {
                       client.println("HTTP/1.1 200 OK");
                       client.println("Content-type:text/html");
                       client.println();

                       client.print(html);
                       client.println();
                       break;
                   } else {
                       currentLine = "";
                   }
               } else if (c != '\r') {
                   currentLine += c;
               }

               if (currentLine.endsWith("GET /?fo")) {
                   stop();
                   digitalWrite(0, HIGH);
                   digitalWrite(4, HIGH);
               }
               if (currentLine.endsWith("GET /?le")) {
                   stop();
                   digitalWrite(0, HIGH);
               }
               if (currentLine.endsWith("GET /?ri")) {
                   stop();
                   digitalWrite(4, HIGH);
               }
               if (currentLine.endsWith("GET /?ba")) {
                   stop();
                   digitalWrite(2, HIGH);
                   digitalWrite(5, HIGH);
               }
               if (currentLine.endsWith("GET /?bl")) {
                   stop();
                   digitalWrite(2, HIGH);
               }
               if (currentLine.endsWith("GET /?br")) {
                   stop();
                   digitalWrite(5, HIGH);
               }
               if (currentLine.endsWith("GET /?st")) {
                   stop();
               }
           }
       }
       client.stop();
       Serial.println("Client Disconnected.");
   }
}