I am using a Blynk with Arduino and a GSM module
I am using Blynk Lib 1.3.2
blynk is not connecting to the Arduino it will get online status for a time and then automatically turn in to offline.
Communication is not stable with the device
but when I disable all functions it will work correctly
Serial monitor OUTPUT
[416] Modem init...
[640] Connecting to network...
[730] Network: AirTel
[731] Connecting to iot.com ...
[5202] Connected to GPRS
[5273] Connecting to blynk.cloud:80
[8194] Ready (ping: 1047ms).
[15349] Connecting to blynk.cloud:80
[75227] Connecting to blynk.cloud:8080
[81702] Connecting to blynk.cloud:80
[83890] Ready (ping: 642ms).
[91044] Connecting to blynk.cloud:80
[144612] Connecting to blynk.cloud:8080
[146857] Connecting to blynk.cloud:80
[148529] Ready (ping: 901ms).
[155684] Connecting to blynk.cloud:80
[215541] Connecting to blynk.cloud:8080
[222163] Connecting to blynk.cloud:80
[224529] Ready (ping: 722ms).
[230856] Connecting to blynk.cloud:80
[232715] Ready (ping: 687ms).
[239079] Connecting to blynk.cloud:80
[245042] Connecting to blynk.cloud:80
[247252] Ready (ping: 644ms).
Actual code I have used in the project
#define BLYNK_TEMPLATE_ID "TMPL3yYUOdHY4"
#define BLYNK_TEMPLATE_NAME "demo"
#define BLYNK_AUTH_TOKEN "GNENki1kx4VKE5ot_hoe-xgnzU4q_abx"
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
#include <SoftwareSerial.h>
#include <EEPROM.h>
SoftwareSerial SerialAT(4, 3); // RX, TX
const char apniot[] = "iot.com";
const char user[] = "";
const char pass[] = "";
TinyGsm modem(SerialAT);
#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon1; // Create an instance
String logev;
int vmax = 0;
int vmin = 1000;
int motorpinadd = 0;
int runtimeadd = 1;
int sendtime = 0;
bool pushbutton = false;
bool oldbutton = false;
bool motorbutton = false;
bool supplystate = false;
bool timerled = false;
bool timerv = false;
bool motorstate = false;
bool waterstate = true;
bool limitset = false;
#define button 2
#define supplyledblue 5
#define supplyledred 6
#define supplyledgreen 7
#define watergreen 8
#define waterred 9
#define motorledred 10 //right
#define motorledgreen 11
#define motorpin 12
int tim = 0;
//String msg_data;
int ti = 0;
unsigned long motor_start_time;
unsigned long motor_runm;
BlynkTimer timer;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(motorledred, OUTPUT); // this pin is connected to supply led red
pinMode(motorledgreen, OUTPUT); //this pin is connected to supply led green
pinMode(waterred, OUTPUT); //this pin is connected to motor led red
pinMode(watergreen, OUTPUT); //this pin is connected to motor led green
pinMode(supplyledred, OUTPUT); // push button on the device
pinMode(supplyledgreen, OUTPUT); //this pin is connected to water pump
pinMode(supplyledblue, OUTPUT);
pinMode(motorpin, OUTPUT);
emon1.current(A0, 100);
pinMode(A1, INPUT);
oldbutton = digitalRead(2);
Serial.begin(9600);
SerialAT.begin(9600);
getsupply();
motorbutton = EEPROM.read(motorpinadd);
Serial.print("privius motor status was : ");
Serial.println(motorbutton ? "ON" : "OFF");
delay(100);
String modemInfo = modem.getModemInfo();
Serial.print("Modem Info: ");
Serial.println(modemInfo);
if ((EEPROM.read(runtimeadd)) > 0) {
sendtime = EEPROM.read(runtimeadd);
Serial.println("privius Motor run time is: " + String(sendtime));
}
modem.simUnlock("1234");
delay(100);
Blynk.begin(BLYNK_AUTH_TOKEN, modem, apniot, user, pass);
timer.setInterval(2000L, getwater);
timer.setInterval(1000L, getsupply);
timer.setInterval(1000L, manageoperations);
timer.setInterval(10000L, timert);
}
void loop() {
Blynk.run();
timer.run();
}
//void buttonISR() {
// buttonPressed = true;
//}
void getwater() {
double Irms = emon1.calcIrms(1480);
Serial.println("Current reading : " + String(Irms));
if ((motor_runm < 3) && (motorstate == true) && (motor_runm > 0)) {
if (vmax < Irms) {
vmax = Irms;
}
if (vmin > Irms) {
vmin = Irms;
}
}
if ((motor_runm == 3) && (limitset == false)) {
vmax = vmax * 1.10;
vmin = vmin * 1.10;
limitset = true;
}
if ((Irms < vmin) && (limitset == true)) {
waterstate = false;
} else {
waterstate = true;
}
if ((motorstate == false) && (Irms > 3)) {
digitalWrite(motorpin, LOW);
}
}
void getsupply() {
int val = analogRead(A1);
Serial.println("Supply reading : " + String(val));
if ((val > 50) && (val < 300)) {
timerv = true;
tim++;
} else {
//fault supply state
timerv = false;
supplystate = false;
tim = 0;
digitalWrite(supplyledred, HIGH);
digitalWrite(supplyledgreen, LOW);
digitalWrite(supplyledblue, LOW);
}
if (tim > 40) {
//correct supply state
timerled = true;
supplystate = true;
digitalWrite(supplyledred, LOW);
digitalWrite(supplyledgreen, HIGH);
digitalWrite(supplyledblue, LOW);
}
if ((supplystate == false) && (tim < 40) && (timerv == true)) {
//timer state
digitalWrite(supplyledred, LOW);
digitalWrite(supplyledgreen, LOW);
digitalWrite(supplyledblue, HIGH);
}
}
BLYNK_WRITE(V1) {
motorbutton = param.asInt();
EEPROM.update(motorpinadd, motorbutton ? 1 : 0);
}
void manageoperations() {
pushbutton = digitalRead(2);
if (pushbutton != oldbutton) {
oldbutton = pushbutton;
motorbutton = !motorbutton;
EEPROM.update(motorpinadd, motorbutton ? 1 : 0);
Blynk.virtualWrite(V1, motorbutton ? 1 : 0);
delay(50);
}
digitalWrite(motorledred, motorstate ? LOW : HIGH);
digitalWrite(motorledgreen, motorstate ? HIGH : LOW);
digitalWrite(waterred, waterstate ? LOW : HIGH);
digitalWrite(watergreen, waterstate ? HIGH : LOW);
Blynk.virtualWrite(V2, supplystate ? 1 : 0);
Blynk.virtualWrite(V3, motorstate ? 1 : 0);
Blynk.virtualWrite(V4, waterstate ? 1 : 0);
if ((motorbutton == true) && (supplystate == true) && (motorstate == false)) {
digitalWrite(motorpin, HIGH);
motor_start_time = millis();
logev = "motor_started";
//msg_data="motor_started";
motorstate = true;
Blynk.logEvent(logev);
} else if ((motorbutton == true) && (supplystate == false) && (motorstate == false)) {
//Blynk.virtualWrite(V1, 0);
logev = "motor_not_started_supply";
sendSMS();
} else if ((motorstate == true) && (supplystate == false)) {
digitalWrite(motorpin, LOW);
logev = "motor_stoped_supply";
//msg_data = ": run time is : ";
sendSMS();
motorstate = false;
limitset = false;
vmax = 0;
vmin = 1000;
} else if ((motorstate == true) && (waterstate == false)) {
digitalWrite(motorpin, LOW);
//msg_data = ": run time is : ";
logev = "motor_stoped_water";
sendSMS();
motorstate = false;
motorbutton = false;
Blynk.virtualWrite(V1, 0);
limitset = false;
waterstate = true;
vmax = 0;
vmin = 1000;
EEPROM.write(runtimeadd, 0);
sendtime = EEPROM.read(runtimeadd);
} else if ((motorbutton == false) && (motorstate == true)) {
digitalWrite(motorpin, LOW);
logev = "motor_off";
//msg_data = ": run time is : ";
sendSMS();
limitset = false;
motorstate = false;
vmax = 0;
vmin = 1000;
EEPROM.write(runtimeadd, 0);
sendtime = EEPROM.read(runtimeadd);
}
}
void sendSMS() {
Blynk.logEvent(logev);
}
void timert() {
if (motorstate == true) {
unsigned long motor_run_time = millis();
unsigned long motor_run = motor_run_time - motor_start_time;
unsigned long motor_run_sec_tot = motor_run / 1000;
motor_runm = motor_run_sec_tot / 60;
ti = motor_runm + sendtime;
EEPROM.write(runtimeadd, ti);
Blynk.virtualWrite(V6, ti);
Serial.println("Motor running time : " + String(ti));
} else if (motorstate == false) {
}
}
please help me with my project