BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

OTA NodeMCU

esp8266

#1

I wrote some code for a light management project for my house to be controlled with the Blynk app and Google Assistant with the help of ifttt but I tried to add the lines of code for the OTA updates. According to you, will it work ???

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;

#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxx"; //nodemcu test
 
char ssid[] = "xxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxx";

//ifttt Esempio: http://139.59.206.133/xxxxxxxxxxxxxxx/update/V0
//Method: PUT
//Content Type: application/json
//Body: ["1"]


//INIZIO PROGRAMMAZIONE PER IFTTT GOOGLE
BLYNK_WRITE(V1)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D1, LOW); // (D1 NODEMCU) GPIO 5 LOW (......... RELE IN1)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D1, HIGH);  // Set  Pin 5 LOW
          Blynk.virtualWrite(V1, HIGH);// Set  Virtual Pin 0 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE

}
}

BLYNK_WRITE(V2)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D2, LOW); // (D2 NODEMCU) GPIO 4 LOW (......... RELE IN2)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D2, HIGH);  // Set  Pin 4 LOW
          Blynk.virtualWrite(V2, HIGH);// Set  Virtual Pin 2 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V3)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D3, LOW); // D3 is GPIO 0 (............. RELE IN3)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D3, HIGH);  // Set  Pin 0 LOW
          Blynk.virtualWrite(V3, HIGH);// Set  Virtual Pin 3 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V4)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D4, LOW); // D4 is GPIO 2 (............. RELE IN4)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D4, HIGH);  // Set  Pin 2 LOW
          Blynk.virtualWrite(V4, HIGH);// Set  Virtual Pin 4 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V5)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D5, LOW); // D5 is GPIO 14 (............. RELE IN5)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D5, HIGH);  // Set  Pin 14 LOW
          Blynk.virtualWrite(V5, HIGH);// Set  Virtual Pin 5 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V6)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D6, LOW); // D6 is GPIO 12 (............. RELE IN6)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D6, HIGH);  // Set  Pin 12 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 6 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V7)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D7, LOW); // D7 is GPIO 13 (............. RELE IN7)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D7, HIGH);  // Set  Pin 13 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 7 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V8)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D8, LOW); // D8 is GPIO 15 (............. RELE IN8)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D8, HIGH);  // Set  Pin 15 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 8 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}
//FINE PROGRAMMAZIONE PER IFTTT GOOGLE
void setup() {
  Serial.begin(115200);
  Serial.println("Booting");
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);
  while (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Connection Failed! Rebooting...");
    delay(5000);
    ESP.restart();

    ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH)
      type = "sketch";
    else // U_SPIFFS
      type = "filesystem";

 Serial.println("Start updating " + type);
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
      

  pinMode(D1, OUTPUT); //D1 is GPIO 5 (..........RELE' IN1) (RELAY BUTTON)
  digitalWrite(D1, HIGH); // Set GPIO 5 HIGH

  
  pinMode(D2, OUTPUT); //D2 is GPIO 4 (..........RELE' IN2) (RELAY BUTTON)
  digitalWrite(D2, HIGH); // Set GPIO 4 HIGH
  
  pinMode(D3, OUTPUT); //D3 is GPIO 0 (..........RELE' IN3) (RELAY BUTTON)
  digitalWrite(D3, HIGH); // Set GPIO 0 HIGH
 
  
  pinMode(D4, OUTPUT); //D4 is GPIO 2 (..........RELE' IN4) (RELAY BUTTON)
  digitalWrite(D4, HIGH); // Set GPIO 2 HIGH
  
  pinMode(D5, OUTPUT); //D5 is GPIO 14 (..........RELE' IN5) (RELAY BUTTON)
  digitalWrite(D5, HIGH); // Set GPIO 14 HIGH

  pinMode(D6, OUTPUT); //D6 is GPIO 12 (..........RELE' IN6) (RELAY BUTTON)
  digitalWrite(D6, HIGH); // Set GPIO 12 HIGH

  pinMode(D7, OUTPUT); //D7 is GPIO 13 (..........RELE' IN7) (RELAY BUTTON)
  digitalWrite(D7, HIGH); // Set GPIO 13 HIGH

  pinMode(D8, OUTPUT); //D8 is GPIO 15 (..........RELE' IN8) (RELAY BUTTON)
  digitalWrite(D8, HIGH); // Set GPIO 15 HIGH
  
 Blynk.begin(auth, ssid, pass);
  

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

#2

it seems to be ok.
had you tried to upload the code with OTA?


#3

I use a much simpler block of OTA code in Void Setup()

  ArduinoOTA.onError([](ota_error_t error) { ESP.restart(); });
  ArduinoOTA.setHostname("Your Device Name In here");
  ArduinoOTA.begin();

Defining a host name is optional, but really useful when you have multiple devices and want to ensure that you flash the correct one.

All of the Serial.print statements in the OTA code are fairly useless, as serial monitor doesn’t work over the air, so you’ll only see them when it’s connected to a serial port - in which case there’s no need for OTA anyway.

Pete.


#4

…or if you have esp with OLED integrated, then you can implement something like this:


{ corrected the link! }

This is a @Gunner excellent idea and work though


#5

I also have some basic OTA code here…


#6

Hi Gunner I took inspiration from your example and I rewrote the code. You can check if there is any error.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>  // For OTA w/ ESP8266
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA
#include <SimpleTimer.h>
SimpleTimer timer;

#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //nodemcu test
 
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxx";
char server[] = "192.168.1.63";  // IP for your Local Server
int port = 8080;

