0

i've been working on a project using an Arduino UNO, Bluetooth Module HC-06 and NodeMCU esp8266, the project consists on turning leds on via bluetooth and telling the esp8266 that the leds were turned ON, so then we can display some particular webpage data, everything that is not related to the serial communication works perfectly fine.

#include <NewPing.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#define TRIGGER_PIN 2
#define ECHO_PIN 3
#define MAX_DISTANCE 200

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

SoftwareSerial Serial1(5, 4);

Servo myservo;

int ledazul = 8; // LED RED
int scLed1 = 9; // LED 1 
int scLed2 = 10; // LED 2 
int scLed3 = 11; // LED 3
int scLed4 = 12; // LED 4
int scLed5 = 13;
int fan = 6;
bool br = false;
bool bra = false;
bool porta = false;
int pos = 0;


char val;
void setup() {

  Serial1.begin(115200);
  Serial.begin(9600);
  myservo.attach(7);
  pinMode(ledazul, OUTPUT);
  pinMode(scLed1, OUTPUT);
  pinMode(scLed2, OUTPUT);
  pinMode(scLed3, OUTPUT);
  pinMode(scLed4, OUTPUT);
  pinMode(scLed5, OUTPUT);
  pinMode(fan, OUTPUT);

}
void loop() {
  if (Serial.available() > 0)
  {
    val = Serial.read();

    Serial.print("Ping: ");
    int dist = sonar.ping();
    Serial.print(sonar.convert_cm(dist));
    Serial.println("cm");

  } else {
    //Serial.println("");
  }
  bool ledState = false;

  if (val == '1' && Serial1.available() > 0)
  {

    Serial1.print(val);
    Serial.print("SENT");
  }

  if (val == '0' && Serial1.available() > 0)
  {

    Serial1.print(val);
    Serial.print("SENT");
  }

  if (val == 'A')
  {
    digitalWrite(ledazul, !digitalRead(ledazul));
    ledState = true;
  }
  if (val == 'B')
  {
    Serial.print("Ping: ");
    int dist = sonar.ping();
    Serial.print(sonar.convert_cm(dist));
    Serial.println("cm");
  }
  if (val == 'C')
  {
    digitalWrite(scLed1, HIGH);
    ledState = true;
  }
  if (val == 'D')
  {
    digitalWrite(scLed2, HIGH);
    ledState = true;
  }
  if (val == 'E')
  {
    digitalWrite(scLed3, HIGH);
    ledState = true;
  }
  if (val == 'F')
  {
    digitalWrite(scLed4, HIGH);
    ledState = true;
  }

  if (val == 'G')
  {
    digitalWrite(scLed5, HIGH);
  }
  if (val == 'S')
  {
    digitalWrite(fan, HIGH);
  }
  if (val == 'O')
  {
    switch (porta)
    {
        if (pos < 90)
        {
        case false:
          porta = true;
          for (int x = 0; x < 90; x++)
          {
            myservo.write(x);
            delay(15);
          }
        }
        break;
    }
  }
  if (val == 'P')
  {
    switch (porta)
    {
        if (pos >= 90)
        {
        case true:
          porta = false;
          for (int y = 90; y > 0; y--)
          {
            myservo.write(y);
            delay(15);
          }
        }
        break;
    }
  }
}

What happens is, i can toggle ESP led on or off as long as i dont use commands that uses bluetooth module, it works perfectly fine. Now, if i do use bluetooth commands and then esp commands, it'll lose connection with the bluetooth module.

What im looking for is a workaround for the baudrates, because i have a 9600baudrate for the HC-06 and 115200 for the nodemcu, how do i do to fix it?

  • You have a copy paste error in the code (nested setup functions). Also I don't really understand the code. With which Serial you connect the HC-06? And what does "it does not work" mean?what are you expecting as output? – chrisl Jun 28 '18 at 20:32
  • Hello Chrisl, There arent any errors with the code. HC-06 is connected to pin 0 and 1 respectively on arduino, and esp8266 is connected to 5 and 4 of the arduino using SoftwareSerial, My problem is that, whenever i send a message to ESP8266 it loses bluetooth connection, i can no longer trigger bluetooth leds, however the opposite doesnt happen. If i toggle bluetooth leds FIRST, i can still toggle esp8266 leds – John Jones Jun 28 '18 at 20:39
  • 1. Yes you have a copy paste error in the code above. Look at the two lines with void setup(). 2. Does the HC-06 lose the bluetooth connection or does the Serial code stop working? This is an important difference, because the Arduino doesn't know anything about the bluetooth connection. It only write on it's serial interface – chrisl Jun 28 '18 at 21:17
  • There is only 1 part in the code, where you read anything from the ESP, but it is commented out. Even if it weren't, it would only be executed, if you first send a 0 or 1 from your bluetooth device. It is unclear, what do you want to achieve with this if-statement, and what with the following else-statement (why is it an error, if there is nothing to read in the receive buffer?) And this else-statement has a syntax error ( you must enclose both commands into the { } pair. All in all correct these errors and describe better, what you want to achieve with these parts of the code. – chrisl Jun 28 '18 at 21:22
  • Hey again chrisl, i have editted the main post with the full code, 0 and 1 sends a message via serial to the ESP8266(NodeMCU), let me remind me that both communication works as long as i dont attempt another module commucation after. Hopefully it should look easier to understand now. – John Jones Jun 28 '18 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.