Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Cannot use IRSend along with BLE #1031

Closed
Make-It-Racing opened this issue Jan 29, 2020 · 6 comments
Closed

[ESP32] Cannot use IRSend along with BLE #1031

Make-It-Racing opened this issue Jan 29, 2020 · 6 comments
Assignees
Labels

Comments

@Make-It-Racing
Copy link

Device used:

ESP32 (nodemcu-32s) with Arduino framework

Version/revision of the library used

v2.7.2

I can send IR codes from my ESP32 very reliably using this code:

void loop() {
        unsigned long t1 = micros(), t2;
        irsend.sendSony(0x9999, 16, 2);  
        delay(1000);  
        t2 = micros();  
        Serial.printf("round of ir takes %ld micros\n", (t2 - t1));          
    }

it works very good with all the following libraries imported:

#include <Arduino.h>  
#include <BLE2902.h>  
#include <BLEDevice.h>  
#include <BLEServer.h>  
#include <BLEUtils.h>  
#include <IRrecv.h>  
#include <IRremoteESP8266.h>  
#include <IRsend.h>  
#include <IRutils.h>

IRsend is defined in my code as a global variable:
IRsend irsend(18); (using pin D18)

and my setup() method looks like this [relevant code only...]

void setup() {
    Serial.begin(38400);
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    // BLEDevice::init(NAMESTRING);

   /*
   ... 
   more commented out BLE code
   ...
   */

  irsend.begin();
}

this code (with the BLEDevice::init commented out) runs perfectly without any issues.
as soon as I un-comment this line, I cannot seem to send my IR codes in a reliable fashion. (meaning that on average about 5-10% of the times the code is sent correctly, but most of the times garbage values are seems to be sent.)

To check myself for receiving the correct codes, I use multiple Arduinos and ESP32 with several types of receivers (all work on 38 KHz).

@crankyoldgit
Copy link
Owner

irsend.sendSony(0x9999, 16, 2);

FYI, there is no 16 bit Sony protocol that I'm aware of. It's 12, 15, or 20 bits per the published docs.

See: https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRremoteESP8266.h#L829-L831

Re: Bluetooth
You're the first person to report issues with BLE on the ESP32. So we are in uncharted waters.
I've never used BLE on the ESP32 so I don't have any references, experience, or expectations either.

So here are some things to try:

  1. Try a different GPIO pin? GPIO18 is VSPI CLK. No idea if BLE is using VSPI. (Unlikely, but worth a shot)
  2. Try setting ALLOW_DELAY_CALLS to false. See https://github.com/crankyoldgit/IRremoteESP8266/blob/master/src/IRremoteESP8266.h#L589 (This might making timing more reliable)
  3. Download and try the latest master branch and try the new sendSony38() (See issue with Sony reception on some new(er) Sony devices in Issue sendSony once again #1018) (Unlikely, but worth a shot)

Also, can you provide me with rawData samples of a "successful" message and an "unsuccessful" message captured using IRrecvDumpV2 on a separate ESP32/ESP8266 please?

What I think is happening ...

I think Bluetooth is interfering with the timing routines used by IRremoteES8266's send functions.
Perhaps disable Bluetooth when you go to send, and re-enable afterwards?
Doing some quick research, it looks the Bluetooth code uses Core 0 on the ESP32, thus it's competing for CPU time/resources with the IR library. We could explore using hardware PWM for generating the IR signal. This would solve timing issues with the software PWM implementation, but it won't solve issues with how long the mark & space IR pulses are. Hence I'm going to need more capture data (or data/analysis from a Oscilloscope) to see what's going on.

@crankyoldgit
Copy link
Owner

For reference, the quick research was some Googling which led me to: nkolban/esp32-snippets#937

@Make-It-Racing
Copy link
Author

Thank you for the detailed and fast reply!
#1 and #2 didn't work but I moved the ir send functionality to CORE 1 by using xTaskCreatePinnedToCore
and everything seems to be working now!

@crankyoldgit
Copy link
Owner

Excellent. I'm glad you solved it. Care to give a snippet of example code so I can add it to an FAQ so someone else can find it?

@Make-It-Racing
Copy link
Author

@crankyoldgit

TaskHandle_t send_ir_task;
IRsend irsend(18);

void send_ir_f(void *param) {
    irsend.begin();
    delay(100);
    while (1) {
        unsigned long t1 = micros(), t2;
        irsend.sendSony(0x6666, 16, 2);
        delay(100);
        t2 = micros();
        Serial.printf("round of ir takes %ld micros\n", (t2 - t1));        
    }
}


void setup() {
    ... 
    xTaskCreatePinnedToCore(
        send_ir_f,     /* Task function. */
        "Send IR code",    /* name of task. */
        2000,           /* Stack size of task */
        NULL,           /* parameter of the task */
        1,              /* priority of the task */
        &send_ir_task, /* Task handle to keep track of created task */
        1); 
    ...
}

@crankyoldgit
Copy link
Owner

@playrobotics-alex Many thanks. It's now in the FAQ.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants