Advertisement
LrdArc

intip.in/arduinojsonv2

Dec 19th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. // Sample Arduino Json Web Client
  2. // Downloads and parse http://jsonplaceholder.typicode.com/users/1
  3. //
  4. // Copyright Benoit Blanchon 2014-2016
  5. // MIT License
  6. //
  7. // Arduino JSON library
  8. // https://github.com/bblanchon/ArduinoJson
  9. // If you like this project, please add a star!
  10.  
  11. #include <ArduinoJson.h>
  12. #include <SPI.h>
  13. #include <Ethernet.h>
  14.  
  15. EthernetClient client;
  16. IPAddress ip(192, 168, 137, 2);
  17.  
  18. const char* server = "kp.intip.in"; // server's address
  19. const char* resource = "/tost/MarinaSafitri/the.json"; // /tost/<account>/<keyword>.json
  20. const unsigned long BAUD_RATE = 9600; // serial connection speed
  21. const unsigned long HTTP_TIMEOUT = 10000; // max respone time from server
  22. const size_t MAX_CONTENT_SIZE = 512; // max size of the HTTP response
  23.  
  24. // The type of data that we want to extract from the page
  25. struct UserData {
  26. char name[32];
  27. char company[32];
  28. };
  29.  
  30. // ARDUINO entry point #1: runs once when you press reset or power the board
  31. void setup() {
  32. initSerial();
  33. initEthernet();
  34. }
  35.  
  36. // ARDUINO entry point #2: runs over and over again forever
  37. void loop() {
  38. if (connect(server)) {
  39. if (sendRequest(server, resource) && skipResponseHeaders()) {
  40. char response[MAX_CONTENT_SIZE];
  41. readReponseContent(response, sizeof(response));
  42.  
  43. UserData userData;
  44. if (parseUserData(response, &userData)) {
  45. //printUserData(&userData);
  46. }
  47. }
  48. disconnect();
  49. }
  50. wait();
  51. }
  52.  
  53. // Initialize Serial port
  54. void initSerial() {
  55. Serial.begin(BAUD_RATE);
  56. while (!Serial) {
  57. ; // wait for serial port to initialize
  58. }
  59. Serial.println("Serial ready");
  60. }
  61.  
  62. // Initialize Ethernet library
  63. void initEthernet() {
  64. byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  65. if (!Ethernet.begin(mac)) {
  66. Serial.println("Failed to configure Ethernet");
  67. return;
  68. }
  69. Serial.println("Ethernet ready");
  70. delay(1000);
  71. }
  72.  
  73. // Open connection to the HTTP server
  74. bool connect(const char* hostName) {
  75. Serial.print("Connect to ");
  76. Serial.println(hostName);
  77.  
  78. bool ok = client.connect(hostName, 80);
  79.  
  80. Serial.println(ok ? "Connected" : "Connection Failed!");
  81. return ok;
  82. }
  83.  
  84. // Send the HTTP GET request to the server
  85. bool sendRequest(const char* host, const char* resource) {
  86. Serial.print("GET ");
  87. Serial.println(resource);
  88.  
  89. client.print("GET ");
  90. client.print(resource);
  91. client.println(" HTTP/1.1");
  92. client.print("Host: ");
  93. client.println(server);
  94. client.println("Connection: close");
  95. client.println();
  96.  
  97. return true;
  98. }
  99.  
  100. // Skip HTTP headers so that we are at the beginning of the response's body
  101. bool skipResponseHeaders() {
  102. // HTTP headers end with an empty line
  103. char endOfHeaders[] = "\r\n\r\n";
  104.  
  105. client.setTimeout(HTTP_TIMEOUT);
  106. bool ok = client.find(endOfHeaders);
  107.  
  108. if (!ok) {
  109. Serial.println("No response or invalid response!");
  110. }
  111.  
  112. return ok;
  113. }
  114.  
  115. // Read the body of the response from the HTTP server
  116. void readReponseContent(char* content, size_t maxSize) {
  117. size_t length = client.readBytes(content, maxSize);
  118. content[length] = 0;
  119. Serial.println(content);
  120. }
  121.  
  122. bool parseUserData(char* content, struct UserData* userData) {
  123. // Compute optimal size of the JSON buffer according to what we need to parse.
  124. // This is only required if you use StaticJsonBuffer.
  125. const size_t BUFFER_SIZE =
  126. JSON_OBJECT_SIZE(8) // the root object has 8 elements
  127. + JSON_OBJECT_SIZE(5) // the "address" object has 5 elements
  128. + JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements
  129. + JSON_OBJECT_SIZE(3); // the "company" object has 3 elements
  130.  
  131. // Allocate a temporary memory pool on the stack
  132. //StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
  133. // If the memory pool is too big for the stack, use this instead:
  134. DynamicJsonBuffer jsonBuffer;
  135.  
  136. // JsonObject& root = jsonBuffer.parseObject(content);
  137. JsonArray& root = jsonBuffer.parseArray(content);
  138.  
  139. if (!root.success()) {
  140. Serial.println("JSON parsing failed!");
  141. return false;
  142. }
  143.  
  144. for ( int x=1; x<=5; x++ ) {
  145. boolean is_contained = root[x-1];
  146. //boolean is_contained = root["tweet"][x-1]["isi"];
  147. //String twit = root["tweet"][x-1]["tweet"];
  148. //Serial.println( twit );
  149. if ( is_contained > 0 ) Serial.println( "ada" );
  150. else Serial.println( "gak" );
  151. }
  152.  
  153. return true;
  154. }
  155.  
  156. // Close the connection with the HTTP server
  157. void disconnect() {
  158. Serial.println("Disconnect");
  159. client.stop();
  160. }
  161.  
  162. // Pause for a 1 minute
  163. void wait() {
  164. Serial.println("Wait 60 seconds");
  165. delay(60000);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement