I am trying to read a sensor output from Pin A0 and, at the moment only trying to get the integer output value onto my phone. I think I am having trouble with the BluetoothLE1 module and the .ReadintValue command. I believe I have the correct service_uuid and characteristic_uuid but I cannot verify they are correct. And since that part of the blocks seems not to work, I suspect they are not correct. I tried the Android app BLE scanner but the information does not makes sense to me as to which numbers I should use. Can someone help me? In a previous post, ChrisWard commented that I was mixing basic Bluetooth and BLE, but I don't see that. My Feather code is attached - it has things that are not applicable in the code but that comes from cutting/pasting other peoples code.
/*********************************************************************
Bluefruit LE Connect Plotter
for Feather Bluefruit -> Bluefruit LE Connect app
outputs dummy values for demo use with BLuefruit LE Connect app
change SEND_SECOND_PLOT define to 1 to output second plot using sine wave table
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
//#include "sine.h"
/*=========================================================================
APPLICATION SETTINGS
FACTORYRESET_ENABLE Perform a factory reset when running this sketch
Enabling this will put your Bluefruit LE module
in a 'known good' state and clear any config
data set in previous sketches or projects, so
running this at least once is a good idea.
When deploying your project, however, you will
want to disable factory reset by setting this
value to 0. If you are making changes to your
Bluefruit LE device via AT commands, and those
changes aren't persisting across resets, this
is the reason why. Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.
Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won't work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won't be able to reconnect.
MINIMUM_FIRMWARE_VERSION Minimum firmware version to have some new features
MODE_LED_BEHAVIOUR LED activity, valid options are
"DISABLE" or "MODE" or "BLEUART" or
"HWUART" or "SPI" or "MANUAL"
-----------------------------------------------------------------------*/
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
#define SEND_SECOND_PLOT 0
/*=========================================================================*/
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/
/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
float zeroTank = 6.39; //560 = 4.5" 720=6.3"
float tankHeight = 5.1; //total height of tank
float totalGal= 39.0 ; //total capacity of tank
float maxInch = 8.0; //calibration point
int calMin = 128; //0 pressure at sensor, .5V=102, .75V=153 .63V=128
int calMax = 895; //895=8.0"
float tankZero = 6.3; //# inches tank bottom is from sensor
float heightinTank;
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
delay(500);
Serial.begin(9600);
}
void loop(void) {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
float absHeight = ((sensorValue-calMin) *maxInch )/(calMax-calMin);
heightinTank = absHeight-zeroTank;
if (heightinTank <0) { heightinTank = 0;
}
float gallons = heightinTank*totalGal/tankHeight;
// print out the value you read:
ble.print("AT+BLEUARTTX=");
ble.println(sensorValue); //send value to bluefruit uart
ble.println("AT+BLEUARTRX");
Serial.print(sensorValue);
Serial.print(" ");
Serial.print(voltage);
Serial.print(" ");
Serial.print("V ");
Serial.print(absHeight);
Serial.print(" in ");
Serial.print(heightinTank);
Serial.print(" in ");
Serial.print(gallons);
Serial.println(" gal ");
delay(2000);
ble.println(); //print newline so app knows to plot the values
}
So you believe they are correct but at the same time suspect they are not correct. Certainly confusing. Back to basics then: Where do those UUIDs come from? They should be identical in both the Sketch and the App and they should be specific to purpose.
Chris - Thanks for the reply. Here's the original sketch:
/*********************************************************************
Bluefruit LE Connect Plotter
for Feather Bluefruit -> Bluefruit LE Connect app
outputs dummy values for demo use with BLuefruit LE Connect app
change SEND_SECOND_PLOT define to 1 to output second plot using sine wave table
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#include "sine.h"
/*=========================================================================
APPLICATION SETTINGS
FACTORYRESET_ENABLE Perform a factory reset when running this sketch
Enabling this will put your Bluefruit LE module
in a 'known good' state and clear any config
data set in previous sketches or projects, so
running this at least once is a good idea.
When deploying your project, however, you will
want to disable factory reset by setting this
value to 0. If you are making changes to your
Bluefruit LE device via AT commands, and those
changes aren't persisting across resets, this
is the reason why. Factory reset will erase
the non-volatile memory where config data is
stored, setting it back to factory default
values.
Some sketches that require you to bond to a
central device (HID mouse, keyboard, etc.)
won't work at all with this feature enabled
since the factory reset will clear all of the
bonding data stored on the chip, meaning the
central device won't be able to reconnect.
MINIMUM_FIRMWARE_VERSION Minimum firmware version to have some new features
MODE_LED_BEHAVIOUR LED activity, valid options are
"DISABLE" or "MODE" or "BLEUART" or
"HWUART" or "SPI" or "MANUAL"
-----------------------------------------------------------------------*/
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
#define SEND_SECOND_PLOT 0
/*=========================================================================*/
// Create the bluefruit object, either software serial...uncomment these lines
/*
SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);
Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,
BLUEFRUIT_UART_CTS_PIN, BLUEFRUIT_UART_RTS_PIN);
*/
/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */
// Adafruit_BluefruitLE_UART ble(BLUEFRUIT_HWSERIAL_NAME, BLUEFRUIT_UART_MODE_PIN);
/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
/* ...software SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_SCK, BLUEFRUIT_SPI_MISO,
// BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
// BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
/**************************************************************************/
/*!
@brief Sets up the HW an the BLE module (this function is called
automatically on startup)
*/
/**************************************************************************/
void setup(void)
{
// while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(9600);
Serial.println(F("Adafruit Bluefruit Command <-> Data Mode Example"));
Serial.println(F("------------------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ) {
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in UART mode"));
Serial.println(F("Then Enter characters to send to Bluefruit"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("******************************"));
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
}
// Set module to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("******************************"));
}
void loop(void) {
delay(1000);
int ReadValue = analogRead(A0);
//wait 1 second
int val = ReadValue; //random(0,255); //find random value
ble.print(val); //send value to bluefruit uart
Serial.print(ReadValue);
Serial.print(" , ");
Serial.println(val);
if (SEND_SECOND_PLOT) { //change SEND_SECOND_PLOT to 1 for add'l sine plot
if (sineIndex > 255) sineIndex = 0; //stay within bounds of sine table
ble.print(","); //print delimiter for second plot
ble.print(sine_wave[sineIndex]); //print value from sine table
sineIndex++; //increment index
}
ble.println(); //print newline so app knows to plot the values
}
Part of the problem is that I just do not understand service/characteristic UUIDs. Any description I have found doesn't get into them deeply. I can't trace back where those UUIDs came from, but I did find others now from Adafruit's website and from Nordic's app:
From Nordic's app, I assume the info I need is:
Service ID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
Characteristic id: 6e400003-b5a3-f393-e0a9-e50e24dcca9e
I plugged those into the appinventor but no change in its operation.
The other thing I know I don't understand is how I designate what data is transferred to the appinventor. I've tried:
ble.print("AT+BLEUARTTX=");
ble.println(sensorValue); //send value to bluefruit uart
ble.println("AT+BLEUARTRX");
But I don't know that is correct.
Certainly if you have a suggestion on reading material, I'd like to get it. I have spent inordinate amount of time already trying to figure it out. It's not that I don't want to spend the time at it!
That's because they are not used in the sample Sketch provided by AdaFruit - so the App never receives any data sent since that data is effectively anonymous. Adafruit boards are tricky to use with App Inventor because Adafruit have their own way of doing things.
There are a lot of changes to make to get the Adafruit and App to meet in the middle. I can't help you right now as my daughter has called in to visit us (first time in a very long time because of the Covid lockdowns etc).
If I get time tonight I will try to give you something that has a chance of working.
Actually, I think the sample Project (by Gerrikoio) is exactly what you need - you should be able to trim the Sketch down to the number of values you need (and type).
For this project, I am expecting to use a pressure sensor who's output is 0.5 to 4.5V that is then reduced to 3.3V using a voltage divider. On my breadboard, I am simply using a variable resistor connected to Pin A0. I have been successful in reading the pin's output and have used the data to calculate the height of water in a tank and then from there the total gallons in the tank. I have successfully done that on another project using Arduino Uno and an OLED display. This project does not have a display so I want to use my cell phone to see the data and therefore I am using the Feather M0 that has the Bluetooth capability.
Your task would be a lot simpler using the UNO and an HC-05 bluetooth module Doug.
Anyway, you are sending just the one float value to the App, which could be represented as a string to suit processing in the App, is that right? How often would the value need to be sent to the App?
Yes, I'm beginning to think that I should switch back to the Uno. Neither Sparkfun nor Adafruit carries the HC-05/6 - what's up with that?
Ultimately I will be sending just one float value to the App ("gallons"), but in my code above I was simply trying to get the analogRead integer value ("sensorValue") to the App:
Yeah, I'm in Colorado. I saw that others carry it but I wondered why my go-to suppliers didn't. They say it's old technology and do not carry it any longer.
I have decided to indeed use my Arduino and an HC-05 board for my project. Although I would rather use BLE, I think I will get experience first with the HC-05 and then try again with my Feather. So thank you very much for your effort and help on this. I may still need help, but hopefully it will be more straightforward.
Attached is an example BT project that should work straight off the shelf, many people have used it as the basis of their own projects. Also attached is a matching Sketch that sends a fake data stream to the App.