Publicité
Publicité

Contenu connexe

Publicité
Publicité

Arduino basics

  1. LTKA Labs Arduino Basics Eueung Mulyana http://eueung.github.io/ET3010/arduino ET-3010 | Attribution-ShareAlike CC BY-SA 1 / 44
  2. Outline Short Intro Quick Start Networking MQTT 2 / 44
  3. Short Intro 3 / 44
  4. 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
  5.   5 / 44
  6.   6 / 44
  7. 7 / 44
  8. Quick Start 8 / 44
  9. 9 / 44 This Checklist Please: UNO Board Compatible + Acessories Components + Wires ARDUINO IDE
  10. 10 / 44
  11.   11 / 44
  12.   Tools - Config 12 / 44
  13.   Sketch Upload 13 / 44
  14. 14 / 44
  15. 15 / 44
  16. 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 1TX0 RX0 RESET 11 55 1010 1515 2020 2525 3030 A A B B C C D D E E F F G G H H I I J J Fritzing Breadboard 16 / 44
  17. 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) 21 21 12 12 13 2 Part1 LED1 Red (633nm) R1 100Ω R2 10kΩ S1 R3 10kΩ Fritzing Schematic 17 / 44
  18. Project #1 Sketch intinPin =2; intoutPin =3; intpotPin =A0; intstate=LOW; intreading; intprevious=LOW; longtime=0; longdebounce=1000; intpotVal=0; intprevPotVal=0; voidsetup(){ Serial.begin(9600); delay(500); pinMode(inPin,INPUT); pinMode(outPin,OUTPUT); Serial.println("Programstarted..."); } voidloop(){ reading=digitalRead(inPin); if(reading&&(millis()-time>debounce)){ if(previous==LOW){ Serial.println("[PHYSICAL]LEDturnedon");state=HIGH }else{ Serial.println("[PHYSICAL]LEDturnedoff");state=LOW } time=millis(); digitalWrite(outPin,state); previous=state; prevPotVal=potVal-10; } potVal=analogRead(potPin); if((state==HIGH)&&(abs(potVal-prevPotVal)>4)){ analogWrite(outPin,potVal/4); Serial.print("[PHYSICAL]LEDintensity"); Serial.println(potVal/4); prevPotVal=potVal; } } 18 / 44
  19. 19 / 44
  20.   Serial Monitor 20 / 44
  21. Networking 21 / 44
  22. 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 1TX0 RX0 RESET 13 12 11 ETH 9 8 7 6 5 SDCS 3 2 0 1TX 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
  23. 23 / 44
  24. Example #1 Web Server #include<SPI.h> #include<Ethernet.h> bytemac[]={ 0xDE,0xAD,0xBE,0xEF,0xFE,0xED }; IPAddressip(192,168,0,177); EthernetServerserver(80); voidsetup(){ Serial.begin(9600); while(!Serial){} Ethernet.begin(mac,ip); server.begin(); Serial.print("Serverisat"); Serial.println(Ethernet.localIP()); } voidloop(){ //... } EthernetClientclient=server.available(); if(client){ Serial.println("newclient"); booleancurrentLineIsBlank=true; while(client.connected()){ if(client.available()){ charc=client.read(); Serial.write(c); if(c=='n'&&currentLineIsBlank){ client.println("HTTP/1.1200OK");client.println( client.println("Refresh:5"); client.println(); client.println("<!DOCTYPEHTML>");client.println( for(intanalogChannel=0;analogChannel<6;analo intsensorReading=analogRead(analogChannel); client.print("analoginput");client.print(analog client.println("<br/>"); } client.println("</html>"); break; } if(c=='n') {currentLineIsBlank=true;} elseif(c!='r'){currentLineIsBlank=false;} } }//while delay(1); client.stop(); Serial.println("clientdisconnected"); } 24 / 44
  25. 25 / 44
  26. Test $>ping192.168.0.177 Pinging192.168.0.177with32bytesofdata: Replyfrom192.168.0.177:bytes=32time=2msTTL=128 Replyfrom192.168.0.177:bytes=32time=3msTTL=128 Replyfrom192.168.0.177:bytes=32time=2msTTL=128 Replyfrom192.168.0.177:bytes=32time=2msTTL=128 Pingstatisticsfor192.168.0.177: Packets:Sent=4,Received=4,Lost=0(0%loss), Approximateroundtriptimesinmilli-seconds: Minimum=2ms,Maximum=3ms,Average=2ms Serverisat192.168.0.177 newclient GET/HTTP/1.1 Host:192.168.0.177 Connection:keep-alive Cache-Control:max-age=0 Accept:text/html,application/xhtml+xml,application/xml;q=0.9 Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/ Referer:http://192.168.0.177/ Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 clientdisconnected newclient GET/favicon.icoHTTP/1.1 Host:192.168.0.177 Connection:keep-alive User-Agent:Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/ Accept:*/* Referer:http://192.168.0.177/ Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 clientdisconnected 26 / 44
  27.   Browser 27 / 44
  28.   Serial Monitor 28 / 44
  29. Example #2 Web Client Single Request #include<SPI.h> #include<Ethernet.h> bytemac[]={ 0xDE,0xAD,0xBE,0xEF,0xFE,0xED }; IPAddressip(192,168,0,177); charserver[]="www.google.com"; EthernetClientclient; voidloop(){ if(client.available()){ charc=client.read(); Serial.print(c); } if(!client.connected()){ Serial.println(); Serial.println("disconnecting."); client.stop(); while(true); } } voidsetup(){ Serial.begin(9600); while(!Serial){} if(Ethernet.begin(mac)==0){ Serial.println("FailedtoconfigureEthernetusingDHCP" Ethernet.begin(mac,ip); } delay(1000); Serial.println("connecting..."); if(client.connect(server,80)){ Serial.println("connected"); client.println("GET/search?q=arduinoHTTP/1.1"); client.println("Host:www.google.com"); client.println("Connection:close"); client.println(); }else{Serial.println("connectionfailed");} } 29 / 44
  30. Example #3 Web Client Repeated Requests #include<SPI.h> #include<Ethernet.h> bytemac[]={ 0xDE,0xAD,0xBE,0xEF,0xFE,0xED }; IPAddressip(192,168,0,177); charserver[]="www.arduino.cc"; EthernetClientclient; unsignedlonglastConnectionTime=0; constunsignedlongpostingInterval=10L*1000L; voidsetup(){ Serial.begin(9600); while(!Serial){} delay(1000); Ethernet.begin(mac,ip); Serial.print("MyIPaddress:"); Serial.println(Ethernet.localIP()); } voidloop(){ if(client.available()){ charc=client.read(); Serial.write(c); } if(millis()-lastConnectionTime>postingInterval){ httpRequest(); } } voidhttpRequest(){ client.stop(); if(client.connect(server,80)){ Serial.println("connecting..."); client.println("GET/latest.txtHTTP/1.1"); client.println("Host:www.arduino.cc"); client.println("User-Agent:arduino-ethernet"); client.println("Connection:close"); client.println(); lastConnectionTime=millis(); }else{Serial.println("connectionfailed");} } 30 / 44
  31. MQTT 31 / 44
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. That's all for now..   Enough talking Let's get our hands dirty!! 37 / 44
  38. 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 1TX0 RX0 RESET 11 55 1010 1515 2020 2525 3030 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 1TX 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
  39. 39 / 44 Additional SW Checklist: mosquitto message broker MQTTLens Chrome Ext./App pubsubclient lib @knolleary
  40. AREF GND RESET 3V3L TX RX USB EXT PWRSEL 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: { "data":{ "status":"ON", "intensity":200 } } 40 / 44
  41. MQTTLens - Client Node Subscribe to the topic sensors/led/status.  41 / 44
  42. Refs 42 / 44
  43. 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
  44. END Eueung Mulyana http://eueung.github.io/ET3010/arduino ET-3010 | Attribution-ShareAlike CC BY-SA 44 / 44
Publicité