Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public...

78
โครงการจัดอบรมพัฒนาความรู ้ทักษะด้าน Internet of Things ได้รับการสนับสนุนจาก งบประมาณแผ่นดิน ปี 2560 ตามแผนบูรณาการการพัฒนาคนตลอดช่วงวัย 1 Outline Internet of Thing Definition, IoT Architecture Arduino Platform Digital & Analog Input Output LCD, I 2 C , Ethernet Internet of things Protocol MQTT Protocol , Websocket Building IoT with Arduino NodeMCU Basic for NodeMCU Building IoT with NodeMCU Raspberry Pi Basic for Raspberry Pi MQTT Broker Local Broker & Public Broker 2

Transcript of Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public...

Page 1: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

โครงการจดอบรมพฒนาความรทกษะดาน Internet of Things

ไดรบการสนบสนนจาก งบประมาณแผนดน ป 2560ตามแผนบรณาการการพฒนาคนตลอดชวงวย

1

Outline

• Internet of Thing– Definition, IoT Architecture

• Arduino Platform– Digital & Analog Input Output– LCD, I2C , Ethernet 

• Internet of things Protocol– MQTT Protocol , Websocket– Building IoT with Arduino

• NodeMCU– Basic for NodeMCU– Building IoT with NodeMCU

• Raspberry Pi– Basic for Raspberry Pi– MQTT Broker– Local Broker & Public Broker

2

Page 2: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

What is Internet of Things

นยามของ Internet of Things ทเผยแพรในเอกสารของ IEEE เมอ 27 May 2015

(IEEE_IoT_Towards_Definition_Internet_of_Things_Revision1_27MAY15.pdf)

“An IoT is a network that connects uniquely identifiable “Things” to the Internet. The “Things”  have sensing/actuation and potential programmability capabilities. Through the exploitation of  unique identification and sensing information about the “Thing” can be collected and the state  of the ‘Thing’ can be changed from anywhere, anytime, by anything.“

Page 3: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

What is Internet of Things

Internet of Things features– Interconnection of Things

– Connection of Things to the Internet

– Uniquely Identifiable Things

– Ubiquity

– Sensing/Actuation capability

– Embedded intelligence

– Interoperable Communication Capability

– Self configurability

– Programmability

เทคโนโลยการเชอมตอของสรรพสง

• Internet of Things หรอ IoT คอ สภาพแวดลอมอนประกอบดวยสรรพสงทสามารถสอสารและเชอมตอกนไดผานโพรโทคอลการสอสารทงแบบใชสายและไรสาย โดยสรรพสงตางๆ มวธการระบตวตนได รบรบรบทของสภาพแวดลอมได และมปฏสมพนธโตตอบและทางานรวมกนได

• IoT จะเปลยนรปแบบและกระบวนการผลตในภาคอตสาหกรรมไปสยคใหม หรอทเรยกวา Industry 4.0 ทจะอาศยการเชอมตอสอสารและทางานรวมกนระหวางเครองจกร มนษย และขอมล เพอเพมอานาจในการตดสนใจทรวดเรวและมความถกตองแมนยาสง

• โดยเทคโนโลยททาให IoT เกดขนไดจรงและสรางผลกระทบในวงกวางได แบงออกเปนสามกลมไดแก 1) เทคโนโลยทชวยใหสรรพสงรบรขอมลในบรบททเกยวของ เชน เซนเซอร 2) เทคโนโลยทชวยใหสรรพสงมความสามารถในการสอสาร เชน ระบบสมองกลฝงตว รวมถงการสอสารแบบไรสายทใชพลงงานตา อาท Zigbee, 6LowPAN, Low‐power Bluetooth 3) เทคโนโลยทชวยใหสรรพสงประมวลผลขอมล ในบรบทของตน เชน เทคโนโลยการประมวลผลแบบคลาวด และเทคโนโลยการวเคราะหขอมลขนาดใหญ หรอ Big Data Analytics 

ทมา : แผนพฒนาดจทล กระทรวงดจทล

Page 4: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

The IoT: Different Services, Technologies, Meanings for Everyone   [1]

Source: http://www.mouser.com/applications/iot‐reality/?cm_mmc=PressRelease‐PR‐_‐Mouser‐_‐IoT_Site_Update‐_‐2015‐07‐01

IoT Layer Architecture

ทมา ITU‐T  Y.2060

Page 5: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• The device layer lie devices (sensors, actuators, RFID devices) and gateways used to collect the sensor readings for further processing 

• The network layer provides the necessary transport and networking capabilities for routing the IoT data to processing places. 

• The support layer is a middleware layer that serves to hide the complexity of the lower layers to the application layer and provide specific and generic services such as storage in different forms (database management systems and/or cloud computing systems) and many other services such as translation.

• The application layer contains IoT applications.

IoT Layer Architecture

The Internet of Things Protocol stack – from sensors to business value

Souce:  https://entrepreneurshiptalk.wordpress.com/2014/01/29/the‐internet‐of‐thing‐protocol‐stack‐from‐sensors‐to‐business‐value/

Page 6: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• Connectivity layer: What kind of physical connectors you 

can find. RS‐232, RS‐485, ModBus, USB (as a connector type, not the communication protocol), SPI, ODB2 (in Cars)

• Link Protocol: How do those device actually send the data. Ethernet 802.3, Wifi 802.11a/b/g/n, BlueTooth, BLE, Dash 7, ZigBee, RFid, GSM, 6LoWPAN, 802.14.5e.

• Transport: IPv4 , IPv6 and also added 6LoWPAN and RPL despite the fact that they are both based on Ipv6. 

The Internet of Things Protocol stack – from sensors to business value

• Session / Communication: This section is an interesting bunch with a lot of new protocols that have been build for super high volumes and large networks of things. The most famous right now is MQTT, also find CoAP (kind of a REST Based protocol but much more efficient than its HTTP counterpart), DDS, XMPP and AMQP that are all well suited for IoT and will map different use cases.

• Data Aggregation / Processing: This is where it gets really interesting. When device send data, lots of data, you need an end point to do something with it. Be it processing it in real time (with Storm), but at minimum getting the data and sending it somewhere else at very large scale, which Kafka is a great example of. Other solution exists like RapidMQ, Scribe, Plume, Luxun.

The Internet of Things Protocol stack – from sensors to business value

Page 7: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• Data Storage / Retrieval: The realm of Big Data backend and NoSQL solutions. Hadoop, HBase, MongoDB and Cassandra dominate the field.

• Business Model: This layer is trying to capture the fact 

that business value and business processes always rely on an underlying business model.  Open or Closed, Integrated or platform, direct sales or indirect, cloud based or on‐premise (or private cloud), on‐demand pricing, etc.

The Internet of Things Protocol stack – from sensors to business value

IoT Smart‐X Application

Page 8: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Smart Energy

Smart Transport

Page 9: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Smart building

Smart Health

Page 10: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.1 Basic Components of computer

• องคประกอบของคอมพวเตอร– หนวย input/output

• จอ monitor

• Mouse, keyboard

• Port usb, serial, parallel

– หนวยประมวลผล• Central Processor Unit

– หนวยความจา• Harddisk

• RAM

Page 11: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Microcontrollers

A single chip

CPUROM     RAM

Input/Output

Subsystems:Timers, Counters, AnalogInterfaces, I/O interfaces

Memory

• Microcontroller มาจากคาวา “Micro” ทแปลวาเลก ๆ รวมกบคาวา “Controller” ซงหมายถง ตวควบคมหรออปกรณควบคม ดงนน Microcontroller หมายถง อปกรณควบคมทมขนาดเลก

ซงตว microcontroller เปรยบเสมอนกบเครองคอมพวเตอรขนาดยอมๆ เพราะวา ประกอบดวย CPU, memory และ Port ตางๆ ทรวมกนใน IC ตวเดยว

22

1. CPU (หนวยประมวลผลกลาง) : ทาหนาทประมวลผลทกอยางเหมอนกบ CPU ในคอมพวเตอร

2. Memory (หนวยความจา): มหนาทในการเกบขอมลตางๆ แบงออกเปนสองสวน คอ Program Memory และ Data Memory  โดยท Program Memory นจะเกบขอมลหลกของโปรแกรมเอาไว เสมอน Harddisk ของเครองคอมพวเตอร แต Data Memory หรอ RAM เปนทพกขอมลชวคราว ถาไมไดจายไฟเลยง ขอมลกจะหายไป

โครงสรางโดยทวไปของ Microcontroller

Page 12: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

23

3. Port (สวนตดตอภายนอก) : ม 2 แบบ คอ Input กบ Output

– Input ทาหนาทเพอ รบขอมลเขามา เชนรบขอมลจาก Switch ,Sensor

– Output ทาหนาทเพอ สงขอมลออกไป เชนสงขอมลไปยง หลอดไฟ LED หนาจอ LCD หรอสงไปยงคอมพวเตอร

4. Bus (ชองทางเดนของสญญาณ) : คอเสนทางทใชในการแลกเปลยนสญญาณขอมลระหวาง CPU, Memory และ Port  ซงแบงเปน Data Bus , Address Bus และ Control Bus

โครงสรางโดยทวไปของ Microcontroller

Where we can find microcontroller ?

Page 13: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.2 สถาปตยกรรมของไมโครคอนโทรลเลอร

• MCS‐51 (8‐bit) ออกแบบโดย intel

• MCS‐96 (16‐bit) ออกแบบโดย intel

• PIC  ออกแบบโดย Microchip Technology 

• AVR ออกแบบโดย Atmel 

• ARM ออกแบบโดย ARM Holdings 

• 68HC11 ออกแบบโดย Motorola 

• Rabbit 2000  ออกแบบโดย Rabbit Semiconductor

25

1.3 สถาปตยกรรมของ AVR• แบงออกเปน 2 ตระกลคอ 8‐bit AVR และ 32‐bit AVR• ในทนจะกลาวถงสถาปตยกรรมของ AVR ขนาด 8 บตเทานน • สถาปตยกรรม AVR ออกแบบโดย ATMEL เมอป 1996 เปนซพยแบบ RISC (Reduced Instruction Set Computer) 

• มสถาปตยกรรมการตอหนวยความจาแบบ Harvard ซงแยกหนวยความจาโปรแกรมและหนวยความจาขอมล ออกจากกนโดยเดดขาด

• ใชหนวยความจาแบบ Flash สาหรบเปนหนวยความจาโปรแกรม • ใชหนวยความจาแบบ SRAM สาหรบหนวยความจาขอมล • นอกจากนยงมหนวยความจาแบบ EEPROM ซงสามารถเกบขอมลเอาไวได

โดยไมจาเปนตองมไฟเลยง

26

Page 14: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.4 ประเภทการใชงาน AVR ขนาด 8 บต

• tinyAVR เปนซพยในรนเลก ซงตองการความเลกกะทดรดของวงจร โดยเหมาะกบระบบควบคมขนาดเลกๆ ทตองการหนวยความจาและวงจรสนบสนนไมมากนก ซพยในรนนจะมราคาถกกวากลมอน

• megaAVR จะมชออกอยางวา ATmega โดยมวงจรสนบสนนภายในเพมเตมตลอดจนเพมขนาดของหนวยความจาใหใชงานมากกวาตระกล Tiny เหมาะกบงานควบคมทวๆ ไป

27

• XMEGA เพมความละเอยดของวงจร A/D จากปกตมความละเอยด10 บตในรนเลกกวาเปน 12 บต และวงจร DMA controller ซงชวยลดภาระของซพยในการควบคมการรบสงขอมลระหวาง อปกรณ I/O กบหนวยความจา

• FPSLIC (AVR core with FPGA) สาหรบงานทตองการควบคมทตองการความยดหยนในขนตอนการออกแบบและพฒนา โดยผออกแบบสามารถออกแบบวงจรในระดบฮารดแวรเพมเตมดวยภาษาบรรยายฮารดแวร (HDL: Hardware Description Language) เชน ภาษา VHDLหรอภาษา Verilog และใหวงจรทออกแบบทางานรวมกบซพย AVR core

28

Page 15: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• Application Specific AVR เปนซพยทออกแบบมาโดยเพมวงจรควบคมเฉพาะดานเขาไปซงไม พบในซพยกลมอนๆ เชนวงจร USB controller หรอวงจร CAN bus เปนตน

AVR มใหเลอกใชงานหลายเบอร แตละเบอรจะมขนาด ราคา ความสามารถ และขนาด หนวยความจาตลอดจนถงวงจรสนบสนนภายในทแตกตางกนออกไป

29

1.5.1 ATmega168

• หนวยความจาโปรแกรมแบบ FLASH ขนาด 16 Kbyte• หนวยความจาขอมลแบบ SRAM ขนาด 1 Kbyte• หนวยความจาขอมลแบบ EEPROM ขนาด 512 byte• สนบสนนการเชอมตอแบบ I2C bus• พอรตอนพตเอาตพตจานวน 20 ports• วงจรสอสารอนกรม• วงจรนบ/จบเวลาขนาด 8 บต จานวน 2 ตว และขนาด 16 บตจานวน 1 ตว• สนบสนนชองสญญาณสาหรบสราง PWM จานวน 6 ชองสญญาณ• วงจรแปลงอนาลอกเปนดจตอลขนาด 10 บตในตว จานวน 6 ชอง• ทางานไดตงแตยานแรงดน 1.8‐5.5 Volts• ความถใชงานสงสด 20 MHz

30

Page 16: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

รายละเอยด ขา ตางๆ ของ ATmega168

31

1.5.2 ATmega328

