I have a basic sketch on an Arduino Mega and I am using a BLE module to connect it to a Samsung tablet running the Blynk app which I will use to input different variables when the sketch is complete and also to monitor other variables on screen.
My problem is how the Blynk code works in the sketch, it tries to connect to the BLE and tablet before going on to the rest of the code. If it cannot connect to the bluetooth device, it just cycles over and over again and the rest of the sketch does not initiate.
Does anyone know, or can you see a way that I can work around this. I cannot have the whole sketch not run just because the bluetooth doesn't connect, it needs to run even it it don't connect. IS there a way of maybe trying to connect for a given time and if unsuccessful, it bypasses this part?
#define BLYNK_PRINT Serial
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
char auth[] = "f685142********************854b";
int fanstatus, fantimer;
void setup()
{
Serial.begin(9600);
pinMode(34, OUTPUT);
Serial1.begin(9600); // via BT Module
Blynk.begin(Serial1, auth); // Connect to App via BT
Serial.println("Waiting for connections...");
fanstatus=LOW;
digitalWrite(34, fanstatus);
fantimer=millis();
}
void resetfans()
{
Serial.println("RESET FANS CALLED");
if (fanstatus == HIGH)
{
fanstatus=LOW;
Serial.println("FANS LOW");
}
else
{
fanstatus=HIGH;
Serial.println("FANS HIGH");
}
digitalWrite(34, fanstatus);
fantimer=millis();
}
void loop()
{
Blynk.run();
if (millis() - fantimer > 4000)
{
resetfans();
}
}
The sketch is only a play around test. I have several elements/sensors/switches connected to this project so was testing the cable work on the fan relay, hence just making it switch on and off continuously, take no notice of what its actually doing.
You would think that it would just run the section of the loop that you posted, but it doesn't. It doesn't even exit the setup until BT is established. Most strange.
Also that "waiting for connections" message doesn't come up after connection, it comes up prior. Ill add some screen shots.
Actually looking at it again, that 'waiting for connections' doesn't even show up above.
I remember now that it goes through that connecting stage, then when it connect a it says 'waiting for connections', then a ping speed is displayed, then it says ready.
So it appears to halt before the waiting for connections message.
So it appears to halt before the waiting for connections message.
It is the Blynk.begin() call that is not returning. Without a link to the library you are using, there is no way to determine whether there are optional arguments to begin() that would cause it only try for some period of time, or how difficult it would be to make begin() return after a reasonable amount of time if no device to pair with is found.
I am a novice at this so please go easy on me if I say something stupid, but if it was not returning from the Blynk.begin(), shouldn't the serial monitor show all the other serial.print lines that are in the void setup?
I assumed that as it didn't print these lines in the void setup, it can't be getting to the end of the setup, which will explain why nothing in void loop works either.
Also with regards to the missing software library, if the sketch compiles and works without it being on my laptop, isn't it safe to say that it isn't actually being used?
shouldn't the serial monitor show all the other serial.print lines that are in the void setup?
Any of them that occur before the call to Blynk.begin(), yes.
For the code in reply #3, are we to assume that you don't see one or more of the ZERO Serial.print() statements between Serial.begin() and Blynk.begin()?
isn't it safe to say that it isn't actually being used?
Yes. But, SoftwareSerial is a standard library, so I'd be very surprised if you really didn't have it. Not finding it is a different story.
It's basically not doing anything at all after the Blynk.begin. It just stops there unless BT is established. In reply 3 is a screenshot of the serial monitor of exactly what I get when I load and run the code in the same reply 3.
I'm basically making a reef Tank controller and cannot afford for it to not restart properly if the power cuts out for a bit. Even if the BT don't restablish, I need the Arduino to kick in and run the pumps and heaters etc.
You are not providing enough information. You got the Blynk library from somewhere. It IS on your PC somewhere. If you can't find it, you need to at a minimum tell us what operating system you are using and where you looked.
Regardless of whether or not you can find it again on your PC, you should be able to tell us where you got it.
It is nowhere on my Macbook. I have fun a search of every file on the hard drive and it is not there. However it doesn't matter any more as I have abandoned the bluetooth connectivity as I have found an easier way of doing it.