Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Arduino Basics

Arduino Basics

Lab #1 - Arduino Basics
ET-3010 Connected Services and Cloud Computing
http://eueung.github.io/ET3010/arduino

Eueung Mulyana

February 09, 2016
Tweet

More Decks by Eueung Mulyana

Other Decks in Technology

Transcript

  1. Arduino An open-source hardware and software platform for building electronics

    projects. Arduino is an open-source electronics platform based on easy-to-use hardware and software. It's intended for anyone making interactive projects. Arduino senses the environment by receiving inputs from many sensors, and affects its surroundings by controlling lights, motors, and other actuators. You can tell your Arduino what to do by writing code in the Arduino programming language and using the Arduino development environment. Several Arduino-Board variants exist e.g.: UNO, NANO, MEGA, DUE, YUN, etc. 4 / 44
  2. 9 / 44 This Checklist Please: UNO Board Compatible +

    Acessories Components + Wires ARDUINO IDE
  3. Project #1 13 12 11 10 9 8 7 6

    5 4 3 2 L 5V A0 ANALOG IN AREF 1 GND TX RX RESET 3V3 A1 A2 A3 A4 A5 VIN GND GND DIGITAL (PWM= ) Arduino TM IOREF ICSP ICSP2 ON POWER 0 1 TX0 RX0 RESET 1 1 5 5 10 10 15 15 20 20 25 25 30 30 A A B B C C D D E E F F G G H H I I J J Fritzing Breadboard 16 / 44
  4. Project #1 D0/RX D1/TX D2 D3 PWM D4 D5 PWM

    D6 PWM D7 D8 D9 PWM D10 PWM/SS D11 PWM/MOSI D12/MISO D13/SCK RESET RESET2 AREF ioref A0 A1 A2 A3 A4/SDA A5/SCL N/C GND 3V3 5V VIN Arduino Uno (Rev3) 2 1 2 1 1 2 1 2 1 3 2 Part1 LED1 Red (633nm) R1 100Ω R2 10kΩ S1 R3 10kΩ Fritzing Schematic 17 / 44
  5. Project #1 Sketch i n t i n P i

    n = 2 ; i n t o u t P i n = 3 ; i n t p o t P i n = A 0 ; i n t s t a t e = L O W ; i n t r e a d i n g ; i n t p r e v i o u s = L O W ; l o n g t i m e = 0 ; l o n g d e b o u n c e = 1 0 0 0 ; i n t p o t V a l = 0 ; i n t p r e v P o t V a l = 0 ; v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) ; d e l a y ( 5 0 0 ) ; p i n M o d e ( i n P i n , I N P U T ) ; p i n M o d e ( o u t P i n , O U T P U T ) ; S e r i a l . p r i n t l n ( " P r o g r a m s t a r t e d . . . " ) ; } v o i d l o o p ( ) { r e a d i n g = d i g i t a l R e a d ( i n P i n ) ; i f ( r e a d i n g & & ( m i l l i s ( ) - t i m e > d e b o u n c e ) ) { i f ( p r e v i o u s = = L O W ) { S e r i a l . p r i n t l n ( " [ P H Y S I C A L ] L E D t u r n e d o n " ) ; s t a t e = H I G H } e l s e { S e r i a l . p r i n t l n ( " [ P H Y S I C A L ] L E D t u r n e d o f f " ) ; s t a t e = L O W } t i m e = m i l l i s ( ) ; d i g i t a l W r i t e ( o u t P i n , s t a t e ) ; p r e v i o u s = s t a t e ; p r e v P o t V a l = p o t V a l - 1 0 ; } p o t V a l = a n a l o g R e a d ( p o t P i n ) ; i f ( ( s t a t e = = H I G H ) & & ( a b s ( p o t V a l - p r e v P o t V a l ) > 4 ) ) { a n a l o g W r i t e ( o u t P i n , p o t V a l / 4 ) ; S e r i a l . p r i n t ( " [ P H Y S I C A L ] L E D i n t e n s i t y " ) ; S e r i a l . p r i n t l n ( p o t V a l / 4 ) ; p r e v P o t V a l = p o t V a l ; } } 18 / 44
  6. UNO + Ethernet Shield 13 12 11 10 9 8

    7 6 5 4 3 2 L 5V A0 ANALOG IN AREF 1 GND TX RX RESET 3V3 A1 A2 A3 A4 A5 VIN GND GND DIGITAL (PWM= ) Arduino TM IOREF ICSP ICSP2 ON POWER 0 1 TX0 RX0 RESET 13 12 11 ETH 9 8 7 6 5 SDCS 3 2 0 1 TX RX AREF GND 5V A0 ANALOG IN TX RX RESET 3V3 A1 A2 A3 A4 A5 VIN GND GND DIGITAL (PWM SPI ) SCL SDA < IOREF ICSP CS < < 22 / 44
  7. Example #1 Web Server # i n c l u

    d e < S P I . h > # i n c l u d e < E t h e r n e t . h > b y t e m a c [ ] = { 0 x D E , 0 x A D , 0 x B E , 0 x E F , 0 x F E , 0 x E D } ; I P A d d r e s s i p ( 1 9 2 , 1 6 8 , 0 , 1 7 7 ) ; E t h e r n e t S e r v e r s e r v e r ( 8 0 ) ; v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) ; w h i l e ( ! S e r i a l ) { } E t h e r n e t . b e g i n ( m a c , i p ) ; s e r v e r . b e g i n ( ) ; S e r i a l . p r i n t ( " S e r v e r i s a t " ) ; S e r i a l . p r i n t l n ( E t h e r n e t . l o c a l I P ( ) ) ; } v o i d l o o p ( ) { / / . . . } E t h e r n e t C l i e n t c l i e n t = s e r v e r . a v a i l a b l e ( ) ; i f ( c l i e n t ) { S e r i a l . p r i n t l n ( " n e w c l i e n t " ) ; b o o l e a n c u r r e n t L i n e I s B l a n k = t r u e ; w h i l e ( c l i e n t . c o n n e c t e d ( ) ) { i f ( c l i e n t . a v a i l a b l e ( ) ) { c h a r c = c l i e n t . r e a d ( ) ; S e r i a l . w r i t e ( c ) ; i f ( c = = ' \ n ' & & c u r r e n t L i n e I s B l a n k ) { c l i e n t . p r i n t l n ( " H T T P / 1 . 1 2 0 0 O K " ) ; c l i e n t . p r i n t l n ( c l i e n t . p r i n t l n ( " R e f r e s h : 5 " ) ; c l i e n t . p r i n t l n ( ) ; c l i e n t . p r i n t l n ( " < ! D O C T Y P E H T M L > " ) ; c l i e n t . p r i n t l n ( f o r ( i n t a n a l o g C h a n n e l = 0 ; a n a l o g C h a n n e l < 6 ; a n a l o i n t s e n s o r R e a d i n g = a n a l o g R e a d ( a n a l o g C h a n n e l ) ; c l i e n t . p r i n t ( " a n a l o g i n p u t " ) ; c l i e n t . p r i n t ( a n a l o g c l i e n t . p r i n t l n ( " < b r / > " ) ; } c l i e n t . p r i n t l n ( " < / h t m l > " ) ; b r e a k ; } i f ( c = = ' \ n ' ) { c u r r e n t L i n e I s B l a n k = t r u e ; } e l s e i f ( c ! = ' \ r ' ) { c u r r e n t L i n e I s B l a n k = f a l s e ; } } } / / w h i l e d e l a y ( 1 ) ; c l i e n t . s t o p ( ) ; S e r i a l . p r i n t l n ( " c l i e n t d i s c o n n e c t e d " ) ; } 24 / 44
  8. Test $ > p i n g 1 9 2

    . 1 6 8 . 0 . 1 7 7 P i n g i n g 1 9 2 . 1 6 8 . 0 . 1 7 7 w i t h 3 2 b y t e s o f d a t a : R e p l y f r o m 1 9 2 . 1 6 8 . 0 . 1 7 7 : b y t e s = 3 2 t i m e = 2 m s T T L = 1 2 8 R e p l y f r o m 1 9 2 . 1 6 8 . 0 . 1 7 7 : b y t e s = 3 2 t i m e = 3 m s T T L = 1 2 8 R e p l y f r o m 1 9 2 . 1 6 8 . 0 . 1 7 7 : b y t e s = 3 2 t i m e = 2 m s T T L = 1 2 8 R e p l y f r o m 1 9 2 . 1 6 8 . 0 . 1 7 7 : b y t e s = 3 2 t i m e = 2 m s T T L = 1 2 8 P i n g s t a t i s t i c s f o r 1 9 2 . 1 6 8 . 0 . 1 7 7 : P a c k e t s : S e n t = 4 , R e c e i v e d = 4 , L o s t = 0 ( 0 % l o s s ) , A p p r o x i m a t e r o u n d t r i p t i m e s i n m i l l i - s e c o n d s : M i n i m u m = 2 m s , M a x i m u m = 3 m s , A v e r a g e = 2 m s S e r v e r i s a t 1 9 2 . 1 6 8 . 0 . 1 7 7 n e w c l i e n t G E T / H T T P / 1 . 1 H o s t : 1 9 2 . 1 6 8 . 0 . 1 7 7 C o n n e c t i o n : k e e p - a l i v e C a c h e - C o n t r o l : m a x - a g e = 0 A c c e p t : t e x t / h t m l , a p p l i c a t i o n / x h t m l + x m l , a p p l i c a t i o n / x m l ; q = 0 . 9 U p g r a d e - I n s e c u r e - R e q u e s t s : 1 U s e r - A g e n t : M o z i l l a / 5 . 0 ( W i n d o w s N T 6 . 1 ; W O W 6 4 ) A p p l e W e b K i t / R e f e r e r : h t t p : / / 1 9 2 . 1 6 8 . 0 . 1 7 7 / A c c e p t - E n c o d i n g : g z i p , d e f l a t e , s d c h A c c e p t - L a n g u a g e : e n - U S , e n ; q = 0 . 8 c l i e n t d i s c o n n e c t e d n e w c l i e n t G E T / f a v i c o n . i c o H T T P / 1 . 1 H o s t : 1 9 2 . 1 6 8 . 0 . 1 7 7 C o n n e c t i o n : k e e p - a l i v e U s e r - A g e n t : M o z i l l a / 5 . 0 ( W i n d o w s N T 6 . 1 ; W O W 6 4 ) A p p l e W e b K i t / A c c e p t : * / * R e f e r e r : h t t p : / / 1 9 2 . 1 6 8 . 0 . 1 7 7 / A c c e p t - E n c o d i n g : g z i p , d e f l a t e , s d c h A c c e p t - L a n g u a g e : e n - U S , e n ; q = 0 . 8 c l i e n t d i s c o n n e c t e d 26 / 44
  9. Example #2 Web Client Single Request # i n c

    l u d e < S P I . h > # i n c l u d e < E t h e r n e t . h > b y t e m a c [ ] = { 0 x D E , 0 x A D , 0 x B E , 0 x E F , 0 x F E , 0 x E D } ; I P A d d r e s s i p ( 1 9 2 , 1 6 8 , 0 , 1 7 7 ) ; c h a r s e r v e r [ ] = " w w w . g o o g l e . c o m " ; E t h e r n e t C l i e n t c l i e n t ; v o i d l o o p ( ) { i f ( c l i e n t . a v a i l a b l e ( ) ) { c h a r c = c l i e n t . r e a d ( ) ; S e r i a l . p r i n t ( c ) ; } i f ( ! c l i e n t . c o n n e c t e d ( ) ) { S e r i a l . p r i n t l n ( ) ; S e r i a l . p r i n t l n ( " d i s c o n n e c t i n g . " ) ; c l i e n t . s t o p ( ) ; w h i l e ( t r u e ) ; } } v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) ; w h i l e ( ! S e r i a l ) { } i f ( E t h e r n e t . b e g i n ( m a c ) = = 0 ) { S e r i a l . p r i n t l n ( " F a i l e d t o c o n f i g u r e E t h e r n e t u s i n g D H C P " E t h e r n e t . b e g i n ( m a c , i p ) ; } d e l a y ( 1 0 0 0 ) ; S e r i a l . p r i n t l n ( " c o n n e c t i n g . . . " ) ; i f ( c l i e n t . c o n n e c t ( s e r v e r , 8 0 ) ) { S e r i a l . p r i n t l n ( " c o n n e c t e d " ) ; c l i e n t . p r i n t l n ( " G E T / s e a r c h ? q = a r d u i n o H T T P / 1 . 1 " ) ; c l i e n t . p r i n t l n ( " H o s t : w w w . g o o g l e . c o m " ) ; c l i e n t . p r i n t l n ( " C o n n e c t i o n : c l o s e " ) ; c l i e n t . p r i n t l n ( ) ; } e l s e { S e r i a l . p r i n t l n ( " c o n n e c t i o n f a i l e d " ) ; } } 29 / 44
  10. Example #3 Web Client Repeated Requests # i n c

    l u d e < S P I . h > # i n c l u d e < E t h e r n e t . h > b y t e m a c [ ] = { 0 x D E , 0 x A D , 0 x B E , 0 x E F , 0 x F E , 0 x E D } ; I P A d d r e s s i p ( 1 9 2 , 1 6 8 , 0 , 1 7 7 ) ; c h a r s e r v e r [ ] = " w w w . a r d u i n o . c c " ; E t h e r n e t C l i e n t c l i e n t ; u n s i g n e d l o n g l a s t C o n n e c t i o n T i m e = 0 ; c o n s t u n s i g n e d l o n g p o s t i n g I n t e r v a l = 1 0 L * 1 0 0 0 L ; v o i d s e t u p ( ) { S e r i a l . b e g i n ( 9 6 0 0 ) ; w h i l e ( ! S e r i a l ) { } d e l a y ( 1 0 0 0 ) ; E t h e r n e t . b e g i n ( m a c , i p ) ; S e r i a l . p r i n t ( " M y I P a d d r e s s : " ) ; S e r i a l . p r i n t l n ( E t h e r n e t . l o c a l I P ( ) ) ; } v o i d l o o p ( ) { i f ( c l i e n t . a v a i l a b l e ( ) ) { c h a r c = c l i e n t . r e a d ( ) ; S e r i a l . w r i t e ( c ) ; } i f ( m i l l i s ( ) - l a s t C o n n e c t i o n T i m e > p o s t i n g I n t e r v a l ) { h t t p R e q u e s t ( ) ; } } v o i d h t t p R e q u e s t ( ) { c l i e n t . s t o p ( ) ; i f ( c l i e n t . c o n n e c t ( s e r v e r , 8 0 ) ) { S e r i a l . p r i n t l n ( " c o n n e c t i n g . . . " ) ; c l i e n t . p r i n t l n ( " G E T / l a t e s t . t x t H T T P / 1 . 1 " ) ; c l i e n t . p r i n t l n ( " H o s t : w w w . a r d u i n o . c c " ) ; c l i e n t . p r i n t l n ( " U s e r - A g e n t : a r d u i n o - e t h e r n e t " ) ; c l i e n t . p r i n t l n ( " C o n n e c t i o n : c l o s e " ) ; c l i e n t . p r i n t l n ( ) ; l a s t C o n n e c t i o n T i m e = m i l l i s ( ) ; } e l s e { S e r i a l . p r i n t l n ( " c o n n e c t i o n f a i l e d " ) ; } } 30 / 44
  11. IoT Protocols The IoT needs standard protocols. Two of the

    most promising for small devices are MQTT and CoAP. MQTT gives flexibility in communication patterns and acts purely as a pipe for binary data. CoAP is designed for interoperability with the web. Both MQTT & CoAP: Are open standards Are better suited to constrained environments than HTTP Provide mechanisms for asynchronous communication Run on IP Have a range of implementations See: MQTT and CoAP, IoT Protocols 32 / 44
  12. Architecture CoAP packets are much smaller than HTTP TCP flows.

    Bitfields and mappings from strings to integers are used extensively to save space. Packets are simple to generate and can be parsed in place without consuming extra RAM in constrained devices. CoAP runs over UDP, not TCP. Clients and servers communicate through connectionless datagrams. Retries and reordering are implemented in the application stack. Removing the need for TCP may allow full IP networking in small microcontrollers. CoAP allows UDP broadcast and multicast to be used for addressing. CoAP follows a client/server model. Clients make requests to servers, servers send back responses. Clients may GET, PUT, POST and DELETE resources. CoAP is designed to interoperate with HTTP and the RESTful web at large through simple proxies. Because CoAP is datagram based, it may be used on top of SMS and other packet based communications protocols. CoAP CoAP is the Constrained Application Protocol from the CoRE (Constrained Resource Environments) IETF group. Architecture Like HTTP, CoAP is a document transfer protocol. Unlike HTTP, CoAP is designed for the needs of constrained devices. 33 / 44
  13. MQTT MQTT is a publish/subscribe messaging protocol designed for lightweight

    M2M communications. It was originally developed by IBM and is now an open standard. It was designed in 1999 for use on satellites and as such is very light-weight with low bandwidth requirements making it ideal for M2M or IoT applications. Architecture MQTT has a client/server model, where every sensor is a client and connects to a server, known as a broker, over TCP. MQTT is message oriented. Every message is a discrete chunk of data, opaque to the broker. Every message is published to an address, known as a topic. Clients may subscribe to multiple topics. Every client subscribed to a topic receives every message published to the topic. 34 / 44
  14. MQTT For example, imagine a simple network with three clients

    and a central broker. All three clients open TCP connections with the broker. Clients B and C subscribe to the topic temperature . At a later time, Client A publishes a value of 22.5 for topic temperature . The broker forwards the message to all subscribed clients. The publisher subscriber model allows MQTT clients to communicate one-to-one, one-to- many and many-to-one. 35 / 44
  15. MQTT - Publish / Subscribe The publish / subscribe (often

    called pub-sub) pattern lies at the heart of MQTT. It's based around a message broker, with other nodes arranged around the broker in a star topology. This is a very different model to the standard client/server approach, and at first it might seem a little strange, but the decoupling it provides is a huge advantage in many situations. Clients can publish or subscribe to particular topics which are somewhat like message subjects. They are used by the broker to decide who will receive a message. Topics in MQTT have a particular syntax. They are arranged in a hierarchy using the slash character (/) as a separator, much like the path in a URL. So a temperature sensor in your kitchen might publish to a topic like sensors/temperature/home/kitchen. See: Zoetrope 36 / 44
  16. Project #2 13 12 11 10 9 8 7 6

    5 4 3 2 L 5V A0 ANALOG IN AREF 1 GND TX RX RESET 3V3 A1 A2 A3 A4 A5 VIN GND GND DIGITAL (PWM= ) Arduino TM IOREF ICSP ICSP2 ON POWER 0 1 TX0 RX0 RESET 1 1 5 5 10 10 15 15 20 20 25 25 30 30 A A B B C C D D E E F F G G H H I I J J 13 12 11 ETH 9 8 7 6 5 SDCS 3 2 0 1 TX RX AREF GND 5V A0 ANALOG IN TX RX RESET 3V3 A1 A2 A3 A4 A5 VIN GND GND DIGITAL (PWM SPI ) SCL SDA < IOREF ICSP CS < < Previous Circuit + Ethernet Shield 38 / 44
  17. 39 / 44 Additional SW Checklist: mosquitto message broker MQTTLens

    Chrome Ext./App pubsubclient lib @knolleary
  18. AREF GND RESET 3V3 L TX RX USB EXT PWR

    SEL PWR ICSP TX RX 3 1 2 1 1 1 0 1 9 8 DIGITAL 7 6 5 4 3 2 1 0 1 5V Gnd POWER www.adruino.cc ANALOG IN Vin 0 1 2 3 4 5 ADRUINO Arduino - Sensor Node Publish data to the topic sensors/led/status every 2 seconds. These values are the actual device state with considering local input to the sensors (potentio and push button) The data consist of a status (either "ON" or "OFF") and of an intensity (any integer ranging 0 - 254) in the following JSON format: { " d a t a " : { " s t a t u s " : " O N " , " i n t e n s i t y " : 2 0 0 } } 40 / 44
  19. Refs 1. Arduino - Official Site | Tutorials 2. Guide

    - Getting Started | Windows 3. Tutorials - WebClient | WebClientRepeating | EthernetBegin 4. Playground - WebClient POST 5. MQTT and CoAP, IoT Protocols 6. A Brief, but Practical Introduction to the MQTT Protocol and its Application to IoT | Zoetrope 7. Earthshine Design, Arduino Starter Kit Manual: A Complete Beginners Guide to the Arduino 43 / 44