Arduino Tutorial: Networked Temperature Sensor with Swift

In this Arduino Tutorial, you’ll work with a temperature sensor and the companion iPhone app in Swift to connect with it! By Tony Dahbura.

Leave a rating/review
Save for later
Share

Monitor the temperature around you from your iPhone!

Monitor the temperature around you from your iPhone!

Monitor the temperature around you from your iPhone!

Update note: Team member Tony Dahbura has updated this tutorial for iOS 8 and Swift! If you already built the circuit from the previous tutorial, you can skip ahead to the Swift portion of this tutorial.

If you’re like me, you wonder whether you should wear a jacket before heading outside. You might not have time to check outside your front door, but you always have time to check your iPhone! :]

The problem is, most weather apps tell you about the temperature in a nearby city or suburb – but not necessarily about local conditions. And what about that aquarium you would like to monitor, or your mini indoor greenhouse?

This is a job for the latest open source microcontroller – the Arduino.

In this tutorial, you’ll build a networked temperature-monitoring station with multiple temperature probes you can place in different locations. To tie it all together, you’ll also build the companion iPhone app to check the readings.

This tutorial assumes you have been through some of the earlier tutorials on the Arduino. If not, you may want to check out our beginner Electronics for iPhone Developers tutorial series before starting.

Getting Started

First things first, download the starter project and components for this tutorial from here. The zip file includes various libraries you’ll need and a starter iPhone Swift project for later.

This tutorial requires some specific hardware components to interface with the temperature probes. You’ll be making use of a microcontroller (the Arduino), an Ethernet interface, and connecting status and temperature probes. If you’ve completed other Arduino tutorials on the site, you should have some of these parts – and as a budding electronics hobbyist, now is your chance to begin a collection of reusable modules!

Buildallthethings

The full parts list of required components is below, with URLs to purchase them. The total cost of all the parts is approximately $160 USD. If this seems like a lot of money, remember that you are stocking yourself with plenty of components that you can tinker with beyond this tutorial. If you’ve done previous tutorials, then you already have an Inventor’s Kit and maybe the Ethernet Shield, which drops the total price to $38.00.

For the cheaper components, it makes sense to order a few extra to have around and use for other projects.

Parts List

inventorskit

ethernetshield

stackableheaders

groveshield

Once you’ve gathered your parts together, it’s time to get started!

Installing the Arduino IDE

You need to install the Arduino Integrated Development Environment if you haven’t already. The IDE is available at http://arduino.cc/en/main/software. Download the appropriate version for your platform, Mac OS X of course!

Note:There is a version of the IDE for OS X that supports JDK 1.6 and an experimental version for Java 7+. I recommend using the experimental version for Java 7+.

Note:There is a version of the IDE for OS X that supports JDK 1.6 and an experimental version for Java 7+. I recommend using the experimental version for Java 7+.

Install the IDE by unzipping the file and placing it in your Applications folder. The IDE does require Java, so if you don’t have Java installed, open a Terminal window and type java -version to have your Mac prompt to install the language. At the prompt click the More Info… button and you will be taken to the Java download site. Download the JDK download button and select the version for Mac OS X x64. Download the latest JDK and install it.

javainstall

A couple of seconds later, you’ll see a dialog on your screen asking if you want to download and install Java. Click the Install button.

A Little Electronics Theory

Your project will make use of Arduino shields. Shields are special boards you can purchase that support a specific function, analogous to classes when building a program.

The Grove Shield brings a bunch of connectors to the board, allowing you to plug in many different sensors using one common connector.

groveshield

Alternatively, you could wire your sensors directly to the breadboard, but these connectors ensure you don’t plug the wrong wires into the wrong pins and keep things looking neat. Seeed Studio makes many other Grove connectors that you can try out once you get the hang of working with the Arduino.

Your temperature probe uses a technology called 1-Wire bus. This technology is a bus system originally designed by Dallas Semiconductor that allows multiple devices to reside on a single data pin. The bus has a single master – the Arduino – and various workers that each sit on the bus and are identified by their own unique 64-bit serial number.

Using 1-Wire bus, each digital pin can support up to 255 devices! For more on 1-Wire bus, see this wiki page.

Relax – there is a library to handle all the timing for you!

HardwareYes

OK, enough theory! Let’s get back to building your temperature-monitoring station.

Installing the Libraries

While you’re waiting for all packages to arrive in the mail, you can install the various libraries you’ll need to talk to the shields. The Arduino IDE already includes the libraries for communicating with the Ethernet Shield.

The temperature sensors need a library for the 1-Wire portion, and Dallas Semiconductor has some useful routines specifically for the probes. These libraries are all in the tutorial download in the folder Arduino_Libraries.

To install the libraries, first find the Arduino_Libraries folder in the resources you downloaded earlier and double click both files to unzip them into folders:

Unzipping files

Then select the icon for the SDK and Show Package Contents. The window will display a folder called Contents. Install the DallasTemperature and OneWire folders in the Contents/Java/libraries folder, as shown:

arduino_libraryinstalled

If you had the Arduino IDE open, Quit and restart to have it load the new libraries. That’s it! Check to see if they’ve loaded successfully by going to File/Examples and looking for DallasTemperature and OneWire in the menu.

arduino_menu_sdk

Now it’s time to do some wiring!

Tony Dahbura

Contributors

Tony Dahbura

Author

Over 300 content creators. Join our team.