Bluetooth Sketch won't run until connection is made

I have a heater that I control with Blynk over BLE. All is fine, except it won’t actually work until the BLE connection is made. I’m wondering if there’s something in my code doing this?
This is the first part of my code…

    //high heat is turned off!
#define BLYNK_USE_DIRECT_CONNECT
#include <BlynkSimpleSerialBLE.h>
char auth[] = "3aa8fd6ab4a049c8a062024d7b63d68a";
//register LEDwidget
BlynkTimer timer; //timer for how often to send/receive info to the app
BlynkTimer tiltTimer; //timer for how often to send/receive info to the app
WidgetLED LEDfan(V17);
WidgetLED LEDheat(V18);
WidgetLCD lcd(V0);


#include <Wire.h>
#include "DS3231.h"
DS3231 Clock;
bool h12 = true; //true is 24H time
bool PM;
boolean AM_Time;   //set to TRUE if the time is later than hourchange time

#include <EEPROM.h>
#include "writeAnything.h"

#include "DHT.h"
#define DHTPIN A3     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
//define pins
const int fanPin = A0;   //digital pin to control fan relay
const int tiltPin = A2;  //tilt sensor pin
const int heatPin1 = A1;  //digital pin to control low heat relay
const int heatPin2 = 8;  //digital pin to control high heat relay
const int RTCpin = 2;    //interrupt pin from DS3231 (not used)
const int VersionID = 1971; //arbitrary val for EEPROM check

//define variables
unsigned long fanMillis = 0; //keep track of heat stop time
boolean Heater_Fault; //flag for heater tilted or bad temp
boolean Temp_Fault; //flag for heater tilted or bad temp
boolean updateLEDS; //flag to update LEDS on app connect


float humidity;  //value from dht
float temperature; //value from dht
float heatIndex; //value from dht

struct confing_mySettings { //these are stored in EEPROM
  int VersionID; //for EEPROM check
  int AMtemp; //morning temp set point
  int PMtemp;      //evening temp set point
  int AMfan; //true = auto; false = on constantly
  int PMfan; //true = auto; false = on constantly
  int AMstartTime; //seconds from midnight for morning time to start
  int AMstopTime; //seconds from midnight for morning time to end
  int highHeatDelay;  //time heater runs before we switch to high heat (in minutes)
  int unitOff; //set to true to power off the unit
}
settings;

void setup() {
  EEPROM_readAnything(1, settings);     //read settings from EEPROM
  if (settings.VersionID != VersionID) initEEPROM();
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  Wire.begin(); //RTC communication over I2C
  dht.begin(); //start the DHT
  delay(2000);
  pinMode(fanPin, OUTPUT);
  pinMode(heatPin1, OUTPUT);
  pinMode(heatPin2, OUTPUT);
  pinMode(tiltPin, INPUT);
  timer.setInterval(4000L, timerEvents);
  tiltTimer.setInterval(500L, tiltLEDS);
  Blynk.virtualWrite(V17, 255); //Turn on LED
  Blynk.virtualWrite(V18, 255); //Turn on LED
}
unsigned long prevMillis;
void loop() {
  Blynk.run();
  timer.run(); //checks temp, tilt sensor, and RTC every 2 seconds
  tiltTimer.run();
  safetyCheck(); //check tilt and temp errors
  updateHeater(); //update the heater relays
  LEDS_status(); //update the status of the LEDS
}

This has always been a blocking command… no further processing until connected to Blynk’s server (or in this case your BT/BLE device).

The normal way of working around that is with the Blynk.config() option… but I don’t know if that will work on BT/BLE

:frowning:
I was hoping there was a way to make my heater work without aways having to connect to it with my phone. I looked at Blynk.config() a little but it’s way over my head

All that is different from Blynk.begin() is that you need to setup your network connection separately first… that’s all. However that is assuming a true network link, like Ethernet, WiFi, even serial via USB scripting on a networked PC.

BT/BLE is a direct to phone link and thus not applicable in this case.

I recommend trying out a WiFi based device like a Wemos D1 Mini or NodeMCU, then writing you code as recommended so that it works independently of the phone and even the server if necessary.

I’ve done projects with the Wemos D1 Pro Mini, and also the Particle Photon. Both work well. But this particular project needs occasional connection to my phone (BLE) but it’s in a different location all the time, so wifi would be a real hassle, even more so. than the current condition, where I have to start the Blynk app before the device will start working.
Too bad there’s not a way to bypass the block call. Once the BLE is connected, I can leave the area, and it still continues to work. …just the initial startup hangs up.

I don’t understand… does your heater move around as well?

Blynk is touted as IoT… so I suspect the BT/BLE option was more of a limited addition in the early stages… no idea what they plan on doing with it in the future.

Short answer, yes, every day pretty much! Long answer, It’s complicated.
It’s not that difficult to fire up the blynk app, though, to get it started, so it’s no big deal.

Perhaps a portable Local Server & AP setup for “No Internet required” WiFi use?

I don’t guess there’d be a way to mod the Blynk.begin(Serial, auth); to not block? But I’d need it to be able to connect later, if I wanted. It does that now – I can power it up, connect with my Blynk app, then leave the building, and the sketch will continue to run. also, I can come back, open the blynk app again, and it’ll reconnect. …would there be a way to mod the Blynk.begin() call somehow, maybe do away with it. Because when I leave the building it must go into a “reconnect mode” but it doesn’t stop the code from continuing… Maybe I could just trick it into thinking it did the blynk.begin and then lost connection?

Not with a BT/BLE connection method. And as mentioned, the alternative Blynk.config() only works with networked connection methods like WiFi, Ethernet, etc.

The developers have hinted at further improvements to the BT/BLE connection option… but no details or ETAs

Ok, thanks again. It’s great having this BLE option, even if it is in beta, and a little limited. It still functions great for my application.

Since my hardware DOES continue to operate, after I’ve left the area (BLE disconnect), and it will reconnect when I return and refresh the app, would there be a way that I could mod the Blynk library just for this BLE project so that I doesn’t need the Blynk.begin(). Maybe trick it into thinking it connected and disconnected?
Guess I’m grasping at straws, but in this application it would indeed be so useful…

Not without redesigning the entire Blynk App/Server/Device process :stuck_out_tongue: