Hc 06(bluetooth module) slows down when I send data

I used wemos d1 mini to check this situation with Arduino IDE.

double counter = 0;
double Setpoint;
String Signal;

void setup() {
   // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  
  while (Serial.available()>0)
    {
      char wait = Serial.read();         
      //Signal.concat(wait);
      if ( wait == '\n')
      {
        //Serial.print("I receved : ");
        //Serial.println(Signal);
        //Setpoint = Signal.toInt();
        //Signal = "";
      }
    }
    
    
    Serial.println(counter);   
    counter = counter+1;
    delay(500);

}

this is simple code.

When I use the USB serial port, it works fine regardless of the terminal input.

But when I use Rx,Tx pin for HC-06, it works not fine.(It works fine before when I send data from my smartphone.)

What do I need to fix to run it like when using the usb port???????????

I would recommend you to not use Arduino strings...You can use char arrays instead...

Have you posted the correct code? You have delay(500) at the end of loop, but you are showing a print out of the incremented count at around 90-100 ms intervals.

cattledog

You are right
last delay was 100ms

delay(500) is my typo.

I tested other situation.

I tried connecting hc06 directly to the gps module.
(China Beitian Gnss Module Flight Control Glonass GPS Module Compass Hmc5883L AMP2.6/Pix4/Pixhawk Bn-880 - China GPS Module, Flight Control GPS Module)

It works fine until I send the data using the Bluetooth terminal(android smart phone).
When I send data, hc06 was starting to slow down.

I guess what I have to hc06 setup at ATmode.

What shoul I do at ATmode ?

Can you please provide a schematic of how the HC06, the flight module, and the Wemos d1 mini are all connected.

How is everything powered? What is running at what voltage?

Please explain more about what you are trying to do.

https://ibb.co/7byqCTW
It’s so hard to draw lol.

This is my project. I made pid controller for heat line.

Testing with usb port(not connect BT) was good! but connecting BTmodule was not fine.

So I testing this

double counter = 0;
double Setpoint;
String Signal;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {

while (Serial.available()>0)
{
char wait = Serial.read();
//Signal.concat(wait);
if ( wait == ‘\n’)
{
//Serial.print("I receved : ");
//Serial.println(Signal);
//Setpoint = Signal.toInt();
//Signal = “”;
}
}

Serial.println(counter);
counter = counter+1;
delay(100);

}

testing about usb_port vs BT_module

In this testing, despite the same code, there was a difference between usb port and BT_module.

usb port mode(testing 1 in fig.) didn’t slowing down when I send any data.

But BT_module mode(testing 2 in fig.) was starting to slow down when I send data.

so I replacment String to char arry but it was not effect.

So I guess the code is not a problem and the BT_module configuration is wrong.

cattledog

if you wnat about gps<->BT_module

this is testing with gps

This is my project. I made pid controller for heat line.

Let's focus on the actual project and sketch and not introduce additional things like the GPS module.

Why are you using a motor controller to drive the heat line?
What are you trying to change with either Serial or BT input?

In my experience full PID is not very useful in heating applications, and there may be more simple ways to do what you need. What are you heating, and with what kinds of thermal loads and changing setpoints?

In my experience BT is just Serial without wires, and I would not expect any functional difference with input from a terminal or input from a phone/bt module.

It's not clear how you measure the "slowing down". Yes there may be some latency from the phone and bt but I'm not certain how that effects the program.

Perhaps it would be best to post the actual program with the PID. To post the program, first have it in a IDE window. Then use Tools > Auto Format to get it properly aligned. Then use Edit > Copy for Forum and paste into your post.

You are right

PID can be inefficient at controlling heat.

I just wanted to try it. Just think of it as an intellectual vanity

and why I uesd motor controller.

I needed FET for voltige controll but I don’t have it

so I used that. I just have it.

The reason I used wemos d1 is also the same.

And I trying to change temp of heat line with BT.

My heating wire is small and controlled with voltage. Resistance is 3.5 ohm so I used motor controller.

this video is problem slowing down(through esp8266)
this video is problem slowing down(through GPSmodule)
This problem occurs regardless of the code.

So I guess I have wrong BT_module setup at AT_mode.

My BT_module setup is AT+UART:9600,0,0

The reason I didn’t upload all my code is because my code is not perfect.

So tested with very short and easy code that can testing on the problem and the problem was clearly reproduced.

That’s why I didn’t use my stupid code to ask people about this problem, but instead asked the answer to the problem in short and easy code. For protecting their eyes lol.

“”
In my experience BT is just Serial without wires, and I would not expect any functional difference with input from a terminal or input from a phone/bt module.
“”
I completely agree with this.

