IOS to Arduino Hacking Tips
Technology has been on a fast track to helping our lives since the computer. We’ve seen software to help us with mundane paperwork and calculation type tasks, but now technology is reaching an entirely new level. The ease with which we’re able to control the physical world from something as common place as our smartphones is here!
Let’s talk a little bit about the Arduino and its insane capacity to control our physical world from a mobile device.
There are already a couple good resources on setting up the iPhone to communicate with an Arduino. I recommend going through this tutorial first:
http://makeprojects.com/Project/Connect-an-iPhone-iPad-or-iPod-touch-to-Arduino-with-the-Redpark-Serial-Cable/1130/1
Currently, one of the simplest ways to make your iPhone control a motor, array of LED lights, or a household appliance is to use serial communication. Serial communication is a standard protocol that allows you to transmit bytes over a cable link. The Red Park cable allows you to convert an iPhone port to a traditional serial communication port that you can feed into the Arduino. This gives you the ability to communicate messages between your iPhone/iPad and a physical device connected to an Arduino.
When communicating via the serial communication port, one of the more complicated tasks is managing the messages that go between the iPhone device and the Arduino. There are a range of communication protocols that you can define using the byte length characters that can be sent over serial communication. We are going to go over two basic types of communication: state-based execution and a data load sequence.
1) state-based execution
A simple switch communication protocol is as easy as it sounds. Within your Arduino code, you will read in the serial data:
byte cmd;
cmd = Serial.read();
and then create a switch statement to execute different functions based on the “read-in” character (which indicates the desired state).
switch(cmd){
case ‘p’:
//play function
break;
case ‘u’:
//pause function
break;
case ‘r’:
//resume function
break;
2) Load SequenceA more complex implementation of Serial communication may require you to actually transmit useful data between the iPhone and the Arduino. In this case, you would need to define a protocol to communicate that information. We will again use an implementation of the flag-based execution, but will add a character ‘l’ to define a mode to load data.
case ‘l’:
//load function
break;
Next we need to define the data package that we will be sending to the Arduino. An example would be to define the number of bytes for necessary metadata and then assume the remainder of the package is the described data. An example of metadata would be to have the first 4 bytes of each data package contain the size of the data package by taking the sum of the first 4 bytes. This allows you to correctly parse the data in the Arduino code.We should then define the load function that will actually read in data from the iPhone. This function will need to read in a series of data pieces from the iPhone. The first piece would be the meta data so you could immediately set a mode for reading the meta data as follows (mode = 1):
if(mode ==-1){
if(index < 4){
dataLength = dataLength+Serial.read();
index++;
}
}
This bit of code will read in the meta data while in “meta data” mode and give you the total data package length.With the data package length, you can then easily read in the remainder of the data using a similar mode for “data read” (mode = 2):
if(mode ==’2’){
if(index < dataLength){
data[index] = S...
0 comments
[ 3.4 ms ] story [ 9.1 ms ] threadNo comments yet.