• หนวยความจาโปรแกรมแบบ FLASH ขนาด 32 Kbyte• หนวยความจาขอมลแบบ SRAM ขนาด 2 Kbyte• หนวยความจาขอมลแบบ EEPROM ขนาด 1 Kbyte• สนบสนนการเชอมตอแบบ I2C bus• พอรตอนพตเอาตพตจานวน 22 ports• วงจรสอสารอนกรม• วงจรนบ/จบเวลาขนาด 8 บต จานวน 2 ตว และขนาด 16 บตจานวน 1 ตว• สนบสนนชองสญญาณสาหรบสราง PWM จานวน 6 ชองสญญาณ• วงจรแปลงอนาลอกเปนดจตอลขนาด 10 บตในตว จานวน 8 ชอง• ทางานไดตงแตยานแรงดน 1.8‐5.5 Volts• ความถใชงานสงสด 20 MHz

32

Page 17: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

รายละเอยด ขา ตางๆ ของ ATmega328

33

1.6 Platform Platform หมายถง การทางานรวมกนของ Hardware และ Software

สาหรบการทางานของ Application

Hardware

• หมายถง สถาปตยกรรมคอมพวเตอร หรอ สถาปตยกรรมหนวยประมวลผล ตวอยางเชน CPU ทใชสถาปตยกรรม x86 หรอ x86‐64 เปนตน

Software

• หมายถง ระบบปฏบตการ หรอสภาพแวดลอมทางโปรแกรมมง

ตวอยางเชนMicrosoft Windows,  Mac OS X x86  , Mac OS X PowerPC, Linux x86 , Java Platform

34

Page 18: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.7 What is Arduino ? [1]

• Arduino เปนโครงการพฒนาไมโครคอนโทรลเลอรของตระกล AVR แบบ Open Source โดยเปนชอเรยกของ platform ซงประกอบไปดวย – Development Board ทใช microcontroller ตระกล AVR– IDE เพอใชในการพฒนาโปรแกรม โดย IDE พฒนาโดยภาษา Java สงผลใหสามารถทางานไดทก OS เชน Linux, MAC OS, Windows

และ IDE ม library มาตรฐาน ในการอางองกบบอรด Arduino จานวนมาก ทาใหสะดวกในการพฒนาโปรแกรม

• โดย AVR ทเอามาใชนน จะตองมการตดตง Firmware ไวแลว โดยหนาทหลกของ Firmware คอ การรบโปรแกรมท Compile แลว จาก IDE มาเขยนไวทตวมนเอง เพอทางานตามโปรแกรมทเขยน

35

What is Arduino ? [2]

36

Page 19: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.7.1 Arduino Hardware Variety

37

1.7.2. Arduino Software

38

Page 20: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1.7.3 ขอด-ขอเสย Arduinoขอด- พฒนาโปรแกรมดวยภาษา c++ จงสามารถใชคณสมบต OOP ได- ไมตองใชอปกรณ ISP ในการโหลดโปรแกรมไปยง microcontroller- โครงสรางการเขยนเขาใจงาย ไมซบซอน - ตนทนมราคาถก- เปนโครงการ opensource จงทาใหมผ ใหความสนใจมาก และม Library ใหใชงานเปนจานวนมาก

ขอเสย- เนองจากพฒนาโปรแกรมดวยภาษา c++ จงใชทรพยากรมากพอสมควร

39

40

Page 21: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

2. การสงขอมลออกพอรต

arduino cube

42

Page 22: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

2.1 ขนตอนการพฒนาโปรแกรมดวย Arduino( Development Circle)

43

2.2 Arduino Language

Language is standard C (but made easy)

core functionDigital I/O

• pinMode() 

• digitalWrite() 

• digitalRead() 

Analog I/O

• analogReference() 

• analogRead() 

• analogWrite() ‐ PWM

Communication

• Serial

Advanced I/O

• tone() 

• noTone() 

• shiftOut() 

• shiftIn() 

• pulseIn() 

Time

• millis() 

• micros() 

• delay() 

• delayMicroseconds() 

Math

• min() 

• max() 

• abs() 

• constrain() 

• map() 

• pow() 

• sqrt() 

Trigonometry

• sin() 

• cos() 

• tan()

Random Numbers

• randomSeed() 

• random() 

44

Page 23: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

2.3 Some useful function

• pinMode() – set a pin as input or output

• digitalWrite() – set a digital pin high/low

• digitalRead() – read a digital pin’s state

• analogRead() – read an analog pin

• analogWrite() – write an “analog” value

•delay() – wait an amount of time

• millis() – get the current time

45

2.4 การเขยนโปรแกรมบน Arduino

46

การเขยนโปรแกรมบน Arduino จะคลายกบการเขยนโปรแกรม C/C++

ไฟลโปรแกรม ททาการเขยนจะเรยกวา sketch files มสวนประกอบสาม

สวน คอ

Page 24: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

2.5 การสงคาออกพอรต แบบดจตอลการทดลองท 2.1. LED Blink : Hello world of Microcontroller

schematic

To turn on LED use digitalWrite(13,HIGH)

47

Digital Pin 13

รายละเอยดขา อยหนา 19

48

Page 25: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

49

50

Page 26: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

โปรแกรม LED Blink แบบท 1

51

โปรแกรม LED Blink แบบท 2

52

Page 27: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

53

แบบฝกหดท 2.1 โปรแกรมควบคม Color LED

จงเขยนโปรแกรมแสดงส 8 ส โดยเวนชวงสละ 1 วนาทกาหนดสเอง ตามใจชอบ

แบบฝกหดท 2.2 โปรแกรมควบคม 7-segment

• ดดแปลงวงจร จากการทดลองท 1 โดยแทน LED ดวย 7‐segment

• จงเขยนโปรแกรมแสดง 0-9 โดยเวนชวงละ 1 วนาท

การจดขาของ 7‐segment

54

Page 28: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

analogWrite()• Description• Writes an analog value (PWM wave) to a pin. Can 

be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to  analogWrite(), the pin will generate a steady square wave of the specified duty. The frequency of the PWM signal is approximately 490 Hz.

• On most Arduino boards (those with the ATmega168 or ATmega328), this function works on digital pins 3, 5, 6, 9, 10, and 11.

• You do not need to call pinMode() to set the pin as an output before calling analogWrite(). 

55

2.6 การสงคาออกพอรต แบบอนาลอก

การทดลองท 1.2 LED Fading

• Same circuit as Blink circuit but pin 9 instead of pin 13

• The PWM pins work with the “analogWrite(pin,value)” command  where 

pin: the pin to write to. 

value:   the duty cycle: between 0 (always off) and 255 (always on).

• To turn LED to half‐bright,  use   analogWrite(9,128) 56

Digital Pin 9

Page 29: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

โปรแกรม LED Fading : Off to Full bright

57

Things to Try With “Fading”

• Make it go really fast or really slow

• Fading from half‐ to full‐bright

• Try other PWM pins

• Multiple fading LEDs,  at different rates

58

Page 30: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

59

60

Page 31: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

สรป : ขอสงเกต

61

To turn ON:   digitalWrite(9,HIGH)To turn OFF:   digitalWrite(9,LOW)To set brightness:   analogWrite(9,val)

Digital Pin 9

3. การรบขอมลเขาพอรต

