NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf ·...

97
NODEMCU FOR INTERNET OF THING Basic IoT Training Book Document Version 1.0 by Maximus [email protected]

Transcript of NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf ·...

Page 1: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

NODEMCU FOR INTERNET OF THING Basic IoT Training Book

Document Version 1.0 by Maximus [email protected]

Page 2: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

1

Contents มารจก NodeMCU Dev Kit V1.0......................................................................................................................... 3

Features ของ NodeMCU ............................................................................................................................. 4

ขนตอนตดตง NodeMCU ใน Arduino IDE .................................................................................................... 5

ทดสอบ NodeMCU ดวยการ Scan Wifi ..................................................................................................... 12

ทดลองใชงาน NodeMCU .................................................................................................................................. 15

LAB01: Digital Output .............................................................................................................................. 15

LAB02: Digital Input .................................................................................................................................. 17

LAB03: การสงคาออก Serial ....................................................................................................................... 19

LAB04: การอานคาจาก Serial ..................................................................................................................... 20

LAB05: ทดลอง NodeMCU รบคาจาก serial เพอเปดปดไฟ ...................................................................... 21

LAB06: Analog Input ................................................................................................................................ 23

LAB07: Pulse Width Modulation (PWD) ................................................................................................ 25

LAB08: การแสดงขอความ LCD display I2C .............................................................................................. 27

LAB09: การใช Ultrasonic วดระยะทาง ...................................................................................................... 31

LAB10: การใช Motion Sensor .................................................................................................................. 36

LAB11: การวดอณหภม และ ความชนดวย DHT11 ..................................................................................... 41

LAB12: ท า NodeMCU เปน Web Server.................................................................................................. 46

LAB13: ท า NodeMCU เปน Web Client .................................................................................................. 47

การใชงาน Cloud Broker ส าหรบงาน IoT ........................................................................................................ 49

LAB14: ท า Data Logger โดยสง Data ไปท SparkFun ............................................................................. 49

LAB15: วธเอา Data จาก SparkFun ไปแสดงผลท AnalogIO .................................................................... 54

Page 3: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

2

LAB16: ใช ThingSpeak เปน Data Logger พรอม Plot Graph ................................................................ 57

LAB17: ใช ThingSpeak เปน ควบคมตว NodeMCU ................................................................................. 65

NETPIE ............................................................................................................................................................... 71

LAB18: เปดปดไฟดวย NETPIE ผานเวบ HTML5 ........................................................................................ 74

LAB19: NETPIE Freeboard ....................................................................................................................... 78

LAB20: เปดปดไฟผาน NETPIE Freeboard ................................................................................................ 83

LAB21: วดอณหภม ความชน สงคามาแสดงบนฟรบอรด ............................................................................. 90

Page 4: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

3

การใชงาน NodeMCU เบองตน

มารจก NodeMCU Dev Kit V1.0

NodeMCU นนเปน open-source hardware and firmware ซงเปนบอรดทชวยใหเราสามารถใชงานเกยวกบ IoT (Internet of Thing) ไดอยางงาย เพราะการเขยนโปรแกรมตาง ๆ ควบคม GPIO ของชพ ทใชภาษา Lua ในการเขยน script เพยงไมกบรรทด หรอใช C กไดเชนกน

โดยเปนโมดลทประกอบดวย ESP8266-12E มเสาอากาศแบบ PCB Antenna และรองรบรปแบบการสอสารส าหรบขาสญญาณตาง ๆ หลาย ๆ ชนดไดแก GPIO, PWM, I2C, 1-wire, ADC และ ม SPI เพมขนมาจาก Version เดม ในการสอสารกบ Computer สวนของ USB-to-TTL และพอรต micro USB ใชชพ USB to Serial ของ Silicon Lab CP2102 สงทจ าเปนคอเราตองรรปแบบการจดวางขาสญญาณตาง ๆ ตามรป

Page 5: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

4

Features ของ NodeMCU - Open-source

- สามารถเขยนโปรแกรมสงควบคมได

- ใชตนทนต า

- ใชงานงาย

- เชอมตอ Wi-Fi ได

- Smart

- Interactive

สามารถควบคม hardware I/O เหมอนกบ Arduino

ม API ส าหรบการควบคม I/O ของ Hardware ซงจะชวยลดการท างานทซ าซอนส าหรบการก าหนดคาและจดการพวก Hardware สามารถเขยนโปรแกรมเชนเดยวกบ Arduino ได

เปนอปกรณ WiFi ทใชตนทนตา

ตนทนนอยกวา 2 ดอลลาร NodeMCU WiFi MCU ESP8266 ตวนผลตออกมาไดครบวงจร สามารถพฒนาตอไดงายในงาน IoT และตนทนต า ตอนนขายประมาณ 400 บาท

Specification ของบอรด

เปนชดพฒนาทเปน Base on ESP8266 ม GPIO, PWM , I2C , 1-Wire และ ADC รวมทงหมดในบอรดเดยว เปนชองทางทชวยในการพฒนาทรวดเรวดวย NodeMCU firmware!

- USB-TTL included, plug&play (CP2102)

- ESP-12E 32 Mbps

- 10 GPIO, ทก ๆ GPIO สามารถเปน PWM, I2C, 1-wire

- 1 ADC

- WI-FI module

- PCB antenna

Page 6: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

5

ขนตอนตดตง NodeMCU ใน Arduino IDE

ไปท Arduino IDE เลอก Preference

ท าการปอน Additional Boards Manager URL

http://arduino.esp8266.com/staging/package_esp8266com_index.json

Page 7: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

6

หลงจากนนไปท Menu Tools > Board Manager…

จะขนหนาจอ Board Manager

Page 8: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

7

ใส ESP8266 เพอคนหา package จากนน install version ลาสด

เมอ Install เสรจสนแลวจะม Board NodeMCU ขนมา

Page 9: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

8

ลองเสยบสาย USB ท NodeMCU หากไมพบ Port ทเชอมตอ ใหเชคในสวนของ Device Manager วา Windows ตรวจเจอ Driver ของ Chip CP2102 หรอไม ปญหาสวนใหญคอ Windows ไมเจอ Driver ของ CP2102

ใหท าการ Download Driver CP2102 มาตดตง

Page 10: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

9

ในกรณทไมเจอ Com port ตองท าการ Update USB Driver CP2102

เลอกคนหาจาก Driver ท Copy มาให

Page 11: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

10

Page 12: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

11

เมอตดตงเสรจจะสามารถเลอก Com port ทเชอมตออยได

Page 13: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

12

ทดสอบ NodeMCU ดวยการ Scan Wifi เลอกบอรด NodeMCU โดยเลอก version 1.0 และ Board late อยท 115200

เปดตวอยางในสวนของ File > Examples > ESP8266WiFi > WiFiScan

Page 14: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

13

จะได code ของ Wi-Fi scan ใหเราท าการคอมไพลและอพโหลดเขาไปในตว NodeMCU

เมอเราท าการอพโหลดเสรจสนแลวในสวนของ status box จะแสดงขอความวา Done uploading

Page 15: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

14

ใหเราเปด serial monitor จะแสดงรายการของ wifi ทเปดอยทส าคญคอการเลอกบอรดเรทของ serial monitor ตองตรงกบบอรดเรทของ NodeMCU ทเราใช คอ 115200

ถาขนตามภาพเปนอนเสรจสนกระบวนการเราสามารถใชงาน NodeMCU ได

Page 16: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

15

ทดลองใชงาน NodeMCU

ในการใชการเชอมตอขาสญญาณ Input Output ตาง ๆ จากตว NodeMCU ปกตแลวเราสามารถอางองขาสญญาณตาง ๆ ของ NodeMCU ไดโดยใช d0 d1 d2 หรออางองจาก GPIO ของ ESP8266 โดยตรงเลยกได ซงจะเปนตวเลขอยางเชน d0 จะเปนเลข 16, d1 จะเปนเลข 5, d2 จะเปนเลข 4 ในการใชงานจรงนนแนะน าใหใชเปนเลขทออกมาจากขา GPIO ของ ESP8266 โดยตรง เพราะในกรณทเราน าไปใชเปน production เราไมจ าเปนทจะตองแกโคดจาก d0 เปนเลข 16 เวลาเราใช production สวนใหญเราจะน า ESP8266 ทเบรน firmware และโปรแกรมของเราเอาไวแลวไปใชงาน

LAB01: Digital Output ท าการตอวงจรดงภาพ เปด ArduinoIDE และเลอก Menu File > Example > Basic > Blink แลวน า Code มาแกได

Page 17: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

16

หลกคอ ใช function ทใชก าหนด pinMode ใหเปน OUTPUT และใช function digitalwrite สงคาออกเปน LOW หรอ HIGH

pinMode(pout, OUTPUT); digitalWrite(pout, HIGH); // LOW, HIGH หรอ 0,1 กได

LAB01: Digital Output

const int pout = 2; // เราสามารถใช D4 แทนได GPIO 2 นจะมแอลอดเชอมตออยบนตว ESP8266 ดวย

void setup() { pinMode(pOUT, OUTPUT); }

void loop() { digitalWrite(pOUT, HIGH); delay(1000); digitalWrite(pOUT, LOW); delay(1000); }

ผลทดสอบ run ไฟทตวของ ESP8266 จะกระพรบตดดบ 1 วนาท และ LED ทตอไวจะตดดบสลบกน

Page 18: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

17

LAB02: Digital Input ท าการตอวงจรดงภาพ เปด ArduinoIDE และเลอก Menu File > Example > Digital > button แลวน า Code มาแกได

ในสวนของ digital in นเราจะทดสอบดวยการตอ Switch และ LED เมอกด Switch แลว LED จะตดและเมอปลอย Switch แลว LED จะดบ

ขอควรจ าและตองระวงใหมากทสดคอการดไซนวงจรใชงานแลวท าให Short Circuit หรอแหลงจายไฟไปเชอมตอกบกราวด Ground โดยตรงตองระวงไวใหมากถาไมมนใจไมควรเสยบโดยตรงกบ NodeMCU ใหท าการตอวงจรทดสอบขางนอกกอน

หลกคอ ใช function ก าหนด pinMode ใหเปน INPUT และใช function digitalread จากพอรตทตองการอานคา ซงจะไดคาออกมาเปน logic LOW หรอ HIGH

pinMode(ledPin, INPUT); digitalWrite(buttonPin); //คาทไดจะออกมาเปน logic LOW, HIGH

Page 19: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

18

LAB02: Digital Input

const int buttonPin = 0; // the number of the pushbutton pin

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

int buttonState = 0; // variable for reading the pushbutton status

void setup() { pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input: }

