• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Read encoder value from smartphone with BLE ESP32

Q&A Forum › Category: ESP32 › Read encoder value from smartphone with BLE ESP32
0 Vote Up Vote Down
simone.zanna3 asked 6 years ago

Hi, I wanto to see the value generated from the rotation of encoder connected to the esp32 in my Iphone!
Someone can help me? Someone have the sketch of Arduino?
Thanks,
Simone

5 Answers
0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Which app are you going to use that displays those readings in your iPhone? Do you plan to write a custom iPhone app?

0 Vote Up Vote Down
simone.zanna3 answered 6 years ago

At the moment I just want to see the value that I read on the serial monitor on the pc. 
I would like to use “BLExArduino”.
Can you help me? 
At the moment i can connect the phone with the ESP32 with the app but i can’t see the value. 
I’m looking for a sketch that make it possible.

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Have you tried the Notify example with your ESP32? https://rntlab.com/bluetooth-low-energy-notify-and-scan/ 
In that example, you can send a reading or value to any device/app connected with your ESP32.

0 Vote Up Vote Down
simone.zanna3 answered 6 years ago

I tried this code but it is wrong.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;

int encoderPin1 = 2;
int encoderPin2 = 4;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;
long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

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

class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
void setup() {
Serial.begin(115200);
// Create the BLE Device
BLEDevice::init(“MyESP32”);
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_INDICATE
);

// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());

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

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

pinMode(encoderPin1, INPUT_PULLUP);
pinMode(encoderPin2, INPUT_PULLUP);

attachInterrupt(0, updateEncoder, CHANGE);
attachInterrupt(1, updateEncoder, CHANGE);

}
void loop() {

if (deviceConnected) {

Serial.printf(“*** NOTIFY: %d ***\n”, encoderValue * 0.942/4,1);
pCharacteristic->setencoderValue(&encoderValue, 1);
pCharacteristic->notify();
//pCharacteristic->indicate();
encoderValue++;
}
void updateEncoder(){
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue –;

lastEncoded = encoded; //store this value for next time
}
}

Can you help me to correct it?

0 Vote Up Vote Down
Rui Santos Staff answered 6 years ago

Unfortunately, we don’t have any tutorials on that exact subject. We don’t any exact code with BLE + enconders.

I can only help you if you have trouble following one of our code examples or projects. It is very hard for us to test or write custom projects requests that we receive every day.

I hope you understand. Thanks for asking!

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.