I am trying to run this code in case there is no wifi or cloud connection. If I remove Blynk.begin and Blynk.run the code is executed. Can someone assist me with this please.
// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
// This #include statement was automatically added by the Particle IDE.
#include <MFRC522.h>
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
const uint32_t msRetryDelay = 2*60000; // retry every 2min
const uint32_t msRetryTime = 30000; // stop trying after 30sec
bool retryRunning = false;
bool state = HIGH;
Timer retryTimer(msRetryDelay, retryConnect); // timer to retry connecting
Timer stopTimer(msRetryTime, stopConnect); // timer to stop a long running try
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //Demo
char server[] = "xxxxxxxxxxxxxx";
#define SS_PIN A2
#define RST_PIN D2
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
WidgetLCD lcd(V7); // Blynk LCD Widget pin V7 LCD
WidgetLCD lcd2(V8); // Blynk LCD Widget pin V5 LCD
Servo myServo;
#define A45 V1 // 45 button V1
#define B90 V2 // 90 button V2
#define C135 V6 // 135 button V6
#define readValue V5 // read Servo Angle Labeled Display V5
#define BLYNK_PRINT Serial
const int led7 = D7;
const int servoPin = D3; // servo connected to pin D3 on Photon
void setup() {
myServo.attach(servoPin); // attaches the servo on pin D1 to the servo object
myServo.write(10); // Sets Sservo Position to 10 due to jittering at 0degrees
pinMode(led7, OUTPUT);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
mfrc522.setSPIConfig();
mfrc522.PCD_Init(); // Init MFRC522 card
Particle.connect();
if (!waitFor(Particle.connected, msRetryTime))
WiFi.off(); // no luck, no need for WiFi
Blynk.begin(auth, server);
}
BLYNK_READ(readValue)
{
Blynk.virtualWrite(readValue, (myServo.read() - 10)); // Push Mode sends value instantly
}
BLYNK_WRITE(A45) //Set angle at 45degrees
{
int status1 = param.asInt();
if (status1 == 1)
{
myServo.write(55);
}
else if (status1 == 0)
{
myServo.write(10);
}
}
BLYNK_WRITE(B90) //Set angle at 90degrees
{
int status2 = param.asInt();
if (status2 == 1)
{
myServo.write(100);
}
else if (status2 == 0)
{
myServo.write(10);
}
}
BLYNK_WRITE(C135) //Set angle at 135degrees
{
int status3 = param.asInt();
if (status3 == 1)
{
myServo.write(145);
}
else if (status3 == 0)
{
myServo.write(10);
}
}
void retryConnect()
{
if (!Particle.connected()) // if not connected to cloud
{
stopTimer.start(); // set of the timout time
WiFi.on();
Particle.connect(); // start a reconnectino attempt
}
else // if already connected
{
retryTimer.stop(); // no further attempts required
retryRunning = false;
}
}
void stopConnect()
{
if (!Particle.connected()) // if after retryTime no connection
WiFi.off(); // stop trying and swith off WiFi
stopTimer.stop();
}
void loop() {
digitalWrite(D7, !digitalRead(D7));
if (!retryRunning && !Particle.connected())
{ // if we have not already scheduled a retry and are not connected
stopTimer.start(); // set timeout for auto-retry by system
retryRunning = true;
retryTimer.start(); // schedula a retry
}
delay(500);
Blynk.run();
// Look for new cards
if ( mfrc522.PICC_IsNewCardPresent()) {
if ( mfrc522.PICC_ReadCardSerial()) {
String UID = "";
for (byte i = 0; i < mfrc522.uid.size; i++) {
UID.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
UID.concat(String(mfrc522.uid.uidByte[i], HEX));
}
mfrc522.PICC_HaltA();
UID.toUpperCase();
if (UID.substring(1) == "04 83 26 02 84 46 80") //change here the UID of the card/cards that you want to give access
{
lcd.clear();
lcd.print(0, 0, "UID:");
lcd.print(0, 1, UID);
delay(1000);
lcd.clear();
lcd.print(0, 0, "Door is");
lcd.print(0, 1, "----UNLOCKED----");
lcd2.clear();
lcd2.print(0, 0, "Welcome Nick...");
lcd2.print(0, 1, " Access Granted ");
Blynk.virtualWrite(readValue, 90);
myServo.write(100);
delay(3000);
lcd.clear();
lcd2.clear();
lcd.print(0, 0, "Door is");
lcd.print(0, 1, "-----LOCKED-----");
lcd2.print(0, 0, "Please present");
lcd2.print(0, 1, "security pass");
myServo.write(10);
}
else if (UID.substring(1) == "85 08 06 AB") //change here the UID of the card/cards that you want to give access
{
lcd.clear();
lcd.print(0, 0, "UID:");
lcd.print(0, 1, UID);
delay(1000);
lcd.clear();
lcd.print(0, 0, "Door is");
lcd.print(0, 1, "----UNLOCKED----");
lcd2.clear();
lcd2.print(0, 0, "Welcome Elena...");
lcd2.print(0, 1, " Access Granted ");
Blynk.virtualWrite(readValue, 45);
myServo.write(55);
delay(3000);
lcd.clear();
lcd.print(0, 0, "Door is");
lcd.print(0, 1, "-----LOCKED-----");
lcd2.clear();
lcd2.print(0, 0, "Please present");
lcd2.print(0, 1, "security pass");
myServo.write(10);
}
else {
myServo.write(10);
lcd.clear();
lcd.print(0, 0, "UID:");
lcd.print(0, 1, UID);
delay(1000);
lcd2.clear();
lcd2.print(0, 0, "Unauthorised....");
lcd2.print(0, 1, " ACCESS DENIED ");
delay(2000);
lcd.clear();
lcd2.clear();
lcd2.print(0, 0, "Please present");
lcd2.print(0, 1, "security pass");
}
}
}
}