티스토리 뷰

728x90

출처

M5StickC : BeetleC - Android 원격 제어 (BLE : App Inventor)

M5StickC : BeetleC을 안드로이드 폰으로 제어하기 위해서 BeetleC는 BLE로 구현하고, 안드로이드개발은 App Inventor를 이용해서 개발했습니다.

M5StickC의 BLE 개발은 이전 게시물의 내용을 참고 하시면 됩니다.

M5StickC : BeetleC - BLE 서버 BleBeetleCRcCar.zip

BLE UUID 상수

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

아두이노 소스

#include <M5StickC.h>
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>
#include "carControl.h"

//
int defaultDutyCycle = 20;

#define SERVICE_UUID        "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;

void blink() {
  for(int num = 0; num < 7; num++) {
    uint32_t color = 0x11 << 16;
    led(num, color);
    delay(100);
    led(num, 0x00);
  }
  delay(100);
  for(int num = 0; num < 7; num++) {
    uint32_t color = 0x11 << 16;
    led(num, color);
    delay(100);
    led(num, 0x00);
  }
  delay(100);
  for(int num = 0; num < 7; num++) {
    uint32_t color = 0x11 << 16;
    led(num, color);
    delay(100);
    led(num, 0x00);
  }
}

class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
      deviceConnected = true;
    };

    void onDisconnect(BLEServer* pServer) {
      deviceConnected = false;
    }
};

class MyCallbacks: public BLECharacteristicCallbacks {
  void onRead(BLECharacteristic *pCharacteristic) {
    pCharacteristic->setValue("Hello World!");
  }

  void onWrite(BLECharacteristic *pCharacteristic) {

    std::string value = pCharacteristic->getValue();

    if (value.at(0) == 'S') {
      stopMotor();
      return;
    }
    
    int dutyCycle = defaultDutyCycle;
    //M5.Lcd.println(value.substr(2).c_str());
    if(sscanf(value.substr(2).c_str(), "%d", &dutyCycle) != 1)
      dutyCycle = defaultDutyCycle;

    if (value.at(1) == 'C') {
      centerMotor( value.at(0), dutyCycle );
    } else if (value.at(1) == 'L') {
      leftMotor( value.at(0), dutyCycle );
    } else if (value.at(1) == 'R') {
      rightMotor( value.at(0), dutyCycle );
    }
  }
};

void stopMotor() {
  // Stop the DC motor
  Serial.println("Motor stopped");
  leftwheel(0);
  rightwheel(0);
}

void forwardMotor(int dutyCycle) {
  leftwheel(dutyCycle);
  rightwheel(dutyCycle);
}

void backwardMotor(int dutyCycle) {
  leftwheel(dutyCycle * -1);
  rightwheel(dutyCycle * -1);
}

void centerMotor(char cmd, int dutyCycle) {
  // Stop the DC motor
  Serial.println("Motor center");
  if (cmd == 'F') {
    forwardMotor(dutyCycle);
  } else if (cmd == 'B') {
    backwardMotor(dutyCycle);
  }
}

void rightMotor(char cmd, int dutyCycle) {
  if (cmd == 'F') {
    leftwheel(dutyCycle);
    rightwheel(0);
  } else if (cmd == 'B') {
    leftwheel(dutyCycle * -1);
    rightwheel(0);
  }
}

void leftMotor(char cmd, int dutyCycle) {
  if (cmd == 'F') {
    leftwheel(0);
    rightwheel(dutyCycle);
  } else if (cmd == 'B') {
    leftwheel(0);
    rightwheel(dutyCycle * -1);
  }
}

void setup() {
  // initialize the M5StickC object
  M5.begin();
  Wire.begin(0, 26);

  M5.Lcd.fillScreen(WHITE);
  M5.Lcd.fillScreen(BLACK);
  M5.Lcd.setRotation(1);
  M5.Lcd.setCursor(40, 20, 1);
  M5.Lcd.setTextSize(1);

  //
  M5.Lcd.println("BeetleC BLE");
  
  blink();

  leftwheel(0);
  rightwheel(0);

  BLEDevice::init("m5-stack");
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());
  BLEService *pService = pServer->createService(SERVICE_UUID);
  pCharacteristic = pService->createCharacteristic(
                                         CHARACTERISTIC_UUID,
                                         BLECharacteristic::PROPERTY_READ |
                                         BLECharacteristic::PROPERTY_WRITE |
                                         BLECharacteristic::PROPERTY_NOTIFY |
                                         BLECharacteristic::PROPERTY_INDICATE
                                       );
  pCharacteristic->setCallbacks(new MyCallbacks());
  pCharacteristic->addDescriptor(new BLE2902());

  pService->start();
  BLEAdvertising *pAdvertising = pServer->getAdvertising();
  pAdvertising->start();
}

// the loop routine runs over and over again forever
void loop() {
}

App Inventor 소스 (안드로이드) m5stack_ble_rccar.zip

App Inventor 디자인

App Inventor 블럭

실행결과

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함