Difference between revisions of "Parkovací Senzor"

From Digitálne technológie výroby
Jump to: navigation, search
(Schéma)
m (Zdrojový kód)
 
(2 intermediate revisions by 2 users not shown)
Line 58: Line 58:
 
Táto tabuľka vypisuje ID riadku, kedy bolo zaparkované a aká teplota a vlhkosť bola v ten čas v garáži.  
 
Táto tabuľka vypisuje ID riadku, kedy bolo zaparkované a aká teplota a vlhkosť bola v ten čas v garáži.  
  
Video finálnej verzie [https://www.youtube.com/watch?v=sb5O7WCM1Ho&t tu].
+
Video finálnej verzie:
 +
 
 +
<youtube>sb5O7WCM1Ho</youtube>
  
 
== Zdrojový kód ==
 
== Zdrojový kód ==
 +
[[Media:web_server.zip|Kód webovej stránky a ESP servera]]
 
<syntaxhighlight lang="C++">
 
<syntaxhighlight lang="C++">
  

Latest revision as of 17:14, 30 August 2020

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, ktoré si možete pozrieť online.


Použité súčiastky

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

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


Schéma

  • Schema zapojeniav ver.:1.0
  • 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 vytlačení konceptu vyzerá krabička takto:

  • Hotová krabička (predná časť)
  • Hotová krabička (zadná časť)
  • Hotová lišta
  • 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.

    Na stránke Parkovací Senzor sa nachádza databázna tabuľka. Táto tabuľka vypisuje ID riadku, kedy bolo zaparkované a aká teplota a vlhkosť bola v ten čas v garáži.

    Video finálnej verzie:

    Zdrojový kód

    Kód webovej stránky a ESP servera

    #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 = 300; //maximum(CM)
    const int stop_distance = 80; //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 GREEN
          leds[i] = CRGB ( 0, 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 GREEN
          leds[i] = CRGB ( 0, 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);
      }
    
    }
    
    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\",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 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(10000);
        connect_to_server();
        delay(2000);
        Serial.println("Attempt Done");
        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");
              temp_hum();
              p_status = 1;
          }
        }
         if (p_status == 1) {
          switch (results.value) {
            case 0xFF18E7: //"2"
              //Serial.println("Done");
              p_status = 0;
              for (int i = 0; i <= 14; i++) {
                  leds[i] = CRGB ( 0, 0, 0);
              }
              FastLED.show();
              p_status = 0;
          }
        }
        switch (results.value) {
          case 0xFF6897: //"0"
            //Serial.println("Done");
            led_check();
        }
        if (n_status == 0) {
          switch (results.value) {
            case 0xFF30CF: //"1"
              //Serial.println("Done");
              connect_to_wifi();
              delay(10000);
              connect_to_server();
              delay(500);
          }
        }
        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);
      }
    }