# Stream iPhone Sensor Data to Adafruit IO

## Overview

Sending your&nbsp;iPhone or iPad sensor data to [Adafruit IO](https://io.adafruit.com) is a great way to visualize your sensor data and also connect to an IO project from your mobile device. Adafruit IO also provides UI elements like sliders, charts, buttons, and switches to help you with your IoT (Internet of things) projects without extra programming.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/675/medium800/projects_dashboard.png?1514333756)

Info: 

In this guide, I'll take you through the steps necessary to stream your iOS iPhone or iPad sensor data to Adafruit IO using the&nbsp; **Swift&nbsp;** programming language. iOS mobile devices contains a handful of sensors such as&nbsp;[Gyroscopic sensors](https://en.wikipedia.org/wiki/IPhone#Gyroscopic_sensor), a&nbsp;[Barometer](https://en.wikipedia.org/wiki/IPhone#Barometer), [Magnetometer](https://en.wikipedia.org/wiki/IPhone#Magnetometer)&nbsp;and much more. In this guide we'll be sending our&nbsp;[Accelerometer sensor&nbsp;](https://en.wikipedia.org/wiki/IPhone#Accelerometer)data to our Adafruit IO account feed.&nbsp;This guide doesn't require an extensive knowledge of Swift.&nbsp;

For reference, this guide provides an example app called&nbsp; **IO Connect Example**. This example app&nbsp;sends your iPhone accelerometer sensor data to Adafruit IO.

## In this learn guide we will:

• Setup an iOS app project in Xcode.

If you don't have Xcode installed on your Mac computer, check out the&nbsp;[Installing Xcode](../../../../introduction-to-ios-development?view=all#installing-xcode)&nbsp;portion of the [Introduction to iOS Development](../../../../introduction-to-ios-development?view=all) learn guide.

•&nbsp;Create a function that helps display accelerometer data updates and a function to stop updates.

• Learn about REST to make a POST request with the REST API.&nbsp;

• We'll set up labels in the main.storyboard to display your accelerometer data. We will also add a switch to start and stop updates.

Your completed project will consist of an iOS app that sends our iPhone or iPad&nbsp;Accelerometer sensor data to Adafruit IO using a **HTTP**"POST" request and **JSONSerialization**.

## Before we start...

• Make sure your **Xcode IDE** is up-to-date ( **version 8.3.3 or newer** )

• While in Xcode, make sure the **development target is set to 10.3** or higher.

• If you haven't setup your **Adafruit IO account** with a **Feed** and **Dashboard** , you should do so before beginning the guide.

If you're not familiar or if you need help getting started with Adafruit IO, check out our learn guide:

[Get started with Adafruit IO](../../../adafruit-io/getting-started?view=all)

# Downloading IO Connect Example

You can download the project here:

[Download IO Connect Example](https://github.com/adafruit/IO-Connect-Example/archive/master.zip)
Once the file is downloaded, locate and click on the&nbsp; **IO Connect Example.xcodeproj** &nbsp;file and it will open up the project in Xcode.

Once we have the project open, select your iOS device's **scheme** &nbsp;in **Xcode**.

Info: 

![](https://cdn-learn.adafruit.com/assets/assets/000/044/436/medium800/projects_gui.png?1501256746)

Now just press the **Run** button to test the app on your iOS device.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/437/medium800/projects_feather_www.png?1501257320)

If all goes well, the **IO Connect Example** &nbsp;app will run on your iOS device and you'll be able to use the app as a reference while you explore the guide.

# Stream iPhone Sensor Data to Adafruit IO

## Getting Started

First, open **Xcode** and create a new project. The app we're making will be a **single-view application**. Select the Single-View Application icon then proceed by clicking **Next**.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/453/medium800/projects_ScreenS2.png?1501273361)

Now give your app a name in the Product Name text field. Once you've chosen a product name, make sure **Use Core Data** , **Include Unit Tests** and **Include UI Tests** are all **unchecked** before you click&nbsp; **Next** to save your project on your computer.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/452/medium800/projects_screenS1.png?1501272566)

Before we start, check out the project's file hierarchy on the left side of the project window. The **View Controller** file is where you'll&nbsp;be entering your&nbsp;code and the **Main.storyboard** file is where you'll&nbsp;be adding UI to your app.

![](https://cdn-learn.adafruit.com/assets/assets/000/048/966/medium800/projects_Screen_Shot_2017-12-07_at_11.20.37_AM_copy.png?1512663778)

In the **ViewController.swift&nbsp;** file, we'll begin by&nbsp;importing the **CoreMotion** &nbsp;framework to our project. Below the `import UIKit`&nbsp;we will add:``

```auto
import CoreMotion
```

The `CoreMotion`&nbsp;framework gives us the ability to&nbsp;receive and handle accelerometer data and other motion data&nbsp;such as gyroscope and barometer data for our app.

Next, we'll gain access to our acceleration data, so we'll&nbsp;create an instance of **CMMotionManager** to access it in the **ViewController** class.

```auto
var motionManager = CMMotionManager()
```

CMMotionManager provides services like&nbsp;accelerometer data, rotation-rate data, magnetometer data, and other device-motion data.

Before we go further, let's set up a user interface so that we can see our accelerometer sensor data.

# Stream iPhone Sensor Data to Adafruit IO

## Setting up the UI

The user interface will be pretty simple since we're only displaying one value.&nbsp;In the **Main.storyboard** we're going to&nbsp;give our app two labels and a switch. One of the labels will&nbsp;display our accelerometer data as it updates and our switch will stop and start the updates.

Let's create our labels.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/372/medium800/projects_rn2.png?1501173684)

Select the **Main.storyboard** file.&nbsp;You can find the label and switch in the **&nbsp;Object Library** on the right lower corner of our project window.

Click and drag two **Labels** to the **center** of the storyboard. Place one Label over the other Label. It should look like this.&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/051/622/medium800/sensors_Screen_Shot_2018-03-07_at_9.14.49_AM_copy.png?1520432264)

Then search for a **Switch** , and do the same.

Change the top label's text to "iPhone Accelerometer-X Data:" by double clicking the label.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/240/medium800/projects_layout.png?1501005551)

Then, open the assistant editor menu. Your&nbsp;main.storyboard and view controller class should appear side-by-side.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/368/medium800/projects_vind.png?1501118975)

We'll need to&nbsp;create a reference to one of our labels. Select the bottom&nbsp;label then **control+drag and drop** to the view controller window.

A smaller window will pop up, here you'll give the label its reference name.&nbsp;For the sake of the project, we'll name this reference _accelTagX._

![](https://cdn-learn.adafruit.com/assets/assets/000/049/698/medium800/projects_acceltag.png?1514402228)

We'll do the same with the switch UI and we will name the switch _accelSwitch_.

Select the switch again, we'll **control+drag and drop** &nbsp;into the view controller, but this time in the small pop-up window, we'll select the **Connection** drop down menu and select **Action** instead of Outlet, then we'll name the action " **stateChange**".

![](https://cdn-learn.adafruit.com/assets/assets/000/049/699/medium800/projects_stateChange.png?1514403855)

After you've done that, in the&nbsp; **Type** drop down option, change **Any** to **UISwitch**. When you're all done, hit **Connect**.

Let's head back to our view controller to create a function that will get us our accelerometer data.

# Stream iPhone Sensor Data to Adafruit IO

## Getting Accelerometer data

Now, in our **ViewController.swift** file we're going to create a function that will give us our accelerometer data. Here's the function that I've created:

```auto
func startAccelerometerX () {
        motionManager.accelerometerUpdateInterval = 2.5
        motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: {
            (accelerData:CMAccelerometerData?, error: Error?) in
            if (error != nil ) {
                print("Error")
            } else {
                
                let accelX = accelerData?.acceleration.x
                self.accelTagX.text = String(format: "%.02f", accelX!)                
                print("Accelerometer X: \(accelX!)")
            }
        })
    }
```

First, we'll create a function called startAccelerometerX. Within our function we'll&nbsp;call the`accelerometerUpdateInterval` method to receive updates of the accelerometer's current x-axis position in intervals of the time we choose.

Here, we'll set the interval time to 2.5 seconds:

```auto
motionManager.accelerometerUpdateInterval = 2.5
```

Next, we'll start acceleration updates with the&nbsp;`startAccelerometerUpdate` method. This method starts accelerometer updates on an operation queue with a specified handler.

```auto
motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: {}
```

Within the handler, if we run into an error, the string "error" will be printed in the&nbsp;console. If we don't encounter an error, we'll run the&nbsp;else statement&nbsp;where we'll&nbsp;create a constant called accelX and give it the value of the acceleration data on the x-axis:

```auto
 let accelX = accelerData?.acceleration.x
```

Now that we have our accelerometer data, we'll give our **accelTagX** label the **accelX** value so that our label will display our accelerometer data.

```auto
self.accelTagX.text = String(format: "%.02f", accelX!)
```

Now we are going to create a new function that stops the&nbsp;startAccelerometerX function from updating.

Create a new function, and call it&nbsp; **stopAccelerometerX**.&nbsp;This one is pretty straightforward, we'll call the stopAccelerometerUpdates method to stop accelerometer updates. Then we're going to change the accelTagX label to "--".

```auto
    func stopAccelerometerX () {
        self.motionManager.stopAccelerometerUpdates()
        self.accelTagX.text = "--"
        print("Accelerometer X Stopped")
    }

```

Ok, let's test it out. In the`viewDidLoad` function lets call our startAccelerometerX function to make sure everything is running as it should.

```auto
    override func viewDidLoad() {
        super.viewDidLoad()
        startAccelerometerX()
    }

```

Hit the **Build** button to run your app. If everything runs smoothly, the app should launch on your iOS device.&nbsp; As you turn your device, you should see the accelX label display your iOS accelerometer data as your device tilts.

If your label is not updating, try going through the guide again or look at the IO Connect Example app.

Next, we are going to send our **accelX** data to **Adafruit IO** using the REST "POST" method.

# Stream iPhone Sensor Data to Adafruit IO

## Sending Data with REST "POST" Method

## What is REST?

The **REST API** is an application program interface that uses **HTTP** request method to transfer data between a client and a server. REST&nbsp;is based on REpresentational State Transfer, the architectural style that underpins the internet.

We use **HTTP** resource methods for REST. There are a handful of different methods:

• **POST** - Creates new resources or creates new data. When we creating and sending newer data.

• **GET** - Gives you the ability to read and receive new data.

• **PUT** - This method is usually used to updating or replace data that's being sent.

• **DELETE** - This method is pretty straightforward. This method deletes data.

For this guide we'll be using the "POST" method.

## Post Data to Adafruit IO

Since we've previously collected accelerometer data, we'll continue by sending that data to your Adafruit IO feed. In the **ViewController.swift** file, we'll create a function named&nbsp;`postAccelerometerDataX()`.

Then we'll create a constant dictionary and attach our accelerometer data to the dictionary in a string format:

```auto
let parameters = ["value": "\(String(format: "%.02f", (motionManager.accelerometerData?.acceleration.x)!))"]
```

Now we're going to setup the URL request.&nbsp;We'll use a&nbsp;guard let statement so that we can check if the URL we've given is valid:

```auto
guard let url = URL(string: "https://io.adafruit.com/api/feeds/your-Feed-Key-Here/data.json?X-AIO-Key=Your-A-IO-Key-Here) else { return }
```

As you can see, there are placeholders in the URL Endpoint " **Your-Feed-Key-Here**" and " **Your-A-IO-Key-Here**".

You'll need to add your **Feed Key** and **Adafruit IO Account Key** here.&nbsp;To find your Feed Key, click on the Feeds tab on the left side of the Adafruit IO interface to access your **Feeds**  **page** :

![](https://cdn-learn.adafruit.com/assets/assets/000/049/672/medium800/projects_feedKey.png?1514328509)

Once here, look to your right and your feed key should be below the **Key** column. Copy the key specific to the feed that you'll be using and replace&nbsp;" **Your-Feed-Key-Here**" with your Feed Key.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/674/medium800/projects_feedKey2.png?1514328639)

Next, to get your Adafruit IO account Key, on your left you should see " **View AIO Key**". Click on that, and you should see a pop-over menu that displays your Account username and Active Key. The Active Key is your Adafruit IO Key.

Copy the key and replace&nbsp;" **Your-A-IO-Key-Here**" with your Adafruit IO Key.

![](https://cdn-learn.adafruit.com/assets/assets/000/049/673/medium800/projects_AIOKEY1.png?1514328596)

Next, we'll create a request variable and give it the value of the URLRequest:

```auto
 var request = URLRequest(url: url)

```

Then,&nbsp;we'll give variable request&nbsp;a HTTP `POST` method:

```auto
request.httpMethod = "POST"
```

Now, we&nbsp;use the&nbsp; **JSONSerialization** class to convert JSON into Swift data types like our Dictionary "parameters".

```auto
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
```

This&nbsp;JSONSerialization&nbsp;method,&nbsp;returns a value of type **Any** and throws an error if the data couldn’t be parsed.

Now we'll pass our request to session.dataTask.

```auto
session.dataTask(with: request) { (data, response, error)
```

This creates a task that sends our contents to a&nbsp;specified URL, then calls a handler upon completion. The completion handler is called when the request is completed. This handler is executed on the delegate queue.

This completion handler takes the following parameters:

&nbsp; &nbsp; • **Data** - The data returned by the server.

&nbsp; &nbsp; • **Response** - An object that provides response metadata, such as HTTP headers and status code.&nbsp;

&nbsp; &nbsp; • **Error** - An error object that indicates why the request failed, or `nil` if the request was successful.

The remainder of the function will print in our data in the console or print an&nbsp;error message. Here's the whole function in it's completed form:

```auto
func postAccelerometerDataX() {
        
     let parameters = ["value": "\(String(format: "%.02f", (motionManager.accelerometerData?.acceleration.x)!))"]
     guard let url = URL(string: "https://io.adafruit.com/api/feeds/your-Feed-Key-Here/data.json?X-AIO-Key=Your-A-IO-Key-Here)
        
     var request = URLRequest(url: url)
     request.httpMethod = "POST"
     request.addValue("application/json", forHTTPHeaderField: "Content-Type")
     guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else { return }
     request.httpBody = httpBody
     let session = URLSession.shared
     session.dataTask(with: request) { (data, response, error) in
         if let response = response {
             print(response)
         }
         if let data = data {
             do {
                 let json = try JSONSerialization.jsonObject(with: data, options: [])
                 print(json)
             } catch {
                 print(error)
             }
         }
      }.resume()
 }
```

Ok, now&nbsp;we're going to finish up our app.

# Stream iPhone Sensor Data to Adafruit IO

## Finishing Up

Previously, we created a function that would send your accelerometer data to Adafruit IO. Now we need to call this function whenever there's an update. We can call our&nbsp;`postAccelerometerDataX()` function inside of the `startAccelerometerX()`function so whenever an update happens, our sensor data will be sent to our Adafruit IO feed.&nbsp;

Let's add&nbsp;`self.postAccelerometerDataX()` inside of the else&nbsp;`startAccelerometerX ()`&nbsp;statement.

```auto
    func startAccelerometerX () {
        print("Start Acceleromter Updates")
        motionManager.accelerometerUpdateInterval = 2.5
        motionManager.startAccelerometerUpdates(to: OperationQueue.current!, withHandler: {
            (accelerData:CMAccelerometerData?, error: Error?) in
            if (error != nil ) {
                print("Error")
            } else {
                
                let accelX = accelerData?.acceleration.x
                self.accelTagX.text = String(format: "%.02f", accelX!)
                self.postAccelerometerDataX()
                print("Accelerometer X: \(accelX!)")
            }
        })
    }

```

Now, whenever there are any&nbsp;update events that happens, the postAccelerometerDataX function sends current accelerometer x position data to Adafruit IO.

## Setting up the Switch

With this switch, we'll be able to start and stop updates. In the stateChange function, if the switch is set to On, then we'll run the `startAccelerometerX` function. If not, it'll run the `stopAccelerometerX` function.

```auto
@IBAction func stateChange(_ sender: UISwitch) {
        if (sender.isOn == true){
            startAccelerometerX()
           }else {
            stopAccelerometerX()
        }
    }
```

Ok, lets run our app.

As soon as we open our app, the app will start its updates.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/376/medium800/projects_app.png?1501183899)

Which means that it'll send our sensor data straight to Adafruit IO and can be seen on a graph. Below shows a graph of my acceleration data from my iPhone.

![](https://cdn-learn.adafruit.com/assets/assets/000/044/377/medium800/projects_graphee.png?1501183911)

## Congrats!

You've successfully created your own app that not only displays your iOS device's sensor data, but also sends data using REST to Adafruit IO.

Now that you've completed this learn guide, download Adafruit IO Connect to use as a reference to send different sensor data. This version makes it possible to send&nbsp;accelerometer, magnetometer, or gyroscopic data to Adafruit IO.&nbsp;

[Download Adafruit IO Connect](https://github.com/adafruit/Adafruit-IO-Connect/archive/master.zip)
**Happy&nbsp;Coding :]**


## Related Guides

- [Adafruit IO Connected Animated GIF Display](https://learn.adafruit.com/adafruit-io-connected-animated-gif-display.md)
- [Smart Measuring Cup](https://learn.adafruit.com/smart-measuring-cup.md)
- [Adafruit TSC2046 SPI Resistive Touch Screen Controller](https://learn.adafruit.com/adafruit-tsc2046-spi-resistive-touch-screen-controller.md)
- [Manually bridging MQTT to Adafruit.IO](https://learn.adafruit.com/manually-bridging-mqtt-mosquitto-to-adafruit-io.md)
- [NeoPix Arcade Kit - 1D Arcade Game System - Circuit Playground](https://learn.adafruit.com/neopix-arcade-kit-1d-arcade-game-system-circuit-playground.md)
- [Your First Power Switch Relay Project - Circuit Playground Smart Plug](https://learn.adafruit.com/beginner-first-project-power-switch-relay-circuit-playground-smart-plug.md)
- [3D Printed Adabot Figurine](https://learn.adafruit.com/3d-printed-adabot-figurine.md)
- [CircuitPython Libraries on Linux and Orange Pi](https://learn.adafruit.com/circuitpython-on-orangepi-linux.md)
- [Monitor Your Greenhouse with a No-Code Environmental Sensor](https://learn.adafruit.com/monitor-your-greenhouse-with-a-no-code-environmental-sensor.md)
- [Geofencing with the FONA 808 & Adafruit IO](https://learn.adafruit.com/geofencing-with-the-fona-808-and-adafruit-io.md)
- [Adafruit SensorLab - Gyroscope Calibration](https://learn.adafruit.com/adafruit-sensorlab-gyroscope-calibration.md)
- [All the Internet of Things - Episode Four: Adafruit IO](https://learn.adafruit.com/all-the-internet-of-things-episode-four-adafruit-io.md)
- [Mario Magic Wand](https://learn.adafruit.com/mario-magic-wand.md)
- [Adafruit LIS3DH Triple-Axis Accelerometer Breakout](https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout.md)
- [Quickstart: Adafruit IO WipperSnapper ](https://learn.adafruit.com/quickstart-adafruit-io-wippersnapper.md)
