ESP32基板のLED(IO2) を PWM で調光し、明るさをテキスト(数値)設定する。
方法:
form の <input>でテキストボックスを使用して数値を入力/送信する。
ESP32で 文字列を取得して 数値(0~255) に変換し、 SigmaDelta の duty に与える。
例:
ON ボタンを押す度に 少しづつ明るく なり、OFF ボタンで消灯。
LED調光(PWM) からの主な変更箇所をハイライト。
/*
* WiFi LED ON/OFF TEST
* PWM Control
* Duty Input
*/
#include <WiFi.h>
const char* ssid = "hogehoge";
const char* password = "hogehogepaswd";
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);
byte led_brightness = 0 ;
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.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// for LED PWM Control ---------------------------------------------------
sigmaDeltaSetup(0, 312500); // setup channel 0 with frequency 312500 Hz
sigmaDeltaAttachPin(2,0); // attach pin 2 to channel 0
sigmaDeltaWrite(0, 0); // initialize channel 0 to off
}
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
int pos ;
String cmd = "" ;
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.print("client:") ;
Serial.println(line);
if ((pos= line.indexOf("GET /?led_v")) != -1) {
pos += 12 ;
while((line.charAt(pos) >='0') & (line.charAt(pos) <='9')) {
cmd += line.charAt(pos++) ;
}
pos = cmd.toInt() ;
if (pos>256) pos = 255 ;
led_brightness = (byte)pos ;
Serial.print("led_brightness : ");
Serial.println(led_brightness) ;
}
if (line.indexOf("GET /?on") != -1) { // Client request was "GET /?on"
if (led_brightness < 80) led_brightness = 80 ;
else if (led_brightness > 245) led_brightness = 255 ;
else led_brightness += 10 ;
Serial.print("led_brightness : ");
Serial.println(led_brightness);
}
if (line.indexOf("GET /?off") != -1) { // Client request was "GET /?off"
led_brightness = 0 ;
Serial.print("led_brightness : ");
Serial.println(led_brightness);
}
sigmaDeltaWrite(0, led_brightness) ; // set PWM value to channel#0
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.println("<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>" ) ;
client.println("<style>input.button {margin:8px;width:100px;}" ) ;
client.println(" input.button2 {margin-left:8px; width:40px;}" ) ;
client.println(" input.text {margin-left:8px; width:25px;}" ) ;
client.println(" div {font-size:16pt;text-align:center;width:250px;border:solid 4px #93ff93;} " ) ;
client.println(" </style>" ) ;
client.println("<title>Color LED Controller</title></head>" ) ;
client.println("" ) ;
client.println("<body><div><p>LED ON/OFF</p>" ) ;
client.println(" <form method='get'style='text-align:left'> " ) ;
client.println(" <span style='padding-left:15pt; font-size:8pt ;text-align:left'> LED brightness (0-255)</span> " ) ;
client.print(" <input class='text' type='text' name='led_v' value=" ) ;
client.print(led_brightness ) ;
client.println(" width='50px'><input class='button2' type='submit' name='set' value='SET'></form> " ) ;
client.println(" <form method='get'>" ) ;
client.println(" <input class='button' type='submit' name='on' value='ON'><input class='button' type='submit' name='off' value='OFF'></form>" ) ;
client.println(" </div></body>" ) ;
client.println("</html>" ) ;
client.println();
Serial.println( " --- send responce --- ");
}
HTML で 入力のテキストボックスを led_v の名前で作成。form の method は 'get'。
loop の中で client からの "GET /?led_v" を見つけたら、後に続く数字<0-9> を取得。
値が 255 を超えたら、255とする。
ON ボタンが押された場合 ("GET /?on") は明るさ を +10 して、押されるたびに明るくなるようにする。

実行しましたが、残念ながら応答が遅いためエラーが表示されてしまいました。
返信削除ちなみにGoogle chromeです。IEも同様にエラーとなりました。
当方では、通常 Firefoxを使用しています。再度 chrome, IE でも試しましたが、特にエラー等なく実行できました。
削除どのようなエラーが表示されたのでしょうか?
(ちなみに、9~15行目のWiFi,IP設定は各環境に応じて修正が必要です)
ESP32を勉強中です。大変重宝しています。
返信削除