구피 먹이 주기 자동화 + 수족관 수위 측정

반응형

저번에 방법은 설명 했짐나 이런저런 이요 때문에 작동에 문제가 있어서 하지 못했는데 

뭐하지 뭐하지 하다가 갑자기 띠용!!!!

하고 떠올라서 바로 레스기릿!

https://dazabamuker.tistory.com/9?category=484309 

 

 

구피 자동으로 밥주기

구피 이 친구 밥주기각 겁나게 귀찮아서 자동으로 밥주는 기계 만들라고 했다. esp-32 모듈을 홈어시스턴트에 등록하고 자동화 이용해야 겠다는 아이디어가 떠올라서 구글링 했는데(2시간 정도) 3

dazabamuker.tistory.com

 

준비물은 요롬 ㅋ

[구피 먹이, 생수 뚜껑(플라스틱 뚜껑, 충전기, esp32, 물높이 측정 센서, 모터, l298n모터 드라이버)]

 

나의 단선 때문에 못쓰는 아이폰 충전기는 다시 연결 ㅋ

(니가 내 모터 드라이버에 전기를 공급해줘야겠어)

 

저번에 인두기로 뚤어둔 구피 먹이통 뚜껑에 모터 순간접착제로 고정!

 

 

 생수병도 뚤어주고

 

 

결합하고

 

 

모터 드라이버에 연결!!

 

 


!!!!!!!!!esp32에 납땜!!!!!!


여기서 주의가 필요한데 esp32는 analogRead 할때에 지원하는 핀이 여러개 이지만
와이파이 사용중일 때에는 ADCD2 핀들은 값이 안 읽힘
그래서 이왕이면 adcd1사용하자!(이거 몰라서 정말 오래걸림 )

 

 

근데 이격이 있음....

괜찮겠지........?

 

 

남은 건 폭풍 코딩!!

#include <WiFi.h>
#include <PubSubClient.h>

const char *ssid =  "olleh_WiFi_BA83";   
const char *password =  "0000002583"; 

const char* ID = "구피";  // Name of our device, must be unique
const char* TOPIC = "room/guppy/state";
const char* TOPIC1 = "room/guppy/water/state"; 
const char* INTOPIC = "room/guppy/feed";
const char* mqttUser = "dragon";
const char* mqttPassword = "qazwsxedcrfvtgbyhnujm";
const char* broker = "172.30.1.48";

WiFiClient wclient;
PubSubClient client(wclient); 
char messages[50];
int state = 0;
int feed = 4;
char val[20];

// Connect to WiFi network
void setup_wifi() {
  Serial.print("\nConnecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password); 
  while (WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Received messages: ");
  Serial.print(INTOPIC);
  for(int i=0; i<length; i++){
    Serial.println((char) payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '1') {
    //client.publish(TOPIC, "1");
    digitalWrite(feed, HIGH);
    digitalWrite(2,HIGH); //물 높이 측정 센서가 vcc연결하면 값이 너무 빨리 전달되고 수명이 줄어둘어 측정 할때만 전원 공급
    int lev = analogRead(34);
    Serial.println(lev);
    dtostrf(lev, 5, 1, val);
    snprintf(messages, 75, "%s", val);
    client.publish(TOPIC1, messages);
    delay(3000);
    digitalWrite(feed, LOW);
    digitalWrite(2,LOW);
    client.publish(TOPIC, "0");
  } 
  Serial.print(state);
  if ((char)payload[0] == '3') {
    digitalWrite(2,HIGH); //물 높이 측정 센서가 vcc연결하면 값이 너무 빨리 전달되고 수명이 줄어둘어 측정 할때만 전원 공급
    int lev = analogRead(34);
    dtostrf(lev, 5, 1, val);
    delay(1000);
    snprintf(messages, 75, "%s", val);
    client.publish(TOPIC1, messages);
    digitalWrite(2,LOW);
  }
}


// Reconnect to client
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(ID, mqttUser, mqttPassword)) {
      Serial.println("connected");
      Serial.print("Publishing to: ");
      Serial.println(TOPIC);
      Serial.println('\n');
      client.subscribe(INTOPIC);

    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println("\n try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200); 
  pinMode(feed, OUTPUT);
  pinMode(2, OUTPUT); //물 높이 측정 센서가 vcc연결하면 값이 너무 빨리 전달되고 수명이 줄어둘어 측정 할때만 전원 공급
  delay(100);
  setup_wifi(); // Connect to network
  client.setServer(broker, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()){
    reconnect();
  }
  client.loop();
}

 

 


자동화를 위한 홈어스턴트 코딩

1. configuration.yaml

switch:
#Guppy
  - platform: mqtt
    name: "구피"
    command_topic: "room/guppy/feed"
    state_topic: "room/guppy/state"
    payload_on: "1"
    payload_off: "0"
    state_on: "1"
    state_off: "0"

sensor:    
#Guppy_Water
  - platform: mqtt
    name: "구피워터레벨"
    state_topic: "room/guppy/water/state"
    unit_of_measurement: "%"
    device_class: humidity

 

2.ui-lovelace.yaml

title: Our Awsome House
views:
  - title: Home
    icon: mdi:home
    path: home
    background: center / cover no-repeat fixed url("/local/background.jpg")
    cards:
	  - type: entities
        entities:
          - sensor.gupiweoteorebel
        title: 수족관 수위
        
      - type: horizontal-stack
        cards:
          - type: 'custom:button-card' //아이폰 홈앱 디자인 
            entity: switch.gupi
            name: '구피 밥'
            icon: 'mdi:fish'
            size: 150%
            show_entity_picture: true
            state:
              - value: 'on'
                entity_picture: '/local/light-bulb-on.png'
                styles:
                    card:
                      - background: radial-gradient(circle, rgba(255,234,11,0.8) 10%, rgba(255,240,170,0.8) 60%)
            show_state: true
            aspect_ratio: '1/1'
            styles:
                card:
                  - background: rgba(255, 192, 230, 0.5)
                    border-radius: 10%
                    padding: 10%
                grid:
                  - grid-template-areas: '"i" "n" "s"'
                  - grid-template-columns: 1fr                  
                  - grid-template-rows: 1fr min-content min-content
                icon:
                  - color: pink
                name:                  
                  - justify-self: start
                  - color: white
                  - font-weight: bold
                  - text-shadow: 0px 0px 5px black                  
                  - text-transform: lowercase
                state:
                  - justify-self: start
                  - padding-left: 0px
            hold_action: 
                action: 'more-info'
                haptic: 'light'

아이폰 홈앱 디자인은

https://dazabamuker.tistory.com/24?category=492337 

 

홈어시스턴트 lovelace 커스텀 카드 추가하기

홈 어시스턴트는 기본적으로 오버 뷰에서 간단하게 ui로 설정도 가능하지만 카드를 내 마음데로 커스터 마이징도 가능하다. 근데 그게 좀 복잡? 하다 그렇지만 인터넷상에는 특히 깃허브에는 킹

dazabamuker.tistory.com

https://dazabamuker.tistory.com/26?category=492337 

 

홈어시스턴트(Home Assistant) button-card

저번에 아이콘이 큰 것은 적용이 안되서 차라리 다른 사진을 넣을 수 있지 않을까해서 찾던 도중에 이 깃허브를 찾았다. https://github.com/custom-cards/button-card#styling custom-cards/button-card ❇️ Lov..

dazabamuker.tistory.com

3. 자동화

사진으로 보여주기 힘들어서
만든 것만 알려줌
1. 수위 측정 30분 마다 하도록 만들기(
service:mqtt.publish
topic: 'room/guppy/feed'
payload: '3' //3을 주면 수위측정 하도록!-아두이노 코드 나와있음
2. 수위가 얼마 이하면 알림 오도록
3. 아침 8시 마다 구피 자동으로 밥주도록!!!

자동화로 구피 밥주는게 핵심!!!

 

!!! 어항에 설치 !!!


!! 짠 !!


오늘 구피 밥만 세번 먹음 ㅋㅋㅋㅋㅋ
자동화 설정!
단위가 이상하지만 뭐 값이 측정할 때 마다 달라서...

ㅎㅎ 내일 부터 밥 안준다 ㅎㅎ
반응형