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

Multiply function code

esp8266
#1

dear good morning

I have this code that triggers a lamp and checks the status of the variable using a pulse switch.

in this example, I only switch on one lamp, what is the most effective way to change the code for 4 lamps?

  • board esp8266

thanks in advance!

/* Comente isso para desativar impressões e economizar espaço */ 

#define BLYNK_PRINT Serial 

#include <ESP8266WiFi.h>

#include <BlynkSimpleEsp8266.h>

#include <DHT.h>

  

// You should get Auth Token in the Blynk App. 

// Go to the Project Settings (nut icon). 

char auth[] = "xx"; 

  

// Your WiFi credentials. 

// Set password to "" for open networks. 

char ssid[] = "Leo"; 

char pass[] = "12345678"; 

  

/*DECLARAÇÃO DO PINO RELÉ = D7**/ 

const int relePin = 13; //PINO EM QUE O RELÉ ESTÁ CONECTADO = (D7) 

/*DECLARAÇÃO DO PINO RELÉ = D7**/ 

  

/*DECLARAÇÃO DO PINO PULSADOR = D5*/ 

const int pulsadorPin = 14; //PINO EM QUE O PULSADOR ESTÁ CONECTADO = (D5) 

/*DECLARAÇÃO DO PINO PULSADOR = D5*/ 

  

/*CORRESPONDENTE DHT11 = D4*/ 

#define DHTPIN 2 //PINO DIGITAL CONECTADO DHT11 = D4 

/*CORRESPONDENTE DHT11 = D4*/ 

  

/*QUAL TIPO SENSOR DE TEMPERATURA ESTA EM USO = D4*/ 

#define DHTTYPE DHT11     // DHT 11 

//#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321 

//#define DHTTYPE DHT21   // DHT 21, AM2301 

/*QUAL TIPO SENSOR DE TEMPERATURA ESTA EM USO = D4*/ 

  

/*CORRESPONDENTE DHT11 = D4*/ 

DHT dht(DHTPIN, DHTTYPE); //INICIANDO DHT11 

/*CORRESPONDENTE DHT11 = D4*/ 

  

BlynkTimer timer; //OBJETO DO TIPO BlynkTimer 

  

/*CORRESPONDENTE DHT11 = D4*/ 

  

/*Essa função envia o tempo de atualização do Arduino a cada segundo para o Virtual Pin (5).*/ 

void sendSensor() 

{ 

  float h = dht.readHumidity(); 

  float t = dht.readTemperature(); // ou dht.readTemperature (true) para Fahrenheit 

  

  if (isnan(h) || isnan(t)) { 

 Serial.println("Falha ao ler o sensor DHT!"); 

 return; 

  } 

  // Você pode enviar qualquer valor a qualquer momento. 

  // Por favor, não envie mais que 10 valores por segundo. 

  Blynk.virtualWrite(V5, h); 

  Blynk.virtualWrite(V6, t); 

  

  /* COR */ 

  //Blynk.setProperty(V5, "color", "#D3435C"); 

  //Blynk.setProperty(V6, "color", "#D3435C"); 

  /* COR */ 

} 

  

/*CORRESPONDENTE DHT11*/ 

  

/*CORRESPONDENTE ACIONAMENTO DO RELÉ = D7*/ 

void checkPhysicalButton(); //FUNÇÃO checkPhysicalButton 

int releState = LOW; //ESTADO INICIAL DO RELÉ 

int pulsadorState = LOW; //ESTADO INICIAL DO PULSADOR 

/*CORRESPONDENTE ACIONAMENTO DO RELÉ = D7*/ 

  

void setup() 

{ 

  // Debug console 

  Serial.begin(115200); 

  Blynk.begin(auth, ssid, pass); 

  //Você também pode especificar o servidor: 

  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80); 

  //Blynk.begin(auth, ssid, pass, IPAddress (192,168,1,100), 8080); 

  

  dht.begin(); //EXECUTANDO BIBLIOTECA DHT 

  

  /*CORRESPONDENTE ACIONAMENTO DO RELÉ = D7*/ 

  

  pinMode(relePin, OUTPUT); //DECLARA O PINO COMO SAÍDA 

  pinMode(pulsadorPin, INPUT_PULLUP); //DECLARA O PINO COMO ENTRADA E ATIVA O RESISTOR INTERNO DE PULL UP DO ESP-01 

  digitalWrite(relePin, releState); //RELÉ RECEBE O ESTADO INICIAL 

  timer.setInterval(100L, checkPhysicalButton); //DEFINE O INTERVALO DE 100 MILISSEGUNDOS(100L = 100 MILISSEGUNDOS) PARA EXECUÇÃO DA FUNÇÃO checkPhysicalButton 

  timer.setInterval(1000L, sendSensor); 

} 

