Hello, this is the code i’m currently using. I tried to modify the timeout in Blynk.connect() to multiple values, but there was no success for me.
Edit: I think the guy in this thread is having the same problem: Code isn’t working without connected Blynk
Thanks in advance!
#include <Arduino.h>
#include <Blynk/BlynkApi.h>
#include <Blynk/BlynkHandlers.h>
#include <Blynk/BlynkParam.h>
#include <Blynk/BlynkProtocol.h>
#include <BlynkSimpleEsp8266.h>
#include <c_types.h>
#include <DallasTemperature.h>
#include <Esp.h>
#include <ESP8266WiFi.h>
#include <include/wl_definitions.h>
#include <OneWire.h>
#include <WiFiManager.h>
#include <cmath>
#include "../SmartTempMeter/SimpleTimer-master/SimpleTimer.h"
char auth[] = "[token]";
char server[] = "[server]";
int32 interval = 1000;
int32 blynkInterval = 5000;
boolean relayState;
const int relay = 12;
const int led = 13;
const int button = 0;
int buttonState = 1; // current state of the button
static long startPress = 0;
OneWire oneWire(14);
DallasTemperature sensors(&oneWire);
double_t temperature;
SimpleTimer tempTimer;
SimpleTimer wifiTimer;
SimpleTimer checkTimer;
void blinkLed() {
digitalWrite(led, 0);
delay(20);
digitalWrite(led, 1);
}
void checkBlynk() {
if (WiFi.status() == WL_CONNECTED) {
if (!Blynk.connected()) {
//WiFi ok, trying to connect to blynk
Blynk.connect(3000);
}
}
if (WiFi.status() == 1) {
//no WiFi connection
}
}
void handleTemperatures() {
if (Blynk.connected()) {
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0);
int16_t validation = (int16_t) (temperature);
if (validation > -126) {
Blynk.virtualWrite(V20, temperature);
}
//Serial.println(digitalRead(0));
/*GPOC = 1 << 13; //set gpio13 off LED is now active
delay(10);
GPOS = 1 << 13; //set gpio13 on LED is now off*/
//Blynk.syncAll();
}
}
void sendWifi() {
if (Blynk.connected())
Blynk.virtualWrite(3, map(WiFi.RSSI(), -105, -40, 0, 100));
}
void setup() {
Blynk.config(auth, server);
delay(100);
if (WiFi.status() == WL_CONNECTED)
Blynk.connect(3000);
sensors.begin();
checkTimer.setInterval(blynkInterval, checkBlynk);
tempTimer.setInterval(interval, handleTemperatures);
wifiTimer.setInterval(interval, sendWifi);
//Blynk.begin(auth, ssid, pass, server);
pinMode(button, INPUT);
pinMode(relay, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(led, 1);
digitalWrite(relay, 0);
}
void toggleRelay() {
if (relayState == 0) {
digitalWrite(relay, 1);
relayState = digitalRead(relay);
if (Blynk.connected())
Blynk.virtualWrite(V4, map(relayState, 0, 1, 0, 255));
} else {
digitalWrite(relay, 0);
relayState = digitalRead(relay);
if (Blynk.connected())
Blynk.virtualWrite(V4, map(relayState, 0, 1, 0, 255));
}
blinkLed();
}
void toggleRelayOff() {
digitalWrite(relay, 0);
relayState = digitalRead(relay);
if (Blynk.connected())
Blynk.virtualWrite(V4, map(relayState, 0, 1, 0, 255));
blinkLed();
}
void toggleRelayOn() {
digitalWrite(relay, 1);
relayState = digitalRead(relay);
if (Blynk.connected())
Blynk.virtualWrite(V4, map(relayState, 0, 1, 0, 255));
blinkLed();
}
BLYNK_WRITE(V0) //Button Widget is writing to pin V0
{
int pinData = param.asInt();
if (pinData == 1) {
toggleRelayOn();
}
}
BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
int pinData = param.asInt();
if (pinData == 1) {
toggleRelayOff();
}
}
BLYNK_WRITE(V2) //Button Widget is writing to pin V2
{
int pinData = param.asInt();
if (pinData == 1) {
toggleRelay();
}
}
void handleButton() {
int currentState = digitalRead(button);
if (currentState != buttonState) {
if (buttonState == LOW && currentState == HIGH) {
long duration = millis() - startPress;
if (duration < 1000) {
toggleRelay();
} else if (duration < 5000) {
ESP.reset();
} else if (duration < 10000) {
WiFiManager wifiManager;
wifiManager.setTimeout(300);
if (!wifiManager.startConfigPortal("OnDemandAP",
"esp-config")) {
for (int var = 0; var < 100; ++var) {
blinkLed();
}
delay(3000);
}
}
} else if (buttonState == HIGH && currentState == LOW) {
startPress = millis();
}
buttonState = currentState;
}
}
void loop() {
checkTimer.run();
if (Blynk.connected()) {
Blynk.run();
tempTimer.run();
wifiTimer.run();
}
handleButton();
}