keypadphotocell

Force sensor

Tact switch

Page 32: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

3.1 การรบคา Digital Input จาก port

63

• Resistor pulls input to +5V• Press switch is LOWNot pressed is HIGH

3.1 Using digitalRead()

64

Page 33: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 3.1 การรบคา Digital Input 

• ตอวงจรตามรป

65

ถา กด Switch  จะไดวา ขา Digital Pin2 จะมคา Lowถา ปลอย Switch จะไดวา ขา Digital Pin2 จะมคา High

66

โปรแกรมน เมอกด Switch จะทาให LED ตดเมอปลอย Switch จะทาให LED ดบ

โปรแกรมอานคาจาก switch และแสดงคาออก LED

Page 34: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

แบบฝกหดท 3.1

ตอ Switch จานวน 4 ตว เขากบ บอรด Arduino

และเขยนโปรแกรมโดยท

‐ กดสวตซ S1 แลว 7‐segment แสดงเลข 1

‐ กดสวตซ S2 แลว 7‐segment แสดงเลข 2

‐ กดสวตซ S3 แลว 7‐segment แสดงเลข 3

‐ กดสวตซ S4 แลว 7‐segment แสดงเลข 4

67

3.2 การรบคา Analog Input จาก port

68

Page 35: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Analog Input

69

• How to make a varying voltage ?

70

++อ +5VMeasure hereGround

Page 36: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 3.2 อานคา สญญาณ Analog

71

• ตอวงจรตามรป

เมอหมนตวตานทาน คาแรงดนทขา Analog Pin2จะมคาเปลยนแปลงตงแต 0 ถง 5 Volt

Note: คาสง analogWrite ใชไดกบขา digital pin 3, 5, 6, 9, 10, 11.

72

โปรแกรมอานคาจากตวตานทานปรบคาได แสดงคาออก LED

Note: 1. การระบขา analog input ตองมตว A นาหนา เชนขา A0, A1,…A52.การใชงานขา analog input ไมตองใชคาสง pinMode เนองจากทาหนาทเปน input อยแลว

Page 37: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

แบบฝกหดท 3.2 Tuning Color LED

73

จงเขยนโปรแกรมปรบส color LED  โดยรบคาจาก ตวตานทานปรบคาได

จานวน 3 ตว เพอไปปรบคาส Red, Green, Blue เพอใหปรบสตางๆ ไดตามใจชอบ

ArduinoBoard

ตวอยาง สนคา

แนวทางการพฒนา

• นาไปใชอานคาจาก Resistive Sensor ตางๆ ได เชน

74

Page 38: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

แบบฝกหดท 3.3 Light Sensor

75

จงเขยนโปรแกรม

Sensing the Dark 

โดยท ให LED สวาง ตามระดบความมด

เชนถาหองสวางมาก ให LED ดบ

ถาหองมดทบ ให LED สวางทสด

ตอวงจรเซนเซอร ตรวจจบแสงดงรป

โดยให RL = 10k , VDD = 5V

และไปเขาขา Analog input

ตอเขาขา Analog input

รายละเอยดขา Sensor

Output voltage is linear with light intensity

4. Serial Communication

Page 39: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

77

4. Communicating with other

4.1 Serial Communication

• การสอสารแบบอนกรม หรอ Serial เปนสงขอมล โดยใชเทคนคการเลอนขอมล (Shift Bit) สงไปทละบต บนสายสญญาณเสนเดยว โดยการสงขอมลแบบ Serial จะไมมการ sync สญญาณนาฬการะหวางตวรบและตวสง แตจะอาศยวธตงคาความเรวในการรบสงสญญาณใหเทากน หรอ เรยกวาตงคา baud rate และสงสญญาณ start และ stop เพอบอกวา เปนสวนตนของขอมล (start bit) หรอ สวนทายของขอมล (stop bit) ดงรป

78

Page 40: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

รปแบบของ ขอมลจากทสงผาน Serial จะมการเพม Start bit และ Stop bit เขาไปเพมจากขอมลเดม– บตเรมตน (Start bit) จะมขนาด 1 บต จะเปนลอจก LOW 

– บตขอมล (Data bit) 8 บต ขอมลทจะสง

– บตภาวะคหรอค (Parity bit) มขนาด 1 บต ใชตรวจสอบขอมล ถาขอมลทไดรบไมสมบรณ นาเขาคามา check กบ Parity bit จะไดคาไมตรงกน

– บตหยด (Stop bit) เปนการระบถงขอบเขตของการสนสดขอมล จะเปนลอจก HIGH 

79

4.2 Arduino Communications

• Psst,   Arduino doesn’t really do USB

• It really is “serial”,  like old RS‐232 serial

• All microcontrollers can do serial

• Not many can do USB

• Serial is easy,  USB is hard

80

Page 41: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

4.3 Serial command• Talking to other uses the “Serial” command

– Serial.begin() : prepare to use serial – Serial.print() : send data to serial port– Serial.println() : send data and newline to serial port  – Serial.read() : read data from serial port– Serial.available() : ready to read– Serial.flush() : clear buffer at incoming serial data– Serial.parseInt(): returns the first valid (long) integer number 

from the serial buffer

• Can talk to not just computers. • Most things more complex than simple sensors/actuators speak serial.

81

การทดลองท 4.1 Serial Hello world !

ทดลองสงคาไปยงคอมพวเตอร

• Send “Hello World” to computer

and Blink LED 

• Click on “Serial Monitor” button to see output

• Watch LED at DigitalPin13

82

int ledPin = 13;

int i = 0;

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(19200);

}

void loop(){

Serial.print(i++);

Serial.println(“Hello world”);

digitalWrite(ledPin,HIGH);

delay(500);

digitalWrite(ledPin,LOW);

delay(500);}

Page 42: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Note!! :  Serial.print()

• Prints data to the serial port as human‐readable ASCII text. This command can take many forms. Numbers are printed using an ASCII character for each digit. Floats are similarly printed as ASCII digits, defaulting to two decimal places. Bytes are sent as a single character. Characters and strings are sent as is. 

For example: 

– Serial.print(78)  gives "78" 

– Serial.print(1.23456)  gives "1.23" 

– Serial.print(byte(78))  gives "N" (whose ASCII value is 78) 

– Serial.print('N')  gives "N" 

– Serial.print("Hello world.")  gives "Hello world." 

An optional second parameter specifies the base (format) to use– Serial.print(78, BIN)  gives "1001110"  BIN  : เลขฐานสอง

– Serial.print(78, OCT)  gives "116"  OCT  : เลขฐานแปด

– Serial.print(78, DEC)  gives "78"  DEC  : เลขฐานสบ

– Serial.print(78, HEX)  gives "4E"  HEX  : เลขฐานสบหก83

การทดลองท 4.2 Serial Read Basic

ทดลองรบคาจากคอมพวเตอร

และสงคาออกกลบไปคอมพวเตอร

ในโปรแกรม “Serial Monitor”, ใหพมพอะไร กได

จากนน กดป ม Send 

84

char inByte = 0; void setup() {

Serial.begin(19200); Serial.println("Hello,type something"); }

void loop() { if (Serial.available()) {

inByte = Serial.read(); Serial.print ("> "); Serial.println (inByte);

} }

Note !!!• Serial.available() tells 

you if data present to read.

• Always check Serial.available() or if Serial.read() != -1 to determine if there’s actual data to read.

Page 43: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 4.3 Controlling from computer

ทดลองรบคาจากคอมพวเตอร• In “Serial Monitor”, You type  

“H”, Press Send 

• When you type “H”, LED Blink.

85

int ledPin = 13;

void setup() {

pinMode(ledPin, OUTPUT);

Serial.begin(19200);

}

void loop(){

if( Serial.available() ){

int val = Serial.read();

if (val == ‘H’) {

digitalWrite(ledPin,HIGH);

delay(500);

digitalWrite(ledPin,LOW);

delay(500);

}

}}

แบบฝกหดท 4.1• สงคาจากคอมพวเตอรเปนตวเลข 0-9 แลวแสดงผลออก

7‐segment

86

แบบฝกหดท 4.2• สงคาจากคอมพวเตอรเปนตวเลข 0-255 ไปควบคมความสวางของ LED

ทตอท Digital pin 9. ( ใชคาสง analogWrite ในการควบคมความสวาง)

Page 44: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

87

แบบฝกหดท 5.3 จากการทดลอง Analog input ( การทดลองท 3.2)  จงเขยนโปรแกรม อานคาแรงดน จากการปรบตวตานทานปรบคาได แลวไปแสดงผลทจอคอมพวเตอร

แบบฝกหดท 5.4

จากการทดลอง Light Sensor (การทดลองท 3.2)

จงเขยนโปรแกรมอานคาแรงดนทไดจาก Sensor และแสดงคาออก

จอคอมพวเตอรวา เมอมดทสด ( เอามอปด Sensor ) มคาเทาใด และเมอสวางสด มคาเทาใด

5. Liquid Crystal Display 

Page 45: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

5.1 Liquid Crystal Display : LCD

89

รายละเอยดขาตางๆ 1. GND2. VDD 3. Vb ทาหนาทปรบความเขมของจอ4. RS (Register select)   5. R/W6. E (Enable) 7‐14  Data DB7‐DB0

5.2 Function ทใชในการควบคมจอ LCD

Arduino ม Library มาตรฐานสาหรบการเชอมตอจอ LCD ทใช chip ของHitachi HD44780 ( หรอ chip อนๆ ท compatible) 

โดยม function หลกๆ ทอยใน Library  LiquidCrystal.h ดงน

• LiquidCrystal()  ใชในการกาหนดขา LCD ทตอ กบขา ATMega

Syntax

– LiquidCrystal(rs, enable, d4, d5, d6, d7)

– LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)

• begin() ใชในการกาหนดคอลมน และ แถว ของ LCD• clear() ใชในการ clear หนาจอ LCD ทงหมด และ cursor มาอย ตาแหนงเรมตน ตรงแถวบนสดซายมอ

90

Page 46: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

91

• home() ใชในการยายตาแหนง cursor มายงแถวบนสดซายมอ

• setCursor() ใชในการระบตาแหนงของ cursor

• cursor() , noCursor()   ใชกาหนดการแสดง cursor

• blink(), noBlink() ใชกาหนดการกระพรบของ cursor

• print() ใชในการเขยน ขอความ ลง LCD

• write() ใชในการเขยน ตวอกษร ลง LCD

• display() , noDisplay()  ใชในการควบคมการปด-เปด หนาจอ

• scrollDisplayLeft()  ใชในการเลอนขอความไปทางซาย

• scrollDisplayRight()  ใชในการเลอนขอความไปทางขวา

• leftToRight()  ใชในการกาหนดทศทางการเขยนขอความ จากซายไปขวา

• rightToLeft()  ใชในการกาหนดทศทางการเขยนขอความ จากขวาไปซาย

• createChar()  ใชในการสรางตวอกษรตวใหม

การทดลองท 5.1 Hello world LCD

• ตอ LCD เขากบบอรดทดลองดงน

92

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {lcd.begin(16, 2);lcd.print("hello, world!");

}

void loop() {lcd.setCursor(0, 1);lcd.print(millis()/1000);

}

Note!!!   lcd.setCursor(0, 1);set the cursor to column 0, line 1line 1 is the second row, since counting begins with 0

Page 47: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 5.2 Text Direction#include <LiquidCrystal.h>LiquidCrystal lcd(12, 11, 5, 4, 3, 2);int thisChar = 'a';

void setup() {lcd.begin(16, 2);lcd.cursor();

}void loop() {

if (thisChar == 'm') {lcd.rightToLeft();

}if (thisChar == 's') {

lcd.leftToRight(); }if (thisChar > 'z') {

lcd.home(); thisChar = 'a';

}lcd.write(thisChar);delay(1000);thisChar++;

}

93

• ตอ LCD เขากบบอรดทดลองดงน

This program prints a through l right to left, then m though r left to right, then s through z right to left again. 

Create a custom character

• Create a custom character for use on the LCD. Up to eight characters of 5x8 pixels are supported (numbered 0 to 7). 

• To display a custom character on the screen, write() its number. 

94

Page 48: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

95

การทดลองท 5.3 Create custom character• ตอ LCD เขากบบอรดทดลองดงน

• โปรแกรมนเปนการ

สรางตวอกษร

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte newChar[8] = { B00000, B01110, B10001, B10001, B10001, B01010, B11011, B00000 };

void setup() { lcd.createChar(1, newChar); lcd.begin(16, 2); lcd.write(1); }

void loop() {}

แบบฝกหดท 5• จงสรางตวอกษรรป จากนนใหแสดงออกจอ LCD โดยท

- จอแถวแรก แสดงวา I  KMITL  และ แสดงตรงกลางแถว- จอแถวทสอง ใหแสดง ชอ นามสกล

• จงเขยนโปรแกรมอานคา แรงดน จากตวตานปรบคาได ไปแสดงผลออกทจอ LCD แสดง (0‐1023)

• จงเขยนโปรแกรมสงคาจาก คอมพวเตอร (ผานทาง serial monitor) มาแสดงผลทจอ LCD

96

Page 49: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

6. Inter Integrate Circuit Bus (I2C) 

Synchronous serial bus with shared a data linea little network for your gadgets

• Up to 127 devices on one bus• Up to 1Mbps data rate• Really simple protocol (compared to USB,Ethernet,etc)• Most microcontrollers have it built-in

98

I2C , “Two Wire”

Page 50: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• I2C Bus ยอมาจาก Inter Integrate Circuit Bus เปนการสอสารอนกรม แบบซงโครนส (Synchronous) เพอใช ตดตอสอสาร ระหวาง ไมโครคอนโทรลเลอร (MCU) กบอปกรณภายนอก

• ถกพฒนาขนโดยบรษท Philips Semiconductors โดยใชสายสญญาณเพยง 2 เสนเทานน คอ serial data (SDA) และสาย serial clock (SCL) ซงสามารถ เชอมตออปกรณ จานวนหลายๆ ตว เขาดวยกนได ทาให MCU ใชพอรตเพยง 2 พอรตเทานน

99

I2C , “Two Wire”

Many I2C devices

100

Page 51: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

I2C on Arduino

• I2C built‐in on Arduino’s ATmega168 chip

• Use “Wire” library to access it

• Analog In 4 is SDA signal

• Analog In 5 is SCK signal

101

• Wire.begin() join i2c bus (master)

• Wire.begin(4) join i2c bus with address #4 (Slave)

• Wire.beginTransmission(112) transmit to device #112

• Wire.endTransmission() stop transmitting

• Wire.requestFrom(112, 2) request 2 bytes from slave device #112

• Wire.available() Returns the number of bytes available for reading

• Wire.read() receive a byte

• Wire.write("x is ") sends five byte

• Wire.write(x) sends one byte 

102

Wire library Functions

Page 52: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Arduino “Wire Library”

103

#include <Wire.h>void setup(){Wire.begin(); // join i2c bus (master)

}

byte x = 0;

void loop(){Wire.beginTransmission(4); // transmit to device #4Wire.write("x is "); // sends five bytesWire.write(x); // sends one byte Wire.endTransmission(); // stop transmitting

x++;delay(500);

}

Master Writing data to Slave

Join i2c

Load wire Library

Start sendingSend dataStop sending

Arduino “Wire Library”

104

#include <Wire.h>

void setup(){Wire.begin(); // join i2c bus (master)Serial.begin(9600); // start serial for output}

void loop(){Wire.requestFrom(2, 6); //request 6 bytes from device #2while(Wire.available()) //slave may send less than requested{

char c = Wire.read(); // receive a byte as characterSerial.print(c); // print the character

}

delay(500);}

Master Reading data from Slave

Join i2c

Request dataGet data

Page 53: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลอง Temperature Sensor DS1621

• ตอวงจรตามรป

105

รายละเอยดขา

Address คอ 1001A2A1A0

หรอ 48H-4FH

ผผลตคอ Dallas Semiconductor เปนไอซวดอณหภมทใชการตดตอผานระบบบส I2C สาหรบคณสมบตทสาคญของ DS1621 มดงน -สามารถวดอณหภม ไดตงแ ต -55 เซลเซยส ถง +125 เซลเซยส โดยมความละเอยดในการวด 0.5 เซลเซยส- ความละเอยดของขอมลอณหภมดจตอล 9 บต - ใชไฟเลยงไดตงแ ต 2.7-5.5 โวลท‐ ใชเวลาในการวดอณหภมแลวแปลงเปนขอมลดจตอล 1 วนาท ‐ สามารถทางานเปนเทอรโมสตต (thermostat) ได พรอมขาเอาตพต 1 ขา- สามารถตงคาอณหภมทตองการเมอทางานเปนเทอรโมสตตได โดยตดตอผานระบบบส I2C และคาทกาหนดนจะคงอยตลอดไป แมปลดไฟเลยงแลวกตาม สามารถเปลยนแปลงไดโดยการ กาหนดทางซอฟตแวรเทานน ‐สามารถตอพวงกนไดสงสด 8 ตว

106

รายละเอยด IC เบอร DS1621

Page 54: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

107

รายละเอยดขา และ การแปลงขอมล

รจสเตอรภายในไอซ DS1621

Page 55: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

คาสงการทางาน

Page 56: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

โปรแกรมอานคาจาก DS1621

111

#include <Wire.h>  void setup(){    Serial.begin(9600);     Wire.begin();    Wire.beginTransmission(0x48);             // connect to DS1621 (#0)    Wire.write(0xAC);                          // Access Config    Wire.write(0x02);                    // set for continuous conversion    Wire.beginTransmission(0x48);             // restart    Wire.write(0xEE);                          // start conversions    Wire.endTransmission();} 

112

void loop(){    int8_t firstByte;    int8_t secondByte;    float temp = 0; 

    delay(1000);                       // give time for measurement 

    Wire.beginTransmission(0x48);    Wire.write(0xAA);                   // read temperature command    Wire.endTransmission();

    Wire.requestFrom(0x48, 2);        // request two bytes from DS1621 (0.5 deg. resolution) 

    firstByte = Wire.read();        // get first byte    secondByte = Wire.read();        // get second byte 

    temp = firstByte; 

    if (secondByte) // if there is a 0.5 deg difference        temp += 0.5; 

    Serial.println(temp); 

}

Page 57: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Internet of Things Protocol

MQTT Protocol

‐ Invented by Andy Stanford Clark  (IBM) and Arlen Nipper (Eurotech) in 1999‐ Originally envisioned for use over Satellite link from an oil pipe line 

Page 58: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

MQTT Protocol

MQTT Protocol

Page 59: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

MQTT Protocol

The publish/subscribe pattern

MQTTBroker

Bi‐directional , Async “Push” Communication

Page 60: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

MQTT Protocol

MQTT Protocol

Page 61: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• A client needs to send a SUBSCRIBE message to the MQTT broker in order to receive relevant messages.

Subscribe

Subscribe

Page 62: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Publish

TopicsA topic is a UTF‐8 string, which is used by the broker to filter messages for each connected client. A topic consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator).

QoSA Quality of Service Level (QoS) for this message. The level (0,1 or 2) determines the guarantee of a message reaching the other end 

Retain‐FlagThis flag determines if the message will be saved by the broker for the specified topic as last known good value. New clients that subscribe to that topic will receive the last retained message on that topic instantly after subscribing.    ( ให Broker เกบ Payload ลาสด ทสงขนไปไวดวย ถามคนใหมเขามา Subscribe ทหลง กจะไดรบขอมลนดวย )

PayloadThis is the actual content of the message.

DUP flagThe duplicate flag indicates, that this message is a duplicate and is resent because the other end didn’t acknowledge the original message. This is only relevant for QoS greater than 0

Packet IdentifierThe packet identifier is a unique identifier between client and broker to identify a message in a message flow. This is only relevant for QoS greater than zero.

Publish

Page 63: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

• QoS 0 – at most onceThe minimal level is zero and it guarantees a best effort delivery. A message won’t be acknowledged by the receiver or stored and redelivered by the sender. 

• QoS 1 – at least onceit is guaranteed that a message will be delivered at least once to the receiver. The sender will store the message until it gets an acknowledgement in form of a PUBACK command message from the receiver.

QoS

• QoS 2 – Exactly once

The highest QoS is 2, it guarantees that each message is received only once by the 

counterpart.  It is the safest and also the slowest quality of service level. The 

guarantee is provided by two flows there and back between sender and receiver.

QoS

Publish completePublish releasePublish received

Page 64: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

QoS

Retain messages

Page 65: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Payload

Unsubscribe

Page 66: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Connect & Disconnect

