Go Down

Topic: ArduinoBLE library and MIT App Inventor (Read 682 times) previous topic - next topic

ccomparey

Dec 12, 2018, 01:09 pm Last Edit: Dec 12, 2018, 01:46 pm by ccomparey
Hello,

I am trying to make use of some Arduino MKR Wifi 1010 boards with built in ublox module with BLE.

To get my bearings I am trying to use the peripheral LED example given by the arduino BLE library.

I was very easily able to set up the BLE connection following the IOT tutorial (http://iot.appinventor.mit.edu/assets/tutorials/MIT_App_Inventor_Basic_Connection.pdf)

But moving beyond that I am struggling, from the arduino peripheral LED example if the switch characteristic is written to with any value other than 0 the LED should turn on.

In App Inventor I used the same connection tutorial program to connect to my device and simply added a scan for advertisements and two buttons, one to turn the LED on and the other off.

I can still connect to the arduino and both the app and serial monitor confirm this, but when I use the LED ON button of the app, the arduino switch characteristic does not change. Does anyone know a way if I can check if my app is not writing or if the arduino is not receiving or both? In the screen capture attached below in the app I write integers, but I have also tried with floats, shorts and bytes but none work. Ive also tried both signed and unsigned

a picture of the app inventor block diagram for the LED ON and LED OFF button are attached




And The Arduino peripheral LED example Code is here
Code: [Select]
/*
  LED

  This example creates a BLE peripheral with service that contains a
  characteristic to control an LED.

  The circuit:
  - Arduino MKR WiFi 1010 or Arduino Uno WiFi Rev2 board

  This example code is in the public domain.
*/

#include <ArduinoBLE.h>

BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE 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 BLE 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 BLE peripherals to connect:x
  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()) {

      Serial.println(switchCharacteristic.written());
      // if the remote device wrote to the characteristic,
      // use the value to control the LED:
      if (switchCharacteristic.written()) {
        Serial.println("got something");
        Serial.println(switchCharacteristic.value());
       
        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());
  }
}


Any Insights are appreciated
Chris

ccomparey

#1
Dec 12, 2018, 03:02 pm Last Edit: Dec 13, 2018, 02:31 pm by ccomparey
If anyone is interested figured out the issue, in App inventor I needed to be using the write bytes with response block instead of the write integers block. and having register bytes

jcj99

If anyone is interested figured out the issue, in App inventor I needed to be using the write bytes with response block instead of the write integers block. and having register bytes
can you explain how this was done? my app keeps crashing - im not sure where to put the register bytes thing

ccomparey

can you explain how this was done? my app keeps crashing - im not sure where to put the register bytes thing
I followed this, http://iot.appinventor.mit.edu/assets/tutorials/MIT_App_Inventor_Basic_Connection.pdf

and within the "When BLE is connected" block I added the register for bytes block from the BLE extension, and then I have another button which when pressed, writes bytes with response to my corresponding characteristic uuid in my arduino code, then in the arduino code I have an if statement with if characteristic.value() == (the bytes sent) do something and then at the end of something i use characteristic.writeValue(bytes to write back)

Go Up