Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- StaticJsonBuffer<200> jsonBuffer;
- /*
- * Simple HTTP get webclient test
- */
- #include <ESP8266WiFi.h>
- char json[] = {0};
- char x[] = {0};
- JsonArray& root = jsonBuffer.parseArray(json);
- const char* ssid = "lehigh-guest";
- const char* password = "";
- #define MAX_JSON_SIZE 256
- const char* host = "api.thingspeak.com";
- String field1;
- String field2;
- void setup() {
- Serial.begin(115200);
- delay(100);
- StaticJsonBuffer<200> jsonBuffer;
- // We start by connecting to a WiFi network
- char json[] = {0};
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- }
- int value = 0;
- void loop() {
- char json[256] = {0u};
- delay(5000);
- ++value;
- // Use WiFiClient class to create TCP connections
- WiFiClient client;
- const int httpPort = 80;
- if (!client.connect(host, httpPort)) {
- return;
- }
- // We now create a URI for the request
- String url = "/channels/103752/feeds/last";
- // This will send the request to the server
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(500);
- // Read all the lines of the reply from server and print them to Serial
- while(client.available()){
- String line = client.readStringUntil('\r\n\r\n');
- line.toCharArray (x,256);
- Serial.print(line);
- char json = x[256];
- JsonArray& root = jsonBuffer.parseArray(json);
- if (!root.success()){
- Serial.println("parseObject() failed");
- return;
- }
- String field1 = root["field1"];
- String field2 = root["field2"];
- Serial.println(field1);
- Serial.println(field2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement