Putting 3 codes to 1 code / exit status 1

can sombedy explain how to do it ?
thanks

/* Zakomentujte to, aby ste zakázali tlač a ušetrili miesto */
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>

// Prejdite do nastavení projektu (ikona orecha).  (nut icon).

#define BLYNK_TEMPLATE_ID "000000"
#define BLYNK_DEVICE_NAME "000000"
#define BLYNK_AUTH_TOKEN "000000"


// Odkomentujte akýkoľvek typ, ktorý používate!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

/************************* Definícia pinov *********************************/
//Relé na spínanie spotrebičov
#define Relay1            D6  // v aplikacii dat GPIO cisla
#define Relay2            D2  // v aplikacii dat GPIO cisla
#define Relay3            D1  // v aplikacii dat GPIO cisla
#define Relay4            D5  // v aplikacii dat GPIO cisla

//DHT na čítanie hodnoty teploty a vlhkosti
#define DHTPIN            D7    // v aplikacii dat GPIO cisla

//bzučiak, ak ho chcete použiť na upozornenie
#define buzzer            D0    // v aplikacii dat GPIO cisla

//Výberové kolíky pre modul multiplexora na prepínanie medzi rôznymi snímačmi a poskytovanie údajov na jednom analógovom kolíku.
#define S0                D3    // v aplikácii dát GPIO cisla
#define S1                D4    // v aplikácii dát GPIO cisla

//Analógový kolík na čítanie prichádzajúcej analógovej hodnoty z rôznych snímačov.
#define analogpin         A0    // v aplikácii dát GPIO cisla

#define SwitchPin1 10  //SD3
#define SwitchPin2 0   //D3 
#define SwitchPin3 13  //D7
#define SwitchPin4 3   //RX

#define wifiLed   16   //D0

#define VPIN_BUTTON_1    V1
#define VPIN_BUTTON_2    V2
#define VPIN_BUTTON_3    V3
#define VPIN_BUTTON_4    V4

int toggleState_1 = 1; //Definujte celé číslo na zapamätanie stavu prepínania pre relé 1
int toggleState_2 = 1; //Definujte celé číslo na zapamätanie stavu prepínania pre relé 2
int toggleState_3 = 1; //Definujte celé číslo na zapamätanie stavu prepínania pre relé 3
int toggleState_4 = 1; //Definujte celé číslo na zapamätanie stavu prepínania pre relé 4

int wifiFlag = 0;

const int reedSwitch = 4;
const int led = 2; //optional

// Zistí vždy, keď sa zmení stav dverí
bool changeState = false;
// Drží stav jazýčkového spínača (1=otvorené, 0=zatvorené)
bool state;
String GarazStav;

// Pomocné premenné (zistí iba zmeny, ktoré sú od seba vzdialené 1500 milisekúnd)
unsigned long previousMillis = 0;
const long interval = 1500;


const char* ssid = "Internet";
const char* password = "000000";
const char* host = "maker.ifttt.com";  //  https://ifttt.com/maker_webhooks    
const char* apiKey = "000000";   //  https://ifttt.com/maker_webhooks

char auth[] = BLYNK_AUTH_TOKEN;

int proximity;
int light;
int gas;
int motion;


DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;

// Spustí sa vždy, keď jazýčkový spínač zmení stav
ICACHE_RAM_ATTR void changeDoorStatus() {
  Serial.println("State changed");
  changeState = true;
}

// Táto funkcia posiela čas zapnutia Arduina každú sekundu na Virtual Pin (5).
// V aplikácii by mala byť frekvencia čítania miniaplikácie nastavená na PUSH. To znamená
// že definujete, ako často sa majú odosielať údaje do aplikácie Blynk.

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    Serial.println("Nepodarilo sa čítať zo senzora DHT!");
    return;
  }

  // Môžete poslať ľubovoľnú hodnotu kedykoľvek.
  // Prosím, neposielajte viac ako 10 hodnôt za sekundu.
  Blynk.virtualWrite(V10, t);
  Blynk.virtualWrite(V11, h);
  Blynk.virtualWrite(V12, proximity);
  Blynk.virtualWrite(V13, gas);
  Blynk.virtualWrite(V14, motion);
  Blynk.virtualWrite(V15, light);
}


