Last active
November 5, 2017 13:01
-
-
Save misternay/8d3226ba384da81bdfc45249c402f70f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void callRestAPI(String jsonString, String host, String url, String key) { | |
WiFiClientSecure client; | |
DynamicJsonBuffer jsonBuffer; | |
Serial.print("Requesting URL: "); | |
Serial.println(" "); | |
if (client.connect(Host, 443)) { //443 | |
Serial.println("Connected"); | |
client.println("POST " + url + key + " HTTP/1.1"); | |
client.println("Host: " + (String)Host); | |
client.println("Connection: close"); | |
client.println("Content-Type: application/json"); | |
client.println("User-Agent: Arduino/1.0"); | |
client.print("Content-Length: "); | |
client.println(jsonString.length()); | |
client.println(); | |
client.print(jsonString); | |
delay(500); | |
} | |
//Read and parse all the lines of the reply from server | |
while (client.available()) { | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
JsonObject& root = jsonBuffer.parseObject(line); | |
if (root.success()) { | |
latitude = root["location"]["lat"]; | |
longitude = root["location"]["lng"]; | |
accuracy = root["accuracy"]; | |
} | |
} | |
Serial.println("closing connection"); | |
Serial.println(); | |
client.stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment