Advertisement
gwynplaine

Kode Program SCT 013

Jun 15th, 2025
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266WiFiMulti.h>
  3. #include <ESP8266HTTPClient.h>
  4. // #include <WiFiClientSecureBearSSL.h>
  5.  
  6. const char* ssid ="prakarya.id";
  7. const char* pass = "qwertyqwerty";
  8.  
  9. // const char* ssid ="Subarashi!";
  10. // const char* pass = "qwertyqwerty";
  11.  
  12. WiFiClient client;
  13.  
  14. // #define analogPin A0
  15. // float gainFactor = 7.6; // rasio resistor 11,5 : 1,5
  16. // int adcValue = 0;
  17. // float nilaiTegangan = 0.00;
  18.  
  19. const int sensorPin = A0;
  20. const float VREF = 1.0;            // Max ADC voltage on NodeMCU
  21. const int samples = 2000;
  22. const float calibration = 10.0;   // Kalibrasi sesuai datasheet 100A = 1V
  23.  
  24. void setup()
  25. {
  26.     pinMode(LED_BUILTIN, OUTPUT);
  27.  
  28.        Serial.begin(9600);
  29.        delay(10);
  30.                
  31.        Serial.println("Connecting to ");
  32.        Serial.println(ssid);
  33.        WiFi.mode(WIFI_STA);
  34.  
  35.        WiFi.begin(ssid, pass);
  36.        while (WiFi.status() != WL_CONNECTED)
  37.           {
  38.             delay(500);
  39.             Serial.print(".");
  40.           }
  41.       Serial.println("");
  42.       Serial.println("WiFi connected");
  43. }
  44.  
  45. void loop()
  46. {      
  47.   //http://127.0.0.1:8000/getnilai/20.59
  48.   //http://192.168.43.128/Laravel/solarpanellaravel/public/
  49.   //http://192.168.43.128/Laravel/solarpanellaravel/public/getnilai/20.59
  50.     if (WiFi.status() == WL_CONNECTED)
  51.           {
  52.             delay(5000);
  53.             /*
  54.             Serial.println("conek");
  55.             digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  56.             delay(1000);                      // wait for a second
  57.             digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  58.             delay(1000);
  59.             */
  60.  
  61.              float sum = 0;
  62.  
  63.             for (int i = 0; i < samples; i++) {
  64.               int raw = analogRead(sensorPin);
  65.               float voltage = (raw / 1024.0) * VREF;   // 10-bit ADC
  66.               float centered = voltage - (VREF / 2);   // Hilangkan offset (0.5V)
  67.               sum += centered * centered;
  68.             }
  69.  
  70.             float voltageRMS = sqrt(sum / samples);
  71.             float currentRMS = voltageRMS * calibration;
  72.            
  73.            
  74.             // Serial.print("nilai tegangan = ");
  75.             // Serial.print(nilaiTegangan);
  76.             // Serial.println(" V");
  77.             delay(5000);
  78.  
  79.             // std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  80.  
  81.             // Ignore SSL certificate validation
  82.             // client->setInsecure();
  83.  
  84.             String url;
  85.             HTTPClient http;
  86.             // url = "http://192.168.1.101/Laravel/solarpanellaravel/public/getnilai/" + String(nilaiTegangan);
  87.             //https://monitora.my.id/getnilai/19.54
  88.             //url = "https://monitora.my.id/getnilai/" + String(currentRMS, 2);
  89.             url = "http://192.168.48.30/getnilai/" + String(currentRMS, 2);
  90.  
  91.             http.begin(client, url);
  92.             http.GET();
  93.             http.end();
  94.             Serial.println(url);
  95.             delay(5000);
  96.  
  97.           }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement