Week03: Arduino IoTs – ThingSpeak Interface

Arduino IoTs – ThingSpeak Interface    

สำหรับในสัปดาห์นี้น่ะครับ เราจะได้ทดลองการใช้งาน Thingspeak ในการเก็บข้อมูลด้วย sensor แล้วนำไปเก็บใว้บน cloud ซึ่งจะทำให้เราเขาถึงข้อมูลและสั่งงานอุปกรณ์ IOT ของเราได้จากระยะไกลได้น่ะครับ
    Thingspeak คืออะไร ?
Thingspeak เป็นเว็ปที่ให้การบริการในการเก็บข้อมูล และสามารถแสดงข้อมูลแบบ real-time ได้ ซึ้งเราสามารถ update ข้อมูล หรือจะเรียกดูข้อมูลได้ตลอดเวลา ที่ไหนก็ได้ เพราะทางานบน cloud ซึ่ง thingspeak สร้างมาเพื่อต้องการให้ตอบโจทย์ของ IoT อยู่แล้ว ส่วนข้อมูลที่เก็บอยู่บน cloud นั้นก็ขึ้นอยู่กับเราว่าจะใช้ยังไง รูปแบบไหน ในการที่จะส่งข้อมูล data ไปไว้บน cloud นั้น ทาง thingspeak ได้มี api ในการติดต่อไว้เรียบร้อยแล้ว

    การเปิดใช้งาน Thingspeak 
  • อันดับแรกเลยให้ไปที่ https://thingspeak.com ทำการสมัครสมาชิกให้เรียบร้อยน่ะครับ
  • จากนั้นก็สร้าง channel ขึ้นมา โดยให้เรากดไปที่ My channel แล้วก็  new channel ขึ้นมา



  •  หลังจากที่ new channel ขึ้นมาแล้ว ไปที่ channel setting ก็ป้อนข้อมูลเข้าไป
  • อย่าลืมเลือก ตั้งค่าให้เป็น Make Public
  • เสร็จแล้วก็กด save channel
  • เมื่อสร้างเสร็จแล้ว ก็จะแสดงหน้าต่าง ในหน้าต่างนี้จะแสดงข้อมูลเป็นแบบเส้นกราฟ ถ้าตอนนี้ยังไม่มีข้อมูลใด ๆ ส่งมาก็จะไม่เกิดอะไรขึ้น



    การ update ข้อมูลไปยัง cloud ผ่าน API ของ thingspeak
  • อันดับแรกให้เราดู API key ของเราว่ามันคืออะไร เช่น Write API Key 4741AGU8WGCJP0J5
        Read API Keys TYJ1J2AU2OV7Q0U8
  • ในการ update ข้อมูลจะเป็นการส่ง HTTP Request ไปยัง server เพื่อ update ข้อมูลที่ต้องการตาม
          field ต่าง ๆ ที่เรากำ หนด วิธีการ update โดยเราจะส่งข้อมูลแบบนี้
          https://api.thingspeak.com/update?key=4741AGU8WGCJP0J5&field1= 0 โดยที่
          field1=0 คือ field ที่เราต้องการ update ในตัวอย่างคือ field ที่ 1 และ กา หนดให้มีค่าเท่ากับ 0
    


ตัวอย่างการใช้งาน Thingspeak ร่วมกับ sensor สำหรับวัดอุณหภูมิและความชื้นในอากาศ



   นอกเหนือจากนี้ใน week ที่ 3 นี้ เรายังได้ทำการทดลองการเปิด-ปิด ไฟ, การอ่านค่า input จาก switch การ อ่านค่า Humidity และ Temperature ผ่าน Internet โดยใช้ Anto เป็น Broker อีกด้วย



หมายเหตุ: ข้อมูลของ Temperature และ Humidity ในภาพอาจไม่ถูกต้อง เนื่องจากยังไม่ทำการต่อ sensor วัดอุณหภูมิ

code for esp8266

#include "ThingSpeak.h"
#include
#include
#include "ClosedCube_HDC1080.h"
#if !defined(USE_WIFI101_SHIELD) && !defined(USE_ETHERNET_SHIELD) && !defined(ARDUINO_SAMD_MKR1000) && !defined(ARDUINO_AVR_YUN) && !defined(ARDUINO_ARCH_ESP8266)
  #error "Uncomment the #define for either USE_WIFI101_SHIELD or USE_ETHERNET_SHIELD"
#endif

#if defined(ARDUINO_AVR_YUN)
    #include "YunClient.h"
    YunClient client;
#else
  #if defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_ARCH_ESP8266)
    // Use WiFi
    #ifdef ARDUINO_ARCH_ESP8266
      #include
    #else
      #include
      #include
    #endif
    char ssid[] = "SSID";    //  your network SSID (name)
    char pass[] = "PASSWORD";   // your network password
    int status = WL_IDLE_STATUS;
    WiFiClient  client;
  #elif defined(USE_ETHERNET_SHIELD)
    // Use wired ethernet shield
    #include
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
    EthernetClient client;
  #endif
#endif

unsigned long myChannelNumber = CHANNEL_NUMBER;
const char * myWriteAPIKey = "API_KEY";
ClosedCube_HDC1080 hdc1080;

void setup() {

  Serial.begin(9600);
  #ifdef ARDUINO_AVR_YUN
    Bridge.begin();
  #else
    #if defined(ARDUINO_ARCH_ESP8266) || defined(USE_WIFI101_SHIELD) || defined(ARDUINO_SAMD_MKR1000)
      WiFi.begin(ssid, pass);
    #else
      Ethernet.begin(mac);
    #endif
  #endif
    hdc1080.begin(0x40);
    Serial.print("Manufacturer ID=0x");
    Serial.println(hdc1080.readManufacturerId(), HEX);
    Serial.print("Device ID=0x");
    Serial.println(hdc1080.readDeviceId(), HEX);
  ThingSpeak.begin(client);
}

void loop() {

  float temp = hdc1080.readTemperature();
  float humid = hdc1080.readHumidity();
  ThingSpeak.setField(1,temp);                      //sent temp to field1
  ThingSpeak.setField(2,humid);                    //sent humid to field2
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  delay(30000); // Note that the weather station only updates once 30s


}
        แหล่งข้อมูล
https://www.youtube.com/watch?v=_ZF71GhXCjc&feature=youtu.be

ความคิดเห็น