I have written some apps for the Iphone so I decided to create my own app. After going thru the Apple documentation which is always good sleep inducing material, I went to Github. Searching there, I found some excellent software created by Robbie Hanson of Deusty LLC. Check out the link: (https://github.com/robbiehanson/CocoaAsyncSocket). Not only was Robbie's software able to do the job, but he had some excellent examples.
Before we get started, I'll list some of the hardware and software requirements for part 1:
Digilent Chipkit Max32
Digilent ChipKit Network Shield
Wireless Router (I'm using a Linksys 3000)
Computer, iPhone and Software:
MacMini Macintosh with Xcode 4.2
MPIDE (latest version 0023)
Iphone (My setup iPhone 4S with iOS 5)
Apple Developers License
A little background information about the hardware and software:
I love the MicroChip line of PIC processors. So when Digilent came out with the Chipkit products, I was really excited and ordered the Max32, Network shield and there Basic I/O shield. I was not disappointed, they have great documentation and install of MPIDE was easy. Being new to the Arduino programming setup, I was concerned on how hard it was to install the Network libraries from Digilent. Digilent provided a very good readme.txt on how to do it. The install was easy.
Normally I do all my programming from a PC. Since I have to use a Mac to write code for the Iphone, I thought it would be nice to use the Mac to also do the programming for the Max32.
Steps to install MPIDE and Networking code on the Mac:
Download the latest:
Use 'FINDER' and go to the Download directory to find the file. Another way to find the file, at the bottom of your web browser, you should see the download. Click the drop down arrow and select 'Show in Finder'.
Double click the download file and the following screen is displayed:
Now dragged the MPIDE icon and put it on your desktop.
Double click the FTDIUSB drivers and the following screen displays:
Now go to the Digilent website and get the network drivers.
Click the download for the Libraries and documentation for using the Network Shield.
When its finished downloading, go to the file in download, create a new folder called 'ChipKit'. Drag the downloaded file into the ChipKit folder. I like to keep my download organized, so everything from Digilent is in the ChipKit folder.
Double click the downloaded file. It should create a new folder 'chipKit Network and USB Libs_v1-4-1'.
In Finder find the following ReadMe.txt. This explains how to install the Network Library. Install the library at this time.
After the Library has been installed, open MPIDE. Select 'File', then 'Examples'. Should look the same as the following photo.
At this point, you should have your Max32 with the Network shield, connected by USB and a Ethernet cable.
I should explain a little more about my Networking setup. I'm using a Linksys 3000 wireless router. I have my MAC ChipKit Network board hardwired with Cat 5 cables to my Linksys 3000. In order for this setup to work, all equipment must be on the same subnet.
In MPIDE, select the following sketch:
In the sketch I used the default IP address and port as shown in the following photo.
The sketch selected is read only. When exiting or saving it will ask for a new location to save the file and leave the original intact.
Throughout this blog, I will be using this IP address and port.
After modifying the IP address and port, click the 'Upload'. If it compiles, after a few seconds, it will be uploaded to your Max32. If you did not install the Digilent Library you will get errors.
Now on to testing the setup. This is where the fun begins.
Go to the Github to get the software from Robbie Hanson.
Click on 'zip' and download the CocoaAsyncSocket software.
Go into 'Finder' and unzip the file. It should create the following:
Load the UdpEchoClient into Xcode by double clicking the UdpEchoClient.xcodeproj. Wait a minute, this is a program to run on the MAC. Thats correct. Run the program. You should see the following:
If your setup is correct and working you should see the following:
The sketch in MPIDE returns 'acknowledged'.
So, this part works, lets move on to the next step. Lets create the actual iPhone app!
In Xcode, leave 'UdpEchoClient' open. We are going to rob code from it.
Create a new project, simple view application, name it 'IPHONE_UDP_CLIENT', class prefix 'JLC'. Select 'Iphone' and click 'Use Automatic Reference Counting'.
Lets create a new group for the 'GCDAsyncUdpSocket' class. Right click on the target, select 'New Group'. Change the Name to 'Udp' in Identity - Group Name.
Go into 'Finder' and select 'GCDAsyncUdpSocket.h' and GCDAsyncUdpSocket.m'. Drag those files into the newly created Udp group.
Add '#import <GCDAsyncUdpSocket.h'> to JLCViewController.h.
At this point try to run the app. You should get about 20 errors. Lets fix them now.
Click on the target, then Build phases. Open up Compile Sources. Find GCDAsyncUdpSocket.m. Double click the file GCDAsyncUdpSocket.m and add '-fno-objc-arc, then click 'done'. If some reason GCDAsyncUdpSocket.m is not there, then press the '+', and include the file.
Try running the app now. All the error messages should be clear.
Now click on JLCViewController.xib and place the following: 3 Labels, 3 Text Fields and 1 button.
Now create the outlets: addrField, portField, messageField.
Also create 1 action: sendData.
Add the following code in JLCViewController.h below @interface JLCViewController :
Add the following code to JLCViewController.m replacing ViewDidLoad to the @end.
There are 2 sections of code that should be of interest:
First, is where the data is sent:
Second, where the data is received back from the Max32:
Lets add some code so when we are done entering text, we can close the keyboard.
In JLCViewController.h add the following:
For the Results text field, do the same as above, but for Keyboard select 'Default'.
Next for the address, port and results field do the following:
Right click on the address field, click on 'Did End On Exit', hold down the ctrl key and drag to First Responder.
Repeat for the port and results text fields.
Run the app. The Address and Port text fields should be already have the IP Address and Port Number. Put 'TEST' in the messageField. Press 'Send'. the app should return 'Results - TEST'.
Download the iPHONE_UDP_CLIENT: