转自http://teachmetomake.com/wordpress/arduino-yun-networking
Arduino Yun for Intermediate Arduino Users:****Using the Onboard Linux Computer to Communicate with Other Computers and the Internet
http://teachmetomake.com/wordpress/arduino-yun-networking
Michael Shiloh****Arduino****m.shiloh@arduino.cc
Tutorial created for OSCON 20141:30pm Sunday, 07/20/2014Link on OSCON schedule
Contents [hide]
1 Please prepare for this tutorial before you arrive:
2 Arduino Yun Overview
3 Selected Yun details
4 Configuring your Yun over WiFi
5 Configuring your Yun over wired ethernet
6 Basic examples6.1 Bridge class
6.2 Process class
6.3 HttpClient class
6.4 Create a web server, and serve data from the IO pins
6.5 More on the Bridge library
7 Bidirectional Server Client exercise
Please prepare for this tutorial before you arrive:
Participants must be comfortable with the Arduino hardware and software, know how to attach sensors and LEDs to an Arduino, and know how to write programs for Arduino
An Arduino Yun, and an Arduino component kit (breadboard, wires, assorted sensors, LEDs, motors, speakers, etc.) is required in order to participate. We have sourced the kit and parts for you, and the cost of this kit is $93. There will be an additional charge of $93 when you register for this tutorial. You will be provided with the kit onsite at the conference with proof of registration.
Participants must have a laptop
Participants must install Arduino 1.6.4 or later from Arduino
Selected Yun details
Linux distribution is OpenWrt, so almost anything that’s true for OpenWrt is true for the Yun. OpenWrt Technical Reference
Serial1 (pins 0 and 1) of Arduino environment is connected to console of the Linux environment and so can’t be used otherwise
No built in 5V regulator, so Vin must never be above 5V. 3.3V generated internally
Best to use microSD card or thumb drive for saving data, scripts, web pages, etc.
Individual resets for WiFi, Linux, 32U432U4 reset requires 2 clicks
WiFi 3 options<5 seconds: Restart WiFi
5 and <30: Reconfigure WiFi
30: Reset OpenWrt-Yun to default state
ResourcesArduino Yun product page
Arduino Yun Getting Started Guide
Excellent set of community curated Yun notes
The Bridge library reference
Configuring your Yun over WiFi
Connect:Yun to laptop with USB cable
Access the Linux console from the Serial Monitor by uploading the YunSerialTerminal example:File->Examples->Bridge->YunSerialTerminal
Remember to select “Yun” in “Tools->Board”.
In the following steps, all the lines you type are in the top line of the serial monitor.
Open your Serial Monitor and select New line line ending and 57600 baud (down near the bottom right corner). Put your cursor in the top line and press your <ENTER> key and you should see a prompt, and possibly a banner:BusyBox v1.19.4 (2014-07-14 20:05:06 CEST) built-in shell (ash)Enter 'help' for a list of built-in commands. _______ ________ __ | |.-----.-----.-----.| | | |.----.| |_ | - || _ | -| || | | || || | |_______|| |_____||||________||| |____| |__| W I R E L E S S F R E E D O M -----------------------------------------------------root@Arduino:/#
Wait until you see the prompt:
root@Arduino:/#
Press and hold the button labelled “WLAN RST” for at least 30 seconds.
Watch the messages in the console and wait until it finishes booting. You will have to put your cursor in the top line and press enter again to see the prompt and the banner.
Verify that the Yun is now an access point by typing:iwconfig <ENTER>
You should see something like this:
o no wireless extensions.eth1 no wireless extensions.wlan0 IEEE 802.11bgn Mode:Master Tx-Power=16 dBm RTS thr:off Fragment thr:off Power Management:off eth0 no wireless extensions.
The important thing is that wlan0 is in “Master” mode.
Find your Yun’s MAC address by typing: ifconfig <ENTER>
Look for the line that says: wlan0 Link encap:Ethernet HWaddr 90:A2:DA:F3:02:19
the code following HWaddr is your Yun’s MAC address. In this case it’s
90A2DAF30219
Using your laptop’s network manager, connect to the network with a name like Arduino Yun-90A2DAF30219. If you see more than one Arduino Yun, look for the one with your Yun’s MAC address.
In your web browser, browse to http://arduino.local****
When asked, the password is ****arduino****
Congratulations! You are connected to your Yun
Configuring your Yun over wired ethernet
Connect:Yun to laptop with USB cable
Yun to network with Ethernet cable
Access the Linux console from the Serial Monitor using the YunSerialTerminal example:File->Examples->Bridge->YunSerialTerminal
Open your Serial Monitor and select New line line ending. Press enter and you should see a prompt, and possibly a banner:BusyBox v1.19.4 (2014-07-14 20:05:06 CEST) built-in shell (ash)Enter 'help' for a list of built-in commands. _______ ________ __ | |.-----.-----.-----.| | | |.----.| |_ | - || _ | -| || | | || || | |_______|| |_____||||________||| |____| |__| W I R E L E S S F R E E D O M -----------------------------------------------------root@MShilohServer:~#
Get the IP address of the wired Ethernetifconfig
Look for the section that is NOT lo (loopback) and NOT wlan (wireless). It’s most likely eth1. Look for the line that starts with:
inet addr:192.168.1.145
Make note of this IP address as it is the way you will access your Yun
Open a web browser and browse to that IP address. Enter password (arduino), clickLOGIN, then click** **CONFIGURE****Change YUN NAME to e.g. YunMShiloh. I suggest you leave the password as is.
Deselect CONFIGURE in CONFIGURE A WIRELESS NETWORK(Wireless network does not allow peer-to-peer)****
At REST API ACCESS select Open
Click Configure and Restart (Might take a couple of minutes to reboot)
Close the browser window
Test by logging in to your Yun via ssh. (Linux: Terminal Mac: Applications -> Utilities -> Terminal. Windows: Download putty). Your user name is root. Accept key and enter your password:$ ssh root@192.168.1.145The authenticity of host '192.168.1.145 (192.168.1.145)' can't be established.RSA key fingerprint is 55:32:43:18:a6:99:99:55:96:74:dc:b5:3e:24:e2:14.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.1.145' (RSA) to the list of known hosts.root@192.168.1.145's password: BusyBox v1.19.4 (2014-07-14 20:05:06 CEST) built-in shell (ash)Enter 'help' for a list of built-in commands. _______ ________ __ | |.-----.-----.-----.| | | |.----.| |_ | - || _ | -| || | | || || | |_______|| |_____||||________||| |____| |__| W I R E L E S S F R E E D O M -----------------------------------------------------root@MShilohServer:~#
Verify that the computer name is the one you set (in my case MShilohServer).
Test that you can see your neighbor’s Yun. Ask your neighbor for the IP address of their Yun, and then use ping to verify that you can see it: ping 192.168.1.107
To exit ping type <CTRL> C
Basic examples
BRIDGE CLASS
Read inputs and control outputs from a web browser. Upload this sketch:
File -> Examples -> Bridge -> Bridge
Control your pin 13 LED by visiting, with your browser, (replace IP address with yours):
http://192.168.1.145/arduino/digital/13/0orarduino.local/arduino/digital/13/0
Read an analog input by visiting:
http://192.168.1.145/arduino/analog/0orarduino.local/arduino/analog/0
More details in the bridge tutorial
Optional exercise: Add some LEDs and sensors, then read and control them from a web browser.
PROCESS CLASS
Execute commands on the Linux side, from a sketch.
File -> Examples -> Bridge -> Process
Process tutorial
HTTPCLIENT CLASS
Create an HTTP client that connects to the internet and downloads content.
File -> Examples -> Bridge -> HttpClient
HttpClient tutorial
CREATE A WEB SERVER, AND SERVE DATA FROM THE IO PINS
(requires microSD card)
File -> Examples -> Bridge -> TemperatureWebPanel
TemperatureWebPanel tutorial
MORE ON THE BRIDGE LIBRARY
Bridge library reference
Bridge tutorials: links under Bridge Libraries on the tutorial home page
Bidirectional Server Client exercise
Get code from github
Practical application
Either client or server can be replaced by process written in any other language, as long as it can open a connection to an arbitrary IP address and port
Netcat or nc
Wayne’s Android controller for an Arduino robot: Android code, Arduino code
Temboo
Create a Temboo account
Create an application name/key pair on Temboo
Create unimportant Gmail account
Open exampleFile -> Examples -> Bridge -> Temboo -> SendAnEmail
Enter Gmail account details and destination address in SendAnEmail sketch. Note that GMAIL_USER_NAME must be full email address e.g. bob@gmail.com
Click on second tab (TembooAccount.h)
Enter Temboo account and application name/key pair
Upload
Time Permitting
Adafruit tutorial
Spacebrew
Arduino Yun Kit of Parts
Cost: $93 (tax included)
1 Arduino Yun
1 USB cable
1 400 point breadboard
30 Jumper wires M-M
LED pack (3R, 3G, 3Y at 3mm and 3R, 3G at 5mm)
2 x 10K ohm trimmer potentiometers with knobs (breadboard mount)
10 x 470 ohm resistors
10 x 10K resistors
1 Light dependent resistor (CdS cell)
1 Thermistor
1 DHT11 temperature/humidity sensor (example)
1 Passive piezo element