"no BLE device was found."


#1

I’m generating a code (code A) that sends data to both the sd card and the red bear app. The sd prints fine, but for some reason my BLE device cannot be discovered. I have a different code (code B), however, which makes the BLE device discoverable, but I can’t seem to find out where I went wrong in code A while referencing code B! I’m using an Arduino Mega2560 and a RedBear bluetooth shield.
The code (code A) that won’t let me discover is as follows:

libraries

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#include <Boards.h>
#include <RBL_nRF8001.h>

boolean use_processing = false;  

//Dallas  definitions
#define ONE_WIRE_BUS 5 
 //Data wire plugged into pin 5 of the arduino

//Dallas functions
OneWire oneWire(ONE_WIRE_BUS);         
DallasTemperature sensors(&oneWire);  

//various variables for sensors
long time_var;   //time variable
float dtemp;     //value of temperature in degrees celsius measured by the Dallas temp probe

//to transmit data over BLE the data has to be in the form of strings which are defined here
char strdtemp[8];          // string for temperature from Dallas temperature probe

//variable for the file that will save data to the SD
File MyFile;


void setup()
{
  // start serial port
  Serial.begin(9600);
  Serial.println("TAMB Startup");
  
  //WHY WONT YOU SHOW UPPPPPPPP
  // names the BLE device BLE6
  ble_set_name("BLE100");  
  ble_begin();

  //default chip select pin set to output
  pinMode(53, OUTPUT);


  //checking if the SD is present and can be initialized
  if(!SD.begin(53))
  {
     Serial.println("Card Failed, or not present error");
     while (1);
  }

  Serial.println("SD Card initialized.");

  // Opening up the file to be written on
  MyFile = SD.open("Dallas.txt", FILE_WRITE);

  if (! MyFile)
  {
    Serial.println("Error opening Dallas.txt");
    while(1);
  }
  
  // Start up the library
  sensors.begin();
  delay (3000);          //5 second delay after reseting to take out SD


}
 


 
void loop(void)
{
  
  delay(50);
  time_var = millis();

  //Dallas Probe
  sensors.requestTemperatures();             // Sends command to get temperatures
  float dtemp = sensors.getTempCByIndex(0);  // Temperatures from Dallas

  //prints temperatures to the serial monitor
  Serial.print("Temperature: ");
  Serial.println(dtemp);

  //Writing in SD
  MyFile = SD.open("Dallas.txt", FILE_WRITE);
  MyFile.println("New Test");
  
  if (MyFile)
    {
      Serial.print("Writing to file...");
      SDwrite(time_var, dtemp);           //this function writes the data

      MyFile.close();
      Serial.println("done.");
    }
    else
    {
      //if the file didn't open, print the error:
      Serial.println("error opening Dallas.txt");
    }
}



void SDwrite(long time_var, float dtemp)
{
  if(!use_processing)
  {
    Serial.print("Writing to Dallas.txt...");
      MyFile.print(time_var); MyFile.print(", ");
      MyFile.print(dtemp); MyFile.print(", ");
  
      // close the file:
      MyFile.close();
    }
    else 
    {
    // if the file didn't open, print an error:
    Serial.println("error opening t1.txt");
  }

//must be in strings to be read on BLE; this converts from float to string
dtostrf(dtemp, 4, 1, strdtemp); 
  
  if ( ble_connected() )
    {
        ble_write('A'); ble_write('I'); ble_write('R'); ble_write(' '); ble_write('D'); ble_write('A'); ble_write('T'); ble_write('A'); ble_write(':');             
        ble_write(' '); ble_write(' '); ble_write(' ');           // all of these ble_write(' '); just print spaces on the app. These can be added   
                                                                  // and deleted to make the formatting on a device look better.
                                                                  // Currently the spaces are in place to make the output look nice on an iPhone 6/6s screen.
  
        // send temperature to device
        ble_write('T');
        ble_write(':');  
        ble_write (' ');
        ble_write_bytes((unsigned char *)strdtemp, strlen(strdtemp) + 1), ble_write (' ');
        ble_write ('C');
        ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' '); ble_write(' ');               
  
        //this is just printing the same information to the serial monitor when the TAMB is connected to the computer with the USB cable. It is NOT Bluetooth data
        Serial.print("Temperature: ");
        Serial.print(dtemp);
        Serial.print(" *C ");
      
    }
    ble_do_events();
  
    if ( ble_available() )
    {
      while ( ble_available() )
      {
        Serial.write(ble_read());
      }
    
      Serial.println();
    }

    delay(200);
}

Sorry about the formatting of the code. anywhere you would expect a “#” or “<” has been modified to make it look better.

Thanks in advance!!


#2

Hi @denisclement,

Please add ble_do_events(); at the end of the loop();. This function deals with BLE events and executes BLE read/write operation. It’s required in your every sketch.