Build a WiFi-enabled remote temperature sensor with Arduino Yún

Temp Sensitive

Arduino's new Yún is a complete computer-on-a-chip, with WiFi access and a working version of Linux. We show you how to use Yún to manage a remote temperature sensor.

Lots of people use the now ubiquitous Arduino to automate everything from cocktail dispensers to unmanned aerial vehicles to everyday gadgets that simply make life easier.

The WiFi-enabled Arduino Yún [1] appeared on the market recently. Not only does it have a WiFi chip, it also sports an Atheros AR9331 microprocessor running a slimmed-down version of Linux. What's cool about bolting an Atheros chip to an Arduino is that you can off-load all the networking, connectivity, security, and programming tasks, so your sketches can focus on what they do best  – read inputs, perhaps do a few calculations, and set outputs.

Integrating everything is the Yún's Bridge library, whose functions you can embed in your sketches, so they can talk to other systems on the network. The WiFi chip is also special, because it can either operate connected to a LAN or function as its own access point.

This project grew out of an OSCON talk that I gave on building a remote temperature sensor using an Arduino, some XBee radios, a DS18B20 digital chip, and Linux. In this article, I'll cover the switch to the Yún and how to exchange data with a companion Processing language script that updates a Linux notebook display in a nifty on-screen box. I'll conclude with a rundown on accessing the generated data from the command line.

Engineers, scientists, and designers will see plenty of uses for this type of project. With a bit of tweaking, you could easily prototype other projects that monitor pressure, light, activities, and so on (also see the "My Setup" box).

My Setup

My current machine is an aging Asus X83VM notebook with an Intel Core2 Duo Processor T8400 running at 2.27GHz with 4GB of RAM, an NVidia GeForce 9600GS video chip, and a 1,280x800 screen that I've upgraded with a 750GB, 7,200RPM SATA drive. The notebook has five USB ports, Gigabit Ethernet, 802.11g WiFi, and VGA and HDMI video outputs. A gigabyte of dedicated video memory and an LED back-lit LCD, along with its 14-inch screen, make it the perfect portable Linux notebook. It is also running Xubuntu 13.04 with the 3.8.0-35-generic kernel [2]. I like the simple look of the Xfce desktop [3], which has served me well for the past few years.

The Arduino Yún runs version 1.0 of the Linino firmware. I'm using version 1.5.5 of the Arduino IDE [4] and version 2.0.3 of the Processing language environment [5]. The Yún is powered by a regular mini-USB cable connected to an Asus 5-volt, 2-amp (5V, 2A) power adapter from a Transformer Prime tablet.

Setting Up the Yún

If you've spent any time with the Arduino, you likely know how to use the Arduino IDE via a USB cable. The Yún works with version 1.5.5 and up, so once that's loaded on your Linux notebook, you can power up the little board with a wall wart or USB connection to your notebook. Be aware that the Yún doesn't have a built-in 5V regulator, so using external power is not recommended initially: Just go through the mini-USB power connector and everything will be fine.

Out of the box, the Yún will act as its own access point. Search for it using your notebook's WiFi manager. It should be something like Arduino Yun-{device MAC address}. After connecting, wait a few seconds, then fire up the Arduino IDE. Under the Tools menu, make sure the Board selection points to the Arduino Yún. You'll find the Yún's IP address under Tools | Port, and it also shows up in the bottom right corner of the editing window (Figure  1).

Figure 1: View of the Arduino IDE window.

Once connected, you'll go through the same workflow of writing and editing the code, verifying and compiling the code (under the Sketch menu), and uploading the code (using File | Upload or the Upload button). Don't forget to save your sketch occasionally.

The code is pretty straightforward, with the library calls at the top, the usual communication initialization in the setup() section, and the bulk of the logic living under the loop() section (Listing 1) [6].

Listing 1

Arduino Code

01 #include <OneWire.h>
02 #include <DallasTemperature.h>
03 #include <Bridge.h>
04 #include <YunServer.h>
05 #include <YunClient.h>
06 #include <Console.h>
07
08 #define ONE_WIRE_BUS 2
09 #define PORT 6666
10
11 YunServer server(PORT);
12
13 OneWire oneWire(ONE_WIRE_BUS);
14 DallasTemperature sensors(&oneWire);
15
16 void setup(void)
17 {
18      sensors.begin();
19
20      Serial.begin(115200);
21      Bridge.begin();
22
23      server.noListenOnLocalhost();
24      server.begin();
25      Console.begin();
26      while (!Console) {
27        ;
28      }
29      Console.println("You're Connected");
30 }
31
32 void loop(void) {
33
34       YunClient client = server.accept();
35       int inByte = NULL;
36       Console.println("server waiting");
37
38            while(client.connected()>0) {
39            Console.println("client connected");
40
41            inByte = client.read();
42            if (inByte == '\n') {
43
44                sensors.requestTemperatures();
45                Console.println(sensors.getTempFByIndex(0));
46                client.println(sensors.getTempFByIndex(0));
47                inByte = NULL;
48                }
49            }
50        client.stop();
51        delay(50);
52 }

I chose to implement what I call a simple call-response scheme. The Arduino waits for a connection and then a '\n' from the client. If those conditions are met, it will simply print the temperature read by the DS18B20 sensor as a text string to the client. When the client needs another reading, it sends another '\n', and the Yún sends another text string. This method has a lot of flexibility as far as network usage and rate of updates goes. You'll get an updated temperature every second or once per hour, as defined in the program on the client side.

Note that the sketch uses the DallasTemperature and OneWire libraries. These libraries are needed to create an interface between the Dallas DS18B20 digital temperature sensor  [7] and the Arduino. The sensor works as follows: The Arduino sends a pulse down the OneWire bus, and each device sends back its unique identifier and the value of the sensor. The libraries encode and decode all those details; therefore, you just need a call to read and assign the returned value to a variable. Those calls appear as the sensor.request and sensor.getTempFByIndex calls (Figure 2 shows the sensor hookup).

Figure 2: Breadboard view of the Arduino Yún and the DS18B20 temperature sensor.

The Processing Data-Viewing Sketch

One of the things I like about the Processing language is that you can write a sketch on the Arduino and a companion sketch on your Linux notebook with essentially the same language (Figure 3). The syntax and many of the functions are identical, although different libraries and functions may be applicable to one platform but not the other.

Figure 3: View of the Processing IDE window.

You'll note that there isn't much to the Processing temperature readout sketch (Listing 2). It has a reference to the processing.net library to handle network client/server handshaking and some variable definition at the beginning. The draw() function does the same job as the loop() function on the Arduino. Notice the client.write('\n') in the setup and in the body of the draw function. This is the character sent to the server (Arduino Yún) to tell it to return a reading. The draw function loops endlessly – sending '\n', getting a reading from the server, then updating the displayed text (Figure 4).

Listing 2

Processing Sketch

01 import processing.net.*;
02
03 PFont font;
04 Client client;
05 String temp1 = "0";
06
07 void setup() {
08   client = new Client(this, "192.168.240.1", 6666); /* connect to server */
09   client.write('\n');
10
11   size(310, 200);
12   strokeWeight(5);
13   stroke(255, 160);
14
15   textAlign(LEFT);
16   textFont(createFont("Georgia", 36));
17
18 }
19
20 void draw() {
21
22   background(0);
23
24   temp1 = client.readStringUntil('\n');
25
26   text("temp = " + temp1, 55, 100);
27   print(temp1);
28   delay(1000);
29   client.write('\n');
30   }
Figure 4: Temperature frame displayed on the desktop.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content