I want to communicate between an arduino and an iPhone. The Idea is that the arduino hosts a small server accessible from the local network and the iPhone gives it information by connecting to it and doing a specific http GET request (I hope thats what its called, please don't bite if it isn't). For example: 124.566.123(someIp)/p1 (the GET request with information). "p1" would maybe set pin 1 high. The whole process should be invisible to the user, meaning no browser should be opened or so. Is there a way to do this (using objective-c)?
Asked
Active
Viewed 152 times
1
-
You will need to write an app, in which you do the request. Have a look at JSON and `AFNetworking` . But the app will have to be active to communicate to the arduino. – rckoenes Aug 19 '15 at 12:26
1 Answers
0
There are a number of ways to do this!
When you say "HTTP GET" - you are referring to communicating to the server over port 80.
So, there are multiple ways to do an "HTTP GET" request from an iPhone - the best way to be to use cURL
- which this question should get you on the right track.
All-in-all --- arduinos are not the best chips for web-serving - - - while it is possible, your server-side rendering and whatnot will be limited -
If you are serious about this kind of project, I would recommend upgrading to a Pi or a Beagle Bone Black - - which will not only serve more effectively, but it will also allow you more flexibility -
Good luck!
-
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"http://%@:%@@www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setURL:url]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSError *error; NSURLResponse *response; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; – user5227744 Aug 19 '15 at 12:55
-
Very sorry about the layout. I'm new and I'll have to research how to display code. Anyway: do you think this would work If I just took away the password bits? (I found this code in the question you linked). Is this all you need to get it to work or do I have to add additional code? – user5227744 Aug 19 '15 at 13:02
-
Ultimately, you don't need the `API_USERNAME` or password - that example is a bit more complicated of a request - you'll just want to play with it and find out! And I'll tell you: I learned more from the error logs than I've learned from anything else - so go ahead! cause some crashes and see where you land. And you can always return to SO to ask a new question. Cheers! – rm-vanda Aug 19 '15 at 13:36
-
Thanks for the help!! Its up and running (more or less). One question: I used this tutorial [link](http://codewithchris.com/tutorial-how-to-use-ios-nsurlconnection-by-example/). Which prints data received from google. The data I receive is a big list of numbers. What is this? (not vital for my project, but interesting to know) – user5227744 Aug 19 '15 at 13:55
-
I'm not sure - could you link to a pastebin with the output? I'll be happy to take a look - – rm-vanda Aug 19 '15 at 14:18
-
Ok. Well if the arduino only prints a standard http response, and then a "1" It gives me this: <310d0a> Does this mean anything to you? – user5227744 Aug 20 '15 at 11:17