สั่งงาน Sonoff ด้วยเสียง ผ่าน App "ฟ้าใส เลขาส่วนตัว"
Sonoff นอกจาก จะสามารถเปิด-ปิดไฟ ผ่านมือถือได้แล้ว สำหรับนัก Modify ทั้งหลายมักจะนำเจ้า Sonoff ไปดัดแปลงทำอย่างอื่นได้อีกหลากหลาย ถือว่าเป็นอุปกรณ์เล็กๆ ราคาเบาๆ ที่คุ้มค่า คุ้มราคา ตัวนึงเลยทีเดียว
สำหรับวันนี้เราจะมาแนะนำวิธีการ สั่งงาน Sonoff ด้วยเสียง ผ่าน แอพที่ชื่อว่า ฟ้าใส "ฟ้าใส" คือแอพ ของคนไทย พูดง่ายๆ คือ Siri คนไทย นั่นเอง ในบทความนี้จะพูดถึงการควบคุม Sonoff ผ่าน Anto Cloud แล้วนำคำสั่งไปใช้งานผ่านแอพฟ้าใส อีกที เพื่อเป็นการไม่เสียเวลา มาเริ่มกันเลยดีกว่าครับ
1. ก่อนอื่น เข้าไปสมัครสมาชิก Anto.io กันก่อนครับ โดยเลือกที่ Sign Up จากนั้น กรอกรายละเอียดให้เรียบร้อย แล้วดลือก Create a new account (จำ Username ที่เราตั้งไว้ด้วยนะครับ)
3. เมื่อสร้าง Thing เสร็จแล้ว ต่อไปเป็นการสร้าง Channel เพื่อใช้ควบคุม Input/Output ดดยเข้าไปที่ รูปแว่นขยายสีน้ำเงิน
คลิก +Create New Channel เพื่อ สร้าง Channel
สำหรับวันนี้เราจะมาแนะนำวิธีการ สั่งงาน Sonoff ด้วยเสียง ผ่าน แอพที่ชื่อว่า ฟ้าใส "ฟ้าใส" คือแอพ ของคนไทย พูดง่ายๆ คือ Siri คนไทย นั่นเอง ในบทความนี้จะพูดถึงการควบคุม Sonoff ผ่าน Anto Cloud แล้วนำคำสั่งไปใช้งานผ่านแอพฟ้าใส อีกที เพื่อเป็นการไม่เสียเวลา มาเริ่มกันเลยดีกว่าครับ
1. ก่อนอื่น เข้าไปสมัครสมาชิก Anto.io กันก่อนครับ โดยเลือกที่ Sign Up จากนั้น กรอกรายละเอียดให้เรียบร้อย แล้วดลือก Create a new account (จำ Username ที่เราตั้งไว้ด้วยนะครับ)
กรอกหมายเลขโทรศัพท์ และจุดประสงค์ในการใช้งาน แล้วกด Update Account and Continue
2. Login เพื่อเข้าใช้งาน Anto.io จากนั้นเข้าไปที่เมนู Thing ที่แถบเมนูด้านซ้ายมือ จากนั้นเลือก Create new thing
จากนั้นกรอกข้อมูลลงไป ดังนี้
NAME = ชื่องานที่เราจะสร้าง (ภาษาอังกฤษ)
Description = คำอธิบาย
Hardware = ใส่เป็น ESP8266
แล้วกด +Add New
คลิก +Create New Channel เพื่อ สร้าง Channel
กรอกข้อมูล Channel ให้เรียบร้อย จากนั้นกด +Add new
4. สร้าง Key เพื่อนำไปใช้งาน โดยไปที่ เมนู Key แล้วเลือก +Add New Key จากนั้นเลือก Permission ให้ LED1 โดยติ๊กเครื่องหมายถูกที่ Read และ Update ดังรูป แล้วคลิกที่ +Add New
จะได้ Key สำหรับนำไปใช้ในการเขียนโปรแกรมพเื่อควบคุม ดังรูป
5. เมื่อทำการตั้งค่าบน Anto.io แล้ว ต่อไปจะเป็นการ เขียนโปรแกรมลงบน Sonoff (คำเตือน : เมื่อเขียนลงไปแล้ว จะไม่สามารถใช้งาน Software เก่าได้นะครับ) โดยต่อ Sonoff เข้ากับ Bootloader ดังรูป
ขอบคุณภาพจาก http://tridenttd-iot.blogspot.com/2016/03/sonoff-hack-pin-upload.html
6. ติดตั้ง Library Anto (Download คลิก) จากนั้นเขียนโปรแกรมบน Arduino IDE ดังตัวอย่าง
แก้ Code ต่อไปนี้
1. บรรทัดที่ 4 >> user = Username ที่ตั้งไว้ จากข้อ 1
2. บรรทัดที่ 5 >> key = key ที่ได้จากข้อ 4
3. บรรทัดที่ 8 >> thing = ชื่อ thing ที่ได้จากข้อ 2
4. บรรทัดที่ 19 >> ssid = ชื่อ Wifi ที่ต้องการเชื่อมต่อ
บรรทัดที่ 20 >> pwd = รหัสผ่าน Wifi
5. บรรทัดที่ 101 และ 104 >> เปลี่ยนเป็น ขา 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <AntoIO.h> | |
// username of anto.io account | |
const char *user = "montienmcu"; | |
const char* key = "mBGoLWI6PYe0YYMyF5TZHr8Rcm8WvriYcWMO6g2w"; | |
// your default thing. | |
const char* thing = "Sonoff"; | |
AntoIO anto(user, key, thing); | |
bool bIsConnected = false; | |
int Led1,Led2,Led3 = 0; | |
int value = 0; | |
void setup() { | |
// SSID and Password of your WiFi access point. | |
const char* ssid = "iFocus"; | |
const char* pwd = "00000000"; | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Anto library version: "); | |
Serial.println(anto.getVersion()); | |
// Connect to your WiFi access point | |
if (!anto.begin(ssid, pwd)) { | |
Serial.println("Connection failed!!"); | |
// Stop everything. | |
while (1); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("Connecting to MQTT broker"); | |
// register callback functions | |
anto.mqtt.onConnected(connectedCB); | |
anto.mqtt.onDisconnected(disconnectedCB); | |
anto.mqtt.onData(dataCB); | |
anto.mqtt.onPublished(publishedCB); | |
// Connect to Anto.io MQTT broker | |
anto.mqtt.connect(); | |
//port output | |
//pinMode(D0,OUTPUT); | |
pinMode(12,OUTPUT); | |
pinMode(D2,OUTPUT); | |
pinMode(D3,OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} | |
/* | |
* connectedCB(): a callback function called when the connection to the MQTT broker is establised. | |
*/ | |
void connectedCB() | |
{ | |
// If the connection is establised, subscribe channels. | |
bIsConnected = true; | |
Serial.println("Connected to MQTT Broker"); | |
anto.sub("LED1"); | |
anto.sub("LED2"); | |
anto.sub("LED3"); | |
} | |
void disconnectedCB() | |
{ | |
bIsConnected = false; | |
Serial.println("Disconnected to MQTT Broker"); | |
} | |
/* | |
* msgArrvCB(): a callback function called when there a message from the subscribed channel. | |
*/ | |
void dataCB(String& topic, String& msg) | |
{ | |
uint8_t index = topic.indexOf('/'); | |
index = topic.indexOf('/', index + 1); | |
index = topic.indexOf('/', index + 1); | |
topic.remove(0, index + 1); | |
Serial.print(topic); | |
Serial.print(": "); | |
Serial.println(msg); | |
if(topic.equals("LED1")){ | |
value = msg.toInt(); | |
if(value == 1){ | |
digitalWrite(12,HIGH); | |
} | |
else{ | |
digitalWrite(12,LOW); | |
} | |
} | |
else if(topic.equals("LED2")){ | |
value = msg.toInt(); | |
if(value == 1){ | |
digitalWrite(D2,HIGH); | |
} | |
else{ | |
digitalWrite(D2,LOW); | |
} | |
} | |
else if(topic.equals("LED3")){ | |
value = msg.toInt(); | |
if(value == 1){ | |
digitalWrite(D3,HIGH); | |
} | |
else{ | |
digitalWrite(D3,LOW); | |
} | |
} | |
} | |
void publishedCB(void) | |
{ | |
Serial.println("published"); | |
} |
จากทำการ Upload โปรแกรม ลงบน Sonoff ได้เลยครับ (อย่าลืมกดปุ่ม Reset ก่อนเสียบ USB นะครับ)
7. ทดลองเปิด-ปิดไฟ ผ่านหน้าเว็บ Anto.io โดยเ้าไปที่ เมนุ Thing แล้วไปที่ Channel ที่เราสร้างไว้
ถ้าเปิด-ปิด ได้ ให้ข้ามไปข้อ 8 ได้เลย แต่ถ้ายังไม่ได้ให้กลับไปเช็คใหม่ อีกรอบครับ
8. ดาวน์โหลดแอพ ฟ้าใส จาก Play Store จากนั้นเปิด แอพ ขึ้นมา แล้วทำการสมัครสมาชิกให้เรียบร้อยครับ
พูดคำสั่ง "เชื่อมต่ออุปกรณ์ผ่านเน็ต"
พูดคำสั่ง "เพิ่มคำสั่ง"
พูดคำสั่ง "ส่งค่าผ่าน URL"
จากนั้น พิมพ์คำสั่งที่ต้องการ เช่น ต้องการใช้คำสั่ง เปิดไฟ ให้พิมพ์ว่า
เปิดไฟ ส่งค่า https://api.anto.io/channel/set/key/thing/channel/ค่าที่ต้องการส่ง
จากตัวอย่างที่ผ่านมา จะได้
เปิดไฟ ส่งค่า https://api.anto.io/channel/set/mBGoLWI6PYxxxx/Sonoff/LED1/1
เช่นเดียวกันกับคำสั่งปิดไฟ ครับ จะได้
ปิดไฟ ส่งค่า https://api.anto.io/channel/set/mBGoLWI6PYxxxx/Sonoff/LED1/0
จากนั้นทดลอง ใช้คำสั่ง เปิด-ปิด ไฟ ผ่านฟ้าใสดูครับ (ดูวิธีการใช้งานได้จาก ในคลิปด้านล่างครับ)
สุดยอดมากเลยครับ ต้องลองทำบ้างแล้วครับ
ตอบลบขอบคุณสำหรับบทความดีๆมากค่ะ ขออนุญาตนำไปลองทำนะคะ
ตอบลบสอบถามหน่อยครับแล้วถ้าเป็นกรณีที่เป็น sonoff dual จะต้องเขียน code ยังไงครับ ถ้าเป็นไปได้รบกวนช่วยอธิบาย code ด้วยได้มั๊ยครับ ขอบคุณครับ
ตอบลบมีวิธีการสั่งผ่าน Ifttt ไหมครับ น่าจะดีกว่า ถ้าไม่ต้องไปยุ่งกับ code เดิมของ Sonoff
ตอบลบstopทั้งหมดที่เชื่อมต่อ
ตอบลบของผม error ครับ ตรงโค้ด มันบอกว่า
ตอบลบexit status 1
no matching function for call to 'AntoIO::begin(const char*&, const char*&)'
มันขึ้นบรรทัดแดงตรงบรรทัดที่ 31 ครับ