0

[SOLVED] - Communicating between nRF52832 and Android App us
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

[SOLVED] - Communicating between nRF52832 and Android App us

by vader7071 on Tue Jun 04, 2019 5:00 pm

I am trying to complete a project that uses an MIT App Inventor application on an android device talking to an nRF52832.

So far I have been able to get the app to connect to the nRF52. When I monitor serial data from the nRF52, I can see when the app connects to the nRF52.

Where I am having issues is transferring data from the app to the nRF52.

Here is the sketch I am using:
Code: Select all | TOGGLE FULL SIZE
#include <bluefruit.h>

BLEDfu  bledfu;
BLEDis  bledis;
BLEUart bleuart;
BLEBas  blebas;

uint8_t ch;

void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb
 
  Serial.println("Bluefruit52 BLEUART Example");
  Serial.println("---------------------------\n");

  Bluefruit.autoConnLed(true);

  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);

  Bluefruit.begin();
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values
  Bluefruit.setName("FettPool_Helmet");
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  bledfu.begin();

  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  bleuart.begin();

  blebas.begin();
  blebas.write(100);;

  startAdv();

  Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode");
  Serial.println("Once connected, enter character(s) that you wish to send");
}

void startAdv(void)
{
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  Bluefruit.Advertising.addService(bleuart);

  Bluefruit.ScanResponse.addName();
 
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);
  Bluefruit.Advertising.setFastTimeout(30);
  Bluefruit.Advertising.start(0);
}

void loop()
{
  while ( bleuart.available() )
  {
    ch = (uint8_t) bleuart.read();
    Serial.println(ch);
  }
}

void connect_callback(uint16_t conn_handle)
{
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char central_name[32] = { 0 };
  connection->getPeerName(central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.println("Disconnected");
}


This is the bleuart.ino example modified slightly. I took out all the comments for space and removed the little bit I didn't use.

Additional information, if I connect using the Bluefruit LE app on my smart phone, I see data received from the app to the nRF52. So I know the nRF52 can receive data.

My question is, what "format" do I need to transmit the data in from the MIT App Inventor app so the nRF52 will be able to read it?

Thank you in advance for your help.
Last edited by vader7071 on Thu Jun 06, 2019 8:38 am, edited 1 time in total.

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by siddacious on Wed Jun 05, 2019 1:36 pm

I don't know the answer to your question but I'll forward this to someone who can help.

siddacious
 
Posts: 116
Joined: Fri Apr 21, 2017 3:09 pm

Re: Communicating between nRF52832 and Android App using BLE

by adafruit2 on Wed Jun 05, 2019 1:46 pm

what happens if you use the exact same script? we don't have much support for app inventor (someone wrote a guide for us but we haven't used it besides)

adafruit2
Site Admin
 
Posts: 18591
Joined: Fri Mar 11, 2005 7:36 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 2:32 pm

Thank you for the response. Here is what I have found testing.

When I load the modified or the original Adafruit sketch, I get the same results. Here they are:

bleuart.ino sketch on the nRF52832, using the Bluefruit LE App from the Google Play app store.
Open serial monitor on computer.
Any data I send from the Bluefruit app gets displayed (in some fashion) on the serial monitor.

blueuart.ino sketch on the nRF52832, using my MIT App Inventor 2 app.
Open serial monitor on computer.
No data update at all on the serial monitor.

An update that happened this morning during my testing (and I cannot tell you why it changed).
Yesterday, when I would send data from the MIT app, there was no visible change in either the serial monitor or via the MIT App. This morning, the MIT app is returning an error when I try to send data. The error is "Invalid UUID 6E400001-B5A3-F393-­E0A9-­E50E24DCCA9E" (which is the Service UUID I have programmed in the app).

So my thoughts are along these lines. With this new information, obviously I am sending the svc UUID incorrectly. Since the nRF52832 cannot recognize the svc UUID, then it does not know what to do with the data.

In my MIT App, I am sending the whole UUID. Do I need to reduce my UUID to just 0x****?

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by adafruit2 on Wed Jun 05, 2019 2:51 pm

