Entering into loop in ESP32 with BLE

Hi Talents!

This is my first time posting as a beginner and trying to get an answer from the forum. I wish the question is understandable.

So I am trying to use ESP32 with BLE to control a servo motor(should be easy). the idea is that every time I get a signal "1" from my phone, the servo will rotate. Then it will wait for the next '1' coming in and rotate again. I am using a code developed by this post to develop the program because I want to be able to reconnect to a different device every time I lost connection. ESP32_BLE_Arduino/BLE_notify.ino at master · nkolban/ESP32_BLE_Arduino · GitHub

The problem is that I do get the '1' from serial monitor but I was not able to trigger the motor by putting this function in the //connecting filed.

Here is my code:

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <DabbleESP32.h>
#include <ESP32Servo.h>
#include <Arduino.h>
#include <U8x8lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=/ 15, / data=/ 4, / reset=*/ 16);

Servo myservo;
int pos = 0;
int servoPin = 13;

BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
uint8_t _rxValue;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"

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

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

};

class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();

  if (rxValue.length() > 0) {
    //Serial.println("*********");
    Serial.print("Received Value: ");
    //for (int i = 0; i < rxValue.length(); i++){
      Serial.print(uint8_t(rxValue[6]));
      _rxValue = uint8_t(rxValue[6]);
      Serial.println();
      Serial.print(_rxValue);
      Serial.println();
      Serial.println("*********");
}

};
};

void setup() {
Serial.begin(115200);
myservo.setPeriodHertz(50); // standard 50 hz servo
myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object,500-2400旋转度数最大值最小值

u8x8.begin();
u8x8.setFont(u8x8_font_chroma48medium8_r);
// Create the BLE Device
BLEDevice::init("502Family");

// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());

// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);

// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);

pTxCharacteristic->addDescriptor(new BLE2902());

BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);

pRxCharacteristic->setCallbacks(new MyCallbacks());

// Start the service
pService->start();

// Start advertising
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");

// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);

}

void loop() {
if (deviceConnected) {
pTxCharacteristic->setValue(&txValue, 1);
pTxCharacteristic->notify();
txValue++;
delay(10); // bluetooth stack will go into congestion, if too many packets are sent
}

// disconnecting
if (!deviceConnected && oldDeviceConnected) {
    delay(500); // give the bluetooth stack the chance to get things ready
    pServer->startAdvertising(); // restart advertising
    Serial.println("start advertising");
    oldDeviceConnected = deviceConnected;
}
// connecting
***if (deviceConnected && !oldDeviceConnected) {***

*** // do stuff here on connecting***
*** u8x8.drawString(0,0,"Connected");***
*** if (_rxValue == 1)***
*** {***
*** u8x8.drawString(0,0,"WELCOME HOME,502");***
*** u8x8.drawString(0,2,"Gate opening");***
*** for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees***
*** // in steps of 1 degree***
*** myservo.write(pos); // tell servo to go to position in variable 'pos'***
*** delay(15); // waits 15ms for the servo to reach the position***
*** }***
*** for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees***
*** myservo.write(pos); // tell servo to go to position in variable 'pos'***
*** delay(15); // waits 15ms for the servo to reach the position***
*** }***
*** }***

    oldDeviceConnected = deviceConnected;
}

}

Thank you in advance!!!

Richard

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