I've got to a part in a project where I have an ESP32 that I can communicate with from an ios app I've made. Now I've got the portion where the ESP32 sends messages to the app working great but it's sending commands from the app to the esp32 that is making me trouble. I have 2 led's attached to my arduino. When the app sends "ON" (Or '1' if easier') I'd like LED 3 (Pin 27) to turn ON and LED 4 (Pin 13) OFF. Then when the app sends "OFF" LED 3 OFF and LED 4 ON.
Below is my current code. I apologize if something if the code is messy i'm fairly new to c++ any help making the code better is appreciated.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLEServer* pServer = NULL;
BLECharacteristic* pCharacteristic = NULL;
bool deviceConnected = false;
bool oldDeviceConnected = false;
boolean oldState = LOW;
uint32_t value = 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;
}
};
const int bt1 = 14;
boolean bt1g = true;
int bt1t = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
pinMode(33, OUTPUT);
pinMode(27, OUTPUT);
pinMode(bt1, INPUT_PULLUP);
Serial.begin(9600);
BLEDevice::init("ESP32");
pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
BLEService *pService = pServer->createService(SERVICE_UUID);
pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristic->addDescriptor(new BLE2902());
pService->start();
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(false);
pAdvertising->setMinPreferred(0x0);
BLEDevice::startAdvertising();
Serial.println("Waiting a client connection to notify...");
}
void loop()
{
if (bt1g) {
if (digitalRead(bt1) == LOW ) {
bt1t = (bt1t + 1) % 2;
Serial.println(bt1t);
bt1g = false;
}
}
if (!bt1g) {
if (digitalRead(bt1) == HIGH) {
bt1g = true;
}
}
if (bt1t == 0) {
digitalWrite(15, LOW);
digitalWrite(33, HIGH);
}
if (bt1t == 1) {
digitalWrite(15, HIGH);
digitalWrite(33, LOW);
}
boolean newState = digitalRead(15);
if (deviceConnected) {
if (newState != oldState) {
if (newState == LOW) {
pCharacteristic->setValue("ON");
}
else {
pCharacteristic->setValue("OFF");
}
pCharacteristic->notify();
};
oldState = newState;
}
delay(50);
}
So what does it do or not do that doesn't meet your expectations? You kind of left that most important part out. What's broke?
Delta_G:
So what does it do or not do that doesn't meet your expectations? You kind of left that most important part out. What's broke?
Sorry I guess I meant to say I haven't implemented communication from the ios app via ble to the esp32. That's the part I can't figure out. I need to indicate a receiving write command from my ios app. Something along the lines of
'if message value =1 turn on Pin 27 and off Pin 13, else turn off pin 27 and on pin 13
A user here indicates a similar problem where he goes off an example here
A response has a solution of
if (switchCharacteristic.value() == 0) { digitalWrite(led1Pin, LOW); }
else if (switchCharacteristic.value() == 1) { digitalWrite(led1Pin, HIGH); }
else if (switchCharacteristic.value() == 2) { digitalWrite(led2Pin, LOW); }
else if (switchCharacteristic.value() == 3) { digitalWrite(led2Pin, HIGH); }
else if (switchCharacteristic.value() == 4) { digitalWrite(led3Pin, LOW); }
else if (switchCharacteristic.value() == 5) { digitalWrite(led4Pin, HIGH); }
But I don't know how to implement that into my code as switchcharacteristic is not something I currently have at all through my code.
You're looking for pieces of code that you can copy. That's not ever going to work out. That solution may be fine it may just be that this person named their instance differently than you did. You have to be able to see what the code DOES and how and then use that.
So you want to implement your communication. So let's put aside the code you have for now. Save it. And close it. We're not going to look at it again until communication works.
What devices do you have for communication? An ESP32 and an iOS app. OK. You say that you know how to send things from the ESP to the app. Do you know how? OR you mean you found some code that seemed to work. If you don't know how then we need to work on that as well.
But you say that you can't talk from the app to the ESP. So tell us about this app. What about that code. How does it work. What's it built on? How have you tried sending? Do you have any examples of sending from an app like that to your ESP?
The app I've created is from this tutorial. It's for an adafruit ble board, mines an adafruit esp32 board which is similar enough to make it work. Github for app is here. In essence it's an ios swift app that is a basic chat app. When I boot up my esp32 I can see it being advertised, then when I select it I am shown to a screen to communicate with the esp32. Back to the esp32 code I currently have it set up so that If I press a button it turns LED A on and LED B Off. If pressed again LED A OFF and LED B ON. After this occurs the esp32 checks to see if the LED A is on, if it is it sends a notify message (ON) over ble to the app which then displays "[Incoming]: ON". In the case the esp32 checks LED A and it's off then the notify message "[Incoming]: OFF" is sent. Everything above works great.
As far as I know the only thing I need to figure out is how to implement an extra 1 (or 2 not sure) BLE characteristics into my code . Each characteristic will correspond with an LED (LED C and LED D). When I have that I can then follow this page and figure out how to allow the app to write a specific characteristic I believe.
Now I could be wrong in a whole lot of this, I could be going about this the wrong way I'm not sure. From what I'm able to read about ble this seems like the task I want accomplish though.