well good news is BLE works so its some mismatch - but we dont know much about App Inventor's UUID desires, do they have a forum or documentation on how to use with our hardware?

adafruit2
Site Admin
 
Posts: 18591
Joined: Fri Mar 11, 2005 7:36 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 3:10 pm

I was able to find a MIT App Inventor support forum. I have asked them as well. I figured a 2 headed approach on this would be the best way. Speak with both sides and see where I was making my mistakes.

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 3:26 pm

Making progress!!!!

I am now seeing data come across!!!! Not the data I want, and extra in there, but something!!!!

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 3:37 pm

adafruit2 wrote:well good news is BLE works so its some mismatch - but we dont know much about App Inventor's UUID desires, do they have a forum or documentation on how to use with our hardware?


Maybe you can help me with where I am at now.

I am sending 2 different types of data over. When I press button "A" I am sending a string. The string is DN. The serial monitor spits out:
68
78
0

When I press button "B" I am sending an integer. The value is 3. The serial monitor spits out:
3
0
0
0

I know I am getting the ASCII values, which is a step forward. But all I need is just the "3". I am good sending only integers, I just need some assistance in separating out the data coming in into usable segments. It would be great if I could kill the extra "0" trailing.

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by adafruit2 on Wed Jun 05, 2019 3:42 pm

hiya - not sure exactly. we wanted to get your hardware working - sounds like you're on your way! you'll figure it out :)

adafruit2
Site Admin
 
Posts: 18591
Joined: Fri Mar 11, 2005 7:36 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 3:59 pm

I figured it out.

Code: Select all | TOGGLE FULL SIZE
void loop()
{
  // Forward from BLEUART to HW Serial
  while ( bleuart.available() )
  {
    for (int x = 0; x < 4; x++) {
      ch[x] = (uint8_t) bleuart.read();
      Serial.print(ch[x]);
      Serial.print(" - ");
      Serial.println(x);
    }
  }
}


I know I am sending 4 chunks of data, so now I just code to step through the array.

Now the next big question. How can I modify the 2nd, 3rd, and 4th chunks?

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 5:07 pm

Here is what I was able to use to get the function I needed.

Code: Select all | TOGGLE FULL SIZE
/*********************************************************************
 based on the Adafruit bleuart.ino example

 Used in conjunction with the MIT App Inventor 2 application.

 This is the testing code.  The MIT App will send a number, and this
 will display that number on the serial monitor.

 Please note:  The MIT App sends 4 sets of data See comments below.
 Currently I do not know how to send data to the other 3 postions
 in the array from the MIT App, but I am working on it

 There may be some extra code in this sketch that is not needed, however
 I got the project functioning as this sketch is, and I do not want to
 tempt fate.
*********************************************************************/
#include <bluefruit.h>

// BLE Service
BLEDfu  bledfu;  // Over The Air Device Firmware Update service
BLEDis  bledis;  // Device Information Service
BLEUart bleuart; // uart over ble
BLEBas  blebas;  // BAttery Service

uint8_t ch[4] = {0,0,0,0};  // creates 4 storage locations for the data from the MIT App
// The MIT App sends 4 blocks of integers.  This separates the 4 blocks into usable sections.
// Each block gets it's own spot in the array so the sketch can access the data

void setup()
{
  Serial.begin(115200);
  while ( !Serial ) delay(10);   // for nrf52840 with native usb
 
  Serial.println("Bluefruit52 BLEUART Example");
  Serial.println("---------------------------\n");

  // Setup the BLE LED to be enabled on CONNECT
  // Note: This is actually the default behaviour, but provided
  // here in case you want to control this LED manually via PIN 19
  Bluefruit.autoConnLed(true);

  // Config the peripheral connection with maximum bandwidth
  // more SRAM required by SoftDevice
  // Note: All config***() function must be called before begin()
  Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);

  Bluefruit.begin();
  Bluefruit.setTxPower(4);    // Check bluefruit.h for supported values
  Bluefruit.setName("Bluefruit_nRF52");
 
  Bluefruit.Periph.setConnectCallback(connect_callback);
  Bluefruit.Periph.setDisconnectCallback(disconnect_callback);

  // To be consistent OTA DFU should be added first if it exists
  bledfu.begin();

  // Configure and Start Device Information Service
  bledis.setManufacturer("Adafruit Industries");
  bledis.setModel("Bluefruit Feather52");
  bledis.begin();

  // Configure and Start BLE Uart Service
  bleuart.begin();

  // Start BLE Battery Service
  blebas.begin();
  blebas.write(100);

  // Set up and start advertising
  startAdv();

  Serial.println("nRF52 is ready.  Please activate MIT App and connect");
  Serial.println("Data from MIT App will display here");
}