//test web http://139.59.206.133/xxxxxxxxxxxxxxxxxxxxxxxxxx/update/V1?value=1
//ifttt Esempio: http://139.59.206.133/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/update/V1
//Method: PUT
//Content Type: application/json
//Body: ["1"]


//INIZIO PROGRAMMAZIONE PER IFTTT GOOGLE
BLYNK_WRITE(V1)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D1, LOW); // (D1 NODEMCU) GPIO 5 LOW (......... RELE IN1)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D1, HIGH);  // Set  Pin 5 LOW
          Blynk.virtualWrite(V1, HIGH);// Set  Virtual Pin 0 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE

}
}

BLYNK_WRITE(V2)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D2, LOW); // (D2 NODEMCU) GPIO 4 LOW (......... RELE IN2)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D2, HIGH);  // Set  Pin 4 LOW
          Blynk.virtualWrite(V2, HIGH);// Set  Virtual Pin 2 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V3)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D3, LOW); // D3 is GPIO 0 (............. RELE IN3)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D3, HIGH);  // Set  Pin 0 LOW
          Blynk.virtualWrite(V3, HIGH);// Set  Virtual Pin 3 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V4)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D4, LOW); // D4 is GPIO 2 (............. RELE IN4)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D4, HIGH);  // Set  Pin 2 LOW
          Blynk.virtualWrite(V4, HIGH);// Set  Virtual Pin 4 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V5)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D5, LOW); // D5 is GPIO 14 (............. RELE IN5)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D5, HIGH);  // Set  Pin 14 LOW
          Blynk.virtualWrite(V5, HIGH);// Set  Virtual Pin 5 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V6)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D6, LOW); // D6 is GPIO 12 (............. RELE IN6)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D6, HIGH);  // Set  Pin 12 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 6 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V7)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D7, LOW); // D7 is GPIO 13 (............. RELE IN7)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D7, HIGH);  // Set  Pin 13 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 7 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}

BLYNK_WRITE(V8)
{   
  int value = param.asInt(); // Get value as integer
  
  if (value == 1) //VALORE CHE INTERPRETA
  {
     digitalWrite(D8, LOW); // D8 is GPIO 15 (............. RELE IN8)
     timer.setTimeout(500L, []() {  // Tempo funzionamento 0.5 secondi
          digitalWrite(D8, HIGH);  // Set  Pin 15 LOW
          Blynk.virtualWrite(V6, HIGH);// Set  Virtual Pin 8 HIGH
           });  // FINE FUNZIONE TIMER GOOGLE


}
}
//FINE PROGRAMMAZIONE PER IFTTT GOOGLE
void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  Blynk.connect();;

  ArduinoOTA.setHostname("NodeMCU_LUCI_ZONA_GIORNO");  // For OTA - Use your own device identifying name
  ArduinoOTA.begin();  // For OTA


  pinMode(D1, OUTPUT); //D1 is GPIO 5 (..........RELE' IN1) (RELAY BUTTON)
  digitalWrite(D1, HIGH); // Set GPIO 5 HIGH

  
  pinMode(D2, OUTPUT); //D2 is GPIO 4 (..........RELE' IN2) (RELAY BUTTON)
  digitalWrite(D2, HIGH); // Set GPIO 4 HIGH
  
  pinMode(D3, OUTPUT); //D3 is GPIO 0 (..........RELE' IN3) (RELAY BUTTON)
  digitalWrite(D3, HIGH); // Set GPIO 0 HIGH
 
  
  pinMode(D4, OUTPUT); //D4 is GPIO 2 (..........RELE' IN4) (RELAY BUTTON)
  digitalWrite(D4, HIGH); // Set GPIO 2 HIGH
  
  pinMode(D5, OUTPUT); //D5 is GPIO 14 (..........RELE' IN5) (RELAY BUTTON)
  digitalWrite(D5, HIGH); // Set GPIO 14 HIGH

  pinMode(D6, OUTPUT); //D6 is GPIO 12 (..........RELE' IN6) (RELAY BUTTON)
  digitalWrite(D6, HIGH); // Set GPIO 12 HIGH

  pinMode(D7, OUTPUT); //D7 is GPIO 13 (..........RELE' IN7) (RELAY BUTTON)
  digitalWrite(D7, HIGH); // Set GPIO 13 HIGH

  pinMode(D8, OUTPUT); //D8 is GPIO 15 (..........RELE' IN8) (RELAY BUTTON)
  digitalWrite(D8, HIGH); // Set GPIO 15 HIGH
  
 Blynk.begin(auth, ssid, pass);
  

} 
void loop() {
  timer.run();
  ArduinoOTA.handle();  // For OTA
  Blynk.run();
}

#7

Work, thanks


#8

a question in your opinion because when I give power to the circuit the relays to pin D3 and D4 light up for a while and then go off?


#9

Bad choice of pins (GPIO0 and GPIO2).
Read this:

Pete.


#10

I need 4 pins to control the relays. which advise me to pin?


#11

Try the green ones first :stuck_out_tongue: (in the chart already provided)


#12

yes, in fact I had really thought of those:

D1 = Pin 5
D2 = Pin 4
D5 = Pin 14
D6 = Pin 12

P.S.: my past projects I chose the pins at random, you never stop to learn …


#13

the problem occurs if I use the code with OTA updates if I use the code without OTA the problem of the relay at the circuit attention does not occur

Why do you think?


#14

IDK. Try putting the OTA stuff at the end of you setup.


#15

inserting the strings of code OTA at the end works as indicated by the table the pins D3 and D4 I had to exclude them