BLYNK_CONNECTED() { //FUNÇÃO QUE VERIFICA A CONEXÃO COM O SERVIDOR 

  Blynk.syncVirtual(V2); //REQUISITA O ÚLTIMO ESTADO DO PINO VIRTUAL 2 

} 

BLYNK_WRITE(V2) { //FUNÇÃO QUE MUDA O ESTADO DO RELÉ QUANDO O BOTÃO NO APP É PRESISONADO 

  releState = param.asInt(); //VARIÁVEL RESPONSÁVEL POR RECEBER E ARMAZENAR O DADO VINDO DO APP 

  digitalWrite(relePin, releState); //ALTERNA O ESTADO ATUAL DO RELÉ 

} 

  

void checkPhysicalButton() { //FUNÇÃO QUE VERIFICA O PRESSIONAMENTO DO PULSADOR 

  

  if (digitalRead(pulsadorPin) == LOW) { //SE LEITURA DO PULSADOR FOR IGUAL A LOW, FAZ 

  

 if (pulsadorState != LOW) { //SE O ESTADO DO PULSADOR FOR DIFERENTE DE LOW (OU SEJA, HIGH), FAZ 

  

 releState = !releState; //ESTADO DO RELÉ, RECEBE O ESTADO ATUAL INVERTIDO 

 digitalWrite(relePin, releState); //ALTERNA O ESTADO ATUAL DO RELÉ 

  

 Blynk.virtualWrite(V2, releState); //ATUALIZA NO WIDGET O ESTADO ATUAL (INDEPENDENTE SE FOI ACIONADO VIA APP OU PULSADOR) 

    } 

 pulsadorState = LOW; //ESTADO DO PULSADOR RECEBE LOW 

  } else { //SENÃO, FAZ 

 pulsadorState = HIGH; //ESTADO DO PULSADOR RECEBE HIGH 

  } 

} 

  

/*CORRESPONDENTE ACIONAMENTO DOS RELÉS = D7*/ 

  

void loop() { 

  

  Blynk.run(); //INICIALIZA O BLYNK 

  timer.run(); //INICIALIZA O TIMER 

  

}

0 Likes

#2

How about repeting this part of the code 4 times using a different virtual pin?

0 Likes

#3

I still did not do this, I was looking for a better way to do this procedure without replicating code.

I will also need to add new pins

/DECLARAÇÃO DO PINO RELÉ = D7*/

const int relePin = 13; //PINO EM QUE O RELÉ ESTÁ CONECTADO = (D7)

/DECLARAÇÃO DO PINO RELÉ = D7*/

/DECLARAÇÃO DO PINO PULSADOR = D5/

const int pulsadorPin = 14; //PINO EM QUE O PULSADOR ESTÁ CONECTADO = (D5)

/DECLARAÇÃO DO PINO PULSADOR = D5/

0 Likes

#4

Then use arrays

0 Likes

#5

Sorry, could you tell me how to do this?

An example?

I’m new to this area.

0 Likes

#6

If you’re saying that you want to add three more switches, as well as three more relays, then you’re going to run out of useable pins.

Pete.

0 Likes

#7

I thought so,
4 pins for = relePin
4 pins for = Push button
1 pin for = dht11

would be a max setting on esp8266

0 Likes

#8

Especially when you read this:

Pete.

0 Likes

#9

I understand!
theoretically, what would be the best way to make these changes?

I will only know if it works correctly if I try kkk if one esp8266 is not enough I will buy several.

0 Likes

#10

Tell us a bit more about the project.
What will you be controlling? Do the devices being controlled occupy the same physical space, or are they scattered around the room or house?
Do the physical switches that operate the devices need to be next to each other, or are they located next to the devices?

Pete.

0 Likes

#11

I just reformed my house and put plaster on everything!
In my living room I need to control 6 pulse switches (control the lamps by the cell phone and mechanically by the pulse switch), 1 dhp22 sensor to measure the temperature and humidity of the environment.
I have 6 units of esp8266.
I have wifi in my residence
Initially I intend to make the living room only after it becomes easy to replicate the codes

The code above works perfectly! Meets exactly my needs, though it only controls 1 relay. I would like the help of you to help me in this code to make it at most trigger 3 more reles total of 4.

code test video running perfectly below, red wire making paper wrist switch.

0 Likes

#12

One solution is to use an ESP32.

Another is to use multiple ESP8266’s.

Pete.

0 Likes

#13

Okay, I’ll buy the esp32!
And referring to the code how to modify to control 6 reles?
example?

0 Likes

#14

I don’t know, I’ve never gone down the ESP32 route, but start by getting it working with a very simple Blynk sketch by searching this forum for examples, then do some searching for appropriate pins to use. The later comments in this thread might help:

Then it’s simply a choice of code replication versus arrays or pointers. I’d go for code replication myself, it’s easier and there are no real disadvantages.

Pete.

0 Likes