void startAdv(void)
{
  // Advertising packet
  Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  Bluefruit.Advertising.addTxPower();

  // Include bleuart 128-bit uuid
  Bluefruit.Advertising.addService(bleuart);

  // Secondary Scan Response packet (optional)
  // Since there is no room for 'Name' in Advertising packet
  Bluefruit.ScanResponse.addName();
 
  /* Start Advertising
   * - Enable auto advertising if disconnected
   * - Interval:  fast mode = 20 ms, slow mode = 152.5 ms
   * - Timeout for fast mode is 30 seconds
   * - Start(timeout) with timeout = 0 will advertise forever (until connected)
   *
   * For recommended advertising interval
   * https://developer.apple.com/library/content/qa/qa1931/_index.html   
   */
  Bluefruit.Advertising.restartOnDisconnect(true);
  Bluefruit.Advertising.setInterval(32, 244);    // in unit of 0.625 ms
  Bluefruit.Advertising.setFastTimeout(30);      // number of seconds in fast mode
  Bluefruit.Advertising.start(0);                // 0 = Don't stop advertising after n seconds 
}

void loop()
{
  /*
   *  Whatever number is sent from the MIT App will display here in this format:
   *  Value - Array Position
   *  {value} - 0
   *  0 - 1
   *  0 - 2
   *  0 - 3
   *  end of line
   * 
   */
  while ( bleuart.available() )
  {
    Serial.println("Value - Array Position");
    for (int x = 0; x < 4; x++) {
      ch[x] = (uint8_t) bleuart.read();
      Serial.print(ch[x]);
      Serial.print(" - ");
      Serial.println(x);
      Serial.println("end of line");
      Serial.println(" ");
    }
  }
}

// callback invoked when central connects
void connect_callback(uint16_t conn_handle)
{
  // Get the reference to current connection
  BLEConnection* connection = Bluefruit.Connection(conn_handle);

  char central_name[32] = { 0 };
  connection->getPeerName(central_name, sizeof(central_name));

  Serial.print("Connected to ");
  Serial.println(central_name);
}

/**
 * Callback invoked when a connection is dropped
 * @param conn_handle connection where this event happens
 * @param reason is a BLE_HCI_STATUS_CODE which can be found in ble_hci.h
 */
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
  (void) conn_handle;
  (void) reason;

  Serial.println();
  Serial.println("Disconnected");
}


Only trouble is, the MIT app sends 4 sets of integers. I'd like to access the 2nd, 3rd, and 4th spots. I can't figure out how to do that.
Attachments
nRF52 to MIT App blocks.png
nRF52 to MIT App blocks.png (209.01 KiB) Viewed 136 times

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: Communicating between nRF52832 and Android App using BLE

by vader7071 on Wed Jun 05, 2019 6:29 pm

OH LORD!!!! I am such an idiot!!!

I figured out why the 4 chunks. The MIT App is sending 32 bits of data. The sketch is filling an 8 bit variable. Of course it will fill 4 times!!

So, to access the additional chunks, I just adjust the value of the integer to hit those higher bits.

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Re: [SOLVED] - Communicating between nRF52832 and Android Ap

by vader7071 on Tue Jun 11, 2019 4:06 pm

If anyone else is looking for help on this, here is my github where I stored the base code for both sides.

https://github.com/vader7071/BLE-App-Inventor-2-and-Adafruit-nRF52832

vader7071
 
Posts: 62
Joined: Mon Sep 11, 2017 12:45 pm

Please be positive and constructive with your questions and comments.