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 postAccelerometerDataX()
function inside of the startAccelerometerX()
function so whenever an update happens, our sensor data will be sent to our Adafruit IO feed.
Let's add self.postAccelerometerDataX()
inside of the else startAccelerometerX ()
statement.
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 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.
@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.
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.
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 accelerometer, magnetometer, or gyroscopic data to Adafruit IO.
Happy Coding :]
Page last edited March 08, 2024
Text editor powered by tinymce.