void loop() { buttonState = digitalRead(buttonPin); // read the state of the pushbutton value: if (buttonState == HIGH) { // if it is, the buttonState is HIGH: digitalWrite(ledPin, HIGH); // turn LED on: } else { digitalWrite(ledPin, LOW); // turn LED off: } }

Page 20: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

19

LAB03: การสงคาออก Serial ในการพฒนาโปรแกรมนนหลายครงทเราจะตองสงคาผาน serial เพอใหเรารการท างานของโปรแกรมทเราใสไวใน NodeMCU เราจงจ าเปนตองรลกษณะการก าหนดคา serial และการใชงาน serial เบองตน

อยางแรกทจ าเปนตองก าหนดคอการก าหนด Baud Rate ของ serial สวนใหญเรามกจะก าหนดใหฟงกชน setup ในทน Baud Rate ทจะใชกบ NodeMCU คอ 115200 และสวนท 2 คอการน าคาของ serial มาแสดงออกทาง serial monitor ดวยค าสง serial print โดยจะม 2 ฟงกชนหลกคอ serial.print() คอการแสดงผลโดยไมขนบรรทดใหม และ serial.println() คอการแสดงผลโดยขนบรรทดใหม

LAB03: Send to Serial

void setup() { Serial.begin(115200); // initialize serial communication at 115200 bits per second: } void loop() { Serial.print("My Name: "); // แสดงผลโดยไมขนบรรทดใหม Serial.println("Maximus"); // แสดงผลแลวขนบรรทดใหม delay(5000); // delay in between reads for stability

}

ผลทไดคอเมอเปดโปรแกรม hercules จะขนขอความ “My Name: Maximus” แลวขนบรรทดใหมไปไดเรอยๆ

Page 21: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

20

LAB04: การอานคาจาก Serial การอานคาจาก Serial หลงจากทเราสงออกไปไดแลว มาทดลองรบคาจากคอมพวเตอรบาง วธการรบขอมลเขาจาก คอมพวเตอร จะใชฟงกชน Serial.Read() ซงในตวอยางนชวยใหเขาใจมากขน หลงจาก upload แลวเปด Serial Monitor serial.read() จะเปนฟงกชน ทคนคารบมาจาก serial ซงในทนเปนตวอกษร จะน ามาใชงานเราจง ตองประกาศตวแปร มารบคา และน าคาไปประมวลผลตอไป ส าหรบในโปรแกรมน เราไดรบคา และพมพคา ออก Serial Monitor โดยเพม “>” เขาไปขางหนาดวย

LAB04: Read from Serial

void setup() { Serial.begin(115200); Serial.println("Hello Please Type"); } void loop() { if (Serial.available() > 0) // Function available มหนาทตรวจสอบวามการสงคาออกมาจาก serial ไหมถามการสงคามาจะ return value ทมคามากกวา 0

{ char incomingByte = Serial.read(); // serial.print() ท าหนาทอานคาจาก serial ทละตวอกษร

Serial.print ("> "); Serial.println (incomingByte); // แสดงคาตวอกษรทรบมาจาก serial } }

ผลทไดเมอเราสงขอความออกไปตว NodeMCU จะสงคากลบมาทละ Byte โดยใชโปรแกรม Hercules

Page 22: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

21

LAB05: ทดลอง NodeMCU รบคาจาก serial เพอเปดปดไฟ

การใชงาน NodeMCU ควบคม LED ผาน Serial ถอเปนการเรยนรการรบสงขอมลระหวาง คอมพวเตอร กบ NodeMCU หากสามารถเขาใจการรบสงขอมลแบบ Serial แลว การน าไปประยกตใชกบงาน ตาง ๆ กจะสามรถท าไดงายมากยงขน

LAB05: Control LED by Serial

#define LED1_Pin 2

String SerialGET = ""; void setup() { Serial.begin(115200); pinMode(LED1_Pin, OUTPUT); }

void loop(){ while (Serial.available()) { char c = Serial.read(); if (c == '.') { Serial.println(SerialGET); if (SerialGET.indexOf("ON") >= 0) digitalWrite(LED1_Pin, HIGH); else if (SerialGET.indexOf("OFF") >= 0) digitalWrite(LED1_Pin, LOW); SerialGET = ""; }else

SerialGET += c; } }

ผลการทดลอง เมอเราท าการ Run ใหเปด Serial Monitor ขนมา แลวพมพ “ON.” กดปม Send เพอเปด LED และพมพ “OFF.” เพอปด LED การรบขอมลมาจาก Serial จะรบมาทละตวอกษรผานค าสง Serial.read() หลกการคอการวนลปเพอเกบทกตวอกษรทไดจากค าสง Serial.read() โดยน าไปเกบไวในตวแปร

Page 23: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

22

ชนดสตรง เพองายตอการน าไปใช งานหลกการตรวจวารบขอมลมาครบหรอยง คอการตรวจหา “.” เพอก าหนดสนสดตวอกษร การสง ขอมลผาน Serial จะตองมการสงตวอกษร “.” ทกครง เมอตรวจหา “.” เจอ นนหมายความวาขอมลได รบมาครบเรยบรอยแลว แลวจงน าขอมลมาประมวลผลตอไดเลย

Page 24: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

23

LAB06: Analog Input เนองจาก ESP8266 นนใชไฟเลยงอยท 3.3v และท analog pin รบ input ไดเพยง 0-1v เทานน ตางจาก arduino ทใชไฟเลยงท 5v และ analog pin กสามารถรบ input ไดถง 0-5v ซง sensor ทวไปนนสวนมาก Vout ของ sensor จะมแรงดนอยทประมาณ 0-5v ถาเปน Analog Pin ทวไปของ arduino สามารถรบ input ไดสบาย ๆ แตในสวนของ ESP8266 นนจ าเปนตองตอวงจรแบงแรงดนค านวณหาคาแรงดนทไมเกน 1v มาก เพอท ADC Pin ของ ESP8266 สามารถอานคาไดอยางปลอดภย

เมอ NodeMCU V1 ออกมา ซงทางผผลตกไดค านงถงจดน ภายในบอรดของ NodeMCU นนจงไดตอวงจรแบงแรงดนไวภายในบอรด ซงผใชงานไมตองมานงตอวงจรเอง สามารถตอสายสญญาณจาก Vout ของ sensor เขาไปท Analog Pin ของบอรดตรง ๆ ไดเลย

ท าการตอวงจรเพมตามรป โดยใช Potentiometer เพมเขามา

Page 25: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

24

คาทอานไดจาก Analog Pin นน ถาเราจายไฟท 0v คาทอานไดจาก analogRead() กจะไดเทากบ 0 ถาหากจายไฟไปท analog pin 3.3v คาทอานไดจาก analogRead จะไดเทากบ 1023 หมายความวาแรงดนทอานจะอยระหวาง 0-3.3v ทขา Analog Pin ของ NodeMCU

LAB06: Analog Input

void setup() { Serial.begin(115200); }

void loop() { int sensorValue = analogRead(A0); // read the input on analog pin 0: Serial.println(sensorValue); // print out the value you read: delay(1000); // delay in between reads for stability

}

ผลทไดเมอเราหมนปรบคา Potentiometer คาท Serial กจะเปลยนตามดวย ซงคาทไดจะอยในชวง 0 - 1023

Page 26: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

25

LAB07: Pulse Width Modulation (PWD) โดยปกตแลว Digital Port จะสามารถมไดแค 2 สถานะ คอ HIGH (5 โวลท )กบ LOW (0 โวลท) เทานน จงท าใหสรางคาสญญาณ Logic ไดเพยง เปดหรอปด (1 หรอ 0 , มไฟหรอไมมไฟ )แคนน ซงการใชเทคนค PWM นน จะเปนการท าให Digital Port สามารถเขยนคาไดมากกวา HIGH หรอ LOW โดย ท าใหสามารถเขยนคาเปนแบบ Analog ได NodeMCU มคาตงแต 0-1023 โดยวธการนนจะใชการปรบสถานะของสญญาณ Logic HIGH / LOW สลบกนไปมาดวยคาบเวลาหนงๆ โดยคาทไดนนจะขนอยกบ สดสวนเวลาของสญญาณในชวงเวลาทมสถานะเปน HIGH กบชวงเวลาทเปน LOW โดย ชวงเวลาทงหมดทสญญาณมสถานะเปน HIGH นนเราจะเรยกวาเปน "ความกวาง Pulse (Pulse Width)" โดยสญญาณพลส เมอเทยบ %ของชวงเวลาทเปน HIGH (หรอกคอ

%ของ Pulse Width) กบ %ของคาบเวลา ( Period) ของพลสลกนน ๆ เราจะเรยกวา Duty Cycle เพอความเขาใจสามารถดไดจากตวอยางดานลาง

ถาจะสรางสญญาณเอาตพตแบบ PWM (Pulse Width Modulation) ทปรบคา Duty Cycle ได กสามารถใชค าสง analogWrite() ตวอยางหนงตอไปน สาธตการท าให LED เพมหรอลดความสวาง โดยใชหลกการทเรยกวา PWM-based LED Dimming โดยการสรางสญญาณ PWM ทสามารถเปลยนคา Duty Cycle ได โดยสามารถเลอกขา GPIO 0..15 และคา Duty Cycle ไดในชวง 0..1023 (0% ถง 100%)

Page 27: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

26

เปด Menu File > Example > Analog > AnalogInOutSerial แลวน ามาแกไขได

LAB07: PWM

#define analogInPin A0 // Analog input pin that the potentiometer is attached to

#define analogOutPin 2 // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot int outputValue = 0; // value output to the PWM (analog out)

void setup() { Serial.begin(115200); // initialize serial communications at 115200 bps: }

void loop() { sensorValue = analogRead(analogInPin); // read the analog in value: outputValue = map(sensorValue, 0, 1023, 0, 512); // map it to the range of the analog out: analogWrite(analogOutPin, outputValue); // change the analog out value:

// print the results to the serial monitor: Serial.print("sensor = "); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

delay(2); }

Page 28: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

27

LAB08: การแสดงขอความ LCD display I2C กอนอนตองตดตง Library LiquidCrystal I2C ท Library Manager…

Page 29: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

28

LAB08-1: Scan I2C Address #include <Wire.h> void setup() { Serial.begin (115200); while (!Serial) { } // Leonardo: wait for serial port to connect Serial.println (); Serial.println ("I2C scanner. Scanning ..."); byte count = 0; Wire.begin(); for (byte i = 8; i < 120; i++) { Wire.beginTransmission (i); if (Wire.endTransmission () == 0) { Serial.print ("Found address: "); Serial.print (i, DEC); Serial.print (" (0x"); Serial.print (i, HEX); Serial.println (")"); count++; delay (1); // maybe unneeded? } // end of good response } // end of for loop Serial.println ("Done."); Serial.print ("Found "); Serial.print (count, DEC); Serial.println (" device(s)."); } // end of setup void loop() {}

Page 30: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

29

Address ทใชคอ 3F เปด Menu Example > LiquidCrystalI2C > HelloWorld

Page 31: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

30

LAB08-2: Show Text on LCD I2C #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line void setup() { lcd.init(); // initialize the lcd lcd.backlight(); lcd.setCursor(0,0); lcd.print("Hello, world!"); lcd.setCursor(0,1); lcd.print("Max Developer"); } void loop() { }

Page 32: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

31

LAB09: การใช Ultrasonic วดระยะทาง Ultrasonic Ranging Module HC-SR04 โมดลเพอใชในการวดระยะทางโดยไมมการสมผส

การท างานของ Ultrasonic sensor กเหมอนกบคางคาวทบนในเวลากลางคน คอใชการสงคลนเสยงทหมนษยไมสามารถไดยนออกไปสะทอนวตถทตองการวดระยะ แลวจบเวลาเสยงสะทอน เพอค านวณระยะทาง

เพอความเขาใจกนมากขน อปกรณตวนเรมตนท างานโดยการสงสญญาณเรมตนยาว 10 ไมโครวนาท ไปสงใหแหลงก าเนดเสยงท างาน จากนนจะสงคลนเสยงความถ 40 kHz ออกไป 8 พสส แลวรอฟงเสยงสะทอน ตวซายจะเปนตวสงคลนเสยงออกไป สวนตวขวาในรปจะเปนตวรบความถทสะทอนกลบมา เนองจากเสยงทสงออกไปถงแมจะไมไดยนเพราะเกน 20 kHZ ทหมนษยจะรบฟงได แตเนองจากยงคงเปนคลนเสยง ดงนนความเรวของเสยงจงแปรผนตามอณหภมดวยตามสตรน

C ≈ 331.5 + 0.61 θ (m/s)

Page 33: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

32

ดงนนแนนอนเวลาผผลตเขยนโปรแกรมออกแบบไวกอยทอณภมท างานทอาจจะแตกตางจากบานเรา กท าใหคาทวดไดมความผดพลาดไปบาง อกสวนทจะตองรกคอชวงวด และมมทสามารถวดไดครบ และเนองจากคณสมบตของอปกรณทใชในการก าเนดเสยง และรปรางของตวล าโพง (Horn) กท าใหอปกรณตวนมมมวด 15 องศา (Measuring Angle) โดยสามารถวดระยะหางไดตงแต 2 ซม .จนถง 4 เมตร ระยะทางกค านวณไดจากสตรน ระยะทาง =ความยาวของสญญาณสะทอน x 340 (m/s) / 2 และแนนอนครบคณภาพของสญญาณ ความแมนย ากขนกบวสด +ลกษณะของพนผวทใชวดดวย

ระดบแรงดนทใชคอ 5 v

Page 34: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

33

LAB09-1: Basic Ultrasonic #define TRIGGER_PIN 9 // SD2 - GPIO9 #define ECHO_PIN 10 // SD3 - GPI109 void setup() { Serial.begin (9600); pinMode(TRIGGER_PIN, OUTPUT); pinMode(ECHO_PIN, INPUT); pinMode(BUILTIN_LED, OUTPUT); } void loop() { long duration, distance; digitalWrite(TRIGGER_PIN, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(TRIGGER_PIN, HIGH); delayMicroseconds(10); // Added this line digitalWrite(TRIGGER_PIN, LOW); duration = pulseIn(ECHO_PIN, HIGH); distance = (duration/2) / 29.1; Serial.print(distance); Serial.println(" cm"); delay(1000); }

Page 35: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

34

ใช Library LAB09-2: Ultrasonic Library #include <Ultrasonic.h> Ultrasonic ultrasonic(9,10); // (Trig PIN,Echo PIN) void setup() { Serial.begin(9600); } void loop() { Serial.print(ultrasonic.Ranging(CM)); // CM or INC Serial.println(" cm" ); delay(100); }

Code ทปรบใหม LAB09-3: Ultrasonic LCD I2C #include <Wire.h> #include <LiquidCrystal_I2C.h> #include <Ultrasonic.h> LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line Ultrasonic ultrasonic(9,10); void setup() { lcd.init(); // initialize the lcd lcd.backlight(); lcd.setCursor(0,0); lcd.print("Ulteasonic"); lcd.setCursor(0,1); lcd.print("Sensor >");

Page 36: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

35

delay(1000); lcd.print(">"); delay(1000); lcd.print(">"); delay(1000); lcd.print(">"); delay(1000); lcd.print(">"); delay(1000); lcd.print(">"); delay(1000); lcd.print(">"); } void loop() { lcd.clear(); lcd.setCursor(0, 0); lcd.print(ultrasonic.Ranging(CM)); // CM or INC lcd.print(" cm"); lcd.setCursor(0, 1); lcd.print(ultrasonic.Ranging(INC)); // CM or INC lcd.print(" inc"); delay(100); }

Page 37: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

36

LAB10: การใช Motion Sensor PIR Motion Sensor Getting Started PIR (Passive infrared ) คอ อปกรณตรวจจบคลนรงส Infrared จากวตถ ผานอปกรณรวมแสง มายงตว Pyro Electric ซงจะเปลยนพลงงานความรอน จากรงส Infrared เปนพลงงานไฟฟา แมจะมประมาณ Infrared แคเพยงเลกนอย จงท าให PIR สามารถตรวจจบ คลนรงส Infrared และ อณหภมได PIR Motion Sensor คอ อปกรณ Sensor ชนดหนงทใชตรวจจบคลนรงส Infrared ทแพรจาก มนษย หรอ สตว ทมการเคลอนไหว ท าใหมการน าเอา PIR มาประยคใชงานกนเปนอยางมากใชเพอตรวจจบการเคลอนไหวของสงมชวต หรอ ตรวจจบการบกรกในงานรกษาความปลอดภย การท างานของ PIR Sensor

ภายใน PIR จะมอปกรณตรวจจบรงส Infrared อย 2 ชดดวยกนดงรป เมอม คน หรอ สตว ทมความอบอนในรางกายเคลอนทผานเขามาใน พนทโซนท PIR สามารถตรวจจบคลนรงส Infrared ทแพรออกมาจากสงมชวตได PIR จะเปลยนคลนรงส Infrared ใหกลายเปน กระแสไฟฟาดงรป จะเหนวาเมอมสงมชวต เคลอนทผาน อปกรณ

Page 38: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

37

ตรวจจบรงส Infrared ตวท 1 จะไดสญญาณ Output ออกมาสงกวาแรงดนปรกต และ เมอสงมชวตเคลอนทผาน อปกรณตรวจจบรงส Infrared ตวท 2 จะไดแรงดน Output ต ากวาคาแรงดนปรกต

วงจรทใช

Page 39: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

38

LAB10-1: Basic Motion Sensor const int PIRPin = 12; const int ledPin = 2; int PIRState = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(PIRPin, INPUT); Serial.begin(9600); } void loop() { PIRState = digitalRead(PIRPin); if(PIRState == HIGH) { Serial.print("\r\nALARM"); }else{ Serial.print("\r\nNORMAL"); } digitalWrite(ledPin,PIRState); delay (500); }

Page 40: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

39

Code ทปรบแลว LAB10-2: Advance Motion Sensor //the time we give the sensor to calibrate (10-60 secs according to the datasheet) int calibrationTime = 30; //the time when the sensor outputs a low impulse long unsigned int lowIn; //the amount of milliseconds the sensor has to be low //before we assume all motion has stopped long unsigned int pause = 5000; boolean lockLow = true; boolean takeLowTime; int pirPin = 12; // D6 the digital pin connected to the PIR sensor's output int ledPin = 2; // D4 ///////////////////////////// //SETUP void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(pirPin, LOW); //give the sensor some time to calibrate Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){

Page 41: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

40

Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } //////////////////////////// //LOOP void loop(){ if(digitalRead(pirPin) == HIGH){ digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state if(lockLow){ //makes sure we wait for a transition to LOW before any further output is made: lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); } takeLowTime = true; } if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state if(takeLowTime){ lowIn = millis(); //save the time of the transition from high to LOW

Page 42: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

41

takeLowTime = false; //make sure this is only done at the start of a LOW phase } //if the sensor is low for more than the given pause, //we assume that no more motion is going to happen if(!lockLow && millis() - lowIn > pause){ //makes sure this block of code is only executed again after //a new motion sequence has been detected lockLow = true; Serial.print("motion ended at "); //output Serial.print((millis() - pause)/1000); Serial.println(" sec"); delay(50); } } }

LAB11: การวดอณหภม และ ความชนดวย DHT11

DHT11 Humidity and Temperature Sensor กบบอรด NodeMCU ใชในการวดอณหภมกบความชนในอากาศทม Specification ตามน

- ยานวดความชน 20-90% RH โดยมคาความแมนย า +- 5% RH ความละเอยดในการวด 1 % แสดงผลแบบ 8 บต

Page 43: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

42

- ยานวดอณหภม 0 -50 องศาเซลเซยส โดยมคาความแมนย า +- 2 องศาเซลเซยส ความละเอยดในการวด 1 องศาเซลเซยส แสดงผลแบบ 8 บต

- กนกระแส 0.5 - 2.5 mA (ขณะท าการวดคา( ทระดบแรงดน 3 - 5.5 VDC

- อานคาสญญาณ )Sample Rate) ทก 1 วนาท

- การสอสารกบ MCU ของเราดวยวธ Single-wire Two-way Serial interface คอการสอสารแบบอนกรมดวยสายสญญาณเสนเดยว

ในการ install library ของ Arduino IDE เราสามารถไปท Menu Sketch > Include Library > Manage Library และพมพ DHT11 หลงจากนนกดปม install เรากจะได library มาใชไดอยางงายดายหรออกวธนงเราสามารถ install ในลกษณะทเปน zip file ไดอกดวย

Page 44: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

43

ท ำกำรตอวงจรดงภำพ เปด ArduinoIDE และเลอก Menu File > Example > DHT sensor library >

DHTTester แลวน ำ Code มำแกได

Page 45: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

44

LAB11: DHT11 Temperature and Humidity Sensor

#include "DHT.h"

#define DHTPIN 14 // what digital pin we're connected to D5

// Uncomment whatever type you're using! #define DHTTYPE DHT11 // DHT 11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V

// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1

// to 3.3V instead of 5V! // Connect pin 2 of the sensor to whatever your DHTPIN is

// Connect pin 4 (on the right) of the sensor to GROUND

// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor // Initialize DHT sensor. // Note that older versions of this library took an optional third parameter to

// tweak the timings for faster processors. This parameter is no longer needed

// as the current DHT reading algorithm adjusts itself to work on faster procs. DHT dht(DHTPIN, DHTTYPE);

void setup() { Serial.begin(115200); Serial.println("DHT11 test!"); dht.begin(); }

void loop() { delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!

Page 46: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

45

// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Read temperature as Fahrenheit (isFahrenheit = true) float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); return; } // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false);

Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); }

Page 47: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

46

LAB12: ทา NodeMCU เปน Web Server สงเปดปดอปกรณผาน URL

ไปท Menu File > Example > ESP8266 Wifi > WifiWebServer แลวแกไข SSID และ Password ใหตรง

Page 48: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

47

LAB13: ทา NodeMCU เปน Web Client ทดสอบโดยใช Hercules เปน Server

Page 49: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

48

Arduino IDE ไปท Menu File > Example > ESP8266 Wifi > WifiClientBasic แลวแกไข SSID และ Password ใหตรง

Page 50: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

49

การใชงาน Cloud Broker สาหรบงาน IoT

ในการท า IoT นนเราจะใช Cloud เปนตวกลางในการสอสารขอมลระหวาง Node กบ server ซง Cloud ทเปดใหบรการ IoT อยนนสวนใหญจะเปนแคตวกลางในการสอสารหรอบางตวสามารถชวยในการเกบขอมลและแสดงผล โดยทตวมนเองนนจะท าเสมอนเปน broker ซงจะไมมการตดสนใจหรอวาเงอนไขในการท างานใดๆอยทตว broker

นนหมายความวาหากเราตองการใหระบบ IoT ของเรานนมเงอนไขในการท างานอยางเชนอณหภมทตรวจจบจากเซนเซอรวดอณหภมมคามากกวา 30 องศาใหเปดพดลม แตหากนอยกวา 25 องศาใหพดลมหยดท างานการท างานในลกษณะนตว broker ไมสามารถท าไดดงนนเราจงจ าเปนตองม server ส าหรบชวยในการท าเงอนไขตาง ๆ broker เปนเพยงสอกลางในการรบสงขอมลหรออยางดทสดกชวยในการเกบขอมลเพอสงตอและแสดงผลออกมาบน Cloud ไดเลย

LAB14: ทา Data Logger โดยสง Data ไปท SparkFun

SparkFun เปน Broker ทท าหนาทเกบขอมลของตวอปกรณ IoT ตาง ๆ ทสง Data มาเกบไวบน cloud เราสามารถใชงานโดยไมตอง register โดยเขาไปท sparkfun.com และเลอก create

https://data.sparkfun.com/

ใสรายละเอยดในการสราง deta stream ทส าคญคอ Title และ Fields

Page 51: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

50

เมอบนทกเรยบรอยแลวจะเขามาหนาทแสดงคยตางๆทส าคญกคอ public url, public key และ private key รวมถง Field Parameter ทเราสรางเอาไวเพอให node mcu สงมาดวยชอทตรงกน โดยลกษณะการสงนนจะเปนการสงแบบ url Parameter ตามตวอยางทใหมาดานลาง

Page 52: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

51

ในสวนของ Arduino IDE ไปท Menu File > Example > ESP8266WIFI > WIFI Client ใหเราท าการใส ssid และ password ในสวนของ Stream ID ใหใส public key ในสวนของ private key กใสขอมลของ private key และในสวนของ Value ใสใหตรงกบ Parameter ทเราไดสรางเอาไว

Page 53: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

52

หากมการแจงวา success แสดงวาขอมลท node mcu สงไปนนไดอพเดทเขาไปยง spark fun เรยบรอยแลว

Page 54: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

53

เราสามารถเขาไปดขอมลไดจาก public url และขอมลเหลานสามารถ export ออกมาในรปแบบตางๆไดไมวาจะเปน JSON, csv, mysql, ProtgreSQL, atom ซงจะมคาขอมลตางๆพรอมทง TimeStamp

Page 55: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

54

LAB15: วธเอา Data จาก SparkFun ไปแสดงผลท AnalogIO

จากการท SparkFun ไดท าการเกบ data เปนลกษณะ table มนเปนการไมงายเลยทจะน าขอมลเหลานไปใช ดงนนเราจงน า data นไปแสดงผลในรปแบบกราฟโดย SparkFun ไดมอนเตอรเฟสกบทาง AnalogIO ในการน าขอมลเชอมตอและแสดงผลออกมาเปนรปแบบของกราฟทเขาใจไดงาย ในสวนของหนา data stream จะมปมฟงกชน export to analog io

ในสวนของ analog i o จ าเปนทจะตองท าการ register user เพอใชงานในการ manage แตหากเราไมได register เรากสามารถทจะดขอมลทสปารคฟนสงมาใหเปนกราฟไดอยางงาย

Page 56: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

55

เมอท าการ login เขามาแลวเราจะเขามายงหนา manage ของ analog io เราสามารถสราง create streamใหม

Page 57: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

56

และเมอเรา Export มาจาก SparkFun มาท analog IO แลวใหเราเลอก Claim จะขน Dialog มาถามใหใส Private Key เพอยนยน

Page 58: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

57

LAB16: ใช ThingSpeak เปน Data Logger พรอม Plot Graph

Thingspeak เปนเวปทใหการบรการในการเกบขอมล และสามารถแสดงขอมลแบบ real-time ได ซงเราสามารถ update ขอมล หรอจะเรยกดขอมลไดตลอดเวลา ทไหนกได เพราะท างานบน cloud ซง thingspeak สรางมาเพอตองการใหตอบโจทยของ IoT อยแลว สวนขอมลทเกบอยบน cloud นนกขนอยกบเราวาจะใชยงไง รปแบบไหน ในการทจะสงขอมล data ไปไวบน cloud นน ทาง thingspeak ไดม api ในการตดตอไวเรยบรอยแลว

อนดบแรกเลย ใหท าการสมครสมาชกใหเรยบรอย จากนนกสราง channel ขนมา โดยใหเรากดไปท My channel แลวก new channel ขนมา

Page 59: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

58

หลงจากท new channel ขนมาแลว ไปท channel setting กปอนขอมลเขาไป เสรจแลวกกด save channel

เมอสรางเสรจแลว กจะแสดงหนาตาง ในหนาตางนจะแสดงขอมลเปนแบบเสนกราฟ ซงตอนนยงไมมขอมลใด ๆ สงมาจงไมเกดอะไรขน

Page 60: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

59

ตอไปจะท าการ update ขอมลไปยง cloud ผาน api ท thingspeak มไวให อนดบแรกใหดท API KEY ของเราวามนคออะไร API KEY คอ UAV0TAOTBCTLA6V1 ตามรป

ในการ update ขอมลจะเปนการสง HTTP Request ไปยง server เพอ update ขอมลทตองการตาม field ตาง ๆ ทเราก าหนด วธการ update โดยเราจะสงขอมลแบบน

https://api.thingspeak.com/update?key=UAV0TAOTBCTLA6V1&field1=0&field2=0&field3=0

ทขดเสนไวคอ

UAV0TAOTBCTLA6V1 คอ API KEY

bkvalue=0 คอ field ทเราตองการ update ในตวอยางคอ field ท 1 และก าหนดใหมคาเทากบ 0

จากรปตวอยางฝงขวาคอไดท าการ update ขอมล โดยก าหนดให field ท 1 มคาเทากบ 0 และฝงซายนนไดมจดเพมมา 1 จดกคอคาทเราเพมไปเมอซกครนนเอง

Page 61: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

60

ตอมาเรามาเขยนโปรแกรมใหกบ NodeMcu ใหสงขอมล update

LAB16: ThingSpeak Data Logger #include <ESP8266WiFi.h> #include "DHT.h" #define DHTPIN 14 #define DHTTYPE DHT11 int sensorPin = A0; int sensorValue = 0; const char* ssid = "BUAKAEW_AP01"; const char* password = "BuaKaew159357"; const char* host = "api.thingspeak.com"; const char* APIKey = "UAV0TAOTBCTLA6V1";

Page 62: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

61

DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); dht.begin(); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { delay(10000); // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); // Read temperature as Celsius (the default) float t = dht.readTemperature(); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) ) {

Page 63: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

62

Serial.println("Failed to read from DHT sensor!"); return; } 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; } sensorValue = analogRead(sensorPin); // We now create a URI for the request String url = "/update"; url += "?key="; url += APIKey; url += "&field1="; url += h; url += "&field2="; url += t; url += "&field3="; url += sensorValue; 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" +

