feat: receive commands from bt serial and send via ir
This commit is contained in:
parent
4dd48ac3f6
commit
5d28125651
17
.vscode/settings.json
vendored
Normal file
17
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"array": "cpp",
|
||||||
|
"*.tcc": "cpp",
|
||||||
|
"string": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"string_view": "cpp",
|
||||||
|
"functional": "cpp",
|
||||||
|
"iomanip": "cpp",
|
||||||
|
"istream": "cpp",
|
||||||
|
"limits": "cpp",
|
||||||
|
"ostream": "cpp",
|
||||||
|
"ratio": "cpp",
|
||||||
|
"sstream": "cpp",
|
||||||
|
"streambuf": "cpp"
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -13,3 +13,4 @@ platform = espressif32
|
|||||||
board = esp32doit-devkit-v1
|
board = esp32doit-devkit-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
lib_deps = bblanchon/ArduinoJson@^7.3.1
|
||||||
|
|||||||
96
src/main.cpp
96
src/main.cpp
@ -1,73 +1,89 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
const int IR_SEND_PIN = 12; // GPIO12 for the IR LED
|
#include <ArduinoJson.h>
|
||||||
|
const int IR_SEND_PIN = 12;
|
||||||
|
const int IR_RECEIVE_PIN = 14;
|
||||||
|
const int CONTROL_LED_PIN = 13;
|
||||||
#include <IRremote.hpp>
|
#include <IRremote.hpp>
|
||||||
#include "BluetoothSerial.h"
|
#include "BluetoothSerial.h"
|
||||||
|
|
||||||
BluetoothSerial SerialBT;
|
BluetoothSerial SerialBT;
|
||||||
|
|
||||||
const int ReceiverPin = 14; // GPIO14 for the IR receiver (TSOP48)
|
void handleIncomingSerialBTData(void *pvParameters);
|
||||||
const int LedPin = 13; // GPIO13 for the external LED
|
void handleIncomingIRSignals(void *pvParameters);
|
||||||
|
|
||||||
IRData data;
|
void sendDataBT(String data);
|
||||||
|
|
||||||
|
|
||||||
// put function declarations here:
|
|
||||||
int myFunction(int, int);
|
|
||||||
void parallelLoop(void *pvParameters);
|
|
||||||
void handleSerialBT(void *pvParameters);
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200); // Start the serial communication
|
Serial.begin(115200); // Start the serial communication
|
||||||
SerialBT.begin("ESP32");
|
SerialBT.begin("ESP32");
|
||||||
pinMode(LedPin, OUTPUT); // Set LED pin as output
|
pinMode(CONTROL_LED_PIN, OUTPUT); // Set LED pin as output
|
||||||
pinMode(IR_SEND_PIN, OUTPUT); // Set IR LED pin as output
|
pinMode(IR_SEND_PIN, OUTPUT); // Set IR LED pin as output
|
||||||
IrReceiver.begin(ReceiverPin, DISABLE_LED_FEEDBACK); // Initialize the IR receiver
|
IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK); // Initialize the IR receiver
|
||||||
|
IrSender.begin(IR_SEND_PIN); // Initialize the IR sender
|
||||||
Serial.println("IR Receiver Ready!");
|
Serial.println("IR Receiver Ready!");
|
||||||
xTaskCreate(parallelLoop, "parallelLoop", 2048, NULL, 1, NULL); // Create a task for parallel loop
|
|
||||||
xTaskCreate(handleSerialBT, "handleSerialBT", 2048, NULL, 1, NULL); // Create a task for serial BT communication
|
xTaskCreate(handleIncomingSerialBTData, "handleIncomingSerialBTData", 2048, NULL, 1, NULL);
|
||||||
|
xTaskCreate(handleIncomingIRSignals, "handleIncomingIRSignals", 2048, NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
if (IrReceiver.decode())
|
delay(200);
|
||||||
{ // Check if IR signal is received
|
|
||||||
IrReceiver.printIRResultShort(&Serial); // Print IR signal details to Serial Monitor
|
|
||||||
data = IrReceiver.decodedIRData;
|
|
||||||
digitalWrite(LedPin, HIGH); // Turn on the LED
|
|
||||||
delay(200); // Keep LED on for 200ms
|
|
||||||
digitalWrite(LedPin, LOW); // Turn off the LED
|
|
||||||
IrReceiver.resume(); // Prepare to receive the next IR signal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void parallelLoop(void *pvParameters)
|
void handleIncomingIRSignals(void *pvParameters)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
delay(5000);
|
if (IrReceiver.decode())
|
||||||
digitalWrite(IR_SEND_PIN, HIGH);
|
{
|
||||||
delay(5000);
|
IRData data;
|
||||||
digitalWrite(IR_SEND_PIN, LOW);
|
data = IrReceiver.decodedIRData;
|
||||||
|
IrReceiver.printIRResultShort(&Serial, &data, false);
|
||||||
|
digitalWrite(CONTROL_LED_PIN, HIGH);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(CONTROL_LED_PIN, LOW);
|
||||||
|
IrReceiver.resume();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handleSerialBT(void *pvParameters)
|
void handleIncomingSerialBTData(void *pvParameters)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (Serial.available()) {
|
String data = "";
|
||||||
SerialBT.write(Serial.read());
|
while (SerialBT.available())
|
||||||
}
|
{
|
||||||
if (SerialBT.available()) {
|
char c = SerialBT.read();
|
||||||
Serial.write(SerialBT.read());
|
data.concat(c);
|
||||||
}
|
}
|
||||||
delay(25);
|
if (data != "")
|
||||||
|
{
|
||||||
|
Serial.println(data);
|
||||||
|
|
||||||
|
JsonDocument doc;
|
||||||
|
deserializeJson(doc, data);
|
||||||
|
String protocol = doc["protocol"];
|
||||||
|
int address = doc["address"];
|
||||||
|
int command = doc["command"];
|
||||||
|
|
||||||
|
digitalWrite(CONTROL_LED_PIN, HIGH);
|
||||||
|
if (protocol == "samsung") {
|
||||||
|
|
||||||
|
IrSender.sendSamsung(address, command, 4);
|
||||||
|
}
|
||||||
|
digitalWrite(CONTROL_LED_PIN, LOW);
|
||||||
|
}
|
||||||
|
delay(25);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// put function definitions here:
|
void sendDataBT(String data)
|
||||||
int myFunction(int x, int y)
|
|
||||||
{
|
{
|
||||||
return x + y;
|
uint8_t buf[data.length()];
|
||||||
|
memcpy(buf, data.c_str(), data.length());
|
||||||
|
SerialBT.write(buf, data.length());
|
||||||
|
SerialBT.println();
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user