Friday, December 25, 2020

ESP32 receive Bluetooth Classic command from Raspberry Pi/Python, to control Servos.

In my previous posts, I show simple examples of ESP32 Bluetooth Classic serial exampleServo Motor Control and I2C SSD1306 OLED. In this exercise, group three altogether run on ESP32-DevKitC-V4, receive single character command from Raspberry Pi/Python via Bluetooth Classic, control Servo Motors, and display position on 0.96" 128x64 I2C SSD1306 OLED.

Connection:

BTServoServer_20201226.ino

// BTServoServer

#include "BluetoothSerial.h"
#include "esp_bt_device.h"
#include "ssd1306.h"
#include <ESP32Servo.h>

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;
Servo myservoX;  // create servo objects to control a servo
Servo myservoY;
int servoPinX = 18;
int servoPinY = 19;

#define CMD_ORG 'O'
#define CMD_XDEC 'A'
#define CMD_XDEC10 'B'
#define CMD_XINC 'C'
#define CMD_XINC10 'D'
#define CMD_YDEC 'E'
#define CMD_YDEC10 'F'
#define CMD_YINC 'G'
#define CMD_YINC10 'H'

int x = 0;
int y = 0;

void printDeviceAddress() {
 
  const uint8_t* point = esp_bt_dev_get_address();
 
  for (int i = 0; i < 6; i++) {
 
    char str[3];
 
    sprintf(str, "%02X", (int)point[i]);
    Serial.print(str);
 
    if (i < 5){
      Serial.print(":");
    }
 
  }
}

void setup() {
  Serial.begin(115200);
  Serial.println("\n---Start---");
  SerialBT.begin("ESP32test"); //Bluetooth device name
  
  Serial.println("The device started, now you can pair it with bluetooth!");
  Serial.println("Device Name: ESP32test");
  Serial.print("BT MAC: ");
  printDeviceAddress();
  Serial.println();

  ssd1306_setFixedFont(ssd1306xled_font6x8);
  ssd1306_128x64_i2c_init();
  ssd1306_clearScreen();
  ssd1306_printFixed(0,  8, "BTServoServer", STYLE_BOLD);
  ssd1306_printFixed(0,  40, "arduino-er.blogspot.com", STYLE_BOLD);

  // Allow allocation of all timers
  ESP32PWM::allocateTimer(0);
  ESP32PWM::allocateTimer(1);
  ESP32PWM::allocateTimer(2);
  ESP32PWM::allocateTimer(3);
  myservoX.setPeriodHertz(50);    // standard 50 hz servo
  myservoX.attach(servoPinX, 500, 2500); // attaches the servo on pin 18 to the servo object
  myservoY.setPeriodHertz(50);    // standard 50 hz servo
  myservoY.attach(servoPinY, 500, 2500);

  myservoX.write(90);
  myservoY.write(90);
}

void loop() {
  if (SerialBT.available()) {
    char cmd = SerialBT.read();
    switch(cmd) {
      case CMD_ORG:
            x = 0;
            y = 0; 
            break;
      case CMD_XDEC:
            x--;
            break;
      case CMD_XDEC10:
            x = x-10;
            break;
      case CMD_XINC:
            x++;
            break;
      case CMD_XINC10:
            x = x+10;
            break;
      case CMD_YDEC:
            y--;
            break;
      case CMD_YDEC10:
            y = y-10;
            break;
      case CMD_YINC:
            y++;
            break;
      case CMD_YINC10:
            y = y+10;
            break;
      default:
            Serial.println("unknown command!");
            break;
    }

    if(x < -90)
      x = -90;
    if(x > 90)
      x = 90;
    if(y < -90)
      y = -90;
    if(y > 90)
      y = 90;

    String s = String(x, DEC) + " : " + String(y, DEC) + "    ";
    const char* c;
    c = s.c_str();
    Serial.println(s);
    ssd1306_printFixed(0, 25, c, STYLE_NORMAL);

    myservoX.write(x + 90);
    myservoY.write(y + 90);
    delay(200);             // wait for the servo to get there
  }
  delay(20);
}


Python code in Raspberry Pi side, refer to my another blog's post: Hello Raspberry Pi - Raspberry Pi/Python remote control ESP32/Servos via Bluetooth Classic

Next:

No comments:

Post a Comment