Page 64: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

63

"Connection: close\r\n\r\n"); unsigned long timeout = millis(); while (client.available() == 0) { if (millis() - timeout > 5000) { Serial.println(">>> Client Timeout !"); client.stop(); return; } } // Read all the lines of the reply from server and print them to Serial while(client.available()){ String line = client.readStringUntil('\r'); Serial.print(line); } Serial.println(); Serial.println("closing connection"); }

Page 65: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

64

Page 66: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

65

LAB17: ใช ThingSpeak เปน ควบคมตว NodeMCU

วธทเราจะควบคม LED นนเราจ าเปนตองมขอมลเพอน าไปเชคเปนเงอนไขวา ถาเกดเปนคานให LED เปด ถาเปนอกคานงให LED ปด ดงนนหากเราตองการสงให LED เปดหรอปดผาน Internet นน เราจงจ าเปนตองมขอมลบน cloud โดยเราจะท าการสง Http request ไปยง server เพอรองขอขอมลทเราตองการ ถาเราสง http request ไดอยางถกตองกจะม http response ตอบกลบจาก server โดยสงมาพรอมกบขอมลทเราตองการ และเรากท าการเขยนโปรแกรมเพอใชคาท server ตอบกลบมานน มาเปนคาในการควบคม LED เพอเปดหรอปดนนเอง

การ get data หรอจะเปนการ update data เราสามารถศกษา หรอด Example ไดจาก Documentation จาก Support ของ ThingSpeak ไดเลย ในสวนของการ get data นนจะใชวธการ get ตามแบบ Example ตวน ซงเปนการดงคาลาสดทเราบนทกขอมลลง database

จะลอง get data มาด ซงจะพมพ url น ลงในชอง search ของ browser

https://api.thingspeak.com/channels/129121/fields/1/last

ทขดเสนใตไวนน 129121 คอชอง channels ของเรานะครบ สวน 1 คอ ตองการดท field ไหน สวนเลอกดท field 1

Page 67: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

66

จากรปขางบนกคอลองเขาไปยง url ดงกลาวผาน web browser และเชอมตอไปยง link นน แลวจะม response ตอบกลบมาเปนเลข 2 นนกคอคาทเราตองการทจะทราบวา คาทบนทกลาสดของ field ท 1 คอคาอะไร กมการ response กลบมาเปนคาดงกลาว

ตวอยางนเปนเพยงการทดสอบดวาสามารถ get คาจาก server ไดหรอไม ตอมาเรากมาดกนวา เราจะเขยนให NodeMcu ของเรานนไป get คามาแลวมาควบคม LED ไดอยางไร

LAB17: ThinkSpeak Control #include <ESP8266WiFi.h>

const char* ssid = "BUAKAEW_AP01"; //ใส SSID WiFi ทตองการเชอมตอ

const char* password = "BuaKaew159357"; //ใส password ของ WiFi ทตองการเชอมตอ

const char* host = "api.thingspeak.com"; // ชอ host ทตองการตดตอ

const char* url = "/channels/129121/fields/1/last"; // path ทตองการเรยก

int lengthData = 0; //ตวแปรนจะเกบขนาดของขอมลททาง Server มการ response กลบมา

int data = 0; // ตวแปรนจะเกบคาขอมลทเราตองการใชงาน

String line = ""; // ตวแปรนจะเกบคาทงหมดทอานไดจาก Server

int led1 = 14; //ก าหนดขา GPIO OUTPUT ซง pin นจะตรงกบ D1

const int PORT = 80; // ใช port 80

WiFiServer server(80); // สราง object จาก คลาส WiFiServer ก าหนด port 80