void relayOnOff(int relay) {

  switch (relay) {
    case 1:
      if (toggleState_1 == 1) {
        digitalWrite(Relay1, LOW); // zapni relé 1
        toggleState_1 = 0;
        Serial.println("Device1 ON");
      }
      else {
        digitalWrite(Relay1, HIGH); // vypni relé 1
        toggleState_1 = 1;
        Serial.println("Device1 OFF");
      }
      delay(100);
      break;
    case 2:
      if (toggleState_2 == 1) {
        digitalWrite(Relay2, LOW); // zapni relé 2
        toggleState_2 = 0;
        Serial.println("Device2 ON");
      }
      else {
        digitalWrite(Relay2, HIGH);  // vypni relé 2
        toggleState_2 = 1;
        Serial.println("Device2 OFF");
      }
      delay(100);
      break;
    case 3:
      if (toggleState_3 == 1) {
        digitalWrite(Relay3, LOW); // zapni relé 3
        toggleState_3 = 0;
        Serial.println("Device3 ON");
      }
      else {
        digitalWrite(Relay3, HIGH); // vypni relé 3
        toggleState_3 = 1;
        Serial.println("Device3 OFF");
      }
      delay(100);
      break;
    case 4:
      if (toggleState_4 == 1) {
        digitalWrite(Relay4, LOW); // zapni relé 4
        toggleState_4 = 0;
        Serial.println("Device4 ON");
      }
      else {
        digitalWrite(Relay4, HIGH); // vypni relé 4
        toggleState_4 = 1;
        Serial.println("Device4 OFF");
      }
      delay(100);
      break;
    default : break;
  }

}


void with_internet() {
  //Ručné ovládanie prepínačov
  if (digitalRead(SwitchPin1) == LOW) {
    delay(200);
    relayOnOff(1);
    Blynk.virtualWrite(VPIN_BUTTON_1, toggleState_1);   // Aktualizovať miniaplikáciu tlačidla
  }
  else if (digitalRead(SwitchPin2) == LOW) {
    delay(200);
    relayOnOff(2);
    Blynk.virtualWrite(VPIN_BUTTON_2, toggleState_2);   // Aktualizovať miniaplikáciu tlačidla
  }
  else if (digitalRead(SwitchPin3) == LOW) {
    delay(200);
    relayOnOff(3);
    Blynk.virtualWrite(VPIN_BUTTON_3, toggleState_3);   // Aktualizovať miniaplikáciu tlačidla
  }
  else if (digitalRead(SwitchPin4) == LOW) {
    delay(200);
    relayOnOff(4);
    Blynk.virtualWrite(VPIN_BUTTON_4, toggleState_4);   // Aktualizovať miniaplikáciu tlačidla
  }
}
void without_internet() {
  //Ručné ovládanie prepínačov bez internetu
  if (digitalRead(SwitchPin1) == LOW) {
    delay(200);
    relayOnOff(1);
  }
  else if (digitalRead(SwitchPin2) == LOW) {
    delay(200);
    relayOnOff(2);
  }
  else if (digitalRead(SwitchPin3) == LOW) {
    delay(200);
    relayOnOff(3);
  }
  else if (digitalRead(SwitchPin4) == LOW) {
    delay(200);
    relayOnOff(4);
  }
}

BLYNK_CONNECTED() {
  // Vyžiadanie najnovšieho stavu zo servera
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
}

// Keď je stlačené tlačidlo App - prepnite stav

BLYNK_WRITE(VPIN_BUTTON_1) {
  toggleState_1 = param.asInt();
  digitalWrite(Relay1, toggleState_1);
}

BLYNK_WRITE(VPIN_BUTTON_2) {
  toggleState_2 = param.asInt();
  digitalWrite(Relay2, toggleState_2);
}

BLYNK_WRITE(VPIN_BUTTON_3) {
  toggleState_3 = param.asInt();
  digitalWrite(Relay3, toggleState_3);
}

BLYNK_WRITE(VPIN_BUTTON_4) {
  toggleState_4 = param.asInt();
  digitalWrite(Relay4, toggleState_4);
}


void checkBlynkStatus() { // volané každé 3 sekundy pomocou SimpleTimer

  bool isconnected = Blynk.connected();
  if (isconnected == false) {
    wifiFlag = 1;
    digitalWrite(wifiLed, HIGH); //Vypni WiFi LED
  }
  if (isconnected == true) {
    wifiFlag = 0;
    digitalWrite(wifiLed, LOW); //Zapni WiFi LED
  }
}


void setup()
{
  // Ladiaca konzola
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);

  dht.begin();
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(buzzer, OUTPUT);

  // Nastavenie funkcie, ktorá sa má volať každú sekundu
  timer.setInterval(1000L, sendSensor);
}

{
  // Sériový port na účely ladenia
  Serial.begin(115200);

  // Prečítajte si aktuálny stav dverí
  pinMode(reedSwitch, INPUT_PULLUP);
  state = digitalRead(reedSwitch);

  // Nastavte stav LED tak, aby zodpovedal stavu dverí
  pinMode(led, OUTPUT);
  digitalWrite(led, state);

  // Nastavte kolík jazýčkového spínača ako prerušenie, priraďte funkciu prerušenia a nastavte režim CHANGE
  attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);

  // Pripojte sa k sieti Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}

