目的 :
arduino IDE で、ESP32基板のIPアドレスを固定にする。IPアドレスの固定方法 :
WiFi.config() で IPアドレス等を指定した後、WiFi.begin() で 接続を開始する。書式
WiFi.config([IPアドレス],[ゲートウェイアドレス],[サブネットマスク],[DNSアドレス]);
例:
LED ON/OFF するプログラムを 固定IPアドレスで書き直してみた。/*
* WiFi LED ON/OFF TEST
*/
#include <WiFi.h>
const char* ssid = "hogehoge";
const char* password = "hogehogepass";
IPAddress ip(192, 168, 1, 32); // for fixed IP Address
IPAddress gateway(192,168, 1, 1); //
IPAddress subnet(255, 255, 255, 0); //
IPAddress DNS(192, 168, 1, 90); //
WiFiServer server(80);
void setup()
{
Serial.begin(115200);
pinMode(2, OUTPUT); // set the LED pin mode
WiFi.config(ip, gateway, subnet, DNS); // Set fixed IP address
delay(10);
// We start by connecting to a WiFi network -----------------------------
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("***** Client access start *****"); // print a message out the serial port
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
String line = client.readStringUntil('\n'); // Get Line data until '\n'
Serial.println(line);
if (line.indexOf("GET /?on") != -1) { // Client request was "GET /?on"
digitalWrite(2, HIGH); // LED on
}
if (line.indexOf("GET /?off") != -1) { // Client request was "GET /?off"
digitalWrite(2, LOW); // LED off
}
if (line.length() == 1 && line[0] == '\r'){ // end of HTTP request
send_response(client) ; // send response to client
break; // break while loop
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
Serial.println("--------------------------------------------------");
}
}
// ------------------------------------------------------------------
void send_response(WiFiClient client) {
// 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();
// the content of the HTTP response follows the header:
client.print("<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>" ) ;
client.print("<style>input {margin:8px;width:100px;}div {font-size:16pt;text-align:center;width:250px;border:solid 4px #93ff93;}</style>" ) ;
client.print("<title>Color LED Controller</title></head>" ) ;
client.print("<body><div><p>LED ON/OFF</p><form method='get'>" ) ;
client.print("<input type='submit' name='on' value='ON' /><input type='submit' name='off' value='OFF' /></form>" ) ;
client.print("</div></body></html>" ) ;
client.println();
Serial.println( " --- send responce --- ");
}
<メモ>
(行#10~13)
IPアドレス定義
(行#22)
IPアドレス設定
本例では、スケッチにIPアドレスや接続先 SSID 等の WiFi設定を記述しているが、スケッチには記述せずに スマホ等から設定する方法は、
スマホからIPアドレス等のWiFi設定を行う
を参照。
0 件のコメント:
コメントを投稿