Skip to content

Instantly share code, notes, and snippets.

@frederikbrudy
Last active January 11, 2017 17:19
Show Gist options
  • Save frederikbrudy/e5749befa81044e2a6a8dba5a27290b5 to your computer and use it in GitHub Desktop.
Save frederikbrudy/e5749befa81044e2a6a8dba5a27290b5 to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
char ssid[] = "WIFI_SSID"; // your network SSID (name)
char pass[] = "WIFI_PASSWORD"; // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
const char* host = "api.myjson.com";
const int port = 80;
const char* path = "/bins/16lsir";
bool connected = false;
bool headersDone = false;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
StaticJsonBuffer<200> jsonBuffer;
void setup()
{
Serial.begin(9600);
Serial.println("Starting...");
WiFi.begin(ssid, pass);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to WiFi, IP address: ");
Serial.println(WiFi.localIP());
printWifiStatus();
Serial.println("\n[Starting connection to " + String(host) +"]");
if(client.connect(host, port)){
Serial.println("[Connected]\n");
connected = true;
headersDone = false;
// we are connected to the host!
client.print("GET "); client.print(path); client.println(" HTTP/1.1");
client.println("Host: " + String(host));
client.println("Connection: close");
client.println();
}
else{
// connection failure
connected = false;
Serial.print("Error connecting to ");
Serial.print(host);
Serial.println(path);
}
}
String response = "";
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()){
String line = client.readStringUntil('\n');
Serial.println(line);
if(headersDone){
response += line;
}
if (line == "\r") {
headersDone = true;
Serial.println("########### headers received ###########");
break;
}
}
// if the server's disconnected, stop the client:
if (!client.connected() && connected) {
Serial.println("\n[Disconnected]");
client.stop();
connected = false;
JsonObject& root = jsonBuffer.parseObject(response);
const char* name = root["name"];
int age = root["age"];
const char* city = root["city"];
Serial.print("name: ");
Serial.println(name);
Serial.print("age: ");
Serial.println(age);
Serial.print("city: ");
Serial.println(city);
}
delay(500);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment