[text] a

Viewer

  1. #include <ESP8266HTTPClient.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. // Variabel global
  5. const char* ssid = "Chanyeol's wife hotspot";
  6. const char* password = "ceyeloveocha";
  7. int led1 = D1;
  8. int led2 = D2;
  9. int led3 = D3;
  10.  
  11. #define ON HIGH
  12. #define OFF LOW
  13.  
  14. //tambahan
  15. int firstVal, secondVal;
  16. WiFiClient WifiClientnya;
  17.  
  18. void setup() {
  19.   // put your setup code here, to run once:
  20.  
  21.   // Konfigurasi Modul
  22.   Serial.begin(115200);
  23.  
  24.   // Connect to WiFi network
  25.   Serial.print("Connecting to ");
  26.   Serial.println(ssid);
  27.   WiFi.begin(ssid, password);
  28.  
  29.   while (WiFi.status() != WL_CONNECTED) { //Wait till connects
  30.     delay(500);
  31.     Serial.print(".");
  32.   }
  33.  
  34.   
  35.   Serial.println("");
  36.   Serial.println("WiFi connected");
  37.   Serial.println(WiFi.localIP());
  38.  
  39.   // --- End of Connect wifi
  40.  
  41.   // Deklarasi PIN GPIO 1
  42.   pinMode(led1, OUTPUT);
  43.   pinMode(led2, OUTPUT);
  44.   pinMode(led3, OUTPUT);
  45.  
  46. }
  47.  
  48. //tambahan
  49. String getValue(String data, char separator, int index)
  50. {
  51.   int found = 0;
  52.   int strIndex[] = {0, -1};
  53.   int maxIndex = data.length()-1;
  54.  
  55.   for(int i=0; i<=maxIndex && found<=index; i++){
  56.     if(data.charAt(i)==separator || i==maxIndex){
  57.       found++;
  58.       strIndex[0] = strIndex[1]+1;
  59.       strIndex[1] = (i == maxIndex) ? i+1 : i;
  60.     }
  61.   }
  62.   String ketemu = found>index ? data.substring(strIndex[0], strIndex[1]) : "";
  63.   return ketemu;
  64. }
  65.  
  66. void loop() {
  67.   Serial.println("Masyuk loop");
  68.  
  69.   // put your main code here, to run repeatedly:
  70.   if(WiFi.status() == WL_CONNECTED){
  71.  
  72.     HTTPClient http;
  73.     http.begin(WifiClientnya, "http://192.168.6.199/IOT3/control.php");
  74.     int httpCode = http.GET();
  75.     Serial.println("httpcode ---> "+httpCode);
  76.  
  77.     if(httpCode > 0){
  78.       String status = http.getString();
  79.       //status = "1,1,1,1, ";
  80.       //tambahan
  81.       Serial.println(status);
  82.       Serial.print("LED 1 --> ");
  83.       String nilai1 = getValue(status, ',',0);
  84.       Serial.println(nilai1);
  85.       Serial.print("LED 2 --> ");
  86.       String nilai2 = getValue(status, ',',1);
  87.       Serial.println(nilai2);
  88.       Serial.print("LED 3 --> ");
  89.       String nilai3 = getValue(status, ',',2);
  90.       Serial.println(nilai3);
  91.       Serial.println("------------------------");
  92.  
  93.       if(nilai1 == "0"){
  94.         digitalWrite(led1, OFF);
  95.       }else {
  96.         digitalWrite(led1, ON);
  97.       }
  98.       if(nilai2 == "0"){
  99.         digitalWrite(led2, OFF);
  100.       }else {
  101.         digitalWrite(led2, ON);
  102.       }
  103.       if(nilai3 == "0"){
  104.         digitalWrite(led3, OFF);
  105.       }else {
  106.         digitalWrite(led3, ON);
  107.       }
  108.     }
  109.     http.end();
  110.  
  111.   }else {
  112.     Serial.println("Delay...");
  113.   }
  114.   delay(500);
  115. }

Editor

You can edit this paste and save as new:


File Description
  • a
  • Paste Code
  • 29 Apr-2024
  • 2.62 Kb
You can Share it: