ESP32 as wireless controller for arduino uno using bluetooth and wifi

can someone help me with arduino and esp32 and bluetooth im extremely noob

basically i want the esp32 to host a web server and connect its built-in bluetooth to a bluetooth module (JDY-16) thats connected to arduino uno connected to 2 dc motors, basically send http request to esp32 send it through bluetooth then finally arrive at the arduino uno

#include <BluetoothSerial.h>
#include <WiFi.h>
#include <WebServer.h>

BluetoothSerial SerialBT;
WebServer server(80);

const char* ssid = "ssid";
const char* password = "password";

const char* jdy_mac = "mac address";  

void setup() {
  Serial.begin(115200);

  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nConnected to WiFi");
  Serial.println("Web server IP: http://" + WiFi.localIP().toString());

  // start the bluetooth
  SerialBT.begin("ESP32Controller", true); // true = master mode

  Serial.println("Connecting to JDY-16...");
  while (!SerialBT.connect(jdy_mac)) {
    Serial.println("Waiting for JDY-16...");
    delay(2000);
  }
  Serial.println("Connected to JDY-16 via Bluetooth!");

  // Setup web server endpoints
  server.on("/", []() {
    server.send(200, "text/html",
      "<h1>Robot Control</h1>"
      "<button onclick=\"fetch('/forward')\">Forward</button><br>"
      "<button onclick=\"fetch('/backward')\">Backward</button><br>"
      "<button onclick=\"fetch('/left')\">Left</button><br>"
      "<button onclick=\"fetch('/right')\">Right</button><br>"
      "<button onclick=\"fetch('/stop')\">Stop</button><br>"
    );
  });

  server.on("/forward", []() {
    SerialBT.write('F');
    server.send(200, "text/plain", "Forward");
  });

  server.on("/backward", []() {
    SerialBT.write('B');
    server.send(200, "text/plain", "Backward");
  });

  server.on("/left", []() {
    SerialBT.write('L');
    server.send(200, "text/plain", "Left");
  });

  server.on("/right", []() {
    SerialBT.write('R');
    server.send(200, "text/plain", "Right");
  });

  server.on("/stop", []() {
    SerialBT.write('S');
    server.send(200, "text/plain", "Stop");
  });

  server.begin();
  Serial.println("Web server started.");
}

void loop() {
  server.handleClient();
}

the problem is that it stops at "waiting for ..." and never connects, the bluetooth module ik has a pin but it should pair automatically. can the esp32 bluetooth even connect to any bluetooth module?

you appear to have a very complicated setup
the ESP32 WiFi and Bluetooth use the same radio and it can be tricky using both in a project - try a web search for esp32 bluetooth and wifi at the same time
the ESP32 is far more powerful than the UNO why not drop the UNO and just use the ESP32?

1 Like

unfortunately im using a specific motor shield just for the 2 dc motors i use, im thinking of maybe using the r4 wifi instead is that better? would an uno shield work for the r4 wifi board they appear to have the same shape :slightly_smiling_face:

rather than using bluetooth for UNO <> ESP32 communication use TTL serial?
do you require one way or two way communication?
how far apart the UNO and ESP32?

just one way, esp32 sending commands to the arduino uno

try the following
ESP32 code

// ESP32  Serial1 test - for loopback test connect pins RXD1 and TXD1

#define RXD1 16 // can map Serial1 and Serial2 to many ESP32 GPIO pins
#define TXD1 17 // check pin usage https://randomnerdtutorials.com/esp32-pinout-reference-gpios/

// for RS232 shield connect
// ESP32 RXD1 to TTL/RS232 Rx
// ESP32 TXD1 to TTL/RS232 Tx
// connect GND pins together and VCC to 3.3V on ESP32 5V on UNO ect
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)

void setup() {
  // initialize both serial ports:
  Serial.begin(115200);
  Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
  Serial.printf("\n\nESP32 serial1  test RXD1 pin %d TXD1 pin %d\n", RXD1, TXD1);
  Serial.printf(" loopback test connect pin %d to pin %d\n", RXD1, TXD1);
  Serial.printf("RS232: ESP32 pin %d RXD1 to TTL/RS232 Rx and pin %d TXD1 to TTL/RS232 Tx\n", RXD1, TXD1);
  Serial.printf("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}

void loop() {
  // read from Serial1, send to Serial
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }
  // read from Serial, send to Serial1
  if (Serial.available()) {
    int inByte = Serial.read();
    //Serial.write(inByte);     // local echo if required
    Serial1.write(inByte);
  }
}

UNO code

// AltSoftSerial transmit/Receive Test
//
// from https://github.com/PaulStoffregen/AltSoftSerial/blob/master/examples/ReceiveTest/ReceiveTest.ino
//
// Transmit data with Serial1 and try to receive
// it with AltSoftSerial.  You must connect a wire
// from Serial1 TX to AltSoftSerial RX.

#include <AltSoftSerial.h>

AltSoftSerial altser;
const int mybaud = 9600;

// UNO connection to a RS232-TTL
// UNO PIN 8 Rx to RS232 RXD
// UNO pin 9 Tx to RS232 TXD

// Board            Serial1 TX   AltSoftSerial RX
// -----            ----------   ----------------
// UNO/nano              9              8
// Teensy 3.x            1              20
// Teensy 2.0            8 (D3)         10 (C7)
// Teensy++ 2.0          3 (D3)          4 (D4)
// Arduino Leonardo      1              13
// Arduino Mega         18              48

// Serial1 on AVR @ 16 MHz minimum baud is 245
// Serial1 on Teensy 3.2 @ 96 MHz minimum baud is 733

void setup() {
  delay(200);
  Serial.begin(115200);
  while (!Serial)
    ;                    // wait for Arduino Serial Monitor
  altser.begin(mybaud);  // to AltSoftSerial RX
  Serial.println("\n\nAltSoftSerial transmit/ Receive Test");
  Serial.println("on a UNO connect pins 8 and 9 for a loopback test");
  Serial.println("using RS232 module connect pins 2 and 3 for a loopback test");
  Serial.println("characters entered on serial monitor keyboard are echoed back via serial to the display");
}

void loop() {
  // transmit a test byte on Serial 1
  if (Serial.available() > 0) {
    char ch=Serial.read();
    //Serial.write(ch);
    altser.write(ch);
  }
  // attempt to receive it by AltSoftSerial
  if (altser.available() > 0) {
    Serial.write(altser.read());
  }
}

connect ESP32 GPIO17 to UNO GPIO8 - make sure baudrates are the same
text entered on ESP32 serial monitor should appear on UNO serial monitor

once you know the communication works update your project to use the serial

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.