1

How do I get the data that is returned from a rest API call to Raspberry Pi's sensor?

I have been using postman to manipulate the GET and POST calls so I can see that it works and see the response. The information returned in the xcode console is as follows:

Optional( { URL: >http://192.168.1.83:8000/GPIO/10/value } { status code: 200, headers { "Cache-Control" = "no-cache"; "Content-Length" = 1; "Content-Type" = "text/plain"; Date = "Mon, 29 Feb 2016 23:40:32 GMT"; Server = "WebIOPi/0.7.1/Python3.4"; } })

The code I am using is generated from postman and is as follows:

import Foundation

let headers = [
  "authorization": "Basic ***SNIP***",
  "cache-control": "no-cache",
  "postman-token": "e-4d2-2------d---9d---c0bc72a44f7e"
]

var request = NSMutableURLRequest(URL: NSURL(string:         "http://192.168.1.83:8000/GPIO/10/value")!,
                                    cachePolicy:     .UseProtocolCachePolicy,
                                timeoutInterval: 10.0)
request.HTTPMethod = "GET"
request.allHTTPHeaderFields = headers

let session = NSURLSession.sharedSession()
let dataTask = session.dataTaskWithRequest(request, completionHandler:    { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? NSHTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

I want the value returned by the call to the GPIO pin 10, which will be either a 0 or a 1. If I try to get the value from the completion handlers response it returns a Nil, and pretty much anything else I try just crashes it.

Community
  • 1
  • 1
GSiklos
  • 21
  • 2

1 Answers1

0

ok figured it out! I needed to use....

dispatch_async(dispatch_get_main_queue(),
                {
}

im not sure if this is standard practice, but I can get what I need (yipee) so now I can send some kharma my way for a change!!! feel free to comment or call me a putz but if someone would like to give the correct technical explanation that would be better so I can understand it better as well as others who may benefit from it as well

GSiklos
  • 21
  • 2