การทดลองท 7.1 Public MQTT Broker

• เขา Website ท URL http://www.hivemq.com/demos/websocket‐client/

Page 67: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 7.1 Public MQTT Broker

• Connect– กดป ม Connect

• Subscribe

– กด Add Topic Subscription เพอ add หวขอ ทตองการ subscribe

– กด Subscribe

• Publish– กาหนด topic ทตองการจะ publish

– พมพ ขอความ ทจะ publish

– กด publish 

MQTT broker gets Websockets support

มองคประกอบคอ‐ Web Server ทาหนาทเกบไฟล html ทมโคด javascript‐ Web Browser หรอ Web application  ทาหนาท เรยกไฟล html จาก Web Server มาแสดงผล‐ MQTT Broker ทเปด Web Socket เพอให Javascript ไปดงขอมล มาแสดงผล

Page 68: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 7.2 Real Time graph

Simple example of plotting live numbered data from a subscribed mqtt/websockets topic and plotting it using highcharts. http://www.highcharts.com/

สงทตองม คอ ‐Web Server หรอ Web hosting‐ ไฟล html ทเขยนโคด Javascript

ไฟลทเตรยมไวให คอlivemqttchart.html จะ plot กราฟ เมอมการ Publish คาlivemqttchart1.html จะ plot กราฟ แบบ real time

mqttws31.js

ไฟลตวอยางทใหน คอ ทาหนาท Subscribe ตาม Topic ทกาหนด และ รบคามาแสดงผล

การทดลองท 7.2 Real Time graph

1. แกไขไฟล livemqttchart.html ดงน

var MQTTbroker = ' broker.mqttdashboard.com ';  // broker ของ HiveMQ

var MQTTport = 8000; // port ของ Webseocket ทเปดไว

var MQTTsubTopic = 'temp';   //แกชอ topic ไดตามใจชอบ

2.  ทาการ Upload ไฟลทง 2 ไปยง web hosting  หรอ เปดไฟลโดย Web browser 3.  ทดสอบการแสดงผล โดยการ publish คาขอมล ไปยง topic ทกาหนด

‐ การ Publish คา จะ Publish จาก HiveMQ (การทดลองท 1 ) 

Note!!  Broker ITMQ ของ คณะเทคโนโลยสารสนเทศURL:  http://neutron.it.kmitl.ac.th:3030/demo แลวตงคาในไฟล html เปนHost: neutron.it.kmitl.ac.th Port: 1884

Page 69: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 7.3 Control via Websocket

ไฟลทเตรยมไวให คอ

control.html 

ไฟลตวอยางทใหน คอ ทาหนาท Publish ไปยง Topic ทกาหนดเมอกดป ม ON จะ publish คา on ไปยง Brokerเมอกดป ม OFF จะ publish คา off ไปยง Broker

1. แกไขไฟล control.html ดงน

var MQTTbroker = ' broker.mqttdashboard.com ';   // broker ของ HiveMQ

var MQTTport = 8000; // port ของ Webseocket ทเปดไว

var MQTTsubTopic = 'temp';   //แกชอ topic ไดตามใจชอบ

2.  ทาการ Upload ไฟล ไปยง web hosting หรอ เปดไฟลโดย Web browser 3.  ทดสอบการแสดงผล โดยการ ใช Websocket ของ HiveMQ Broker ทาการ Subscribe  topic ทกาหนดเมดกดป มตางๆ จะปรากฎคา on, off และคาตวเลข ท Broker

การทดลองท 7.3 Control via Websocket

Page 70: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Building IoT with Arduino

Ethernet Shield

The Arduino Ethernet Shield allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 ethernet chip. The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP. It supports up to four simultaneous socket connections. Use the Ethernet library to write sketches which connect to the internet using the shield. 

Page 71: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

Ethernet.begin() Initializes the ethernet library and network settings. 

• SyntaxEthernet.begin(mac);Ethernet.begin(mac, ip);Ethernet.begin(mac, ip, dns);Ethernet.begin(mac, ip, dns, gateway);Ethernet.begin(mac, ip, dns, gateway, subnet);

• Example รบคา IP จาก DHCP Server

#include <SPI.h>#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

void setup(){Ethernet.begin(mac); Serial.println(Ethernet.localIP());

}

void loop () {}

Note: Ethernet.localIP() ดงคา IP จาก Ethernet shield 

NoteEthernet.begin(mac), returns an int: 1 on a successful DHCP connection, Int: 0 on failure

Ethernet.begin()Example    ตงคาแบบ Fix IP

#include <SPI.h>#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };IPAddress dnServer(192, 168, 0, 1);IPAddress gateway(192, 168, 0, 1);IPAddress subnet(255, 255, 255, 0);IPAddress ip(192, 168, 0, 2);

void setup() {Serial.begin(9600);

Ethernet.begin(mac, ip, dnServer, gateway, subnet);Serial.println(Ethernet.localIP());

}

void loop() {}

Note: - Ethernet.localIP() ดงคา IP จาก Ethernet shield ‐ IPAddress ประกาศคา IP

Page 72: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

ClientFunctions• EthernetClient() คอ กาหนดใหบอรด arduino ทาหนาทเปน client ซงสามารถสรางการเชอมตอไปยง IP 

และ port ทกาหนดได (Creates a client which can connect to a specified internet IP address and port) 

เชน EthernetClient client;

• connect()  คอ เชอมตอไปยง IP และ port ทกาหนด (Connects to a specified IP address and port)

ถาเชอมตอสาเรจจะใหคาเปน 1  

เชน client.connect("www.google.com", 80)

• available() จะคนคาเปนจานวน byte ทถกเขยนมายง client ซงถกสงมาโดย server

Returns the number of bytes available for reading (that is, the amount of data that has been written 

to the client by the server it is connected to). 

เชน if ( client.available())

ถามการสงขอมลมา จะมความหมายเปน if(true) หรอ if(!0)

ClientFunctions• read() คอ อานคา byte ถดไป ทไดรบจาก Server 

( Read the next byte received from the server the client is connected to (after the last call to read()). ) 

เชน char c = client.read();

• connected()  คอ ตรวจสอบวา client ยงเชอมตออยหรอไม (Whether or not the client is connected.)

ถายงเชอมตออย จะใหคาเปน 1 (true)  , ถาไมไดเชอมตอแลว จะใหคา เปน 0 (false)

เชน if ( !client.connected() )

• stop() คอ ปดการเชอมตอกบ server

เชน client.stop();

• flush() คอ clear คาทถกเขยนมายง client แตยงไมไดถกอาน

เชน client.flush();

Page 73: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

ตดตง Library ของ MQTT Protocol

• Download Library Pub sub  จาก

https://github.com/knolleary/pubsubclient 

โดยการ download  เปน .zip 

• แลวตดตงท sketch   ‐> Include Library ‐> Add .zip library

• เมอตดตงเสรจแลว จะม Library  PubSubClient ปรากฎท

sketch   ‐> Include Library

ขอจากด• It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.

