How to send commands to an ESP32 Via bluetooth

125 views
Skip to first unread message

Jordan Hill

unread,
Aug 14, 2023, 1:09:58 AM8/14/23
to DroidScript
Hi there,

As part of a uni project I need to take the app I have made and use it to control the set pressure on an ESP32 microcontroller. Code is below. I am not sure I am sending the correct data to the device or even setting up bluetooth correctly as we havent been able to connect. It recognises the ESP and addresses it by name but can't communicate with it. Any help on connecting and how to send commands to the ESP from the APP and receive data from the ESP to the app would be so appreciate!!

Thanks heaps

var deviceAddress = "7C:DF:A1:FB:0A:A6";
var pressure = 4;
//Called when application is started.
function OnStart()
{
    app.SetBluetoothEnabled(true);
    app.ShowProgress( "Connecting to Bluetooth Device" );
    app.PairBtDevice(deviceAddress, OnPair);
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );
    lay.SetBackColor("#000000");
    app.AddLayout( lay );

      //Create a button to send request.
  btnUP = app.CreateButton( "UP", 0.8,0.2);
  btnUP.SetMargins( 0, 0.05, 0, 0 );
  btnUP.SetTextSize( 50)
  btnUP.SetBackColor( "#000000" )
  btnUP.SetTextColor( "#FFFFFF" )
      btnUP.SetOnTouch( IncreasePressure );
      btnUP.SetOnTouch(  SendDataIncrease  );
  lay.AddChild( btnUP);
 
  //Create a button to send request
   btnR = app.CreateButton(  + pressure +  "cmH2O", 1,  0.3 );
  btnR.SetMargins( 0, 0.05, 0, 0 );
  btnR.SetTextSize( 50)
  btnR.SetTextColor( "#FFFFFF" )
  btnR.SetBackColor( "#000000" )
      btnR.SetOnTouch( ResetPressure );
      btnR.SetOnTouch(  SendDataReset  );
  lay.AddChild( btnR );
 
  //Create a button to send request.
  btnDOWN = app.CreateButton( "DOWN",  0.8, 0.2);
  btnDOWN.SetMargins( 0, 0.05, 0, 0 );
  btnDOWN.SetTextSize(50 )
  btnDOWN.SetBackColor( "#000000" )
  btnDOWN.SetTextColor( "#FFFFFF" )
        btnDOWN.SetOnTouch( DecreasePressure );
        btnDOWN.SetOnTouch(  SendDataDecrease  );
  lay.AddChild( btnDOWN );
}


function IncreasePressure( )
{
    if ( pressure < 20) {
    pressure += 0.5;
    btnR.SetText(  + pressure +  "cmH2O", 0.6, 0.2 );
 }
  }
 
function DecreasePressure( )
{
   if (pressure > 4 ) {
   pressure -= 0.5;
   btnR.SetText( + pressure +  "cmH2O", 0.6, 0.2 );
 }
  }

function ResetPressure( )
{
   pressure = 4;
   btnR.SetText(  + pressure +  "cmH2O", 0.6, 0.2 );
 
  }
 
 
function OnPair( deviceAddress){
    app.Alert(deviceAddress, "Pairing Succesfull")
    }
   
   
function SendDataIncrease( ){
      Write("pressure increase")
       }
       
           
function SendDataDecrease (){
      Write("pressure decrease")
       }
       
           
function SendDataReset( ){
       Write("pressure reset")
       }
      
Reply all
Reply to author
Forward
0 new messages