So I don’t understand why this problem occurs.

this is my full code

///################power control setting################
#define IN1_pin 14 //d5
#define IN2_pin 15 //d6

///################lm35 setting################
#define outputpin 0
int lm_count = 30;

///################pid Setting################
#include <PID_v1.h>

double Setpoint, Input, Output;
double Kp = 1, Ki = 0.05, Kd = 0.25;
double aggKp = 4, aggKi = 2, aggKd = 1;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

///################BT################
byte buffer[20];
int bufferPosition;

///###############wifi off###############
#include <ESP8266WiFi.h>


void setup()
{
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  delay(1);


  pinMode(IN2_pin, OUTPUT);
  analogWrite(IN1_pin, 0);
  digitalWrite(IN2_pin, 0);
  Serial.begin(9600);


  Input = int(Avg_Temp(lm_count) * 100);
  Setpoint = 0;
  bufferPosition = 0;

  myPID.SetMode(AUTOMATIC);



}

void loop()
{
  while (Serial.available())
  {
    byte data = Serial.read();
    buffer[bufferPosition++] = data;
    if (data == '\n')
    {
      Setpoint = (make_num(bufferPosition, buffer, 3));
      bufferPosition = 0;
    }
    yield();
  }


  double now_temp = Avg_Temp(lm_count);
  Input = now_temp * 100;
  double gap = abs(Setpoint - Input);
  if (gap < 100)
  {
    myPID.SetTunings(Kp, Ki, Kd);
  }
  else
  {
    myPID.SetTunings(aggKp, aggKi, aggKd);
  }
  myPID.Compute();
  Set_power(map(Output, 0, 255, 0, 1023));
  update(Setpoint, now_temp, Output);

  yield();
}

void update(int Setpoint_, double now_temp_, int Output_)
{

  Serial.print("E");
  Serial.print(double(Setpoint_ / 100));
  Serial.print(",");
  Serial.print(now_temp_);
  Serial.print(",");
  Serial.print(map(int(Output_), 0, 255, 0, 10));
  Serial.print("\n");

}


void Set_power(int P_mode)
{
  analogWrite(IN1_pin , P_mode);
}


double get_Temp_Data()
{
  int rawvoltage = analogRead(outputpin);
  double millivolts = (rawvoltage / 1024.0) * 3300;
  double celsius = millivolts / 10;
  return (celsius);
}

double Avg_Temp(int count)
{
  double temp;
  double Max_Min_val[] = {0, 150};
  double sum = 0;
  double avg = 0;
  for (int i = 0; i < count; i = i + 1)
  {

    temp = get_Temp_Data();

    if (temp >= Max_Min_val[0])
    {
      Max_Min_val[0] = temp;
    }
    if (temp < Max_Min_val[1])
    {
      Max_Min_val[1] = temp;
    }
    sum = sum + temp;

    delay(10);
  }
  /*
    #### monitorring for bug fix!
    Serial.print("count = ");
    Serial.println(count);
    Serial.print("Max_Min_val = ");
    Serial.print(Max_Min_val[0]);
    Serial.print(", " );
    Serial.println(Max_Min_val[1]);
  */
  sum = sum - Max_Min_val[0] - Max_Min_val[1];
  avg = sum / (count - 2);

  return (avg);
}

int make_num(int digit, byte data[], int remove_digits)
{
  int output = 0;
  int counter = 0;
  for (int i = digit - remove_digits ; i >= 0 ; i = i - 1)
  {
    output = output + pow(10, int(counter)) * (data[i] - 48);
    counter++;
  }
  return int(output);
}

I have only studied the video output of the code running on the wemos esp 8266.

I think you are seeing artifacts of the serial output and the time stamping. You have free running with timing generated by the pid calculations and serial printing. I'm unclear if your code really turns off all background wifi maintenance.

You can certainly speed up the Serial baud rate. The HC06 will respond to AT commands without doing anything special, and there is plenty on the web about how to change the settings.

I would not be concerned with what you are seeing as to the pattern of output.

@3942368

Do not PM me for asking questions. Post them in the forum so everyone will benefit from seeing the answers. Also, more people can answer your questions. It's better you "learn how to use the forum" especially 17th point in reply #1. Thanks!

One possible test you can do to see if what you have is an artifact is to start with the setpoint at 4500 and then send a phone message to set it to 0. How does the output timing look with this arrangement?

///################power control setting################
#define IN1_pin 14 //d5
#define IN2_pin 15 //d6

///################lm35 setting################
#define outputpin 0
int lm_count = 30;

///################pid Setting################
#include <PID_v1.h>

