Using the Arduino R4 with App Inventor

I have been trying for over a week to connect any of my bluetooth devices to the MIT App Inventor. I tried the HM-10, and now my new Arduino R4. I think I have the correct UUIDs but even though it connects, I can never send any information to it to control even the build-in LED.

Are there any tutorials out there that can help? I tried following one online and did everything he said to do to no avail. I am really frustrated.

I've search this forum but those who had similar problems never seemed to find any answers.

Please advise.

Export the source from both sides and post them here, .ino and .aia

Thanks for responding. Right now the Arduino code is set to only read a 1 or a 0 so only the FORWARD button should work - as I am giving it the value of 1. (I have other values on the blocks page that are not being used at the moment.)

Currently, I am only trying to make the LED go on when I hit the FORWARD button. I will revisit the other buttons if I get this one to work.

So, my confusion is as follows:

  1. I am not sure of the service UUID since I get conflicting information from different BLE scanners I've tried. It is either 4433D40C-6572-F21D-39A157E70F3A or
    19B10000-E8F2-537E-4F6C-D104768A1214 - I've tried both and I get an error message on the Inventor app if I use the second one.

  2. I am not sure if I need to enter the value from the AppInventor as a text or as a math value - I've tried both.

I appreciate any help you can provide. I have really just started playing around with App Inventor and I hope to be able to teach my students how to use it.

code_for_arduino_r4_for_mit_app_inventor1.ino (2.7 KB)
HM10_with_Arduino-2.aia (326.5 KB)

I meant to upload these as well - screenshots showing the UUIDs.


Here's our HM-10 FAQ, until we can look into your code..

For the convenience of our BLE experts, here's your sketch:

/*
  LED

  This example creates a Bluetooth® Low Energy peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010, Arduino Uno WiFi Rev2 board, Arduino Nano 33 IoT,
    Arduino Nano 33 BLE, or Arduino Nano 33 BLE Sense board.

  You can use a generic Bluetooth® Low Energy central app, like LightBlue (iOS and Android) or
  nRF Connect (Android), to interact with the services and characteristics
  created in this sketch.

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

//BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
BLEService ledService("4433D40C-6572-F21D-39A157E70F3A"); // Bluetooth® Low Energy LED Service


// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

const int ledPin = LED_BUILTIN; // pin to use for the LED

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // set LED pin to output mode
  pinMode(ledPin, OUTPUT);

  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  // set advertised local name and service UUID:
  BLE.setLocalName("LED");
  BLE.setAdvertisedService(ledService);

  // add the characteristic to the service
  ledService.addCharacteristic(switchCharacteristic);

  // add service
  BLE.addService(ledService);

  // set the initial value for the characeristic:
  switchCharacteristic.writeValue(0);

  // start advertising
  BLE.advertise();

  Serial.println("BLE LED Peripheral");
}

void loop() {
  // listen for Bluetooth® Low Energy peripherals to connect:
  BLEDevice central = BLE.central();

  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        if (switchCharacteristic.value()) {   // any value other than 0
          Serial.println("LED on");
          digitalWrite(ledPin, HIGH);         // will turn the LED on
        } else {                              // a 0 value
          Serial.println(F("LED off"));
          digitalWrite(ledPin, LOW);          // will turn the LED off
        }
      }
    }

    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
}


Your BLE version is old:

Where did you get it?

Your blocks:

I'm putting this out there for more experienced BLE users, to save them time.

This is more than I can chew.

Can you please clarify what you mean by my BLE version is old. Do you mean my phone BLE scanner app, my BLE library for Arduino Code or something on App Inventor?

I got the Arduino code and library from the Arduino libraries on the IDE.

I’ve tried several BLE scanners.

The information you posted from the AppInventor program refers to Android but I’m using an iPhone and a MAC. Both use IOS. I understand that this might be problematic and may not work with AppInventor.

Sorry, I can't support iOS as I don't have that.

I'm recategorizing this thread.