ESP32 controlled by Android App

// --------------------------------------------------
//
// Code for control of ESP32 through MIT inventor app (Bluetooth).
// device used for tests: ESP32-WROOM-32D
//
// Written by mo thunderz (last update: 20.4.2021)
// https://www.youtube.com/watch?v=aM2ktMKAunw
// 
// Edited for use with joysticks, multiple inputs, and all put together by: Nathan Witte
// Joystick Functionality based on tutorial by Tabletop Robotics
// https://www.youtube.com/watch?v=OnEg_IcMmg4
// rgb functionality added from example by: system?
// https://forum.arduino.cc/t/hue-controllable-rgb-led-lamp/8382
//
// --------------------------------------------------

// this header is needed for Bluetooth Serial -> works ONLY on ESP32
#include "BluetoothSerial.h"

// init Class:
BluetoothSerial ESP_BT;

// Parameters for Bluetooth interface
int incoming;
int dataIn[8] {0, 0, 0, 0, 0, 0, 0, 0};
int index_array = 0;
int counter = 0;

// init PINs: assign any pin on ESP32
int led_pin_1 = 5;
int led_pin_2 = 18;
int led_pin_3 = 19;
int redPin= 15;
int greenPin = 2;
int bluePin = 4;

float h;
int h_int;
int r=0, g=0, b=0;

int val=0;

void h2rgb(float h, int &R, int &G, int &B);

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);
  ESP_BT.begin("ESP32_Control"); //Name of your Bluetooth interface -> will show up on your phone

  pinMode (led_pin_1, OUTPUT);
  pinMode (led_pin_2, OUTPUT);
  pinMode (led_pin_3, OUTPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}


void loop()                     // run over and over again
{
    if (ESP_BT.available())
  {
    incoming = ESP_BT.read(); //Read what we receive
    if (incoming == 255) {
      index_array = 0;
    }
    dataIn[index_array] = incoming;
    index_array ++;
      if (dataIn[1]){
          digitalWrite(led_pin_1, dataIn[1]);
      }
      else{
        digitalWrite(led_pin_1, LOW);
      }
      if (dataIn[2]){
          digitalWrite(led_pin_2, dataIn[2]);
      }
      else{
        digitalWrite(led_pin_2, LOW);
      }
      if (dataIn[3]){
          digitalWrite(led_pin_3, dataIn[3]);
      }
      else{
        digitalWrite(led_pin_3, LOW);
      }
  val= dataIn[5]*4;   // Read the pin and display the value
  //Serial.println(val);
  h = ((float)val)/1024;
  h_int = (int) 360*h;
  h2rgb(h,r,g,b);
  analogWrite(redPin, r);
  analogWrite(greenPin, g);
  analogWrite(bluePin, b);
  }
}

void h2rgb(float H, int& R, int& G, int& B) {

  int var_i;
  float S=1, V=1, var_1, var_2, var_3, var_h, var_r, var_g, var_b;

  if ( S == 0 )                       //HSV values = 0 ÷ 1
  {
    R = V * 255;
    G = V * 255;
    B = V * 255;
  }
  else
  {
    var_h = H * 6;
    if ( var_h == 6 ) var_h = 0;      //H must be < 1
    var_i = int( var_h ) ;            //Or ... var_i = floor( var_h )
    var_1 = V * ( 1 - S );
    var_2 = V * ( 1 - S * ( var_h - var_i ) );
    var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) );

    if      ( var_i == 0 ) { 
      var_r = V     ; 
      var_g = var_3 ; 
      var_b = var_1 ;
    }
    else if ( var_i == 1 ) { 
      var_r = var_2 ; 
      var_g = V     ; 
      var_b = var_1 ;
    }
    else if ( var_i == 2 ) { 
      var_r = var_1 ; 
      var_g = V     ; 
      var_b = var_3 ;
    }
    else if ( var_i == 3 ) { 
      var_r = var_1 ; 
      var_g = var_2 ; 
      var_b = V     ;
    }
    else if ( var_i == 4 ) { 
      var_r = var_3 ; 
      var_g = var_1 ; 
      var_b = V     ;
    }
    else                   { 
      var_r = V     ; 
      var_g = var_1 ; 
      var_b = var_2 ;
    }

    R = (1-var_r) * 255;                  //RGB results = 0 ÷ 255
    G = (1-var_g) * 255;
    B = (1-var_b) * 255;
  }
}

https://gallery.appinventor.mit.edu/?galleryid=f3c141f6-f312-41fb-a8b6-fb18fd245a07

This link has the android app that accompanies this code

I'm confused :confused:

  1. Just starting a topic and only showing code does not explain what you're trying to achieve with your topic. Are you just sharing? Or do you have a problem? Or asking for advice / improvements? Or ... ?
  2. Are you using a Classic Nano? Because you posted this in the Classic Nano section of the forum. Or is it about an ESP32 as in your title?

Sorry I was just using the forum as a way to post my code to share it with someone.

Great :+1:

Is this about a classic Nano or about and ESP32? Or both?

If you give a little description of the project (and the 'protocol'), it might help others to understand.

I would add an additional check to make sure that you don't write outside the boundaries of dataIn; you do reset index_array when you receive a 255 but what happens if you don't receive that (for whatever reason).

The 255 is built into the app, it's just the way I wrote it. Not foolproof, but it works.
This is on the ESP32 and I send the data to a Nano via serial connection which I don't have in this code, because I added it later

:boom: :rofl:

Can you give a description of the data to complete this topic. I can basically get it from the code, others might not be able to.

Ya, so basically the app sends the joystick values, and buttons in a specific order before looping back to the value 255 which resets the receiving side. This allows a continuous flow of data from the app to the ESP32.