Difference between revisions of "Parkovací Senzor"

From Digitálne technológie výroby
Jump to: navigation, search
Line 1: Line 1:
 
'''Upozornenie!!'''
 
'''Upozornenie!!'''
Tento projekt ešte nieje hotový, očakávajte zmeny.
+
Tento projekt je v BETA fáze vývoja, očakávajte zmeny.
  
  
Line 16: Line 16:
 
*VS 1838B a IR ovládač
 
*VS 1838B a IR ovládač
 
*HW-456-SR505
 
*HW-456-SR505
 +
*ESP-8266 s ESP-01 adaptérom
 
*dátový RGB LED pás
 
*dátový RGB LED pás
 
*káble
 
*káble
Line 67: Line 68:
 
*<s>Namodelovanie krabičky pre celý obvod</s>
 
*<s>Namodelovanie krabičky pre celý obvod</s>
 
*<s>Schéma zapojenia</s>
 
*<s>Schéma zapojenia</s>
*Navrhnutie web servera pomocou ESP-8266 a ESP-01, kde budú vypísané informácie o počte zaparkovaní, teplota a vlhkosť a pod.
+
*<s>Navrhnutie web servera pomocou ESP-8266 a ESP-01, kde budú vypísané informácie o počte zaparkovaní, teplota a vlhkosť a pod.</s>
 
*(Prípadne)Implementácia časovača
 
*(Prípadne)Implementácia časovača
 
*(Prípadne)Implementácia BCD displeja alebo 2-line Liquid Crystal displeja na zobrazenie teploty a vlhkosti pre rýchly prístup
 
*(Prípadne)Implementácia BCD displeja alebo 2-line Liquid Crystal displeja na zobrazenie teploty a vlhkosti pre rýchly prístup
Line 81: Line 82:
 
'''Upozornenie!!'''  
 
'''Upozornenie!!'''  
  
Tento kód je v ALPHA fáze vývoja. Očakávajte zmeny.
+
Tento kód je v BETA fáze vývoja. Očakávajte zmeny.
  
 
<syntaxhighlight lang="C++">
 
<syntaxhighlight lang="C++">
Line 91: Line 92:
 
#include <Wire.h>
 
#include <Wire.h>
 
#include "Adafruit_SHT31.h"
 
#include "Adafruit_SHT31.h"
 +
#include <SoftwareSerial.h>
 +
SoftwareSerial ss(2, 4);
 
QuickStats stats;
 
QuickStats stats;
 +
 +
/*
 +
  Database layout:
 +
  mysql> describe park_sensor;
 +
  +-----------+----------+------+-----+-------------------+----------------+
 +
  | Field    | Type    | Null | Key | Default          | Extra          |
 +
  +-----------+----------+------+-----+-------------------+----------------+
 +
  | id        | int(11)  | NO  | PRI | NULL              | auto_increment |
 +
  | date_time | datetime | NO  |    | CURRENT_TIMESTAMP |                |
 +
  | temp      | float    | NO  |    | NULL              |                |
 +
  | hum      | float    | NO  |    | NULL              |                |
 +
  +-----------+----------+------+-----+-------------------+----------------+
 +
  4 rows in set (0.00 sec)
 +
 +
*/
  
 
#define LED_PIN    7 //D pin LED pas
 
#define LED_PIN    7 //D pin LED pas
 
#define N_LEDS    15 //kolko bude svietit
 
#define N_LEDS    15 //kolko bude svietit
 
#define M_LED  3 //D pin motion senzor
 
#define M_LED  3 //D pin motion senzor
 +
 +
//commandline
 +
char cmd[20];
  
 
//ir ovladanie
 
//ir ovladanie
int ir_input_pin = 2; // D2
+
int ir_input_pin = 8; // D8
 
IRrecv irrecv(ir_input_pin);
 
IRrecv irrecv(ir_input_pin);
 
decode_results results;
 
decode_results results;
Line 108: Line 129:
 
//parkovacka
 
//parkovacka
 
int p_status = 0;
 
int p_status = 0;
 +
int n_status = 1;
  
 
const int trigPin = 10;
 
const int trigPin = 10;
 
const int echoPin = 9;
 
const int echoPin = 9;
const int start_distance = 88; //maximum(CM)
+
const int start_distance = 50; //maximum(CM)
const int stop_distance = 10; //minimum(CM)
+
const int stop_distance = 5; //minimum(CM)
  
 
CRGB leds[N_LEDS];
 
CRGB leds[N_LEDS];
Line 123: Line 145:
 
/////////////////////////////////////////////////////
 
/////////////////////////////////////////////////////
  
void led_check() { //test ci svietia vsetky ledky
+
void led_check() { //test svietenia lediek
 
   for (int i = 0; i <= 14; i++) {
 
   for (int i = 0; i <= 14; i++) {
 
     leds[i] = CRGB (255, 0, 0); // 15 lediek RED
 
     leds[i] = CRGB (255, 0, 0); // 15 lediek RED
Line 148: Line 170:
 
}
 
}
  
void parking_sensor() {
+
void parking_sensor() { // hlavny program na ukazku vzdialenosti od parkovacieho systemu
 
   for (int i = 0; i <= 14; i++) {
 
   for (int i = 0; i <= 14; i++) {
 
     digitalWrite(trigPin, LOW);
 
     digitalWrite(trigPin, LOW);
Line 161: Line 183:
 
   //Kalkulacia vzdialenosti
 
   //Kalkulacia vzdialenosti
 
   distance = duration * 0.034 / 2;
 
   distance = duration * 0.034 / 2;
 +
 
 
   Serial.println(distance);
 
   Serial.println(distance);
  
   if (distance < stop_distance) { // ak DISTANCE je mensia ako STOP
+
   if (distance < stop_distance) { // Zastavenie
 
     for (int i = 0; i <= 14; i++) {
 
     for (int i = 0; i <= 14; i++) {
 
       leds[i] = CRGB (255, 0, 0); // 15 lediek RED
 
       leds[i] = CRGB (255, 0, 0); // 15 lediek RED
Line 170: Line 193:
 
   }
 
   }
  
   else if (distance < stop_distance + state) { //ak DISTANCE je vacsia ako STOP + STATE
+
   else if (distance < stop_distance + state) {
 
     for (int i = 1; i <= 14; i++) { //14 lediek nesvieti
 
     for (int i = 1; i <= 14; i++) { //14 lediek nesvieti
 
       leds[i] = CRGB (0, 0, 0);
 
       leds[i] = CRGB (0, 0, 0);
Line 181: Line 204:
 
   }
 
   }
  
   else if (distance < stop_distance + state * 2) { //ak DISTANCE je vacsia ako STOP + STATE * 2
+
   else if (distance < stop_distance + state * 2) {
 
     for (int i = 2; i <= 14; i++) { //13 lediek nesvieti
 
     for (int i = 2; i <= 14; i++) { //13 lediek nesvieti
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
Line 191: Line 214:
 
     delay(50);
 
     delay(50);
 
   }
 
   }
   else if (distance < stop_distance + state * 3) { //ak DISTANCE je vacsia ako STOP + STATE * 3
+
   else if (distance < stop_distance + state * 3) {
 
     for (int i = 3; i <= 14; i++) { //12 lediek nesvieti
 
     for (int i = 3; i <= 14; i++) { //12 lediek nesvieti
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
Line 206: Line 229:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 3; i++) { //4 ledky GREEN
+
     for (int i = 0; i <= 3; i++) { //4 ledky YELLOW
       leds[i] = CRGB ( 0, 255, 0);
+
       leds[i] = CRGB ( 255, 255, 0);
 
     }
 
     }
 
     FastLED.show();
 
     FastLED.show();
Line 214: Line 237:
  
 
   else if (distance < stop_distance + state * 5) {
 
   else if (distance < stop_distance + state * 5) {
     for (int i = 5; i <= 14; i++) {
+
     for (int i = 5; i <= 14; i++) { //10 lediek nesvieti
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 4; i++) { //5 lediek GREEN
+
     for (int i = 0; i <= 4; i++) { //5 lediek YELLOW
       leds[i] = CRGB ( 0, 255, 0);
+
       leds[i] = CRGB ( 255, 255, 0);
 
     }
 
     }
 
     FastLED.show();
 
     FastLED.show();
Line 257: Line 280:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 8; i++) {
+
     for (int i = 0; i <= 8; i++) { //9 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 268: Line 291:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 9; i++) {
+
     for (int i = 0; i <= 9; i++) { //10 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 279: Line 302:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 10; i++) {
+
     for (int i = 0; i <= 10; i++) { //11 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 290: Line 313:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 11; i++) {
+
     for (int i = 0; i <= 11; i++) { //12 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 301: Line 324:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 12; i++) {
+
     for (int i = 0; i <= 12; i++) { //13 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 312: Line 335:
 
       leds[i] = CRGB ( 0, 0, 0);
 
       leds[i] = CRGB ( 0, 0, 0);
 
     }
 
     }
     for (int i = 0; i <= 13; i++) {
+
     for (int i = 0; i <= 13; i++) { //14 lediek GREEN
 
       leds[i] = CRGB ( 0, 255, 0);
 
       leds[i] = CRGB ( 0, 255, 0);
 
     }
 
     }
Line 327: Line 350:
 
   }
 
   }
  
   if(digitalRead(M_LED) == LOW) { //ak Motion senzor nezisti pohyb
+
   if (digitalRead(M_LED) == LOW) { //ak Motion senzor nezisti pohyb
 
     p_status = 0;
 
     p_status = 0;
 
     for (int i = 0; i <= 14; i++) {
 
     for (int i = 0; i <= 14; i++) {
Line 336: Line 359:
 
}
 
}
  
int temp_hum() {
+
int temp_hum() { //meranie teploty a vlhkosti
   float temp = sht31.readTemperature();
+
   int temp = sht31.readTemperature();
   float hum = sht31.readHumidity();
+
   int hum = sht31.readHumidity();
  
 
   if (! isnan(temp)) {
 
   if (! isnan(temp)) {
Line 355: Line 378:
 
     Serial.println("ERROR: Nepodarilo sa zistit vlhkost vzduchu");
 
     Serial.println("ERROR: Nepodarilo sa zistit vlhkost vzduchu");
 
   }
 
   }
 +
  add_record(temp, hum);
 
   return;
 
   return;
 
}
 
}
  
void setup() {
+
void send_packet(char *packet) //posielanie packetu na server
   Serial.begin(9600);
+
{
 +
  int len = strlen(packet);
 +
  ss.print("AT+CIPSEND=");
 +
  ss.print(len);
 +
  ss.write(13);
 +
  ss.write(10);
 +
  while (1) {
 +
    if (ss.available()) {
 +
      if (ss.read() == '>') break;
 +
    }
 +
  }
 +
  ss.print(packet);
 +
}
 +
 
 +
void connect_to_wifi() //pripojenie na wifi
 +
{
 +
  ss.print("AT+CWJAP_DEF=\"SSID\",\"heslo\"");  // Nahradit SSID a heslo WiFi na ktore sa bude pripajat
 +
  ss.write(13);
 +
  ss.write(10);
 +
}
 +
 
 +
void connect_to_server() //pripojenie na server
 +
{
 +
  ss.print("AT+CIPSTART=\"protokol\",\"IP adresa\",port"); // Nahradit protokol, IP adresu a port servera na ktory sa bude pripajat
 +
  ss.write(13);
 +
  ss.write(10);
 +
}
 +
 
 +
char *print_int_to_str(char *str, long a) //zmena typu dat int na str
 +
{
 +
  long q = 1;
 +
  while (q <= a) q *= 10;
 +
  while (q > 1)
 +
  {
 +
    q /= 10;
 +
    *str = '0' + a / q;
 +
    a %= q;
 +
    str++;
 +
  }
 +
  return str;
 +
}
 +
 
 +
void add_record(int temperature, int humidity) // vyrobenie packetu na odoslanie
 +
{
 +
  char packet[30];
 +
  packet[0] = 'd';
 +
  packet[1] = ' ';
 +
  char *next_pos = print_int_to_str(packet + 2, temperature);
 +
  *next_pos = ' ';
 +
  next_pos++;
 +
  next_pos = print_int_to_str(next_pos, humidity);
 +
  *next_pos = 0;
 +
  send_packet(packet);
 +
  Serial.print("Added record using packet: ");
 +
  Serial.println(packet);
 +
}
 +
 
 +
void process_special_command() //spracovanie prikazov
 +
{
 +
  if (cmd[0] == 'w')  connect_to_wifi();
 +
  else if (cmd[0] == 'c')  connect_to_server();
 +
  else if (cmd[0] == 's')  send_packet(cmd + 2); //"s packet_data[EOL]"
 +
  //else if (cmd[0] == 'd')  add_record();
 +
  else Serial.println("# ???");
 +
}
 +
 
 +
void special_command() //tvar prikazu
 +
{
 +
  int cmd_index = 0;
 +
  char c = '#';
 +
  while (1) {
 +
    if (Serial.available())
 +
    {
 +
      c = Serial.read();
 +
      if (c != 10) cmd[cmd_index++] = c;
 +
      else break;
 +
    }
 +
  }
 +
  cmd[cmd_index] = 0;
 +
  Serial.print("cmd: '");
 +
  Serial.print(cmd);
 +
  Serial.println("'");
 +
  process_special_command();
 +
}
 +
 
 +
void setup() { //inicializacia celeho programu
 +
  Serial.begin(38400);
 +
 
 +
  ss.begin(38400);
 +
   Serial.begin(38400);
 +
  Serial.println("Garaz DB Console:");
  
 
   //setup parkovacka
 
   //setup parkovacka
Line 379: Line 493:
 
}
 
}
  
void loop() {
+
void loop() { //vykonavanie programu
 +
  if (n_status == 1){
 +
    connect_to_wifi();
 +
    delay(50);
 +
    connect_to_server();
 +
    n_status = 0;
 +
  }
 +
 
 
   if (p_status == 1) {
 
   if (p_status == 1) {
 
     parking_sensor();
 
     parking_sensor();
 
   }
 
   }
  
   if (irrecv.decode(&results)) { //decoduje hodnotu na hex
+
   if (irrecv.decode(&results)) { //dekoduje hodnotu na hex
 
     //Serial.println(results.value);
 
     //Serial.println(results.value);
 
     switch (results.value) {
 
     switch (results.value) {
 
       case 0xFF906F: //"EQ"
 
       case 0xFF906F: //"EQ"
 +
        //Serial.println("Done");
 
         temp_hum();
 
         temp_hum();
 
     }
 
     }
Line 393: Line 515:
 
       switch (results.value) {
 
       switch (results.value) {
 
         case 0xFFC23D: //">||"
 
         case 0xFFC23D: //">||"
 +
          //Serial.println("Done");
 
           p_status ++;
 
           p_status ++;
 
       }
 
       }
Line 398: Line 521:
 
     switch (results.value) {
 
     switch (results.value) {
 
       case 0xFF6897: //"0"
 
       case 0xFF6897: //"0"
 +
        //Serial.println("Done");
 
         led_check();
 
         led_check();
 +
    }
 +
    if (n_status == 0) {
 +
      switch (results.value) {
 +
        case 0x------: //"1"
 +
          //Serial.println("Done");
 +
          connect_to_wifi();
 +
          delay(50);
 +
          connect_to_server();
 +
      }
 
     }
 
     }
 
     irrecv.resume();
 
     irrecv.resume();
 +
  }
 +
  if (Serial.available())
 +
  {
 +
    char c = Serial.read();
 +
    if (c == '#') special_command();
 +
    else
 +
    {
 +
      ss.write(c);
 +
      Serial.write(c);
 +
    }
 +
  }
 +
  if (ss.available())
 +
  {
 +
    char c = ss.read();
 +
    Serial.write(c);
 
   }
 
   }
 
}
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 09:13, 19 August 2020

Upozornenie!! Tento projekt je v BETA fáze vývoja, očakávajte zmeny.


Parkovacie senzory slúžia na uľahčenie parkovania najmä na miestach, kde je ledva dostatok miesta na zaparkovanie. V novších modeloch áut už nájdeme zabudované parkovacie senzory, ale ľudia, ktorí nevlastnia také autá môžu mať niekedy problém zaparkovať do malej garáže.

Tento parkovací senzor nielen že vám pomôže zaparkovať, ale vás aj informuje aká je teplota a vlhkosť v garáži.

Použité súčiastky

Na to, aby sme vytvorili tento parkovací senzor, potrebujeme následovné:

  • Arduino NANO / UNO
  • HC-SR04
  • 1K Rezistor
  • SHT 31D
  • VS 1838B a IR ovládač
  • HW-456-SR505
  • ESP-8266 s ESP-01 adaptérom
  • dátový RGB LED pás
  • káble


Zapojenie

  • Zapojenie 01
  • Zapojenie 02
  • Schema:

  • Schema zapojenia
  • Krabička

    Krabičku k obvodu som navrhol v OpenSCAD programe.

  • Koncept krabičky (predná časť)
  • Koncept krabičky (zadná časť)
  • Koncept lišty
  • Na prednej stene krabičky sú otvory pre ultrazvukový senzor a IR senzor, na ľavej stene sú otvory pre Arduino UNO vstup a na vrchu je výrez pre vedenie kabeláže z krabičky.

    Výsledky

    Po zapojení Arduino dosky do USB siete, sa vykoná inicializácia a vizuálny test zasvietením 15 lediek na červeno, následne zeleno a potom na modro. Po skončení inicializácie je možne IR ovládačom púštať funkcie programu.

    Po vytlačení konceptu vyzerá krabička takto:

  • Hotová krabička (predná časť)
  • Hotová krabička (zadná časť)
  • Hotová lišta
  • Roadmap

    • Namodelovanie krabičky pre celý obvod
    • Schéma zapojenia
    • Navrhnutie web servera pomocou ESP-8266 a ESP-01, kde budú vypísané informácie o počte zaparkovaní, teplota a vlhkosť a pod.
    • (Prípadne)Implementácia časovača
    • (Prípadne)Implementácia BCD displeja alebo 2-line Liquid Crystal displeja na zobrazenie teploty a vlhkosti pre rýchly prístup
    • (Prípadne)iné....podľa aktuálneho nápadu

    Z nápadu do reality

    (SOON...)


    Zdrojový kód

    Upozornenie!!

    Tento kód je v BETA fáze vývoja. Očakávajte zmeny.

    #include <Arduino.h>
    #include <FastLED.h>
    #include <QuickStats.h>
    #include <IRremote.h>
    #include <Wire.h>
    #include "Adafruit_SHT31.h"
    #include <SoftwareSerial.h>
    SoftwareSerial ss(2, 4);
    QuickStats stats;
    
    /*
      Database layout:
      mysql> describe park_sensor;
      +-----------+----------+------+-----+-------------------+----------------+
      | Field     | Type     | Null | Key | Default           | Extra          |
      +-----------+----------+------+-----+-------------------+----------------+
      | id        | int(11)  | NO   | PRI | NULL              | auto_increment |
      | date_time | datetime | NO   |     | CURRENT_TIMESTAMP |                |
      | temp      | float    | NO   |     | NULL              |                |
      | hum       | float    | NO   |     | NULL              |                |
      +-----------+----------+------+-----+-------------------+----------------+
      4 rows in set (0.00 sec)
    
    */
    
    #define LED_PIN     7 //D pin LED pas
    #define N_LEDS    15 //kolko bude svietit
    #define M_LED  3 //D pin motion senzor
    
    //commandline
    char cmd[20];
    
    //ir ovladanie
    int ir_input_pin = 8; // D8
    IRrecv irrecv(ir_input_pin);
    decode_results results;
    
    //teplota vlhkost
    Adafruit_SHT31 sht31 = Adafruit_SHT31(); //SCL v A5, SDA v A4
    
    
    //parkovacka
    int p_status = 0;
    int n_status = 1;
    
    const int trigPin = 10;
    const int echoPin = 9;
    const int start_distance = 50; //maximum(CM)
    const int stop_distance = 5; //minimum(CM)
    
    CRGB leds[N_LEDS];
    
    int distance = 0;
    int state = (start_distance - stop_distance) / 15; //vypocet stadia polohy, urcene na moznu zmenu start a stop vzdialenosti
    
    float duration = 0;
    float durationarray[15];
    /////////////////////////////////////////////////////
    
    void led_check() { //test svietenia lediek
      for (int i = 0; i <= 14; i++) {
        leds[i] = CRGB (255, 0, 0); // 15 lediek RED
        FastLED.show();
      }
      delay(1000);
    
      for (int i = 0; i <= 14; i++) {
        leds[i] = CRGB (0, 255, 0); // 15 lediek GREEN
        FastLED.show();
      }
      delay(1000);
    
      for (int i = 0; i <= 14; i++) {
        leds[i] = CRGB (0, 0, 255); // 15 lediek BLUE
        FastLED.show();
      }
      delay(1000);
    
      for (int i = 0; i <= 14; i++) {
        leds[i] = CRGB (0, 0, 0); // 15 lediek nesvieti
        FastLED.show();
      }
    }
    
    void parking_sensor() { // hlavny program na ukazku vzdialenosti od parkovacieho systemu
      for (int i = 0; i <= 14; i++) {
        digitalWrite(trigPin, LOW);
        delay(2);
        digitalWrite(trigPin, HIGH);
        delay(10);
        digitalWrite(trigPin, LOW);
        durationarray[i] = pulseIn(echoPin, HIGH);
      }
    
      duration = (stats.median(durationarray, 15));
      //Kalkulacia vzdialenosti
      distance = duration * 0.034 / 2;
      
      Serial.println(distance);
    
      if (distance < stop_distance) { // Zastavenie
        for (int i = 0; i <= 14; i++) {
          leds[i] = CRGB (255, 0, 0); // 15 lediek RED
          FastLED.show();
        }
      }
    
      else if (distance < stop_distance + state) {
        for (int i = 1; i <= 14; i++) { //14 lediek nesvieti
          leds[i] = CRGB (0, 0, 0);
        }
        for (int i = 0; i <= 0; i++) { //1 ledka YELLOW
          leds[i] = CRGB (255, 255, 0);
        }
        FastLED.show();
        delay(50); // delay je na to aby pri prechode rapidne nepreblikaval
      }
    
      else if (distance < stop_distance + state * 2) {
        for (int i = 2; i <= 14; i++) { //13 lediek nesvieti
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 1; i++) { //2 ledky YELLOW
          leds[i] = CRGB ( 255, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
      else if (distance < stop_distance + state * 3) {
        for (int i = 3; i <= 14; i++) { //12 lediek nesvieti
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 2; i++) { //3 ledky YELLOW
          leds[i] = CRGB ( 255, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < (stop_distance + state) * 4) {
        for (int i = 4; i <= 14; i++) { //11 lediek nesvieti
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 3; i++) { //4 ledky YELLOW
          leds[i] = CRGB ( 255, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 5) {
        for (int i = 5; i <= 14; i++) { //10 lediek nesvieti
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 4; i++) { //5 lediek YELLOW
          leds[i] = CRGB ( 255, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
      else if (distance < stop_distance + state * 6) {
        for (int i = 6; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 5; i++) { //6 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
      else if (distance < stop_distance + state * 7) {
        for (int i = 7; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 6; i++) { //7 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
      else if (distance < stop_distance + state * 8) {
        for (int i = 8; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 7; i++) { //8 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
      else if (distance < stop_distance + state * 9) {
        for (int i = 9; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 8; i++) { //9 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 10) {
        for (int i = 10; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 9; i++) { //10 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 11) {
        for (int i = 11; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 10; i++) { //11 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 12) {
        for (int i = 12; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 11; i++) { //12 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 13) {
        for (int i = 13; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 12; i++) { //13 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance < stop_distance + state * 14) {
        for (int i = 14; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        for (int i = 0; i <= 13; i++) { //14 lediek GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      else if (distance >= stop_distance + state * 14) {
        for (int i = 0; i <= 14; i++) { //vsetky ledky GREEN
          leds[i] = CRGB ( 0, 255, 0);
        }
        FastLED.show();
        delay(50);
      }
    
      if (digitalRead(M_LED) == LOW) { //ak Motion senzor nezisti pohyb
        p_status = 0;
        for (int i = 0; i <= 14; i++) {
          leds[i] = CRGB ( 0, 0, 0);
        }
        FastLED.show();
      }
    }
    
    int temp_hum() { //meranie teploty a vlhkosti
      int temp = sht31.readTemperature();
      int hum = sht31.readHumidity();
    
      if (! isnan(temp)) {
        Serial.print("Teplota: ");
        Serial.print(temp);
        Serial.println("°C");
      } else {
        Serial.println("ERROR: Nepodarilo sa zistit teplotu");
      }
    
      if (! isnan(hum)) {
        Serial.print("Vlhkost vzduchu: ");
        Serial.print(hum);
        Serial.println("%");
      } else {
        Serial.println("ERROR: Nepodarilo sa zistit vlhkost vzduchu");
      }
      add_record(temp, hum);
      return;
    }
    
    void send_packet(char *packet) //posielanie packetu na server
    {
      int len = strlen(packet);
      ss.print("AT+CIPSEND=");
      ss.print(len);
      ss.write(13);
      ss.write(10);
      while (1) {
        if (ss.available()) {
          if (ss.read() == '>') break;
        }
      }
      ss.print(packet);
    }
    
    void connect_to_wifi() //pripojenie na wifi
    {
      ss.print("AT+CWJAP_DEF=\"SSID\",\"heslo\"");  // Nahradit SSID a heslo WiFi na ktore sa bude pripajat
      ss.write(13);
      ss.write(10);
    }
    
    void connect_to_server() //pripojenie na server
    {
      ss.print("AT+CIPSTART=\"protokol\",\"IP adresa\",port"); // Nahradit protokol, IP adresu a port servera na ktory sa bude pripajat
      ss.write(13);
      ss.write(10);
    }
    
    char *print_int_to_str(char *str, long a) //zmena typu dat int na str
    {
      long q = 1;
      while (q <= a) q *= 10;
      while (q > 1)
      {
        q /= 10;
        *str = '0' + a / q;
        a %= q;
        str++;
      }
      return str;
    }
    
    void add_record(int temperature, int humidity) // vyrobenie packetu na odoslanie
    {
      char packet[30];
      packet[0] = 'd';
      packet[1] = ' ';
      char *next_pos = print_int_to_str(packet + 2, temperature);
      *next_pos = ' ';
      next_pos++;
      next_pos = print_int_to_str(next_pos, humidity);
      *next_pos = 0;
      send_packet(packet);
      Serial.print("Added record using packet: ");
      Serial.println(packet);
    }
    
    void process_special_command() //spracovanie prikazov
    {
      if (cmd[0] == 'w')  connect_to_wifi();
      else if (cmd[0] == 'c')  connect_to_server();
      else if (cmd[0] == 's')  send_packet(cmd + 2); //"s packet_data[EOL]"
      //else if (cmd[0] == 'd')  add_record();
      else Serial.println("# ???");
    }
    
    void special_command() //tvar prikazu
    {
      int cmd_index = 0;
      char c = '#';
      while (1) {
        if (Serial.available())
        {
          c = Serial.read();
          if (c != 10) cmd[cmd_index++] = c;
          else break;
        }
      }
      cmd[cmd_index] = 0;
      Serial.print("cmd: '");
      Serial.print(cmd);
      Serial.println("'");
      process_special_command();
    }
    
    void setup() { //inicializacia celeho programu
      Serial.begin(38400);
    
      ss.begin(38400);
      Serial.begin(38400);
      Serial.println("Garaz DB Console:");
    
      //setup parkovacka
      pinMode(M_LED, INPUT);
      pinMode(trigPin, OUTPUT);
      pinMode(echoPin, INPUT);
      FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, N_LEDS);
    
      //setup ovladac
      irrecv.enableIRIn();
    
      //setup teplota/vlhkost
      while (!Serial)
        delay(100);
      if (! sht31.begin(0x44)) {
        Serial.println("ERROR: Nenasiel sa SHT31 chip");
      }
      led_check();
    }
    
    void loop() { //vykonavanie programu
      if (n_status == 1){
        connect_to_wifi();
        delay(50);
        connect_to_server();
        n_status = 0;
      }
      
      if (p_status == 1) {
        parking_sensor();
      }
    
      if (irrecv.decode(&results)) { //dekoduje hodnotu na hex
        //Serial.println(results.value);
        switch (results.value) {
          case 0xFF906F: //"EQ"
            //Serial.println("Done");
            temp_hum();
        }
        if (p_status == 0) {
          switch (results.value) {
            case 0xFFC23D: //">||"
              //Serial.println("Done");
              p_status ++;
          }
        }
        switch (results.value) {
          case 0xFF6897: //"0"
            //Serial.println("Done");
            led_check();
        }
        if (n_status == 0) {
          switch (results.value) {
            case 0x------: //"1"
              //Serial.println("Done");
              connect_to_wifi();
              delay(50);
              connect_to_server();
          }
        }
        irrecv.resume();
      }
      if (Serial.available())
      {
        char c = Serial.read();
        if (c == '#') special_command();
        else
        {
          ss.write(c);
          Serial.write(c);
        }
      }
      if (ss.available())
      {
        char c = ss.read();
        Serial.write(c);
      }
    }