• The maximum message size, including header, is 128 bytes by default.

PubSubClient Library• Constructors

– PubSubClient ()– PubSubClient (client)– PubSubClient (server, port, [callback], client, [stream]) ***

• Functions– boolean connect (clientID) ***– boolean connect (clientID, willTopic, willQoS, willRetain, willMessage)– boolean connect (clientID, username, password)– boolean connect (clientID, username, password, willTopic, willQoS, willRetain, willMessage)– void disconnect ()– int publish (topic, payload) ***– int publish (topic, payload, retained)– int publish (topic, payload, length)– int publish (topic, payload, length, retained)– int publish_P (topic, payload, length, retained)– boolean subscribe (topic, [qos]) ***– boolean unsubscribe (topic)– boolean loop ()– int connected ()– int state ()– PubSubClient setServer (server, port)– PubSubClient setCallback (callback)– PubSubClient setClient (client)– PubSubClient setStream (stream)

• Other– Configuration Options– Subscription Callback

Page 74: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

การทดลองท 8.1 Publish 

ขนตอนการทดลอง

1. ใช Public MQTT Broker  ท URL http://www.hivemq.com/demos/websocket‐client/

2. กด Connect  ( ระบบจะ random คา client ID อตโนมต )

3. ไปท Subscriptions แลว Add new topic subscription  กาหนด Topic ทตองการตดตาม

Web Browser

Public Broker

MQTT Client( Arduino + Ethernet Shield)

4. เขยนโปรแกรมลง Arduino เพอทดลอง publish 

#include <SPI.h>#include <Ethernet.h>#include <PubSubClient.h>

char MQTT_SERVER[]=“broker.mqttdashboard.com”; //byte MQTT_SERVER[] = { 161, 246, 38, 78 };

char* outTopic = "testtopic/1";

char* clientId = "clientId-68zjK7WiL5"; // แตละ device ตองไมซากนchar* payload = "test from arduino";

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // แตละ device ตองไมซากน

EthernetClient ethClient; // Ethernet objectPubSubClient client( MQTT_SERVER, 1883, 0, ethClient); // MQTT object

การทดลองท 8.1 Publish 

Note!!  Broker ITMQ ของ คณะเทคโนโลยสารสนเทศURL:  http://neutron.it.kmitl.ac.th:3030/demo แลวตงคาในไฟล โปรแกรม เปนHost: neutron.it.kmitl.ac.th Port: 1883

Page 75: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

โปรแกรม Arduino (ตอ)void setup() {

Serial.begin(9600);Serial.println("Net begin");

if (Ethernet.begin(mac) == 0) // Get connected!{

Serial.println("Failed to configure Ethernet using DHCP");return;

}

Serial.print("IP: "); // A little debug.. show IP addressSerial.println(Ethernet.localIP());

if (client.connect(clientId)) { Serial.println("Successfully connected with MQTT"); }else {Serial.println("cant connected with MQTT");

}}void loop(){client.publish(outTopic, payload);delay(5000);

}

การทดลองท 8.1 Publish 

5. เปดโปรแกรม Serial monitor  เพอดสถานะตางๆ และ สงเกตขอความทไดรบ (Message)  จากการ publish โดย Arduino

การทดลองท 8.1 Publish 

Page 76: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

1. เขยน โปรแกรมลง Arduino เพอทดลอง Subcribe

#include <SPI.h>#include <Ethernet.h>#include <PubSubClient.h>

char MQTT_SERVER[]=“broker.mqttdashboard.com”;//byte MQTT_SERVER[] = { 161, 246, 38, 78 };

char* inTopic = "testtopic/1";

char* clientId = "clientId-68zjK7WiL5"; // แตละ device ตองไมซากน

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // แตละ device ตองไมซากน

void callback(char* topic, byte* payload, unsigned int length) {

Serial.println(topic);payload[length] = '\0';String strPayload = String((char*)payload);Serial.println(strPayload);

}

EthernetClient ethClient; // Ethernet objectPubSubClient client( MQTT_SERVER, 1883, callback, ethClient); // MQTT object

การทดลองท 8.2 Subscribe 

โปรแกรม Arduino เพอทดลอง Subcribe (ตอ) 

void setup() {Serial.begin(9600);Serial.println("Net begin");

if (Ethernet.begin(mac) == 0) // Get connected!{

Serial.println("Failed to configure Ethernet using DHCP");return;

}

delay(2000);Serial.print("IP: "); // A little debug.. show IP addressSerial.println(Ethernet.localIP());

if (client.connect(clientId)) { Serial.println("Successfully connected with MQTT"); client.subscribe(inTopic); // Subcribe}else { Serial.println("cant connected with MQTT"); }

}

void loop(){client.loop(); // loop for ever waiting for MQTT event

}

การทดลองท 8.2 Subscribe 

Page 77: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

2. เปดโปรแกรม Serial monitor  และ publish Message โดย WebSocket

และ สงเกตขอความทไดรบ (Message)  จากการ publish โดย WebSocket

การทดลองท 8.2 Subscribe 

แบบฝกหด

Web Server

Web Browser

Public BrokerMQTT Client

( Arduino + Ethernet Shield)

ณ แบบฝกหดน ทกทานจะมโครงสรางระบบเปนดงรป จงเขยนโปรแกรมให สามารถสง ปด-เปด LED และหรไฟ LED  จาก website ไดNote แก website ทไฟล control.html ดวยนะครบ

Page 78: Internet of Things - it.kmitl.ac.thpanwit/IoT001.pdf · – MQTT Broker – Local Broker & Public Broker 2. What is Internet of Things ... Kafka is a great example of. Other solution

แบบฝกหด : หลอด LED & R ปรบคาได

Public BrokerMQTT Client

( Arduino + Ethernet Shield)

วตถประสงค : ตองการให Arduino ทงสองคยกนผาน Internet - ใหจบค ทาดวยกน1. ปรบตวตานทาน ทฝง Team A แลวใหไป หรไฟ ทฝง Team B 2. ใหฝง Team B หนงใช sensor แสง วดแสง แลวสงไปยง Team A โดยท

‐เมอแสงนอย ใหหลอดไฟ LED ตด ‐เมอแสงมาก ใหหลอดไฟ LED ดบ

MQTT Client( Arduino + Ethernet Shield)

Team A Team B

แบบฝกหด : Sensor อณหภม & หลอด color LED

Public Broker

MQTT Client( Arduino + Ethernet Shield)

‐ ใหจบค ทาดวยกน‐ ใหฝง Team A อานคาจาก Sensor อณหภม สงคาทกๆ 10 วนาท ‐ ฝง Team B แสดงส ของ color LED โดยท

อณหภม 0‐20  แสดงสฟา อณหภม 21‐30 แสดงสเขยวอณหภม 31‐40 แสดงสสม อณหภมมากกวา 50 แสดงสแดง

- แสดงคาอณหภมแบบ Real Time บน Web browser 

MQTT Client( Arduino + Ethernet Shield)

Web Browser

Team A Team B