week04 - Node MCU : to SMS


1. บทนำ
   
    IFTTT ออกเสียงเหมือน Gift แต่ไม่มีตัว G ย่อ มาจาก "If This Then That" หรือแปลตรงๆ ว่า "ถ้าเป็นแบบนี้  ให้ทำแบบนี้" โดย IFFF จะทำงานตามคำสั่งในแบบเงื่อนไขล่วงหน้า  เมื่อถึงเวลามันจะทำงานแทนเรานั่นเอง    เมื่อเรา login เข้าไปแล้ว  จะเจอพื้นที่ที่ให้เราสร้าง Task หรือที่ IFTTT เรียกว่า Personal Reipe นั่นคือการกำหนดว่าเราต้องการให้เจ้า IFTTT นี้เค้าทำงานอย่างอะไร
    IFTTT เป็นฟรี web-based service ซึ่งคนทั่วไปสามารถใช้มันสร้าง conditional-statements ง่ายๆ ได้โดยเรียกมันว่า applets โดย applets เกิดขึ้นเมื่อเกิดการกระตุ้น web service อื่นๆ เช่น gmail, facebook, instagram, or pinterest
    applets อาจจะส่งอีเมล์ข้อความถ้า ผู้ใช้ tweet โดยใช้แฮชแท็ก หรือ คัดลอกภาพบน facebook ไปที่ที่เก็บเอกสารของ user ถ้ามีใครบางคนแท็ก user บนภาพ
    นอกเหนือจากเป็น web-based application แล้ว IFTTT ยังรันบน ios และ android ด้วย

Feature:
    - Service(หรือรู้จักกันในชื่อ channels)
   - Triggers
   - Action
  - Applets
 - Ingredients

    2. เตรียมการทดลอง
        2.1    เตรียม ESP8266 Board
        2.2    เตรียม Arduino IDE พร้อมติดตั้ง ESP8266 Board
        2.3    Download and Install Library - https://github.com/witnessmenow/arduino-ifttt-maker/archive/master.zip
        2.4    Download and Install Library  -  https://github.com/bblanchon/ArduinoJson/archive/master.zip
       
    3. การทดลอง
        1. Signed in IFTTT ที่ https://ifttt.com/
        2. Setting IFTTT โดยไปที่ applet >> new applet
        3. จากนั้นเลือก ที่ if
        4. เลือก Maker Webhooks





        5.เลือก Receive a web request



        6.หลังจากนั้น เลือกที่ that



        7. เลือกที่ sms (service นี้ ใช้บริการได้ฟรี 10 ครั้งต่อเดือน)
     
       8. connect sms




        9.ใส่เบอร์โทรศัพท์ที่เราต้องการส่ง sms จากนั้นยืนยัน รหัสเชื่อต่อ







       
        10. เลือก send me an a SMS

      
         11.ที่นี้เราจะมากำหนดข้อความที่เราจะส่งกันน่ะครับ


 
        12.    เพียงแค่นี้เราก็เสร็จสิ้นการตั้งค่า recipt แล้ว ต่อไปจะเป็นการสั่งงาน เพื่อส่ง SMS น่ะครับ
         13 .  อ่านค่า Key ไปที่ https://ifttt.com/maker_webhooks เลือก Document


 

14.   ทดสอบด้วย Web browser

15.  ทดสอบด้วย NodeMCU ไปที่ File à Example à IFTTTMaker à ESP8266 à EventTriger

#include

#include 8266WiFi.h>
#include

//------- Replace the following! ------
char ssid[] =       "IOT_Test";
char password[] =   "pk12345678";
#define KEY         "b-uGMQ7EmOHFZ9h84Zy0VW"
#define EVENT_NAME  "SUT1234"

WiFiClientSecure client;
IFTTTMaker ifttt(KEY, client);

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);


  //triggerEvent takes an Event Name and then you can optional pass in up to 3 extra Strings
  if(ifttt.triggerEvent(EVENT_NAME, ssid, ip.toString())){
    Serial.println("Successfully sent");
  } else
  {
    Serial.println("Failed!");
  }
}
void loop() {
}



4. คำถาม
4.1    จาก NodeMCU to Mobile Phone SMS: ทดสอบการส่งข้อมูลไปแบบ SMS
o   กดปุ่ม A ที่ต่อกับ NodeMCU – ให้ส่งข้อความ “Over Heat Alarm”
o   กดปุ่ม B ที่ต่อกับ NodeMCU – ให้ส่งข้อความ “Door Open Alarm”
o   กดปุ่ม C ที่ต่อกับ NodeMCU – ให้ส่งข้อความ “Intruders Alarm”


#include

// constants won't change. They're used here to


// set pin numbers:


#define D0 16 // USER LED Wake


int Switch_Pin = D1; // the number of the pushbutton pin

int Switch_Pin2 = D2;
int Switch_Pin3 = D3;
#define LED_Pin D0 // the number of the LED pin
/****** Connect Router Part **********/
const char ssid[] = ".@SEDA_WiFi.";
const char password[] = "sedasut2016";
// IFTTT setup
const char *host = "maker.ifttt.com";
const char *Maker_Event = "ButtonA";
const char *Maker_Event2 = "ButtonB";
const char *Maker_Event3 = "ButtonC";

const char *Your_Key = "YrLC15_rXuD9XfP_ZgTVuVUw4V4AvOMxu8iSz_AjRD";


const int buttonPin = Switch_Pin; 

const int buttonPin2 = Switch_Pin2;
const int buttonPin3 = Switch_Pin3;

const int ledPin = LED_Pin ; // the number of the LED pin

//------- prototype function ---------

void send_event(const char *event);


void setup()


{


Serial.begin(115200);


Serial.println();


Serial.print("Connecting to ");


Serial.println(ssid);


/*********** Connect to WiFi network ***********/


WiFi.begin(ssid, password);


while (WiFi.status() != WL_CONNECTED)


{

delay(250);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected with Router");
Serial.print("IP = ");
Serial.println(WiFi.localIP());
// Set your pin modes
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // turn off LED
}
void loop()

{

int buttonState = digitalRead(buttonPin);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);

if (buttonState == LOW)

{
Serial.print("Button is pressed!");
send_event(Maker_Event);
}
if (buttonState2 == LOW)
{
Serial.print("Button is pressed!");
send_event(Maker_Event2);
}
if (buttonState3 == LOW)
{
Serial.print("Button is pressed!");
send_event(Maker_Event3);
}
delay(2500);
}
void send_event(const char *event)
{
// set the LED on whle we are sending the event
digitalWrite(ledPin, LOW);
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort))
{
Serial.println("Connection failed");
return;
}
// We now create a URI for the request
String url = "/trigger/";
url += event;
url += "/with/key/";
url += Your_Key;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// Read all the lines of the reply from server and print them to Serial,
// the connection will close when the server has sent all the data.
while(client.connected())
{
if(client.available())
{
String line = client.readStringUntil('\r');
Serial.print(line);
}
else
{
// No data yet, wait a bit
delay(50);
};
}
// All done
Serial.println();
Serial.println("closing connection");
client.stop();
// Finished sending the message, turn off the LED
digitalWrite(ledPin, HIGH);
}









           


** ไม่สามารถส่ง sms เข้าโทรศัพท์มือถือเนื่องจากเกินโควต้าของ IFTTT SMS แล้ว
อ้างอิง:
        http://www.olrepublic.com/careerlab/เทคนิคการทำงาน-อ่านแล้วใช้เลย-Working-Tips/1697-IFTTT-If-This-Then-That.html

ความคิดเห็น