Problem ESP8266 wifimanager conect Blynk

Could someone please help me with this code, I got it from here but I don’t remember where honestly, it works, in the sense that it allows me to choose the network through the wifimanager interface, on the mobile I made four buttons and also on the web dashboard , they all work, that is, if I turn the mobile on or off, the same thing happens on the dashboard and vice versa, but I see that I’m offline on the dashboard, and if I try to turn an LED on or off, for example in a GPIO of ESP, nothing happens , what I intend is to have an interface to connect from any network where I am, and be able to trigger an LED, if anyone knows a better idea I would appreciate it.
Thanks.

#include <ESP8266WebServer.h>
#include <WiFiManager.h>      //https://github.com/tzapu/WiFiManager
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#define BLYNK_TEMPLATE_ID "T"
#define BLYNK_DEVICE_NAME "4 reles"
#define BLYNK_AUTH_TOKEN "X"


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XphuvSVTS5ymcH7Sbs8BPrEybxxrakDR";
char ssid[] = "NET"; // username or ssid of your WI-FI
char pass[] = "xxxxxx"; // password of your Wi-Fi

// Your WiFi credentials.
// Set password to "" for open networks.

// Set your LED and physical button pins here
const int ledPin1 = 12;
const int ledPin2 = 13;
const int ledPin3 = 14;
const int ledPin4 = 2;
const int btnPin1 = 5;
const int btnPin2 = 4;
const int btnPin3 = 0;
const int btnPin4 = 1;

BlynkTimer timer;
void checkPhysicalButton();

int led1State = LOW;
int btn1State = HIGH;

int led2State = LOW;
int btn2State = HIGH;

int led3State = LOW;
int btn3State = HIGH;

int led4State = LOW;
int btn4State = HIGH;

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V12);
  Blynk.syncVirtual(V13);
  Blynk.syncVirtual(V14);
  Blynk.syncVirtual(V15);

  // Alternatively, you could override server state using:
  Blynk.virtualWrite(V12, led1State);
  Blynk.virtualWrite(V13, led2State);
  Blynk.virtualWrite(V14, led3State);
  Blynk.virtualWrite(V15, led4State);

}

// When App button is pushed - switch the state
BLYNK_WRITE(V12) {
  led1State = param.asInt();
  digitalWrite(ledPin1, led1State);
}
  
 BLYNK_WRITE(V13) {
  led2State = param.asInt();
  digitalWrite(ledPin2, led2State);
 }
BLYNK_WRITE(V14) {
  led3State = param.asInt();
  digitalWrite(ledPin3, led3State);
}
BLYNK_WRITE(V15) {
  led4State = param.asInt();
  digitalWrite(ledPin4, led4State);
}

void checkPhysicalButton()
{
  if (digitalRead(btnPin1) == LOW) {
    // btn1State is used to avoid sequential toggles
    if (btn1State != LOW) {

      // Toggle LED state
      led1State = !led1State;
      digitalWrite(ledPin1, led1State);

      // Update Button Widget
      Blynk.virtualWrite(V12, led1State);
    }
    btn1State = LOW;
  } else {
    btn1State = HIGH;
  }

  if (digitalRead(btnPin2) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn2State != LOW) {

      // Toggle LED state
      led2State = !led2State;
      digitalWrite(ledPin2, led2State);

      // Update Button Widget
      Blynk.virtualWrite(V13, led2State);
    }
    btn2State = LOW;
  } else {
    btn2State = HIGH;
  }

  if (digitalRead(btnPin3) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn3State != LOW) {

      // Toggle LED state
      led3State = !led3State;
      digitalWrite(ledPin3, led3State);

      // Update Button Widget
      Blynk.virtualWrite(V14, led3State);
    }
    btn3State = LOW;
  } else {
    btn3State = HIGH;
  }

  if (digitalRead(btnPin4) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btn4State != LOW) {

      // Toggle LED state
      led4State = !led4State;
      digitalWrite(ledPin4, led4State);

      // Update Button Widget
      Blynk.virtualWrite(V15, led4State);
    }
    btn4State = LOW;
  } else {
    btn4State = HIGH;
  }
}

void setup()
{
  Serial.begin(115200);
  WiFiManager wifiManager;
wifiManager.autoConnect("AutoConnectAP");
Serial.println("");
 Serial.println("wifi connected...yeey :");
Serial.println("IP address:");
Blynk.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
 
  // Debug console
  Serial.begin(9600);

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

  pinMode(ledPin1, OUTPUT);
  pinMode(btnPin1, INPUT_PULLUP);
  digitalWrite(ledPin1, led1State);
  

  pinMode(ledPin2, OUTPUT);
  pinMode(btnPin2, INPUT_PULLUP);
  digitalWrite(ledPin2, led2State);
  

  pinMode(ledPin3, OUTPUT);
  pinMode(btnPin3, INPUT_PULLUP);
  digitalWrite(ledPin3, led3State);
  

  pinMode(ledPin4, OUTPUT);
  pinMode(btnPin4, INPUT_PULLUP);
  digitalWrite(ledPin4, led4State);

  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
}

void loop()
{
  Blynk.run();
  timer.run();
}

These three lines of code need to be the first three lines of your sketch.
You also need to be using version 1.0.1 of the Blynk library.

Use the Byank Edgent example, as this has built-in provisioning as well as OTA.

Pete

1 Like

Hi PeteKnight,

First thanks for responding quickly, second you were right, just put the three lines #of BLYNK_TEMPLATE_ID “T”
#define BLYNK_DEVICE_NAME “4 lows”
#define BLYNK_AUTH_TOKEN “X”
As you mentioned, and everything is working.
Apparently the library is up to date.

Thank you very much for your help, you are a person who should win several medals from this forum, but real medals, not virtual ones, because your help is priceless.
May God, or whoever commands this world, give you health and happiness always in your life.

Greetings friend!!!

1 Like

Ola Pete,
eu não encontrei este exemplo, você pode me informar o link?

Thank you

You can check the documentation for more details

and I recommend you to check this very useful article as well

1 Like

Thank you for your always much appreciated help!

1 Like

Thank you so much John93

1 Like

I’m using this code, it works ok, but I can’t make the relays of the module I’m using, which work with low level logic, that is, activate when the input level is low, start in a low state, and only turn on when they receive low level, I don’t know if it’s the configuration on the dashboard or in the code that I should change, but when energizing the ESP, it turns on the relays, but I want them to remain off, and they only turn on when they receive logic 0.
Thanks to those who can enlighten me.

This code give two alternative options, the first is to retrieve the values from the Blynk server/app.
The second is to use the values defined at the top of the sketch. You have both pieces of code un-commented, which doesn’t make any sense.

If you’re struggling to track the state of the variables then add some serial print statements.
If there is a situation where you need to write the opposite value to a relay (HIGH instead of LOW for example) then you can use the logical NOT operator which is an exclamation mark (!)

Pete.

1 Like