IRremoteESP8266 icon indicating copy to clipboard operation
IRremoteESP8266 copied to clipboard

About the ESP32-C3 infrared correlation

Open hzhh110 opened this issue 11 months ago • 22 comments

`#include <Arduino.h> #include <ETH.h> //引用以使用ETH #include <WiFiUdp.h> #include <WiFi.h> #include <WiFiClient.h> #include <HTTPClient.h> #include <WiFi.h> #include <Arduino.h> #include <IRremoteESP8266.h> #include <IRrecv.h> #include <IRutils.h> #include <IRtext.h> #include <IRutils.h> #include <IRac.h> #define DEBUG 1 #if DEBUG // 调试定义 #define DebugBegin(baud_rate) Serial.begin(baud_rate) #define DebugPrintln(message) Serial.println(message) #define DebugPrint(message) Serial.print(message) #define DebugPrintF(...) Serial.printf(VA_ARGS) #define BTSerial Serial1 #else #define DebugBegin(baud_rate) #define DebugPrintln(message) #define DebugPrint(message) #define DebugPrintF(...) #define BTSerial Serial1 #endif

const char *ssid = "HUAWEI-10G9TB";

const char *password = "88888888"; const uint16_t kRecvPin = 7; const uint16_t kCaptureBufferSize = 1024; const uint8_t kTimeout = 50; const uint16_t kFrequency = 38000; IRrecv irrecv(kRecvPin, kCaptureBufferSize, kTimeout, true); decode_results results;

const uint16_t kIrLedPin = 5;

IRsend irsend(kIrLedPin); void doWiFiTick(); void linkAll(void *parameter) {

while (1) {

doWiFiTick();
vTaskDelay(50);

} vTaskDelete(NULL); } void doWiFiTick() { if (WiFi.status() == WL_CONNECTED) { return; } wifi_mode_t mode = WiFi.getMode(); DebugPrintln("mode:"); DebugPrintln(mode); WiFi.mode(WIFI_STA); WiFi.disconnect(false, true); delay(100); wl_status_t status = WiFi.begin(ssid, password); DebugPrintln("status:"); DebugPrintln(status); if (status == WL_NO_SSID_AVAIL) { vTaskDelay(60000); } delay(100); vTaskDelay(500); for (size_t i = 0; i < 100; i++) { if (WiFi.status() == WL_CONNECTED) { DebugPrintln("WL_CONNECTED:"); DebugPrintln(i); vTaskDelay(500); break; } vTaskDelay(100); } } #include <BLEDevice.h> #include <BLEServer.h> #include <BLEUtils.h> #include <BLE2902.h> #define isESP32 0 #define SERVICE_UUID "FFF88888-8888-8888-8888-666666666666" // UART service UUID #define CHARACTERISTIC_UUID_RX "FFF88888-8888-8888-8888-777777777777" #define CHARACTERISTIC_UUID_TX "FFF88888-8888-8888-8888-555555555555" BLEServer *_pServer; String tempBtData = "";

BLECharacteristic *pCharacteristic; BLECharacteristic *pRXCharacteristic; // 对应App的writeUUIDString BLECharacteristic *pTXCharacteristic; // 对应App的readUUIDString boolean isNeedMainLoopBTData = false; String btData = ""; bool isBluetoothConnected = false; class MyCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) {

std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0)
{
  for (int i = 0; i < rxValue.length(); i++)
  {

    char a = char(rxValue[i]);
    if (a != '\0')
    {
      tempBtData += char(a);
    }
    else
    {
      if (isNeedMainLoopBTData)
      {
        return;
      }
      if (tempBtData.length() > 1300)
      {
        tempBtData = "";
        return;
      }
      btData = (String)tempBtData;
      tempBtData = "";
      isNeedMainLoopBTData = true;
      break;
    }
  }
}

} }; class MyServerCallbacks : public BLEServerCallbacks { void onConnect(BLEServer *pServer) { DebugPrintln("onConnect"); // connId = pServer->getConnId(); // initValidParams(); isBluetoothConnected = true; };

void onDisconnect(BLEServer *pServer) { // DebugPrintln("onDisconnect"); isBluetoothConnected = false;

pServer->getAdvertising()->start();

} }; void initBLE(const char *name) { DebugPrint("initBLE"); DebugPrintln(name); BLEDevice::init(name); #if isESP32 BLEDevice::setPower(ESP_PWR_LVL_P7, ESP_BLE_PWR_TYPE_DEFAULT); #else // #if isESP32_C3 BLEDevice::setPower(ESP_PWR_LVL_P18, ESP_BLE_PWR_TYPE_DEFAULT); #endif BLEServer *pServer = BLEDevice::createServer(); _pServer = pServer; pServer->setCallbacks(new MyServerCallbacks()); BLEService *pService = pServer->createService(SERVICE_UUID); pTXCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY); pTXCharacteristic->addDescriptor(new BLE2902()); BLECharacteristic *pRXCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX, BLECharacteristic::PROPERTY_WRITE); pRXCharacteristic->setCallbacks(new MyCallbacks()); pService->start(); pServer->getAdvertising()->start(); }

void setup() { Serial.begin(115200); // 设置波特率 // xTaskCreate( // linkAll, /任务函数/ // "linkAll", /带任务名称的字符串/ // 8 * 1024, /堆栈大小,单位为字节/ // NULL, /作为任务输入传递的参数/ // 2, /任务的优先级/ // NULL); /任务句柄/ WiFi.mode(WIFI_STA); WiFi.begin("HUAWEI-10G9TB","88888888"); initBLE("JH-P163-A0001");

irsend.begin(); // doWiFiTick(); irrecv.enableIRIn(); // 启动红外接收

while (!Serial) // 等待建立串口连接 delay(50); Serial.println(); Serial.print("IRrecvDemo is now running and waiting for IR message on Pin "); // 串口显示:接收装置已经就绪等待接收数据 这是你就发送红外信号了 Serial.println(kRecvPin); // 讲接收的红外信息显示在串口显示器中 } const uint8_t kTolerancePercentage = kTolerance; // const uint16_t kCaptureBufferSize = 1024; boolean loopCheckIR() { if (irrecv.decode(&results)) { DebugPrintln("-------------------------------"); DebugPrint("Protocol:"); DebugPrintln(results.decode_type); // Display a crude timestamp. uint32_t now = millis(); DebugPrintF(D_STR_TIMESTAMP " : %06u.%03u\n", now / 1000, now % 1000); // Check if we got an IR message that was to big for our capture buffer. DebugPrintln("44444444444"); if (results.overflow) DebugPrintF(D_WARN_BUFFERFULL "\n", kCaptureBufferSize); DebugPrintln("3333333333"); // Display the library version the message was captured with. DebugPrintln(D_STR_LIBRARY " : v" IRREMOTEESP8266_VERSION "\n"); DebugPrintln("111111111111"); // Display the tolerance percentage if it has been change from the default. if (kTolerancePercentage != kTolerance) DebugPrintF(D_STR_TOLERANCE " : %d%%\n", kTolerancePercentage); // Display the basic output of what we found. DebugPrintln("2222222222::::"); DebugPrintln(results.decode_type); decode_type_t decode_type = results.decode_type; DebugPrintln(resultToHumanReadableBasic(&results));

String detailsStr = (String) "P:" + decode_type + "\n" + resultToHumanReadableBasic(&results);

DebugPrintln("detailsStr:");
DebugPrintln(detailsStr);

DebugPrintln("---------------");
String description = IRAcUtils::resultAcToString(&results);
if (description.length())
  DebugPrintln(D_STR_MESGDESC ": " + description);
yield(); // Feed the WDT as the text output can take a while to print.

#if LEGACY_TIMING_INFO DebugPrintln(resultToTimingInfo(&results)); yield(); // Feed the WDT (again) #endif // LEGACY_TIMING_INFO DebugPrintln(resultToSourceCode(&results)); DebugPrintln(); // Blank line between entries yield(); // Feed the WDT (again) } return true; }

uint16_t rawData[279] = {9068, 4450, 734, 1484, 728, 464, 756, 438, 724, 468, 730, 1508, 700, 1486, 728, 464, 732, 460, 758, 470, 648, 1530, 760, 430, 756, 1458, 734, 490, 696, 466, 728, 464, 726, 466, 716, 474, 716, 480, 752, 440, 752, 438, 730, 464, 726, 1486, 724, 466, 734, 460, 652, 540, 730, 462, 754, 438, 734, 462, 756, 1454, 654, 536, 728, 1486, 712, 550, 698, 466, 756, 1456, 728, 464, 752, 20060, 730, 464, 728, 466, 730, 466, 650, 540, 756, 438, 652, 540, 730, 464, 710, 482, 752, 444, 754, 438, 728, 466, 756, 438, 732, 494, 730, 436, 732, 458, 656, 538, 756, 438, 728, 466, 730, 464, 752, 442, 724, 466, 754, 438, 754, 440, 752, 440, 724, 468, 724, 464, 758, 436, 730, 462, 758, 1456, 652, 540, 730, 1482, 756, 436, 734, 39994, 9082, 4438, 732, 1510, 730, 432, 654, 540, 726, 496, 700, 1512, 686, 1524, 700, 464, 726, 466, 726, 464, 734, 1476, 734, 460, 744, 1468, 730, 492, 700, 462, 760, 434, 754, 438, 756, 466, 694, 468, 746, 444, 734, 460, 728, 470, 744, 1466, 756, 438, 764, 430, 752, 440, 730, 464, 734, 486, 700, 464, 756, 1458, 728, 1486, 726, 1512, 694, 476, 730, 466, 730, 1480, 728, 460, 682, 19840, 762, 434, 756, 436, 652, 540, 656, 536, 730, 464, 730, 462, 758, 436, 738, 454, 758, 438, 712, 478, 728, 462, 760, 462, 730, 432, 656, 538, 728, 492, 730, 438, 732, 464, 728, 462, 736, 458, 652, 540, 730, 468, 728, 488, 728, 1486, 700, 464, 728, 464, 726, 466, 756, 432, 760, 464, 726, 1486, 732, 438, 648, 540, 758, 1454, 728}; // KELVINATOR uint8_t state[16] = {0x31, 0x0A, 0x20, 0x50, 0x00, 0x00, 0x00, 0x50, 0x31, 0x0A, 0x20, 0x70, 0x00, 0x00, 0x40, 0x90}; // uint8_t state[16] = {0x39, 0x0A, 0x20, 0x50, 0x00, 0x00, 0x00, 0xD0, 0x39, 0x0A, 0x20, 0x70, 0x00, 0x00, 0x40, 0x10}; void loop() {

irsend.sendRaw(rawData, 279, 38); loopCheckIR(); delay(2000); int bits = irsend.defaultBits(NEC); DebugPrintln("bits"); DebugPrintln(bits);

irsend.send(NEC, 0xDDDD, bits);

loopCheckIR();

delay(2000); loopCheckIR(); } `

When wifi and Bluetooth are running at the same time, the data transmitted by the infrared function is very unstable, and in many cases the air conditioner can not resolve it, and when the wifi connection is closed, it is very stable, and the air conditioner can receive the signal stably.

hzhh110 avatar Jul 26 '23 00:07 hzhh110