Arduino didnt work without hotspot after installing blynk

I have install multiple sensor in my arduino mega,and at last i install esp8266 for my mega to control led.But after i upload blynk code. Arduino will only work in network environment. All that component i didnt link with blynk cant run,such as i use lcd1602 to display temp,fan will run when temp going high and distance sensor with buzzer,fingerprint .All only will be function after my arduino is connected to my hotspot.

//code
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

#include <DYE_Fingerprint.h>
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino (WHITE wire)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);//finger print green,white

#include <OneWire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#include <DallasTemperature.h>
// Data wire is conntec to the Arduino digital pin 2
#define ONE_WIRE_BUS 5

Servo servol;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

//blynk
char auth[] = "key in already";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "blaze";
char pass[] = "123456789";

#define PIN_UPTIME V5
BLYNK_READ(PIN_UPTIME)
{
double temp1=sensors.getTempCByIndex(0);
Blynk.virtualWrite(PIN_UPTIME,temp1);

}

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1

// or Software Serial on Uno, Nano...
SoftwareSerial EspSerial(17, 16); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&Serial2);

DYE_Fingerprint finger = DYE_Fingerprint(&mySerial);

int INB = 28;//fan

//declare led port
int led1=7;

//distance A declare port

int trigPinA = 14; //Trig - green Jumper
int echoPinA = 15; //Echo - yellow Jumper
long A_duration, A_cm, A_inches;

//distance B declare port

int trigPinB = 18; //Trig - green Jumper
int echoPinB = 19; //Echo - yellow Jumper
long B_duration, B_cm, B_inches;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup()
{
Serial.begin(9600);
while (!Serial);
delay(100);
Serial.println("\n\nfinger detect test");

// set the data rate for the sensor serial port
finger.begin(57600);

if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
Serial.println("Waiting for valid finger...");

// Start up the library
sensors.begin();

//fan
pinMode(INB,OUTPUT);

//build in led
pinMode(13,OUTPUT);

//buzeer
pinMode(22, OUTPUT);

//led
pinMode(led1,OUTPUT);

//distance sensor A
pinMode(trigPinA, OUTPUT);
pinMode(echoPinA, INPUT);

//distance sensor B
pinMode(trigPinB, OUTPUT);
pinMode(echoPinB, INPUT);

// Debug console
Serial2.begin(115200);

delay(10);

Blynk.begin(auth, wifi, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

lcd.begin(16, 2);

for(int i = 0; i < 3; i++) {
lcd.backlight();
delay(250);
lcd.noBacklight();
delay(250);
}
lcd.backlight();

lcd.setCursor(0, 0);
lcd.print("SmartHouseSystem");
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Hello, PSIS!");
delay(8000);
lcd.clear();

}

void loop() // run over and over again
{
getFingerprintIDez();
//don't ned to run this at full speed.
sensors.requestTemperatures();

double temp=sensors.getTempCByIndex(0);

Serial.print("temp: ");
Serial.print(temp);
/*
Serial.print('\n');
Serial.print('\n');
Serial.print("Celsius temperature: ");
Serial.print(sensors.getTempCByIndex(0));

Serial.print('\n');
Serial.print('\n');
Serial.print("Fahrenheit temperature: ");
Serial.println(sensors.getTempFByIndex(0));
Serial.print('\n');
Serial.print('\n');
*/

Blynk.run();

if (temp <= 30){
digitalWrite(INB,LOW);
}

if(temp > 30){
digitalWrite(INB,HIGH);
}

//distance sensor A feedback
digitalWrite(trigPinA, LOW);
delayMicroseconds(5);
digitalWrite(trigPinA, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinA, LOW);

pinMode(echoPinA, INPUT);
A_duration = pulseIn(echoPinA, HIGH);

// convert the time into a distance
A_cm = (A_duration/2) / 29.1;
A_inches = (A_duration/2) / 74;
/*
Serial.print(A_inches);
Serial.print("in_A, ");
Serial.print(A_cm);
Serial.print("cm_A");
Serial.println();
*/

//distance sensor B feedback
digitalWrite(trigPinB, LOW);
delayMicroseconds(5);
digitalWrite(trigPinB, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinB, LOW);

pinMode(echoPinB, INPUT);
B_duration = pulseIn(echoPinB, HIGH);

// convert the time into a distance
B_cm = (B_duration/2) / 29.1;
B_inches = (B_duration/2) / 74;
/*
Serial.print(B_inches);
Serial.print("in_B, ");
Serial.print(B_cm);
Serial.print("cm_B");
Serial.println();
*/

if (A_cm<14)
{
digitalWrite(led1,HIGH) ;

delay(5000);
}

if (A_cm>=14)
{
digitalWrite(led1,LOW) ;

}

//door lock auto
if (B_cm<6)
{
delay(2000);
servol.attach(12);
servol.write(90);
}

if (B_cm>=5)
{
servol.attach(12);
servol.write(0);
}

lcd.setCursor(0, 0);
lcd.print("Temperature");
lcd.setCursor(0, 1);
lcd.print(temp);

}

uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;
}

// OK success!

p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;
}

// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;
}

// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);

return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;

p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;

p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK)
{
//buzzer
digitalWrite(22, HIGH);
delay(2000);
delay(analogRead(0));
digitalWrite(22, LOW);
delay(analogRead(0));

return -1;
}

// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
Serial.print('\n');
Serial.print("testing");
digitalWrite(13,HIGH);
delay(3000);
digitalWrite(13,LOW);

//buzzer
digitalWrite(22, HIGH);
delay(analogRead(0));
digitalWrite(22, LOW);
delay(analogRead(0));
digitalWrite(22, HIGH);
delay(analogRead(0));
digitalWrite(22, LOW);
delay(analogRead(0));

//servo
servol.attach(12);
servol.write(0);
delay(4000);
return finger.fingerID;
}

It is because Blynk.begin() waits for connection. use Blynk.config and Blynk will try to connect in loop in Blynk.run(). You must call WiFi.begin before Blynk.config

see blynk-library/BlynkWiFiCommon.h at master · blynkkk/blynk-library · GitHub

1 Like

Also learn to use CODE TAGS ( </> )

Ok,i will try blynk.config and tag command in new post haha . TQ everyone