void setup() { Serial.begin(115200); //ใช buadrate 115200

delay(10); pinMode(led1,OUTPUT); // ก าหนด ขา ใหเปน ขา OUTPUT

Serial.println("\n\n"); Serial.print("Connecting to ");

Page 68: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

67

Serial.println(ssid); // แสดงขอความออกทาง Serial วา Connecting to (SSID ทเชอมตอ( WiFi.begin(ssid, password); // เชอมตอ wifi จาก SSID และ password ทเราก าหนด

while (WiFi.status() != WL_CONNECTED){ // ถาหากยงเชอมตอกบ wifi ยงไมไดกจะแสดงขอความ ... ไปเรอย ๆ

delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi Connected!!"); Serial.print("IP address : "); Serial.println(WiFi.localIP()); // แสดง IP address ทไดรบจาก wifi ทเราเชอมตอ

}

void loop() { delay(5000); //delay ไว 5 ว Serial.print("Connecting to "); Serial.println(host); //แสดงขอความทาง Serial วา Connecting to (host ทตองการเชอมตอ( WiFiClient client = server.available(); // สราง client ขนมาจาก WiFiClient และก าหนดใหเทากบ Server.available() เพอไวเชคเวลามขอมลสงกลบมา

Serial.print("Requesting URL : "); Serial.println(url); // แสดง url ทเรา request ไป

if(!client.connect(host,PORT)){ Serial.print("."); //ในเงอนไขนเชควาถาไมไดเชอมตอกบ host อยใหแสดง . ออกมา

return; }else{ // และถาหากเชอมตออยใหสง http request ไปท server

Page 69: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

68

client.print(String("GET ")+url+" HTTP/1.1\r\n" +

"Host: " + host +"\r\n" +

"Connection: close\r\n\r\n"); delay(50); Serial.println("SEND !!"); // เมอสงไปแลวใหแสดงขอความ SEND !! ออกมา

} while(!client.available()){ //ถาหากไมม response ตอบกลบมา กจะใหวนลป จนกวาจะม responce ตอบกลบมา

} while(client.available()){ // และเมอม response ตอบกลบมากจะใหเขา loop while แลวท าการเกบคาทงหมดไวท line

line += (char)client.read(); } Serial.println(); Serial.println("GET SUCCESSFULLY !!"); // เมอท าการเกบคาไวหมดแลวกจะแสดง GET SUCCESSFULLY ออกมา

convertDataToInt(&lengthData , &data ,line); // ใน function นเขยนขนมาเพอตดขอความบางสวนออก เอาเฉพาะทเราจ าเปนตองใชงานจรง ๆ กจะม lengthData และ data

String s =(String)"ID "+lengthData+" data "+data; // สรางตวแปร String มาดผลลพธวาขอมลทไดจาก Server Serial.println(s); // แสดงขอมลทผานการ filter เรยบรอยแลว

switch(data){ // แลวกตอมาจะเปนเงอนไขกนเรากเชคกบตวแปร data วาเปนคาอะไรบาง เพอทเราจะเอามาท าเปนเงอนไขในการเปดปด LED

case 1: digitalWrite(led1,HIGH); // ถาเปน 1 led1 ตด

Page 70: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

69

break; case 0: digitalWrite(led1,LOW);// ถาเปน 2 led1 ดบ

break; } line = ""; // เมอจบ process ทกอยางแลว กเคลยขอมลในตวแปร line ใหเทากบ "" เพอรอรบคาใหมใน loop หนา

Serial.println(); Serial.println("Closing Connection"); //แสดงขอความ Closing Connection

}

void convertDataToInt(int* id_ ,int* data_ ,String rawData){ // พารามเตอร กจะม pointer id_ , pointer data_ , คาตวแปรดบทอานไดจากท server response กลบมา

//Serial.println(rawData); // ลบ comment ออก ถาตองการดวาคาท response กลบมามอะไรบาง

String dataSub = rawData.substring(rawData.indexOf("close")+9); //สรางตวแปรมาเพอ substring เฉพาะขอมลทเราตองการใชจรง ๆ

Serial.println(dataSub.length()); // ลบ comment ออก ถาตองการดขนาดขอมลหลงจากทเรา substring จากขอมลทงหมดแลว

Serial.println(dataSub); //ลบ comment ออก ถาตองการดวาขอมลทเรา substring ออกมามขอมลเปนอยางไร

*id_ = dataSub.substring(0,2).toInt(); // ท าการแปลงขอมลจาก String เปน int ไปเกบขอมลไวทตวแปร pointer เพอเปลยนคาใหกบตวแปรทเราอางอง กคอตวแปร lengthData นนเอง

*data_ = dataSub.substring(3 , 3 + *id_).toInt(); // ท าการแปลงขอมลจาก String เปน int ไปเกบขอมลไวทตวแปร pointer เพอเปลยนคาใหกบตวแปรทเราอางอง กคอตวแปร data

data=dataSub.substring(0,1).toInt(); }

เปนการ get คาจาก Server โดยสง http request ขอดในขอมลลาสดของ field 1 ถาสง request ถกตอง Server กจะสง http response กลบมาเปนคาทเราตองการ เมอไดคาทตองการแลวกท าการ substring กคอตดเอาขอความเอาเฉพาะทจ าเปนมาใชงาน ตามทอธบายไวแตละบรรทดทส าคญ

Page 71: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

70

เมอเรา update คาเปน 0

ผลทไดจาก last ของ field 1 กจะเปน 0 และ LED จะดบ

เมอเรา update คาเปน 1

ผลทไดจาก last ของ field 1 กจะเปน 1 และ LED จะดบ

Page 72: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

71

NETPIE

“NETPIE แพลตฟอรม IoT เพอนกพฒนาและอตสาหกรรมไทย” โดยทมพฒนาของ Nectec ซงเปนแพลตฟอรมทางเลอกแรกของนกพฒนาไทยทเชอมอปกรณและเครองมอตาง ๆ หรอ The Internet of Things (IoT) ระยะแรกเนนการสนบสนนนกพฒนาและอตสาหกรรมขนาดยอม)SMEs) เพอสรางขดความสามารถและความเขมแขงใหกบอตสาหกรรมไทยขนาดใหญของไทย ซงเปน Platform ทเราสามารถใชฟรถง 100 อปกรณตอ Account และ Server อยทเมองไทยมาลองเลน NetPIE ไทยท า ไทยใช กน

Download Microgear https://github.com/netpieio/microgear-esp8266-arduino

จะไดเปน zip file ออกมาใหเรากนสะตอเขาไปใน library กอนเรมเราตองสมคร NETPIE กอน การสราง Application (แอพพลเคชน )เปนต วแทนของระบบของเรา (เชน "ระบบสมารทโฮมในบาน)" ในระบบของเรากจะสามารถ เพมอปกรณเขาไปได โดยอปกรณของเรานนจะถกเรยกวา "Gear" (ปจจบนใชค าวา Things) ซงอปกรณทวานนกอยางเชน ตว NodeMCU ทเราก าลงจะท าเปนตวควบคมสวชตไฟ หรอพวกบอรดอนๆทมการตอเซนเซอรส าหรบสงขอมลคาตางๆ เชนอณหภมและความชน เปนตน ใน Application หนงอาจจะมหลายๆ อปกรณ (หลายๆ Gear กได )และแตละ Gear ภายในระบบเดยวกน (ภายใน Application เดยวกน )กจะสามารถสอสารกนเองระหวางอปกรณได

Page 73: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

72

และแตละอปกรณ (แตละ Gear) กจะม Key เปนของตวเอง ซง Key นกจะเปนเหมอนสงทใชระบตวตนของอปกรณเรา (คลายๆกบเลขบตรประจ าตวประชาชน) แตละ Gear กจะม Key เปนของตวเอง และกจะไมเหมอนกบของ Gear อนๆ

เมอเขามาใน Application แลว ท าการสราง Application เชน MaxSmartHome

จากนนกจะปรากฏหนาตางส าหรบการสราง Key ขนมา กใหท าการตงชอใหอปกรณของเรา เชน ตงชอเปน SmartNode1 (ประมาณวากลองสวชตเปดปดไฟของเราชอ SmartNode ตวท 1) แลวกท าการเลอก Type ใหเปน Session Key จากนนกกด CREATE เลยครบ

Page 74: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

73

ทนกใหกดท Key ทสรางไว กจะปรากฏขอมล Key ขนมาหากจะเปลยนชอของอปกรณ กพมพชอใหมดานบน แลวกกดปม RENAME

ขอมลทเราจะน าไปใชกจะม Key กบ SecretKey

Page 75: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

74

LAB18: เปดปดไฟดวย NETPIE ผานเวบ HTML5

Code NodeMCU

LAB18: HTML5 Control by NETPIE

#include <AuthClient.h> #include <MicroGear.h> #include <MQTTClient.h> #include <SHA1.h> #include <Arduino.h> #include <ESP8266WiFi.h> #include <EEPROM.h> const char* ssid = "BUAKAEW_AP01"; const char* password = "BuaKaew159357"; #define APPID "MaxSmartHome" #define GEARKEY "l9kQv3Qhj8khNga" #define GEARSECRET "Pv9nPFA87hnHuIKqgkq2fvB16" #define SCOPE "MaxPlug1" WiFiClient client; AuthClient *authclient; int relayPin = D4; MicroGear microgear(client); void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { Serial.print("Incoming message --> "); Serial.print(topic); Serial.print(" : "); char strState[msglen]; for (int i = 0; i < msglen; i++) { strState[i] = (char)msg[i]; Serial.print((char)msg[i]); }

Page 76: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

75

Serial.println(); String stateStr = String(strState).substring(0, msglen); if (stateStr == "ON") { digitalWrite(relayPin, LOW); microgear.chat("controllerplug", "ON"); } else if (stateStr == "OFF") { digitalWrite(relayPin, HIGH); microgear.chat("controllerplug", "OFF"); } } void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { Serial.println("Connected to NETPIE..."); microgear.setName("pieplug"); } void setup() { Serial.begin(115200); Serial.println("Starting..."); pinMode(relayPin, OUTPUT); microgear.on(MESSAGE,onMsghandler); microgear.on(CONNECTED,onConnected); if (WiFi.begin(ssid, password)) { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //uncomment the line below if you want to reset token -->

Page 77: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

76

microgear.resetToken(); microgear.init(GEARKEY, GEARSECRET, SCOPE); microgear.connect(APPID); } } void loop() { if (microgear.connected()) { microgear.loop(); Serial.println("connect..."); } else { Serial.println("connection lost, reconnect..."); microgear.connect(APPID); } delay(1000); }

