Tema: Protokomjer
View Single Post
Staro 10.03.2023., 22:20   #7
OuttaControl
Premium
Moj komp
 
OuttaControl's Avatar
 
Datum registracije: Feb 2007
Lokacija: Dalmacija
Postovi: 5,214
Malo sam se zaigra sa chatGPTjem, tako da nezz jel ovo radi, koliko znam chatGPT nece radit ali malo tweekanja ce riješiti.

Znaci esp32 i YF-S201 senzor protoka prva verzija je sa webhookom na neki server, druga verzija je spremanje u sql server bazu.

HTML:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Create a web server object
WebServer server(80);

// YF-S201 Sensor
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour;         // Calculated liters/hour
unsigned int total_litres;   // Total liters consumed
unsigned long old_time;

// Webhook URL
const char* webhookUrl = "http://your-webhook-url.com";

void handleRoot() {
  server.send(200, "text/html", "<html><body><h1>Water Flow Monitor</h1><p>Current flow: " + String(l_hour) + " L/hr</p><p>Last minute flow: " + String(l_hour*60) + " L/min</p><p>Last hour flow: " + String(l_hour*3600) + " L/hr</p><p>Last day flow: " + String(l_hour*3600*24) + " L/day</p></body></html>");
}

void handleNotFound(){
  server.send(404, "text/plain", "Not found");
}

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

  // Connect to Wi-Fi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Initialize YF-S201 Sensor
  pinMode(34, INPUT);
  attachInterrupt(34, flow, RISING);

  // Initialize total litres and old time
  total_litres = 0;
  old_time = millis();

  // Start web server
  if (MDNS.begin("esp32")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
  
  // Calculate water flow
  unsigned long now = millis();
  unsigned long delta_time = now - old_time;
  float flow_rate = (float)flow_frequency / (float)delta_time;
  old_time = now;
  flow_frequency = 0;
  l_hour = (int)(flow_rate * 3600 / 7.5);

  // Update total litres consumed
  total_litres += (flow_rate / 60 / 7.5);

  // Send updates to webhook URL
  if (WiFi.status() == WL_CONNECTED) {
    WiFiClient client;
    HTTPClient http;
    String payload = "{";
    payload += "\"litres\":";
    payload += String(total_litres);
    payload += "}";
    Serial.println(payload);
    http.begin(client, webhookUrl);
    http.addHeader("Content-Type", "application/json");
    int httpCode = http.POST(payload);
    http.end();
    Serial.println("HTTP POST code: " + String(httpCode));
  } else {
    // If no network connection, start hotspot
    WiFi.softAP("ESP32AP", "password");
    Serial.println("AP started");
    Serial.print("AP IP address: ");
    Serial.println(WiFi.softAPIP());
    server.handleClient();
  }
}

void flow() {
    flow_frequency++;
}
A evo verzija koja bi spremala u sql bazu
HTML:
#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>
#include <WiFiClientSecure.h>
#include <esp_wifi.h>
#include <esp_wps.h>
#include <esp_event_loop.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/event_groups.h>
#include <WiFi.h>
#include <esp_log.h>
#include <esp_system.h>
#include <nvs_flash.h>
#include <driver/gpio.h>
#include <esp_http_client.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <time.h>
#include <Preferences.h>
#include <string>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// Replace with your SQL Server credentials
const char* server = "your_SQL_Server";
const char* user = "your_SQL_User";
const char* password_sql = "your_SQL_Password";
const char* database = "your_SQL_Database";

// Create a web server object
WebServer server(80);

// YF-S201 Sensor
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour;         // Calculated liters/hour
unsigned int total_litres;   // Total liters consumed
unsigned long old_time;

// SQL Server
SQLClient client;
String query;

void handleRoot() {
  server.send(200, "text/html", "<html><body><h1>Water Flow Monitor</h1><p>Current flow: " + String(l_hour) + " L/hr</p><p>Last minute flow: " + String(l_hour*60) + " L/min</p><p>Last hour flow: " + String(l_hour*3600) + " L/hr</p><p>Last day flow: " + String(l_hour*3600*24) + " L/day</p></body></html>");
}

void handleNotFound(){
  server.send(404, "text/plain", "Not found");
}

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

  // Connect to Wi-Fi network
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Initialize YF-S201 Sensor
  pinMode(34, INPUT);
  attachInterrupt(34, flow, RISING);

  // Initialize total litres and old time
  total_litres = 0;
  old_time = millis();

  // Initialize SQL Server connection
  client.begin(server, user, password_sql, database);
  Serial.println("SQL Server connection established");

  // Start web server
  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
  
  // Calculate water flow
  unsigned long now = millis();
  unsigned long delta_time = now - old_time;
  float flow_rate = (float)flow_frequency / (float)delta_time;
  old_time = now;
  flow_frequency =0;

// Convert flow rate to liters per hour
l_hour = (flow_rate / 7.5) * 1000 * 60 * 60;

// Add to total litres
total_litres += l_hour / 1000;

// Reset flow frequency
flow_frequency = 0;

// Log water flow to SQL Server
if (client.connected()) {
query = "INSERT INTO dbo.Flow (LitersPerHour, TotalLiters) VALUES ('" + String(l_hour) + "', '" + String(total_litres) + "')";
client.execute(query.c_str());
}

// Wait for 1 second
delay(1000);
}
OuttaControl je offline   Reply With Quote