Purpose:
“ESP32 uses BT SPP Profile to connect with mobile phones or PCs and transmit strings through BT function to perform specific functional actions for functional strings to switch relay board.”
Circuit:
Demo:
YouTube:ESP32 code:
#include <BluetoothSerial.h>
BluetoothSerial BT;
void setup()
{
Serial.begin(9600);
BT.begin("BT_winson");// BTName為藍芽廣播名稱
Serial.println(F("init"));
/** #ifdef ENABLE_LEDTASK
initledTask();
#endif
#ifdef ENABLE_UARTTASK
inituartTask();
#endif
**/
}
void loop()
{
while (BT.available())
{
String BTdata = BT.readString();
BTprocessCommand(BTdata); // do something with the command
}//while (BT.available())
}
//------------------------------------------------------------------------------
void BTprocessCommand(String data)
{
if (data == "LED_OFF")
{
Serial.println(F("LED_OFF"));
vTaskSuspend(hled);
}
if (data == "LED_ON")
{
Serial.println(F("LED_ON"));
vTaskResume(hled);
}
if (data == "RELAY1_ON")
{
pinMode(32, OUTPUT);
digitalWrite(32, LOW);
Serial.println(F("RELAY1_ON"));
}
if (data == "RELAY1_OFF")
{
pinMode(32, OUTPUT);
digitalWrite(32, HIGH);
Serial.println(F("RELAY1_OFF"));
}
if (data == "RELAY2_ON")
{
pinMode(33, OUTPUT);
digitalWrite(33, LOW);
Serial.println(F("RELAY2_ON"));
}
if (data == "RELAY2_OFF")
{
pinMode(33, OUTPUT);
digitalWrite(33, HIGH);
Serial.println(F("RELAY2_OFF"));
}
if (data == "RELAY3_ON")
{
pinMode(25, OUTPUT);
digitalWrite(25, LOW);
Serial.println(F("RELAY3_ON"));
}
if (data == "RELAY3_OFF")
{
pinMode(25, OUTPUT);
digitalWrite(25, HIGH);
Serial.println(F("RELAY3_OFF"));
}
if (data == "RELAY4_ON")
{
pinMode(26, OUTPUT);
digitalWrite(26, LOW);
Serial.println(F("RELAY4_ON"));
}
if (data == "RELAY4_OFF")
{
pinMode(26, OUTPUT);
digitalWrite(26, HIGH);
Serial.println(F("RELAY4_OFF"));
}
}
沒有留言:
張貼留言