ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

juanjo__c
Posts: 4
Joined: Wed Apr 29, 2020 3:30 am

ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby juanjo__c » Sun May 03, 2020 6:22 am

Hello everyone

I am trying to make a code to control the intensity of incandescent lamp (AC voltage). I use a signal zero cross circuit to synchronize the signal control of the ac load. The value of the intensity is given by a bluetooth device (android). The initial intensity is 50%. My code is the following:

Code: Select all

#include "BluetoothSerial.h"
BluetoothSerial SerialBT;

short eValvula=4;
short OPTO = 34;

float intensity;
unsigned long shooting_time = 8333;
double t1, t1_1;
long tiempoBLE_1=0;

void setup() 
{
  //Serial.begin(115200);
  SerialBT.begin("Chi"); //Bluetooth device name

  pinMode(eValvula,OUTPUT);
  
  pinMode(OPTO, INPUT);

}

void loop() 
{  
  if (SerialBT.available())
  {
    intensity=SerialBT.parseFloat();
  }

    shooting_time = map(intensity, 0, 100, 8333, 0);
      
    if (digitalRead(OPTO)==1)
    {
      digitalWrite(eValvula, LOW);
      //SerialBT.println(t1-t1_1);
//      SerialBT.print(" ");
//      SerialBT.println(intensity);
      t1_1 = micros();
    }

    t1 = micros();
    if ((abs(t1 - t1_1) >= shooting_time) && (abs(t1 - t1_1) <= 8333))
    {
      digitalWrite(eValvula, HIGH);
    }
    else
    {
      digitalWrite(eValvula, LOW);
    }

    if(millis()-tiempoBLE_1>7)
    {
      tiempoBLE_1=millis();
      SerialBT.println(tiempoBLE_1-millis());
    }
    
}
First when android device is disconnected, the code runs normally even when the intensity of the incandescent lamp is 50%, I mean that the incandescent lamp does not show any visible noise and the intensity is so continuous. BUT when I pair the android device with the ESP32 (via bluetooth), the intensity seems like something injects noise but it still communicating. So I don't know how to fix it. Because the code I put above is the same that i worked with a HC05

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby ESP_Sprite » Sun May 03, 2020 8:48 am

Moving to the Arduino subforum

Bluetooth will eat up some of the processing time of the ESP32 and can indeed cause latency issues, although iirc Arduino runs BT on another core than the user program task. Does your code still have timing issues when you comment out the SerialBT.println line? It might be that that is blocking for some BT-related thing to be ready, causing a slight delay.

juanjo__c
Posts: 4
Joined: Wed Apr 29, 2020 3:30 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby juanjo__c » Sun May 03, 2020 6:22 pm

ESP_Sprite wrote:
Sun May 03, 2020 8:48 am
Moving to the Arduino subforum

Bluetooth will eat up some of the processing time of the ESP32 and can indeed cause latency issues, although iirc Arduino runs BT on another core than the user program task. Does your code still have timing issues when you comment out the SerialBT.println line? It might be that that is blocking for some BT-related thing to be ready, causing a slight delay.
Hi. thank you for your reply. Ok I saw that ESP32 has 2 cores. But I want to use the second core to annother task different from bluetooth connection.

In response to your question " Does your code still have timing issues when you comment out the SerialBT.println line?" Yes, It still. even when I don't send or receive something. The only way to avoid that mistake is:

- Don't pair android device
- comment SerialBT initialization and related methods (SerialBT.begin, SerialBT.read, SerialBT.Write..... etc)

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby ESP_Sprite » Mon May 04, 2020 6:02 am

In that case, there is little you can do with this code; your code effectively assumes that the core is 100% under your disposal at all times, while in practice BT sometimes needs to respond to things and steals some time. You may be better off doing things in hardware: either see if you can put the MCPWM peripheral to some use for this, or see if you can use the LEDC peripheral.

juanjo__c
Posts: 4
Joined: Wed Apr 29, 2020 3:30 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby juanjo__c » Tue May 05, 2020 7:15 pm

ESP_Sprite wrote:
Mon May 04, 2020 6:02 am
In that case, there is little you can do with this code; your code effectively assumes that the core is 100% under your disposal at all times, while in practice BT sometimes needs to respond to things and steals some time. You may be better off doing things in hardware: either see if you can put the MCPWM peripheral to some use for this, or see if you can use the LEDC peripheral.
Ok, but when I run with cable serial it does not fail, it is so faster and the lamp intensity is so clear without noise. I am going to try with the MCPWM peripheral as you suggested but, in my opinion if the SerialBT affects the pin assignment as either HIGH or LOW, it will also affect the assignment of the number that sets the duty cycle of the LEDC. Wouldn't it?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby ESP_Sprite » Tue May 05, 2020 9:04 pm

BT does not affect the IO pins, but it does affect the timing of your loop, causing the GPIO to go lower later than you wanted it to (because the core was busy handling BT requests).

juanjo__c
Posts: 4
Joined: Wed Apr 29, 2020 3:30 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby juanjo__c » Wed May 06, 2020 4:30 pm

Ok. I don't know if there is a way to execute a pin higher priority interrupt than Bluetooth communication?

I mean that a rising, falling, change ... etc. in a pin cause an interrupt no matter what a bluetooth event is triggered?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 code speed depends on "Classic Bluetooth" client connection/disconnection?

Postby ESP_Sprite » Thu May 07, 2020 10:33 am

Erm, no, no interrupt is triggered and the fact that you happen to be toggling GPIOs is entirely not relevant to your problem. Your issue is that you're trying to get exact timing done by having the CPU look at the clock constantly. If it gets distracted because something over BT comes in while the clock gets the value it needs, it won't see that until it's done with the BT request and react too late.

Who is online

Users browsing this forum: Google [Bot] and 111 guests