double Setpoint, Input, Output;
double Kp = 1, Ki = 0.05, Kd = 0.25;
double aggKp = 4, aggKi = 2, aggKd = 1;

PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

///################BT################
byte buffer[20];
int bufferPosition;

///###############wifi off###############
#include <ESP8266WiFi.h>


void setup()
{
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  delay(1);


  pinMode(IN2_pin, OUTPUT);
  analogWrite(IN1_pin, 0);
  digitalWrite(IN2_pin, 0);
  Serial.begin(9600);


  Input = int(Avg_Temp(lm_count) * 100);
  Setpoint = 0;
  bufferPosition = 0;

  myPID.SetMode(AUTOMATIC);



}

void loop()

{

[b]  long now = millis();[/b]


  while (Serial.available())
  {
    byte data = Serial.read();
    buffer[bufferPosition++] = data;
    if (data == '\n')
    {
      Setpoint = (make_num(bufferPosition, buffer, 3));
      bufferPosition = 0;
    }
    yield();
  }


  double now_temp = Avg_Temp(lm_count);
  Input = now_temp * 100;
  double gap = abs(Setpoint - Input);
  if (gap < 100)
  {
    myPID.SetTunings(Kp, Ki, Kd);
  }
  else
  {
    myPID.SetTunings(aggKp, aggKi, aggKd);
  }
  myPID.Compute();
  Set_power(map(Output, 0, 255, 0, 1023));
  update(Setpoint, now_temp, Output);

  yield();

[b]  long now_2 = millis();
  long loop_time = now_2-now;
  Serial.print(",");
  Serial.println(loop_time);
}[/b]

void update(int Setpoint_, double now_temp_, int Output_)
{

  Serial.print("E");
  Serial.print(double(Setpoint_ / 100));
  Serial.print(",");
  Serial.print(now_temp_);
  Serial.print(",");
  Serial.print(map(int(Output_), 0, 255, 0, 10));
  //Serial.print("\n");

}


void Set_power(int P_mode)
{
  analogWrite(IN1_pin , P_mode);
}


double get_Temp_Data()
{
  int rawvoltage = analogRead(outputpin);
  double millivolts = (rawvoltage / 1024.0) * 3300;
  double celsius = millivolts / 10;
  return (celsius);
}

double Avg_Temp(int count)
{
  double temp;
  double Max_Min_val[] = {0, 150};
  double sum = 0;
  double avg = 0;
  for (int i = 0; i < count; i = i + 1)
  {

    temp = get_Temp_Data();

    if (temp >= Max_Min_val[0])
    {
      Max_Min_val[0] = temp;
    }
    if (temp < Max_Min_val[1])
    {
      Max_Min_val[1] = temp;
    }
    sum = sum + temp;

    delay(10);
  }
  /*
    #### monitorring for bug fix!
    Serial.print("count = ");
    Serial.println(count);
    Serial.print("Max_Min_val = ");
    Serial.print(Max_Min_val[0]);
    Serial.print(", " );
    Serial.println(Max_Min_val[1]);
  */
  sum = sum - Max_Min_val[0] - Max_Min_val[1];
  avg = sum / (count - 2);

  return (avg);
}

int make_num(int digit, byte data[], int remove_digits)
{
  int output = 0;
  int counter = 0;
  for (int i = digit - remove_digits ; i >= 0 ; i = i - 1)
  {
    output = output + pow(10, int(counter)) * (data[i] - 48);
    counter++;
  }
  return int(output);
}

I don’t know if I understood me for your advice because I can’t speak English well.

I roughly modified the code and measured one loop time.

The results showed 300ms whether I was sending data or not.

This result suggests that it works correctly on the esp8266.

As you asked, when I send 0 in the 4500 state, the time stamp speed does not return.

Perhaps the way hc06 operates is that when a signal comes into the rx line, it stores data in its own buffer for a while and then outputs it at once.

Thank you so much for giving me a lot of help. Because of you, I studied a lot and learned a lot of good knowledge.

In addition, the wifi function was definitely turned off.
Before turning off the wifi, the USB battery did not enter the sleep mode, but after turning off the wifi, my USB battery enters the sleep mode. Because esp8266’s wifi consume is gone.

3942368:
I don't know if I understood me for your advice because I can't speak English well.

Because the forum does not have an Korean board, you can use English to Korean ( If I am right ) from google translate. Copy paste our posts and translate to Korean ( IF I AM RIGHT AGAIN ), so you can understand and speak better.

포럼에는 한국어 게시판이 없기 때문에 Google 번역에서 영어를 한국어로 (내가 맞다면) 사용할 수 있습니다. 우리의 게시물을 복사하여 한국어로 번역하면 더 잘 이해할 수 있습니다.