Untitled

 avatar
unknown
plain_text
a year ago
2.5 kB
3
Indexable
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>

// INA219 sensör nesnesi oluşturuluyor
Adafruit_INA219 ina219;

// WiFi ağ bilgileri
const char* ssid = "BNet";
const char* password = "35bayraktar35";

// Web sunucusu nesnesi oluşturuluyor
WebServer server(80);

void setup() {
  Serial.begin(115200);

  // INA219 sensörü başlatılıyor
  if (!ina219.begin()) {
    Serial.println("INA219 bulunamadı!");
    while (1);
  }
  Serial.println("INA219 başlatıldı.");

  // INA219 sensörü kalibre ediliyor
  ina219.setCalibration_16V_400mA(); // veya ina219.setCalibration_16V_400mA();

  // WiFi ağına bağlanılıyor
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("WiFi'ye bağlanıyor...");
  }
  Serial.println("WiFi'ye bağlı!");
  Serial.print("IP Adresi: ");
  Serial.println(WiFi.localIP());

  // Web sunucusu istekleri dinleniyor
  server.on("/", handleRoot);
  server.on("/data", handleData);
  server.begin();
  Serial.println("HTTP sunucusu başlatıldı.");
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  // Ana sayfa HTML içeriği
  String html = "<!DOCTYPE html><html><head><title>INA219 Akım ve Gerilim Sensörü Verileri</title></head><body><h1>Akım ve Gerilim Sensörü Verileri</h1><p>Akım: <span id='current'></span> mA</p><p>Gerilim: <span id='voltage'></span> V</p><p>Güç: <span id='power'></span> mW</p><script>setInterval(function() { fetch('/data').then(response => response.json()).then(data => { document.getElementById('current').innerText = data.current; document.getElementById('voltage').innerText = data.voltage; document.getElementById('power').innerText = data.power; }); }, 1000);</script></body></html>";
  server.send(200, "text/html", html);
}

void handleData() {
  // INA219 sensöründen ölçümler alınıyor
  float current_mA = ina219.getCurrent_mA();
  float voltage_V = ina219.getBusVoltage_V();
  float power_mW = ina219.getPower_mW();

  // JSON nesnesi oluşturuluyor
  StaticJsonDocument<200> doc;
  doc["current"] = current_mA;
  doc["voltage"] = voltage_V;
  doc["power"] = power_mW;

  // JSON nesnesi stringe dönüştürülüyor
  String response;
  serializeJson(doc, response);

  // JSON yanıtı gönderiliyor
  server.send(200, "application/json", response);
}
Editor is loading...
Leave a Comment