หลงจากท NodeMCU Connect กบ NETPIE แลว จะม Key ขนในหนาของ Application

ในสวนของ HTML5 แกไฟล smartplug.html แลวกท าการเอา KEY กบ SECRET และ APP ID ทเราไปสรางไวใน NETPIE มาใสลงไป

Page 78: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

77

เสรจแลวกกด File > Save เลยครบ ทส าคญคอ นนคอ Microgear Library (ของฝง HTML 5) ซงไฟลนจะตองแยกมาดาวนโหลดในภายหลง เนองจากทาง NECTEC จะมการพฒนาและแกไขปรบปรง Library นอยเรอยๆกใหท าการ Download ไฟล Microgear Library ส าหรบ HTML5 จากลงคดานลางน มาลงไวในโฟลเดอรทมไฟล HTML https://github.com/netpieio/microgear-html5 ผลทได คอ สามารถเปดปดไฟบน Web โดยผาน NetPie

Page 79: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

78

LAB19: NETPIE Freeboard Freeboard นนเปน Web Application ทท าใหเราสามารถสราง Dashboard (แดชบอรด )ส าหรบระบบ IoT ของเราขนมาได ถาจะใหพดงายๆ กคอคลายๆกบการสราง กระดาน IoT สวนตวทเราสามารถวาง

ปมกด,สวชต ไวใชส าหรบควบคมอปกรณ หรอวางหนาปดไวส าหรบแสดงผลขอมลตางๆทไดจากอปกรณในระบบ IoT ของเรา นอกจากนยงสามารถน าขอมลทไดมาไปแสดงผลเปนกราฟและสามารถปรบแตงไดตามใจชอบไดงายๆ เพยงแค คลก -ลาก-วาง และมการปอนขอมลหรอค าสงอกนดหนอย กสามารถท างานไดแลวครบคองายมากๆ ไมตองเสยเวลานงงมเขยนเปน HTML Webpage ใหวนวาย ทส าคญคอขอมลนนมการอพเดทแบบ real-time มความเสถยรและเชอถอได และเปน Open-Source ซงท าใหนกพฒนาสามารถตอยอดใหดยงขนไดอกดวย

Page 80: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

79

STEP1: ดาวนโหลดและตดตง NETPIE Freeboard ส าหรบตว NETPIE Freeboard (และ Freeboard อนๆ )นนจะมระบบไฟลแบบเดยวกบเวบเพจตางๆ

คอจะมไฟลคอนขางเยอะหนอย แตถกจดเกบไวอยางเปนระบบในโฟลเดอรตางๆ ซงไฟลสวนใหญนนกจะประกอบดวยไฟลเวบเพจทเปน html และไฟลโคด Javascript ตางๆ โดยไฟลทงหมดทมงาน NETPIE ไดอพขนไวท Github สามารถเขาไปดาวนโหลดไดตามลงคดานลางนเลย https://github.com/netpieio/netpie-freeboard STEP2: ท าความเขาใจกบ Freeboard Datasource เนองจาก NETPIE Freeboard นนจะเปนหนาเพจทแสดงขอมลตางๆ ซงตวอปกรณ (Things) ของเราภายใน AppID เดยวกนนนจะมการ chat เพอสงขอมล (เชนคาอณหภม/คาตางๆจากเซนเซอร) หรอสงค าสง

(เปด/ปด )หากน และกน ซงตว Freeboard เองกตองท าตวใหเปรยบเสมอนวาเปน Things ตวหนง ท Things อนๆ สามารถ chat สงขอมลมาหาไดเชนกน เพอทจะไดน าขอมลเหลานนมาแสดงผลบน Dashboard ของเราตามทเราตองการได ซงเราจะตองสราง Datasource ขนมา เพอเปนตวกลางของการสอสารรบสงขอมล (เปนตวแทนของ Freeboard) ซงส าหรบคนทใช NETPIE อยแลว กสามารถสราง Datasource แบบ NETPIE

Microgear ไดเลย (โดยใช Key จาก NETPIE) หรอส าหรบใครทใชงาน Datasource แบบอนกสามารถเพมไดเชนกบ โดย NETPIE Freeboard นนรองรบ Datasource หลากหลายรปแบบเลย ทง JSON, Open Weather Map, Dweet.io, Playback, Clock และ Octoblue แตในทนผมจะขอพดถง Datasource แบบของ NETPIE Microgear

Page 81: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

80

ซงในการเพม Datasource แบบ NETPIE Microgear นน ใหท าการสราง Key ใหมขนมาใหมใน AppID ทเราตองการให Freeboard แสดงผลและรบสงขอมล (AppID ทม Things อนๆอยดวย )เพอน า Key และ Secret ไปใสใน Freeboard สราง Gear ของ Datasource

บนทกคา Key เกบไว

เปดไฟล index.html เลอก add datasource

