library/Arduino/BTCoderacer/BTCoderacer.ino

77 lines
1.6 KiB
Arduino
Raw Normal View History

//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 demonstrate that SerialBT have 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;
bool motioncontrol = false;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
String recvddata = "";
void loop() {
if (Serial.available()) {
SerialBT.print(Serial.readString());
}
if (SerialBT.available()) {
Serial.println("New incoming message");
SerialBT.setTimeout(10);
recvddata = SerialBT.readStringUntil('\0');
recvddata.remove(recvddata.length()-2,2); //remove delimiter from string
Serial.println(recvddata);
BTcontrol();
}
delay(20);
}
void BTcontrol()
{
if(recvddata.startsWith("motion_on"))
{
SerialBT.print("motion_ready");
motioncontrol = true;
return;
}
if(recvddata.startsWith("motion_off"))
{
SerialBT.print("motion_disabled");
motioncontrol = false;
return;
}
if(recvddata.startsWith("apr="))
{
if(motioncontrol == true)
{
SerialBT.print(BTmotioncontrol(recvddata));
}
else
{
SerialBT.print("motion_disabled");
}
}
}
String BTmotioncontrol(String recvddata)
{
return "motion_values_ok";
}