{
  Serial.begin(9600);

  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT);
  pinMode(Relay3, OUTPUT);
  pinMode(Relay4, OUTPUT);

  pinMode(wifiLed, OUTPUT);

  pinMode(SwitchPin1, INPUT_PULLUP);
  pinMode(SwitchPin2, INPUT_PULLUP);
  pinMode(SwitchPin3, INPUT_PULLUP);
  pinMode(SwitchPin4, INPUT_PULLUP);

  //Počas štartu/Reset by sa všetky relé mali VYPNÚŤ
  digitalWrite(Relay1, toggleState_1);
  digitalWrite(Relay2, toggleState_2);
  digitalWrite(Relay3, toggleState_3);
  digitalWrite(Relay4, toggleState_4);

  WiFi.begin(WIFI_SSID, WIFI_PASS);
  timer.setInterval(3000L, checkBlynkStatus); // každé 3 sekundy skontrolujte, či je server Blynk pripojený
  Blynk.config(AUTH);
}


void loop()
{
  // Teraz môžeme publikovať veci!
  // Adresa 00  ( menia sa 01, 00, 10, 11 )
  digitalWrite(S0, LOW);
  digitalWrite(S1, LOW);
  gas = analogRead(analogpin);
  Serial.print("Gas - "); Serial.println(gas);

  // Adresa 10( menia sa 01, 00, 10, 11 )
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);
  motion = analogRead(analogpin);
  if (motion > 512)
  {
    motion = 1;

  }
  else
  {
    motion = 0;

  }
  // Adresa 01 ( menia sa 01, 00, 10, 11 )
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  proximity = analogRead(analogpin);
  Serial.print("Proximity - "); Serial.println(proximity);
  if (proximity < 512)
  {
    proximity = 1;
    digitalWrite(buzzer, HIGH);
  }
  else
  {
    proximity = 0;
    digitalWrite(buzzer, LOW);
  }



  // Adresa 11 ( menia sa 01, 00, 10, 11 )
  digitalWrite(S0, HIGH);
  digitalWrite(S1, HIGH);
  int raw_light = analogRead(analogpin);
  light = map(raw_light, 1024, 0, 0, 100);
  Serial.print("Light - "); Serial.println(light);

  Blynk.run();
  timer.run();
}
{
  if (changeState) {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;
      // If a state has occured, invert the current door state
      state = !state;
      if (state) {
        GarazStav = "closed";
      }
      else {
        GarazStav = "open";
      }
      digitalWrite(led, state);
      changeState = false;
      Serial.println(state);
      Serial.println(GarazStav);

      //Poslať email
      Serial.print("connecting to ");
      Serial.println(host);
      WiFiClient client;
      const int httpPort = 80;
      if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
      }

      String url = "/trigger/garaz_pohyb/with/key/";
      url += apiKey;

      Serial.print("Requesting URL: ");
      Serial.println(url);
      client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                   "Host: " + host + "\r\n" +
                   "Content-Type: application/x-www-form-urlencoded\r\n" +
                   "Content-Length: 13\r\n\r\n" +
                   "value1=" + GarazStav + "\r\n");
    }
  }
}

{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.println("WiFi Not Connected");
  }
  else
  {
    Serial.println("WiFi Connected");
    Blynk.run();
  }

  timer.run(); // Spustí SimpleTimer
  if (wifiFlag == 0)
    with_internet();
  else
    without_internet();

}



















Your code appears to be incomplete.

What are you trying to do?

NodeMcu ,Blynk
control 4 relay online + ofline(button) + 2control with magnetic switch ( when door full close/open END)
control air quality senzor(buzzer ON), Pir (alert SEND email)
read on APP : Tmp,Humidity, light, air quality, motion

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

1 Like

Format the sketch by using cntrl T and check the arrangement of the {} brackets.

There is a tutorial about combining code here:

1 Like

that's exactly how I started 10 days ago but without success, I still have a lot of mistakes
I'm not a C ++ programmer
I'm just learning to work with Arduino and I would like one functional system in the garage

Hi,
Welcome to the forum.
Can you please post your 3 codes separately please, also explain what each code does.
Rather than all in one scrolling screen?

Do all 3 codes work?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

imagine you want to add this functionality

to such a car

what do you think: will it be sufficient to use four of these?

of course not
You need more knowledge than just how to hook up tension straps
knowledge about hydraulics, welding, beeing able to use a CAD-system would be very helpful etc.

It will need estimated 30 to 120 hours of learning how to combine these functionalities.
If you think this is too much time you maybe better of with buying three ESP8266 node-MCU using each one with the ready to use code that needs just to be flashed into the microcontroller.

best regards Stefan