กใหน าขอมลจาก Key ส าหรบ Datasource ทเราสรางไวใน NETPIE มากรอกลงไปและท าการตงชอ Device Alias และ Microgear Reference ไวใชกบค าสงตางๆดวย (Device Alias จะใชเปนเหมอนชอเรยกเวลาเราตองการ chat ไปหา Datasource ผานค าสงตางๆ สวน Microgear Reference เปนชออางอง Microgear ครบ เพราะ Datasource จะท าตวเหมอนกบวาเปน Things หนงๆ (ท าตวเปน Microgear ตวหนงแตไมไดมตวตนจบตองไดจรงๆ) ซงเปนชอท Freeboard จะใชอางถงตวเอง เวลาจะค าสงสงขอความหนงๆ โดยจะมรปแบบน

Page 82: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

81

microgear["ชอ Microgear Reference"].chat("ชอผรบ)Alias)","ขอความ"( เชน ผมตองการใช Freeboard ของผม ซงกรอกขอมลทกอยางดงรปทผานมาไปสงเปดไฟหนาบานของ โดยทชดเปดปดไฟของผมม Alias เปน smartnode1 กจะใชค าสงนบน Freeboard microgear["freeboard_mg1"].chat("smartnode1","ON")

และเมอกรอกขอมลเรยบรอยแลว กใหกด Save ดานลางเลย

หลงจากนนเรากจะเหน Datasource ทเราสรางขนปรากฏในรายชอ Datasource ใน Freeboard หนงจะใชหลายๆ Datasource กได ไมไดจ ากดวาตองใชแค Datasource เดยวโดยหลงชอ Datasource จะแสดงขอมล Last Updated ซงจะแสดงเวลาทมการอพเดทขอมลลาสด (ในตอนน Datasource ของเรายงไมมการรบสง chat ครบเลยยงเปน never อย) และจะมปม Refresh ไวส าหรบรขอมลใหม และปมถงขยะไวส าหรบลบ Datasource นทงไป

Page 83: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

82

Page 84: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

83

LAB20: เปดปดไฟผาน NETPIE Freeboard ท าการสราง KEY ชอ TestLED ไวใน NETPIE และท าการน ามาใสคาในสวนทผมคอมเมนตไว (7 บรรทดนน )ใหถกตองจากนนกโหลดโคดลงบนบอรดไดเลย LAB20: ON/OFF by NETPIE Freeboard #include <AuthClient.h> #include <MicroGear.h> #include <MQTTClient.h> #include <SHA1.h> #include <Arduino.h> #include <ESP8266WiFi.h> #include <EEPROM.h> const char* ssid = "BUAKAEW_AP01"; //== 1 == ใสชอเครอขาย WiFi ทจะให NodeMCU V2 เชอมตอ const char* password = "BuaKaew159357"; //== 2 == ใสรหสผานของเครอขาย WiFi ทจะให NodeMCU V2 เชอมตอ #define LEDPin D4 //== 3 == หากตอหลอด LED ทขาอนทไมใช D2 ใหท าการเปลยนชอขาตรงนดวย #define APPID "MaxSmartHome" //== 4 == ใส Application ID (ชอ Application) ทสรางบน NETPIE #define GEARKEY "l9kQv3Qhj8khNga" //== 5 == ใส Key ของอปกรณ ทไดจากการสรางบน NETPIE #define GEARSECRET "Pv9nPFA87hnHuIKqgkq2fvB16" //== 6 == ใส Secret ของอปกรณ ทไดจากการสรางบน NETPIE #define ALIAS "testled" //== 7 == ตงชอ Alias ใหกบ Things น (เปนชอเรยกเวลา chat มาหา) #define SCOPE "" WiFiClient client; AuthClient *authclient; MicroGear microgear(client);

Page 85: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

84

void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { Serial.println("Connected to NETPIE..."); microgear.setAlias(ALIAS); } void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { Serial.print("Incoming message --> "); Serial.print(topic); Serial.print(" : "); char strState[msglen]; for (int i = 0; i < msglen; i++) { strState[i] = (char)msg[i]; Serial.print((char)msg[i]); } Serial.println(); String stateStr = String(strState).substring(0, msglen); //=========== ชวงประมวลผลค าสง ============= if (stateStr == "ON") { digitalWrite(LEDPin, HIGH); microgear.chat("testledstatus", "ON"); //==== ค าสง chat เพอบอกสงคาสถานะไปยงหลอด LED บน NETPIE Freeboard } else if (stateStr == "OFF") { digitalWrite(LEDPin, LOW); microgear.chat("testledstatus", "OFF"); //==== ค าสง chat เพอบอกสงคาสถานะไปยงหลอด LED บน NETPIE Freeboard } //=========== ชวงประมวลผลค าสง ============= } void setup() { Serial.begin(115200);

Page 86: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

85

Serial.println("Starting..."); pinMode(LEDPin, OUTPUT); microgear.on(MESSAGE,onMsghandler); microgear.on(CONNECTED,onConnected); if (WiFi.begin(ssid, password)) { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); //uncomment the line below if you want to reset token --> microgear.resetToken(); microgear.init(GEARKEY, GEARSECRET, SCOPE); microgear.connect(APPID); } } void loop() { if (microgear.connected()) { microgear.loop(); Serial.println("connect..."); } else { Serial.println("connection lost, reconnect..."); microgear.connect(APPID); } delay(1000); }

เมออพโหลดลงแลว กใหลองเปด Serial Monitor (เลอก Baud Rate เปน 115200) ดวาบอรดของเรานนเชอมตอเรยบรอยหรอไม ตามรปดานลางน

Page 87: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

86

ถาหากไมมปญหาอะไรแลวกเปนอนวาเรยบรอย เพยงเทานเรากจะได Thing ทมหลอด LED หนงหลอด และพรอมทจะทดลองการเปดปดผาน NETPIE Freeboard แลว ในสวนของ Freeboard ใหท าการเปด Freeboard ทเราสราง Datasource ไวใน Part แรกขนมาจากนนกใหกดปม ADD PANE ตามภาพน

สราง Widget เปนปมเปดปด

Page 88: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

87

โดยคาตาง ๆ ไดจาก

Page 89: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

88

เราสามารถควบคมการเปดปดไฟดวยปมบน Freeboard ได

Widget ทนาสนใจอกตวคอ Toogle

มการก าหนดคาตามน

Page 90: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

89

Widget ทนาสนใจอกตวคอ Text

มการก าหนดคาตามน

Widget ทนาสนใจอกตวคอ Indicator Light

Page 91: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

90

LAB21: วดอณหภม ความชน สงคามาแสดงบนฟรบอรด หลงจาก Part ทแลวเราไดลองเปดปดหลอดไฟ (LED) ผาน NETPIE กนแลว ซงนนเปนรปแบบของการสงขอมลสถานะ "เปด "หรอ "ปด "ระหวาง Freeboard ของเรากบอปกรณ (Things) นน คาดวาหลงจากไดทดลองลงมอท าดจรงๆแลวคงท าใหเขาใจระบบการท างาน ของ NETPIE Freeboardกนมากขนแลว และใน Part นจะเปนการทดลอง สงคา "อณหภม "และ "ความชน " มาแสดงบน Freeboard ขอเรยกการทดลองนวา "SmartSensor" กอนอนกใหสราง Key ส าหรบอปกรณนบน NETPIE กอนเชนของผมสรางไวภายใน AppID ชอ MaxSmartHome โดยสรางเปน Device Key ชอวา SmartSensor

Page 92: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

91

LAB21: NETPIE Freeboard Read From Sensor DHT11 #include "DHT.h" #include <AuthClient.h> #include <MicroGear.h> #include <MQTTClient.h> #include <SHA1.h> #include <Arduino.h> #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <EEPROM.h> const char* ssid = "BUAKAEW_AP01"; //ชอ SSID ของ WiFi ทจะเชอมตอ const char* password = "BuaKaew159357"; //รหสผานของ WiFi ทจะเชอมตอ #define APPID "MaxSmartHome" //ใส APPID ทสรางไวใน NETPIE #define KEY "8UqxbpczvuU25WK" //ใส KEY ทสรางไวส าหรบ SmartSensor #define SECRET "C0bbEbmZiGbDLCDgE578tBS9C" //ใส SECRET ของ KEY ทสรางไว #define ALIAS "SmartSensor" //ตงชอใหกบอปกรณน (เชนตอนนผมตงชอวา )"SmartSensor" #define DHTPIN D5 //ระบขาทตอกบเซนเซอร DHT #define DHTTYPE DHT11 //ระบวาใช DHT11 หรอ DHT22 (ในทนผมใชเปน DHT22) #define RefreshTime 100 //คาความถของการสงขอมล เชนในทนคอ สงขอมลออกไปทกๆ 0.1 วนาท (100 ms) WiFiClient client; AuthClient *authclient; DHT dht(DHTPIN, DHTTYPE); int timer = 0; MicroGear microgear(client);

Page 93: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

92

void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { Serial.println("Connected to NETPIE..."); //หากการเชอมตอส าเรจ ใหแสดงขอความนทาง Serial Monitor } void setup() { Serial.begin(115200); Serial.println("Starting..."); dht.begin(); microgear.on(CONNECTED,onConnected); microgear.init(KEY,SECRET,ALIAS); microgear.connect(APPID); if (WiFi.begin(ssid, password)) { while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } } void loop() { if (microgear.connected())

Page 94: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

93

{ microgear.loop(); timer=0; float tempread = dht.readTemperature(); //เกบคาอณหภมทอานไดไวเปนแบบ float ไวในตวแปร "tempread" float humidread = dht.readHumidity(); //เกบคาความชนทอานไดไวเปนแบบ float ไวในตวแปร "humidread" char temp[10]; // char humid[10]; // สรางตวแปรไวใชสงขอมลทาง microgear.chat (ตองใชรปแบบ char) if (isnan(tempread) || isnan(humidread) || tempread > 100 || humidread > 100) { tempread = 0.0; // ถาอานคาไมได หรออานคาไดมากกวา 100 humidread = 0.0; // ใหแสดงคาเปน 0 แทน } int tempread_decimal = (tempread - (int)tempread) * 100; // เกบสวนของตวเลขทอยหลงจดทศนยม ไปเปนแบบ int int humidread_decimal = (humidread - (int)humidread) * 100; // เชน 35.87 จะเกบ 87 ไวในตวแปร sprintf(temp,"%d.%d", (int)tempread,tempread_decimal); // ยดคาตวแปร int เขาไปในตวแปร char โดยมรปแบบ sprintf(humid,"%d.%d", (int)humidread,humidread_decimal); // %d.%d = สวนจ านวนเตม.

สวนทศนยม (จะ chat ตวแปรนไป) Serial.print("temp = "); Serial.print(temp); Serial.print(" Hum = "); Serial.println(humid); microgear.chat("SmartSensor/Temperature",temp); // ท าการ chat ตวแปรตางๆ ออกไป microgear.chat("SmartSensor/Humidity",humid); // โดยใชชอผรบเปน "SmartSensor/Temp,Humid,LightLevel" }

Page 95: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

94

else { Serial.println("connection lost, reconnect..."); microgear.connect(APPID); delay(timer); timer+=100; } delay(RefreshTime); // รอเวลาเปนจ านวนเทากบทตงไวในตวแปร RefreshTime }

แสดงผลดงรป Serial

ในสวนของ Freeboard เรา Add Widget Gauge

ในสวนของ Datasource ก าหนดเปน datasources["Datasource"]["/MaxSmartHome/gearname/SmartSensor/Temperature"]

Page 96: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

95

และ Humidity เชนกน

LAB: เขยนโปรแกรมควบคมผานมอถอดวย APP Inventor

LAB: Facebook BOT API chat NETPIE IoT

Page 97: NodeMCU for Internet of thing - staff.informatics.buu.ac.thnutthanon/887496/NodeMCU4IoT.pdf · Basic NodeMCU for Internet of Things (IoT) 2 LAB16: ใช้ ThingSpeak เป็น

Basic NodeMCU for Internet of Things (IoT)

96

Reference

บทความ Arduitronics (http://www.arduitronics.com/article) บทความ ThaiEasyElec (http://www.thaieasyelec.com/article-wiki-th/electronic-article.html) NETPIE (https://netpie.io/)