Skip to content

Instantly share code, notes, and snippets.

@ikbelkirasan
Last active June 12, 2017 21:36
Show Gist options
  • Select an option

  • Save ikbelkirasan/984daa66207a4b3008a745db5501da1e to your computer and use it in GitHub Desktop.

Select an option

Save ikbelkirasan/984daa66207a4b3008a745db5501da1e to your computer and use it in GitHub Desktop.
This example uses ArduinoJson library to parse json data provided by the server
/*
This example uses ArduinoJson library to parse json data provided by the server
Download it and install it from: https://github.com/bblanchon/ArduinoJson
*/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Your_ssid";
const char* password = "password";
// Upload new values to the webserver
void sendRequestToServer() {
String url = "http://your_url_goes_here";
HTTPClient http;
http.begin(url);
http.addHeader("Accept", "application/json");
int httpCode = http.GET();
Serial.printf("HTTP URL=[%s] code: %d\n", url.c_str(), httpCode);
if (httpCode == 200) {
String payload = http.getString();
Serial.println(payload);
StaticJsonBuffer<2000> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(payload);
// Your json is parsed and you can use it via the root object
// For more information, check out the documentation of
// ArduinoJson
}
else {
Serial.println("(!) ERROR");
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
sendRequestToServer();
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment