[csharp] 1

Viewer

  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <ArduinoJson.h>
  5.  
  6. WiFiClient wifiClient;
  7.  
  8. const char* ssid = "@Sehat";
  9. const char* password = "adam123456";
  10. int pinLED = 5;
  11. #define ON HIGH
  12. #define OFF LOW
  13.  
  14. int firstVal, secondVal;
  15.  
  16. void setup() {
  17.     Serial.begin(115200);
  18.  
  19.     WiFi.begin(ssid, password);
  20.  
  21.     while (WiFi.status() != WL_CONNECTED) {
  22.         delay(500);
  23.         Serial.print(".");
  24.     }
  25.     Serial.println("");
  26.     Serial.println("WiFi connected");
  27.     Serial.println("IP address: ");
  28.     Serial.println(WiFi.localIP());
  29.  
  30.     pinMode(pinLED, OUTPUT);
  31. }
  32.  
  33. void loop() {
  34.     delay(5000);
  35.  
  36.     Serial.print("connecting to ");
  37.  
  38.     while (WiFi.status() != WL_CONNECTED) {
  39.         delay(500);
  40.         Serial.print(".");
  41.     }
  42.     Serial.println(WiFi.localIP());
  43.  
  44.     if (WiFi.status() == WL_CONNECTED) {
  45.         HTTPClient http;
  46.         Serial.print("[HTTP] begin...\n");
  47.  
  48.         http.begin(wifiClient, "http://192.168.103.131/eslolin/sensormulti-json.php");
  49.  
  50.         Serial.print("[HTTP] GET...\n");
  51.         int httpCode = http.GET();
  52.         Serial.print(httpCode);
  53.  
  54.         if (httpCode > 0) {
  55.             Serial.printf("[HTTP] GET... code: %d\n", httpCode);
  56.  
  57.             if (httpCode == HTTP_CODE_OK) {
  58.                 String answer = http.getString();
  59.                 String jsonAnswer;
  60.                 int jsonIndex;
  61.  
  62.                 for (int i = 0; i < answer.length(); i++) {
  63.                     if (answer[i] == '{') {
  64.                         jsonIndex = i;
  65.                         break;
  66.                     }
  67.                 }
  68.  
  69.                 jsonAnswer = answer.substring(jsonIndex);
  70.                 Serial.println();
  71.                 Serial.println("JSON answer: ");
  72.                 Serial.println(jsonAnswer);
  73.                 jsonAnswer.trim();
  74.  
  75.                 StaticJsonDocument<192> doc;
  76.  
  77.                 DeserializationError error = deserializeJson(doc, jsonAnswer);
  78.  
  79.                 if (error) {
  80.                     Serial.print(F("deserializeJson() failed: "));
  81.                     Serial.println(error.f_str());
  82.                     return;
  83.                 }
  84.  
  85.                 const char* idSensor = doc["idSensor"]; // "1"
  86.                 const char* namaSensor = doc["namaSensor"]; // "LED 1"
  87.                 const char* statusSensor = doc["statusSensor"]; // "1"
  88.  
  89.                 int add_0 = doc["add"]["0"]; // 36
  90.                 int add_1 = doc["add"]["1"]; // 40
  91.  
  92.                 Serial.println(F("Response:"));
  93.                 Serial.println(doc["namaSensor"].as<const char*>());
  94.                 Serial.println(doc["idSensor"].as<const char*>());
  95.                 Serial.println(doc["statusSensor"].as<const char*>());
  96.                 Serial.println(add_0);
  97.                 Serial.println(add_1);
  98.             } else {
  99.                 Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
  100.             }
  101.             delay(50);
  102.         }
  103.         http.end();
  104.     } else {
  105.         Serial.println("Delay...");
  106.     }
  107.     delay(1000);
  108. }
  109.  

Editor

You can edit this paste and save as new: