Embedded สัปดาห์ที่ 4 Arduino IOT Twitter

Arduino IOT Twitter

สัปดาห์นี้ได้รับมอบหมายงานให้ใช้ NodeMCU ทำการแจ้งเตือนผ่าน twitter ดังนี้       
         กดปุ่ม A ที่ต่อกับ NodeMCU — ให้ส่งข้อความ “Over Heat Alarm”
        กดปุ่ม ที่ต่อกับ NodeMCU — ให้ส่งข้อความ “Door Open Alarm”
        กดปุ่ม C ที่ต่อกับ NodeMCU — ให้ส่งข้อความ “Intruders Alarm”

IOT Thingspeak to Twitter

        เหตุผลที่ใช้ Thingspeak เนื่องมาจาก Thingspeak มี API สำหรับเชื่อมต่อกับ Twitter 
จึงทำให้เราสามารถเชื่อมต่อได้ง่ายนั้นเอง ขั้นตอนวิธีทำมีดังนี้
           1. เข้าสู่ระบบของ Thingspeak หากยังไม่มีบัญชีให้สมัครก่อน
           2. กดที่แท็บ Apps ด้านบน แล้วเข้าไปที่ Thingtweet 

          3. หลังจากกดเข้ามาแล้วจะพบหน้าต่างดังภาพด้านล่าง ให้กด Link twitter Account
          4.  หากเราล็อกอิน Twitter ไว้กับ Browser ของเราจะปรากฎหน้าต่างดังรูปด้านล่าง แล้วกด  อนุญาตแอพ หากไม่ได้เข้าระบบ twitter ไว้ จะปรากฏหน้าต่างสำหรับ login หลังจากเข้า login เรียบร้อยแล้ว จึงจะปรากฏดังภาพ
          5.  จากนั้น Thingspeak ก็จะเชื่อมกับ Twitter เรียบร้อย และสำหรับ API Key ไว้ใช้เชื่อมต่อกับอุปกรณ์
  

  Arduino esp8266 to Twitter

        - อันดับแรกให้ทำการต่อปุ่มกดกับ esp8266 ให้เรียบร้อย โดยเชื่อมต่อดังนี้
                       Switch 1 ต่อกับขา  D2 , Switch 2 ต่อกับขา  D3 และ Switch3 ต่อกับขา  D4
        - จากนั้น Upload โค้ด Test1 ไปยัง esp8266 โดยแก้ไขโค้ดดังนี้
                       API = API key จากขั้นตอนที่แล้ว
                       ssid = ชื่อ wifi , pass = รหัส wifi

     Code : Test1
            #include <ESP8266WiFi.h>
            #include <WiFiClient.h>
            #include <WiFiServer.h>
            #include <WiFiUdp.h>

           // create an account at thingspeak.com and link your twitter account to get your API key, replace the x's
           String API = "IZYFJXJ*********"; 


          // enter your ssid and password
          const char* ssid = "your wifi";
          const char* password = "your password";

          // your message
          String tweet = "Over%20Heat%20Alarm";
          String tweet2 = "Door%20Open%20Alarm";
          String tweet3 = "Intruders%20Alarm";
          int cout = 0;
          WiFiClient client;
          #define SW1 D2
          #define SW2 D3
          #define SW3 D4

          void setup() {
              Serial.begin(115200);
              delay(10);
              Serial.print("Connecting to ");
              Serial.println(ssid);
              // connect to wifi
             WiFi.begin(ssid, password);
             pinMode(SW1, INPUT_PULLUP);
             pinMode(SW2, INPUT_PULLUP);
             pinMode(SW3, INPUT_PULLUP);

             // allow time to make connection
            while (WiFi.status() != WL_CONNECTED)
              {  delay(500);
                  Serial.print(".");
               }
              Serial.println();
              Serial.println("WiFi connected");
              Serial.print("IP address: ");
              Serial.println(WiFi.localIP());


              // if connection to thingspeak.com is successful, send your tweet!

          }// setup


           void loop()
          {   int s = digitalRead(SW1);
               int s2 = digitalRead(SW2);
               int s3 = digitalRead(SW3);
               if (s == LOW)
               {
                    Serial.println("Button 1 is pressed!");
                    String msg = cout + (String)"%20" + tweet;
                    send_event(msg);
                    cout++;

                }
               if (s2 == LOW)
               {
                     Serial.println("Button 2 is pressed!");
                     String msg = cout + (String)"%20" + tweet2;
                     send_event(msg);
                     cout++;

                }
                if (s3 == LOW)
               {
                     Serial.println("Button 3 is pressed!");
                     String msg = cout + (String)"%20" + tweet3;
                     send_event(msg);
                     cout++;
                }
            }

            void send_event(String wor)
            {
                   if (client.connect("184.106.153.149", 80))
                   {
                        client.print("GET /apps/thingtweet/1/statuses/update?key=" + API + "&status=" + wor + " HTTP/1.1\r\n");
                        client.print("Host: api.thingspeak.com\r\n");
                        client.print("Accept: */*\r\n");
                        client.print("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n");
                        client.print("\r\n");
                    }
                    Serial.println("Send Success!!!!!");
            }

      ผลลัพธ์

         เนื่องจากทาง Twitter ได้มีการป้องการฟลัดข้อความหรือก็คือการ Tweet ข้อความเดิมซ้ำๆ ดังเราจึงได้เพิ่มตัวเข้าไปในข้อความซึ่งมีอยู่ใน Code ตัวอย่าง



Comments

Popular posts from this blog

VHDL

Embedded สัปดาห์ที่ 9 NI labview 2014

Embedded สัปดาห์ที่ 2 Arduino MQTT and Anto Interface