In this article we will demonstrate the ability to monitor sensory data and control devices using an Apple Watch. We will demonstrate this through a Watch controlled thermostat application. Keep in mind this is simply a demonstration as to what can be done with a smart watch, let your imagination run as to what could be done. While we are using an Apple Watch in this demonstration keep in mind this could just as easily be accomplished with an Android watch or a Pebble watch. All we are doing is making HTTP requests to Particle's server to get variable information and call functions on our Photon module running custom code.
Full source code for the Apple Watch app and for the application running on the Photon module are available in this article.Step 1 Setup the Hardware
The first step is to setup your hardware. If you are mimicking this application you will need the hardware listed under the Things section of this article. So plug the Photon module into the relay board and connect the temperature sensor to the I2C port on the Relay controller. Then just plug in your 12VDC power source to the relay board. That's it. No soldering, wasn't that painless!Step 2 Setup your Photon Module
Follow instructions here to setup your Particle Photon module for your WiFi network and associate it with your Particle Account so we can interface with it and flash in firmware.Step 3 Flash Firmware into your Photon module
Click the Target Icon on the left and make sure your Particle Photon module is connected and selected here. Click on the Libraries Icon on the left, then search Community Libraries for NCD1Relay. Make sure the Thermostat.ino tab is selected, then click the Use this Example button. Now click the devices icon on the left (looks like a target) and make sure your controller is online and selected. Now click the Flash button on the left(looks like a lightning bolt) to flash the firmware into your controller. After the module is flashed with this firmware and reboots you are ready. The complete source for this firmware is available on our GitHub repo.Step 4 Create the app for Apple Watch.
First you need to make sure you have XCode installed and setup for Apple Watch development. Apple has a lot of documentation on that here. It's really outside the scope of this article to explain the entire setup for getting XCode loaded and ready for Apple Watch development. However there are numerous articles on this subject all over the internet.
If you want to keep things simple just download our Apple Watch application here. Once downloaded, open the .xcodeproj file at watch tutorial/watch tutorial.xcodeproj in the download. This will open the application in XCode.
Now there is one thing that must be done before this will run for you. You need to get the access token for your Particle Account and the ID of the Photon module you are using. These will need to be entered into the Watch App.
The easiest way to get your Particle Access Token and your Particle Photon module ID is through Particle's Build IDE, so open that up if it is not already. Once open, click on the settings cog icon on the left side of the screen. Here you can copy your Access Token; copy that to a text editor. Now click on the devices list icon on the left (looks like a target). Click the drop down arrow to the right of the Photon module you flashed the firmware into. Here you can see the device ID, copy that to your text editor as well.
Now that you have that, go back into the Watch app in XCode. We need to put that information into two different places. Make sure you have the Project Navigator view open where you can see all the files and folders for the project. Open up the watch tutorial WatchKit Extension folder. Under that, open the InterfaceController.swift file. Take a look at lines 14 and 15, here you need to replace the EnterAccessTokenHere and EnterDeviceIDHere text with your access token and device ID. Once that's done, open up the GlanceController.swift file, and do the same thing on lines 14 and 15 in that file.
That's it, you can now build and run the XCode project either on the Apple Watch Simulator or on your physical Apple Watch (some assembly may be required).
Be sure to change things, it's no fun just using something someone else made. I am sure there are all kinds of optimizations that can be made to the Watch App. I do not develop for iOS much and this is the first Watch App I ever attempted. I would love to get the complication and some notifications working. If you do that definitely let me know, would love to do more.