Introducing or getting started to ESP32
Use Esp 32 Basic by using Wifi and Bluetooth
Components and supplies
1
LED (generic)
1
esp32 WROOM32
2
jumper wires male to female
Tools and machines
1
[HB] Mini Breadboard
Apps and platforms
1
Serial Bluetooth Terminal
1
Arduino IDE
Project description
Code
For ESP 32 Wifi
1/* WiFi Web Server LED Blink */ 2 3#include <WiFi.h> 4 5const char* ssid = "Your wifi ssid"; // Enter SSID name of your wifi router 6const char* password = "your wifi password"; // Enter password of your wifi router 7 8WiFiServer server(80); 9 10void setup() 11{ 12 Serial.begin(115200); 13 pinMode(2, OUTPUT); // set the LED pin mode 14 15 delay(10); 16 17 // We start by connecting to a WiFi network 18 19 Serial.println(); 20 Serial.println(); 21 Serial.print("Connecting to "); 22 Serial.println(ssid); 23 24 WiFi.begin(ssid, password); 25 26 while (WiFi.status() != WL_CONNECTED) { 27 delay(500); 28 Serial.print("."); 29 } 30 31 Serial.println(""); 32 Serial.println("WiFi connected."); 33 Serial.println("IP address: "); 34 Serial.println(WiFi.localIP()); 35 36 server.begin(); 37 38} 39 40void loop(){ 41 WiFiClient client = server.available(); // listen for incoming clients 42 43 if (client) { // if you get a client, 44 Serial.println("New Client."); // print a message out the serial port 45 String currentLine = ""; // make a String to hold incoming data from the client 46 while (client.connected()) { // loop while the client's connected 47 if (client.available()) { // if there's bytes to read from the client, 48 char c = client.read(); // read a byte, then 49 Serial.write(c); // print it out the serial monitor 50 if (c == '\n') { // if the byte is a newline character 51 52 // if the current line is blank, you got two newline characters in a row. 53 // that's the end of the client HTTP request, so send a response: 54 if (currentLine.length() == 0) { 55 // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) 56 // and a content-type so the client knows what's coming, then a blank line: 57 client.println("HTTP/1.1 200 OK"); 58 client.println("Content-type:text/html"); 59 client.println(); 60 61 // the content of the HTTP response follows the header: 62 client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 2 on.<br>"); 63 client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 2 off.<br>"); 64 65 // The HTTP response ends with another blank line: 66 client.println(); 67 // break out of the while loop: 68 break; 69 } else { // if you got a newline, then clear currentLine: 70 currentLine = ""; 71 } 72 } else if (c != '\r') { // if you got anything else but a carriage return character, 73 currentLine += c; // add it to the end of the currentLine 74 } 75 76 // Check to see if the client request was "GET /H" or "GET /L": 77 if (currentLine.endsWith("GET /H")) { 78 digitalWrite(2, HIGH); // GET /H turns the LED on 79 } 80 if (currentLine.endsWith("GET /L")) { 81 digitalWrite(2, LOW); // GET /L turns the LED off 82 } 83 } 84 } 85 // close the connection: 86 client.stop(); 87 Serial.println("Client Disconnected."); 88 } 89}
For ESP32 Bluetooth
1//* Turn LED ON or OFF from your phone 2 //* Using ESP32's Bluetooth and Mobile app 3 //* turn ON and OFF LED 4 5 6//This example creates a bridge between Serial and Classical Bluetooth (SPP) 7//and also demonstrate that SerialBT have the same functionalities of a normal Serial 8 9#include "BluetoothSerial.h" 10 11#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) 12#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it 13#endif 14 15BluetoothSerial SerialBT; 16int received;// received value will be stored in this variable 17char receivedChar;// received value will be stored as CHAR in this variable 18 19const char turnON ='H'; 20const char turnOFF ='L'; 21const int LEDpin = 2; 22 23void setup() { 24 Serial.begin(115200); 25 SerialBT.begin("ESP32_BLUETOOTH_LED"); //Bluetooth device name 26 Serial.println("The device started, now you can pair it with bluetooth!"); 27 Serial.println("To turn ON send: H");//print on serial monitor 28 Serial.println("To turn OFF send: L"); //print on serial monitor 29 pinMode(LEDpin, OUTPUT); 30 31} 32 33void loop() { 34 receivedChar =(char)SerialBT.read(); 35 36 if (Serial.available()) { 37 SerialBT.write(Serial.read()); 38 39 } 40 if (SerialBT.available()) { 41 42 SerialBT.print("Received:");// write on BT app 43 SerialBT.println(receivedChar);// write on BT app 44 Serial.print ("Received:");//print on serial monitor 45 Serial.println(receivedChar);//print on serial monitor 46 //SerialBT.println(receivedChar);//print on the app 47 //SerialBT.write(receivedChar); //print on serial monitor 48 if(receivedChar == turnON) 49 { 50 SerialBT.println("LED ON:");// write on BT app 51 Serial.println("LED ON:");//write on serial monitor 52 digitalWrite(LEDpin, HIGH);// turn the LED ON 53 54 } 55 if(receivedChar == turnOFF) 56 { 57 SerialBT.println("LED OFF:");// write on BT app 58 Serial.println("LED OFF:");//write on serial monitor 59 digitalWrite(LEDpin, LOW);// turn the LED off 60 } 61 62 63 64 } 65 delay(20); 66}
Downloadable files
ESP32 Bluetooth terminal app
VID_20250429_151346.mp4
For Bluetooth app
vid_20250429_145052-2 (360p).mp4
Comments
Only logged in users can leave comments