{"id":144648,"date":"2024-08-06T16:33:11","date_gmt":"2024-08-06T11:03:11","guid":{"rendered":"https:\/\/www.electronicsforu.com\/?p=144648"},"modified":"2025-03-18T11:19:16","modified_gmt":"2025-03-18T05:49:16","slug":"led-controller-using-web-serial","status":"publish","type":"post","link":"https:\/\/www.electronicsforu.com\/electronics-projects\/led-controller-using-web-serial","title":{"rendered":"LED Controller Using WebSerial"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"alignright size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"152\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2016\/12\/Screen-Shot-2016-12-24-at-15.19.33-e1644389186872.png\" alt=\"\" class=\"wp-image-28226\"\/><figcaption class=\"wp-element-caption\">Comment errors or corrections found for this circuit, and get the chance to win big!<\/figcaption><\/figure><\/div>\n\n\n<p>LED controller that interfaces through WebSerial, allowing to control LEDs from a web browser. By setting up a web page with a user interface for controlling the LEDs, the Web Serial API can communicate with the Indus board via a USB connection. The web page sends serial commands to the board, which the Arduino sketch interprets to adjust the LED states accordingly. <\/p>\n\n\n\n<p>This setup leverages the Web Serial API&#8217;s capability to provide a seamless and interactive way to manage hardware devices like LEDs directly from a web application, making it ideal for projects requiring remote or user-friendly control interfaces.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Applications of LED Controllers using Webserial<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Control home lighting systems remotely via a web browser.<\/li>\n\n\n\n<li>Manage and change LED displays in real-time for dynamic visual effects in art projects.<\/li>\n\n\n\n<li>Quickly prototype and test LED-based projects with an easy-to-use web interface.<\/li>\n\n\n\n<li>Enhance DIY electronics projects with web-controlled lighting features.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Bill of Materials (BoM)<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Components <\/strong><\/td><td><strong>Description <\/strong><\/td><td><strong>Quantity<\/strong><\/td><\/tr><tr><td><a href=\"https:\/\/shop.electronicsforu.com\/product\/indusboard-iot-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">IndusBoard<\/a><\/td><td> 3cm sized dev board <\/td><td>1<\/td><\/tr><tr><td>LED<\/td><td> 5mm LED <\/td><td>1<\/td><\/tr><tr><td>Resistor<\/td><td> 1k ohm resistor <\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Code for LED Controller Using WebSerial<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h&gt;\n#if defined(ESP8266)\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;ESPAsyncTCP.h&gt;\n#elif defined(ESP32)\n#include &lt;WiFi.h&gt;\n#include &lt;AsyncTCP.h&gt;\n#endif\n#include &lt;ESPAsyncWebServer.h&gt;\n#include &lt;WebSerial.h&gt;\nAsyncWebServer server(80);\nconst char* ssid = \"ESP wifi\"; \/\/ Your WiFi AP SSID\nconst char* password = \"12345678\"; \/\/ Your WiFi Password\nconst int ledpin = 3;\n\/* Message callback of WebSerial *\/\nvoid recvMsg(uint8_t *data, size_t len){\nWebSerial.println(\"Received Data...\");\nString d = \"\";\nfor(int i=0; i &lt; len; i++){\nd += char(data&#91;i]);\n}\nWebSerial.println(d);\n\/\/control LED based on received message\nif (d == \"ON\") {\ndigitalWrite(ledpin, HIGH);\nWebSerial.println(\"LED is ON\");\n}\nelse if (d == \"OFF\") {\ndigitalWrite(ledpin, LOW);\nWebSerial.println(\"LED is OFF\");\n}\nelse {\nWebSerial.println(\"Unknown command\");\n}\n}\nvoid setup() {\nSerial.begin(115200);\npinMode(ledpin, OUTPUT);\nWiFi.softAP(ssid, password);\nIPAddress IP = WiFi.softAPIP();\nSerial.print(\"AP IP address: \");\nSerial.println(IP);\n\/\/ WebSerial is accessible at \"&lt;IP Address&gt;\/webserial\" in browser\nWebSerial.begin(&amp;server);\n\/* Attach Message Callback *\/\nWebSerial.msgCallback(recvMsg);\nserver.begin();\n}\nvoid loop() {\ndelay(2000);\nWebSerial.print(F(\"IP address: \"));\nWebSerial.println(WiFi.localIP());\nWebSerial.printf(\"Millis=%lu\\n\", millis());\nWebSerial.printf(\"Free heap=&#91;%u]\\n\", ESP.getFreeHeap());\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Connection<\/strong><\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"413\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Connection-LED-5mm-r-R-1k-U1.jpg\" alt=\"LED Controller Circuit\" class=\"wp-image-144654\" style=\"width:383px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Connection-LED-5mm-r-R-1k-U1.jpg 553w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Connection-LED-5mm-r-R-1k-U1-500x373.jpg 500w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Connection-LED-5mm-r-R-1k-U1-80x60.jpg 80w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Testing<\/strong><\/h2>\n\n\n\n<p>Now we connect the board with the USB and upload the code in the indusboard and check output after giving command on web serial. <\/p>\n\n\n\n<p>To connect the indusboard with web serial, type (192.168.4.1\/webserial) in web browser. <\/p>\n\n\n\n<p>Then we give command \u201cON\u201d on web serial, then LED starts glowing and when we give command \u201cOFF\u201d, then LED becomes off. And if there is another command than \u201cON\u201d or \u201cOFF\u201d, then it shows unknown command on web serial.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"805\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1-1024x805.jpg\" alt=\"LED Controller with Indusboard\" class=\"wp-image-144655\" style=\"width:412px;height:auto\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1-1024x805.jpg 1024w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1-500x393.jpg 500w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1-535x420.jpg 535w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1-696x547.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/Indusboard-1.jpg 1064w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"1024\" data-id=\"144658\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3-658x1024.jpg\" alt=\"LED Controller Using WebSerial\" class=\"wp-image-144658\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3-658x1024.jpg 658w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3-321x500.jpg 321w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3-270x420.jpg 270w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3-696x1084.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-3.jpg 822w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"1024\" data-id=\"144657\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1-662x1024.jpg\" alt=\"WebSerial based LED Controller\" class=\"wp-image-144657\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1-662x1024.jpg 662w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1-323x500.jpg 323w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1-272x420.jpg 272w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1-696x1076.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-1.jpg 828w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"698\" height=\"1024\" data-id=\"144656\" src=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2-698x1024.jpg\" alt=\"Control LEDs Using WebSerial\" class=\"wp-image-144656\" srcset=\"https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2-698x1024.jpg 698w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2-341x500.jpg 341w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2-286x420.jpg 286w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2-696x1020.jpg 696w, https:\/\/www.electronicsforu.com\/wp-contents\/uploads\/2024\/07\/WebSerial-2.jpg 873w\" sizes=\"auto, (max-width: 698px) 100vw, 698px\" \/><\/figure>\n<\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>Author(s): Manjeet Vishwakarma, &nbsp;Abhay Verma and Satywanti Kundu are B.Tech ECE students at GJUS&amp;T HISAR<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LED controller that interfaces through WebSerial, allowing to control LEDs from a web browser. By setting up a web page with a user interface for controlling the LEDs, the Web Serial API can communicate with the Indus board via a USB connection. The web page sends serial commands to the board, which the Arduino sketch [&hellip;]<\/p>\n","protected":false},"author":55758,"featured_media":144655,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,23,776,11851],"tags":[],"class_list":{"0":"post-144648","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-electronics-projects","8":"category-hardware-diy","9":"category-prototypes","10":"category-super-innovative-projects"},"_links":{"self":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/144648","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/users\/55758"}],"replies":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/comments?post=144648"}],"version-history":[{"count":2,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/144648\/revisions"}],"predecessor-version":[{"id":159615,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/posts\/144648\/revisions\/159615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/media\/144655"}],"wp:attachment":[{"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/media?parent=144648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/categories?post=144648"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.electronicsforu.com\/wp-json\/wp\/v2\/tags?post=144648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}