How to use buttons on esp32/arduino to control MIT app inventor aps via bluetooth?

Right - Project v2, Sketch v2 :grin:

BT_ReceiveDataAndCmnds2.aia (15.1 KB)

SensorDataOrCmdToApp2.txt (1.4 KB)

Build the App as an APK for testing.

It still shows values in data_in window but not in cmd_in, not changing screens at all

So, we get either the Commands or the Data, but not both?

What are the values in the data-in Label?

Hi again Pawel

I think we can use entirely different code in the Sketch to handle the buttons + sensor data. I need to know what your exact hardware model is and a drawing or photo of the setup.

See this by Juan, Section 6:

Hi Chris, sorry for delay, covid :frowning: , I'll let you know about my project soon,
mainly I just send data by serial print from arduino, I send more detail in a few days

i've got some functionality to change state on the screen by buttons, but it takes even to 10sec. which is too much

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#define BTN1 digitalRead(0)==0
#define BTN2 digitalRead(35)==0

BluetoothSerial SerialBT;

void IRAM_ATTR isr() {
  SerialBT.println(1);
}

void IRAM_ATTR isr2() {
  SerialBT.println(0);
}

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32_work");
  pinMode(0,INPUT);
  pinMode(35,INPUT);
  attachInterrupt(0, isr, FALLING);
  attachInterrupt(35, isr2, FALLING);
}

void loop() {
  // put your main code here, to run repeatedly:
  
}

bth_get_arduino_data_2.aia (3.1 KB)

Your Clock is set at 3000 ms in AI2, way too slow.

Also, you are sending with println('1') and println('0') into a BT component with Delimiter=10, so your blocks should be text-oriented.

Current blocks:

Standard Delimiter advice:

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

1 Like