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
Which app are you going to use that displays those readings in your iPhone? Do you plan to write a custom iPhone app?
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.
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.
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?
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!