Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sample Arduino Json Web Client
- // Downloads and parse http://jsonplaceholder.typicode.com/users/1
- //
- // Copyright Benoit Blanchon 2014-2016
- // MIT License
- //
- // Arduino JSON library
- // https://github.com/bblanchon/ArduinoJson
- // If you like this project, please add a star!
- #include <ArduinoJson.h>
- #include <SPI.h>
- #include <Ethernet.h>
- EthernetClient client;
- IPAddress ip(192, 168, 137, 2);
- const char* server = "kp.intip.in"; // server's address
- const char* resource = "/tost/MarinaSafitri/the.json"; // /tost/<account>/<keyword>.json
- const unsigned long BAUD_RATE = 9600; // serial connection speed
- const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server
- const size_t MAX_CONTENT_SIZE = 512; // max size of the HTTP response
- // The type of data that we want to extract from the page
- struct UserData {
- char name[32];
- char company[32];
- };
- // ARDUINO entry point #1: runs once when you press reset or power the board
- void setup() {
- initSerial();
- initEthernet();
- }
- // ARDUINO entry point #2: runs over and over again forever
- void loop() {
- if (connect(server)) {
- if (sendRequest(server, resource) && skipResponseHeaders()) {
- char response[MAX_CONTENT_SIZE];
- readReponseContent(response, sizeof(response));
- UserData userData;
- if (parseUserData(response, &userData)) {
- //printUserData(&userData);
- }
- }
- disconnect();
- }
- wait();
- }
- // Initialize Serial port
- void initSerial() {
- Serial.begin(BAUD_RATE);
- while (!Serial) {
- ; // wait for serial port to initialize
- }
- Serial.println("Serial ready");
- }
- // Initialize Ethernet library
- void initEthernet() {
- byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
- if (!Ethernet.begin(mac)) {
- Serial.println("Failed to configure Ethernet");
- return;
- }
- Serial.println("Ethernet ready");
- delay(1000);
- }
- // Open connection to the HTTP server
- bool connect(const char* hostName) {
- Serial.print("Connect to ");
- Serial.println(hostName);
- bool ok = client.connect(hostName, 80);
- Serial.println(ok ? "Connected" : "Connection Failed!");
- return ok;
- }
- // Send the HTTP GET request to the server
- bool sendRequest(const char* host, const char* resource) {
- Serial.print("GET ");
- Serial.println(resource);
- client.print("GET ");
- client.print(resource);
- client.println(" HTTP/1.1");
- client.print("Host: ");
- client.println(server);
- client.println("Connection: close");
- client.println();
- return true;
- }
- // Skip HTTP headers so that we are at the beginning of the response's body
- bool skipResponseHeaders() {
- // HTTP headers end with an empty line
- char endOfHeaders[] = "\r\n\r\n";
- client.setTimeout(HTTP_TIMEOUT);
- bool ok = client.find(endOfHeaders);
- if (!ok) {
- Serial.println("No response or invalid response!");
- }
- return ok;
- }
- // Read the body of the response from the HTTP server
- void readReponseContent(char* content, size_t maxSize) {
- size_t length = client.readBytes(content, maxSize);
- content[length] = 0;
- Serial.println(content);
- }
- bool parseUserData(char* content, struct UserData* userData) {
- // Compute optimal size of the JSON buffer according to what we need to parse.
- // This is only required if you use StaticJsonBuffer.
- const size_t BUFFER_SIZE =
- JSON_OBJECT_SIZE(8) // the root object has 8 elements
- + JSON_OBJECT_SIZE(5) // the "address" object has 5 elements
- + JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements
- + JSON_OBJECT_SIZE(3); // the "company" object has 3 elements
- // Allocate a temporary memory pool on the stack
- //StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
- // If the memory pool is too big for the stack, use this instead:
- DynamicJsonBuffer jsonBuffer;
- // JsonObject& root = jsonBuffer.parseObject(content);
- JsonArray& root = jsonBuffer.parseArray(content);
- if (!root.success()) {
- Serial.println("JSON parsing failed!");
- return false;
- }
- for ( int x=1; x<=5; x++ ) {
- boolean is_contained = root[x-1];
- //boolean is_contained = root["tweet"][x-1]["isi"];
- //String twit = root["tweet"][x-1]["tweet"];
- //Serial.println( twit );
- if ( is_contained > 0 ) Serial.println( "ada" );
- else Serial.println( "gak" );
- }
- return true;
- }
- // Close the connection with the HTTP server
- void disconnect() {
- Serial.println("Disconnect");
- client.stop();
- }
- // Pause for a 1 minute
- void wait() {
- Serial.println("Wait 60 seconds");
- delay(60000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement