5/29/2019

SonOff Basic : ติดตั้ง Tasmota Config HA ตั้งเวลา ขับสัญญาณกริ่ง

เดิมใช้ SonOff เขียน Code นับเวลาเอา ใช้ Arduino Flash
https://intranet.sci.com/blog.php?u=281&b=1443

พบปัญหา ใช้ไปนาน ๆ นับ วิ ไม่ตรง ทำให้กริ่ง ดังทีหลัง ไม่ตรงเวลา

จึงทำ SonOff ใหม่
- ใช้ Rom Tasmota
- ใช้ Home Assistant ทำ Automation สั่งงานตามเวลาที่ต้องการ

1. Download Tasmota มาแตกไฟล์ เตรียม Flash
https://github.com/arendst/Sonoff-Tasmota/releases

เลือก Source code ได้ไฟล์ แตก

2. Ubuntu ติดตั้ง Atom
https://intranet.sci.com/blog.php?u=281&b=1598

3. นำ Folder ที่ได้จากข้อ 1. Import เข้า Atom แล้วทำการแก้ไข
แก้ 2 ส่วนคือ
3.1. เข้าไปที่ sonoff --> my_user_config.h
#define WIFI_IP_ADDRESS "192.168.0.159" // [IpAddress1] Set to 0.0.0.0 for using DHCP or enter a static IP address
#define WIFI_GATEWAY "192.168.0.2" // [IpAddress2] If not using DHCP set Gateway IP address
#define WIFI_SUBNETMASK "255.255.255.0" // [IpAddress3] If not using DHCP set Network mask
#define WIFI_DNS "192.168.0.253" // [IpAddress4] If not using DHCP set DNS IP address (might be equal to WIFI_GATEWAY)

#define STA_SSID1 "SCI_EMP" // [Ssid1] Wifi SSID
#define STA_PASS1 "143265c911" // [Password1] Wifi password

#define MQTT_HOST "192.168.2.202" // [MqttHost]
#define NTP_SERVER1 "192.168.0.2" // [NtpServer1] Select first NTP server by name or IP address (129.250.35.250)

3.2. แก้ platformio.ini
เอา ; ออก จะเขียนใส่อุปกรณ์ไหนให้เลือกให้ตรง
env_default = sonoff-basic


ใส่ port ที่จะ Upload เดิมเป็น COM5
upload_port = /dev/ttyUSB0


4. ต่อสาย เพื่อ Flash ใช้ไฟ 3V และ TX RX สลับกัน

Flash ไม่ผ่าน ไม่มีสิทธิ์ cannot access tty/usb0 ให้ใช้คำสั่ง
  1. sudo usermod -a -G dialout $USER

แล้ว Log Off หรือ reboot เครื่องก่อนใช้งาน

Error 255 สายไม่ดีหรือต่อสาย TX RX ไม่ถูก ทำให้ Flash ไม่ผ่าน

5. ต่อเข้า USB ก่อน ต่อ USB กดปุ่มขายสายไฟ 3V ด้วย
6. โปรแกรม Atom เลือก Menu Platform IO เลือก Build และ Upload ตามลำดับ
7. ทดลองเข้า IP SonOff ที่กำหนดไว้ หรือ DHCP หาดูใน Firewall ถ้าเข้าได้ลองสั่งเปิดปิดได้หรือไม่ไฟจะติดจะดับ
8. ตั้งชื่อ Topic ของ sonoff ไม่ให้ซ้ำกับตัวอื่น
เข้าไปที่ Configure MQTT และใส่ User และ Password Mqtt ให้สอดคล้องกับ Server
ไฟล์ Backup Config ใช้งานจริง (เอาไป Restore )

9. ที่เครื่อง HA
- ตั้ง Automation
- และจัด Group

9.1. สร้าง Switch สำหรับลองปิดเปิด Sonoff ที่สร้างขั้น
  1. nano configuration.yaml
switch:
- platform: mqtt
name: "switch_sonoff_bell"
state_topic: "stat/sonoff2/POWER"
command_topic: "cmnd/sonoff2/POWER"

9.2. จัด Group ใช้เครื่อง HA กับสวน ฝพอ. แยกกันเป็นของใครของมันจะได้ไม่กดผิด
  1. nano groups.yaml
GardenManual:
name: GardenManual
#icon: mdi:account-multiple
#view: yes
entities:
- switch.Moisture_Mode
- switch.switch_sonOff_shrine
- switch.Timer_Mode
GardenScript:
name: GardenScript
#icon: mdi:account-multiple
#view: yes
entities:
- script.CloseAll
- script.Zone1
- script.Zone2
- script.Zone3
- script.Zone4
- script.sonoffshrine
BellManual:
name: BellManual
#icon: mdi:account-multiple
#view: yes
entities:
- switch.switch_sonoff_bell


9.3. สร้าง Automation ให้ทำงานเมื่อถึงเวลาที่กำหนด
  1. nano automations.yaml

- id: '1555639966304'
alias: Bell ON At 0:00
trigger:
- at: 0:00
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642007968'
alias: Bell ON At 0:30
trigger:
- at: 0:30
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642250951'
alias: Bell ON At 5:30
trigger:
- at: '5:30'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642348629'
alias: Bell ON At 6:00
trigger:
- at: '6:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642435310'
alias: Bell ON At 7:50
trigger:
- at: '7:50'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642541007'
alias: Bell ON At 8:00
trigger:
- at: '8:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642694769'
alias: Bell ON At 10:20
trigger:
- at: '10:20'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642822938'
alias: Bell ON At 12:00
trigger:
- at: '12:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555642916659'
alias: Bell ON At 12:50
trigger:
- at: '12:50'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555643327026'
alias: Bell ON At 13:00
trigger:
- at: '13:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555643430144'
alias: Bell ON At 17:40
trigger:
- at: '17:40'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555643548082'
alias: Bell ON At 18:00
trigger:
- at: '18:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true
- id: '1555643622243'
alias: Bell ON At 21:00
trigger:
- at: '21:00'
platform: time
condition: []
action:
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_on
- delay: 00:00:09
- data:
entity_id: switch.switch_sonoff_bell
service: switch.turn_off
hide_entity: true

ข้อมูลเพิ่มเติม
http://shuwernpi.blogspot.com/2018/02/sonoff-tasmota-firmware-sonoff-wifi.html

No comments:

Post a Comment