[csharp] 1
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <ESP8266HTTPClient.h>
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ArduinoJson.h>
- WiFiClient wifiClient;
- const char* ssid = "@Sehat";
- const char* password = "adam123456";
- int pinLED = 5;
- #define ON HIGH
- #define OFF LOW
- int firstVal, secondVal;
- void setup() {
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- pinMode(pinLED, OUTPUT);
- }
- void loop() {
- delay(5000);
- Serial.print("connecting to ");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println(WiFi.localIP());
- if (WiFi.status() == WL_CONNECTED) {
- HTTPClient http;
- Serial.print("[HTTP] begin...\n");
- http.begin(wifiClient, "http://192.168.103.131/eslolin/sensormulti-json.php");
- Serial.print("[HTTP] GET...\n");
- int httpCode = http.GET();
- Serial.print(httpCode);
- if (httpCode > 0) {
- Serial.printf("[HTTP] GET... code: %d\n", httpCode);
- if (httpCode == HTTP_CODE_OK) {
- String answer = http.getString();
- String jsonAnswer;
- int jsonIndex;
- for (int i = 0; i < answer.length(); i++) {
- if (answer[i] == '{') {
- jsonIndex = i;
- break;
- }
- }
- jsonAnswer = answer.substring(jsonIndex);
- Serial.println();
- Serial.println("JSON answer: ");
- Serial.println(jsonAnswer);
- jsonAnswer.trim();
- StaticJsonDocument<192> doc;
- DeserializationError error = deserializeJson(doc, jsonAnswer);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.f_str());
- return;
- }
- const char* idSensor = doc["idSensor"]; // "1"
- const char* namaSensor = doc["namaSensor"]; // "LED 1"
- const char* statusSensor = doc["statusSensor"]; // "1"
- int add_0 = doc["add"]["0"]; // 36
- int add_1 = doc["add"]["1"]; // 40
- Serial.println(F("Response:"));
- Serial.println(doc["namaSensor"].as<const char*>());
- Serial.println(doc["idSensor"].as<const char*>());
- Serial.println(doc["statusSensor"].as<const char*>());
- Serial.println(add_0);
- Serial.println(add_1);
- } else {
- Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
- }
- delay(50);
- }
- http.end();
- } else {
- Serial.println("Delay...");
- }
- delay(1000);
- }
Editor
You can edit this paste and save as new:
File Description
- 1
- Paste Code
- 29 Mar-2024
- 3.08 Kb
You can Share it:
Latest Code Pastes