Geert Roumen
Published © CC BY-NC-SA

Create Apps for the ESP32 Using BLE Through P5

The ESP32 is cool, but it is cooler when you can make your own apps for it. In this tutorial, we will guide you to use BLE-UART to do so.

AdvancedProtip1 hour9,545

Things used in this project

Hardware components

DFRobot ESP32 Beetle
×1

Story

Read more

Schematics

Graph circuit

ESP32 Beetle connected to a potentiometer using pin A0

Code

ESPSerialGraph

Arduino
This code sends values read by the analog pin 0 to the bluetooth Serial port
/*

 In this example we use BLESerial to use in a similair way as the "graph" example in the IDE. 
 You could use this P5 sketch to plot the data from the Arduino (if using chrome)
 https://editor.p5js.org/lemio/sketches/qKGGxBG4C
 Or use the Adafruit "Bluefruit Connect" App. 

*/

#include "BLESerial.h"
BLESerial bleSerial;

void setup() {  
  Serial.begin(115200);
  bleSerial.begin("BLE-UART");
}

void loop() {
  //If we're connected
  if (bleSerial.connected()){  
    //Send the analog value
    bleSerial.println(analogRead(A0));
    Serial.println(analogRead(A0));
    //Wait for 0.1 second
    delay(100);
  }
}

ColorPicker

Arduino
/*

P5 Sketch: https://editor.p5js.org/lemio/sketches/Qzz1rncbd


This sketch makes an neopixel LED strip light up based on messages it receives through Bluetooth Low Energy (BLE) Serial (UART)

*/
#include <NeoPixelBrightnessBus.h>
#include <NeoPixelBus.h>
#include <NeoPixelAnimator.h>
#define colorSaturation 255
RgbColor white(colorSaturation);

#include "BLESerial.h"
#include <arduino.h>

BLESerial bleSerial;
const uint16_t PixelCount = 2; // this example assumes 4 pixels, making it smaller will cause a failure
const uint8_t PixelPin = 25;//D2

//Include the libraries; you'll need to install ESP32 through the board manager and add it to the preferecences.

NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);

#define LEDPin 5

void setup() {  
    strip.Begin();
  Serial.begin(115200);
  bleSerial.begin("IoT-Bus Bluetooth Serial"); 
  pinMode(LEDPin, OUTPUT);
  digitalWrite(LEDPin, LOW);
  for(int i=0;i<PixelCount;i++){
  strip.SetPixelColor(i, white);
  }
  strip.Show();
}

void loop() {

  
  if (bleSerial.connected()){  
    digitalWrite(LEDPin, LOW);  
    // Read input
    if(bleSerial.available())
    {
      uint8_t r,g,b;
      if(bleSerial.read() == 'S') {
        while(!bleSerial.available()){}
      r = bleSerial.read();
      while(!bleSerial.available()){}
      g = bleSerial.read();
      while(!bleSerial.available()){}
      b = bleSerial.read();
      }
      bleSerial.print(r);
                  bleSerial.print(",");
            bleSerial.print(g);
                  bleSerial.print(",");

      bleSerial.print(b);
                  bleSerial.println(",");

      RgbColor currentColor(r, g, b);
      strip.SetPixelColor(0,currentColor);
      strip.SetPixelColor(1,currentColor);
      strip.Show();

    }  
  }
  else{
    digitalWrite(LEDPin, HIGH);
  }
}

Credits

Geert Roumen

Geert Roumen

6 projects • 10 followers
I’m a maker and interaction designer, bridging the digital and physical world. and make prototypes and do research in a playful way.

Comments