Cannot make my code Feather 32u4 work without Serial Monitor

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
toygaryagci
 
Posts: 1
Joined: Tue Nov 21, 2017 11:17 am

Cannot make my code Feather 32u4 work without Serial Monitor

Post by toygaryagci »

Hi all,
As you might guess, I am a newbie. So, my question might sound silly but I could not find any helping documentation about this.
I am working on a BLE hardware that provides power readings to my Feather 32u4 and what my code basically does is to transmit this reading to an OTT app on my smartphone over BLE. The code works fine and I can see the readings on my app running on the mobile when the Feather is connected to my PC which runs Serial Monitor.
But when I disconnect the Feather from the PC and connect it to a power supply, I don't see anything on my app.
I think this is because the code uses AT commands to update the characteristic value of the BLE service.

Here is the code (with a basic "increment the value by 5") in question (I tried to strip the code to its base as much as possible. So in case you see some unnecessary variables, etc, that I forgot to remove, please, ignore them):

Code: Select all

#include <Seeed_BME280.h>
#include <Wire.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"

#if SOFTWARE_SERIAL_AVAILABLE
  #include <SoftwareSerial.h>
#endif

Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}

int32_t CPServiceId;
int32_t CPMeasureCharId;
int myPower = 250;

void setup() {
  
  while (!Serial); // required for Flora & Micro
  delay(500);
  Serial.begin(115200);
  if(!myBME.init()){
    Serial.println("Device error!");
  }

  Serial.println(F("---------------------------------------------------"));
  randomSeed(micros());
  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!") );


  /* Perform a factory reset to make sure everything is in a known state */
  if (! ble.factoryReset() ){
       error(F("Couldn't factory reset"));
  }
  /* Disable command echo from Bluefruit */
  ble.echo(false);
  /* Change the device name to make it easier to find */
  if (! ble.sendCommandCheckOK(F("AT+GAPDEVNAME=PwrMeter")) ) {
    error(F("Could not set device name?"));
  }
  /* Add the Cycling Power Service definition (UUID = 0x1818) */
  success = ble.sendCommandWithIntReply( F("AT+GATTADDSERVICE=UUID=0x1818"), &CPServiceId);
  if (! success) {
    error(F("Could not add CP service"));
  }
  /* Add the Cycling Power Measurement characteristic (UUID = 0x2A63) */
  success = ble.sendCommandWithIntReply( F("AT+GATTADDCHAR=UUID=0x2A63, PROPERTIES=0x10, MIN_LEN=2, MAX_LEN=4, VALUE=00-40"), &CPMeasureCharId);
    if (! success) {
    error(F("Could not add CPM characteristic"));
  }
  /* Add the Cycling Power Service to the advertising data (needed for Nordic apps to detect the service) */
  ble.sendCommandCheckOK( F("AT+GAPSETADVDATA=02-01-06-05-02-18-18-0a-18") );
  /* Reset the device for the new service setting changes to take effect */
  ble.reset();

}

void loop() {

    myPower += 5;
    if (myPower > 300) {
	  myPower = 250;
    }
	/*Convert myPower to HEX */
    int coef = myPower / 256;
    int myHex = myPower - (coef * 256);

	/*Update Characteristic with the HEX values using the AT command*/	
    ble.print( F("AT+GATTCHAR=") );
    ble.print( CPMeasureCharId );
    ble.print( F(",01-00-") );
    ble.print(myHex, HEX);
    ble.print("-");
    ble.println(coef, HEX);
    if ( !ble.waitForOK() )
    {
      Serial.println(F("Failed to get response!"));
    }
  }

  delay(100);

}

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: Cannot make my code Feather 32u4 work without Serial Mon

Post by adafruit_support_carter »

Comment out this line:

Code: Select all

  while (!Serial); // required for Flora & Micro
It's getting stuck there in an infinite loop waiting for a serial monitor connection that never occurs.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”