ESP32 Board: Turn On and Off an LED Using Bluetooth from a Mobile Device
In this video, we show how to turn an LED on or off using your mobile phone and an ESP32.
To learn how to control three LEDs, see this page.
237-ESP32 Code to turn ON/OFF 1 LED using Bluetooth (code 1)
Язык: C++
/*
* Turn LED ON or OFF from your phone
*
* Using ESP32's Bluetooth and mobile app
* turn ON and OFF the LED
*
* Updated/Written by Ahmad Shamshiri
* On August 25, 2019 in Ajax, Ontario, Canada
* Watch video instructions for this code:
* https://youtu.be/4OOFlS75owA
* www.Robojax.com
*
Get this code and other Arduino codes from Robojax.com.
Learn Arduino step by step in a structured course with all materials, wiring diagrams and libraries
all in one place.
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal http://robojax.com/L/?id=64
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrates that SerialBT has the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON ='a';
const char turnOFF ='b';
const int LEDpin = 23;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Robojax"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
Serial.println("To turn ON send: a");//print on serial monitor
Serial.println("To turn OFF send: b"); //print on serial monitor
pinMode(LEDpin, OUTPUT);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
if(receivedChar == turnON)
{
SerialBT.println("LED ON:");// write on BT app
Serial.println("LED ON:");//write on serial monitor
digitalWrite(LEDpin, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF)
{
SerialBT.println("LED OFF:");// write on BT app
Serial.println("LED OFF:");//write on serial monitor
digitalWrite(LEDpin, LOW);// turn the LED off
}
}
delay(20);
}
238-ESP32 Code to turn ON/OFF 3 LED using Bluetooth (code 2)
Язык: C++
/*
/*
* Turn LED ON or OFF for 3 LEDs from your phone
*
* Using ESP32's Bluetooth and Mobile app
* turn ON and OFF LEDs for 3 LEDs
*
*
* Using ESP32's Bluetooth and Mobile app
* turn ON and OFF LEDs for 3 LEDs
*
* Updated/Written by Ahmad Shamshiri
* On August 25, 2019 in Ajax, Ontario, Canada
* Updated for 3 LEDs on August 24, 2021
* Watch video instruction for this code:
* https://youtu.be/4OOFlS75owA
* www.Robojax.com
*
Get this code and other Arduino codes from Robojax.com
If you found this tutorial helpful, please support me so I can continue creating
content like this.
or make a donation using PayPal https://paypal.me/robojaxTV
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrates that SerialBT has the same functionalities of a normal Serial
* * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.*
* This code has been downloaded from Robojax.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrates that SerialBT has the same functionalities of a normal Serial
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
BluetoothSerial SerialBT;
int received;// received value will be stored in this variable
char receivedChar;// received value will be stored as CHAR in this variable
const char turnON1 ='a';
const char turnOFF1 ='b';
const int LEDpin1 = 23;
const int LEDpin2 = 22;
const char turnON2 ='c';
const char turnOFF2 ='d';
const int LEDpin3 = 20;
const char turnON3 ='e';
const char turnOFF3 ='f';
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_Robojax"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
pinMode(LEDpin1, OUTPUT);
pinMode(LEDpin2, OUTPUT);
pinMode(LEDpin3, OUTPUT);
}
void loop() {
receivedChar =(char)SerialBT.read();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
SerialBT.print("Received:");// write on BT app
SerialBT.println(receivedChar);// write on BT app
Serial.print ("Received:");//print on serial monitor
Serial.println(receivedChar);//print on serial monitor
//SerialBT.println(receivedChar);//print on the app
//SerialBT.write(receivedChar); //print on serial monitor
if(receivedChar == turnON1)
{
SerialBT.println("LED1 ON:");// write on BT app
Serial.println("LED1 ON:");//write on serial monitor
digitalWrite(LEDpin1, HIGH);// turn the LED ON
}
if(receivedChar == turnON2)
{
SerialBT.println("LED2 ON:");// write on BT app
Serial.println("LED2 ON:");//write on serial monitor
digitalWrite(LEDpin2, HIGH);// turn the LED ON
}
if(receivedChar == turnON3)
{
SerialBT.println("LED3 ON:");// write on BT app
Serial.println("LED3 ON:");//write on serial monitor
digitalWrite(LEDpin3, HIGH);// turn the LED ON
}
if(receivedChar == turnOFF1)
{
SerialBT.println("LED1 OFF:");// write on BT app
Serial.println("LED1 OFF:");//write on serial monitor
digitalWrite(LEDpin1, LOW);// turn the LED off
}
if(receivedChar == turnOFF2)
{
SerialBT.println("LED2 OFF:");// write on BT app
Serial.println("LED2 OFF:");//write on serial monitor
digitalWrite(LEDpin2, LOW);// turn the LED off
}
if(receivedChar == turnOFF3)
{
SerialBT.println("LED3 OFF:");// write on BT app
Serial.println("LED3 OFF:");//write on serial monitor
digitalWrite(LEDpin3, LOW);// turn the LED off
}
}
delay(20);
}
Ресурсы и ссылки
-
ВнешнийРепозиторий ESP32 на GitHubgithub.com
-
Внутренний
Файлы📁
Нет доступных файлов.