Advertisement
ferrynurr

ARDUINO-REST_CLIENT [retrieve Json]

Aug 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.59 KB | None | 0 0
  1. #include "ESP8266WiFi.h"
  2. #include <ESP8266HTTPClient.h>
  3. #include <ArduinoJson.h>
  4.  
  5.  
  6. //SSID of your network
  7. char ssid[] = "ELITE_GLOBAL"; //SSID of your Wi-Fi router
  8. char pass[] = "assasin_cok"; //Password of your Wi-Fi router
  9.  
  10. void setup()
  11. {
  12.    Serial.begin(115200);
  13.   Serial.print("Sedang Menghubungkan -> ");
  14.   Serial.print(ssid);  
  15.  
  16.   WiFi.begin(ssid, pass);
  17.   dht.begin();
  18.   while (WiFi.status() != WL_CONNECTED)
  19.   {
  20.     delay(500);
  21.     Serial.print(".");
  22.   }
  23.    Serial.println(" ");
  24.    Serial.print("Terhubung , IP address : ");
  25.    Serial.println(WiFi.localIP());
  26.  
  27. }
  28.  
  29. void loop() {  
  30.   Serial.println("========= OUTPUT ============");
  31.   tampil();
  32.   Serial.println("=============================");
  33.   Serial.println(" ");
  34.   delay(3000);  
  35. }
  36.  
  37. void tampil()
  38. {
  39.    HTTPClient http;
  40.      http.begin("http://ferrynurr.000webhostapp.com/kelembaban");  // online pake ini
  41.    // http.begin("http://LoliconPC/REST_API/sensor");               // offline pake ini
  42.    
  43.     int httpCode = http.GET();              
  44.     if (httpCode > 0)
  45.     {
  46.    
  47.         String json  = http.getString();                      //Get the request response payload
  48.         const size_t bufferSize = JSON_OBJECT_SIZE(4) + 100;
  49.         DynamicJsonBuffer jsonBuffer(bufferSize);
  50.         JsonArray& root = jsonBuffer.parseArray(json);
  51.         String kondisi = root[0]["kelembaban_tanah"];
  52.         Serial.print("tingkat kelembaban : ");
  53.         Serial.println(kondisi);  
  54.         http.end();   //Close connection
  55.  
  56.     }
  57. }
  58.  
  59. void tanah_post(int kalib)
  60. {
  61.  
  62.     StaticJsonBuffer<300> JSONbuffer;   //Declaring static JSON buffer
  63.     JsonObject& JSONencoder = JSONbuffer.createObject();
  64.     JSONencoder["user"] = user;
  65.     JSONencoder["passwd"] = passwd;
  66.     JSONencoder["no_key"] = no_key;
  67.     JSONencoder["output_sensor"] = kalib;
  68.     JSONencoder["letak"] = "B2";
  69.    
  70.     char JSONmessageBuffer[300];
  71.     JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  72.     //Serial.println(JSONmessageBuffer);
  73.  
  74.     HTTPClient http;    //Declare object of class HTTPClient
  75.     http.begin("http://ferrynurr.000webhostapp.com/rest_json/kel_tanah");
  76.     http.addHeader("Content-Type", "application/json");
  77.    
  78.     int httpCode = http.POST(JSONmessageBuffer);   //Send the request
  79.     if(httpCode > 0){
  80.        String payload = http.getString();                                        //Get the response payload
  81.       Serial.println(payload);    //Print request response payload
  82.     }else{
  83.        Serial.println(http.errorToString(httpCode).c_str());
  84.     }
  85.  
  86.     http.end();  //Close connection
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement