• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Dodawanie do bazy co 30 min
#1
Hej 
Mam kod który dodaje dane z czujnika ds18b20 do bazy danych chciałbym aby dodawanie odbywało się co 30 min 
Obecnie dodaje mi co 7 s

Kod:
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

const char *ssid = ""; //nama wifi
const char *password = "";      //password
const char *host = "";         //IP PC

//memasukkan pin
int sensorPinDS18b20 = 4;

//membuat variabel kosong
float suhuC;

OneWire ourWire(sensorPinDS18b20);
DallasTemperature sensors(&ourWire);

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

   Serial.println();
   Serial.println();
   Serial.print("Connecting to ");
   Serial.println(ssid);

   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());
}

void loop() {
  sensors.requestTemperatures();
  suhuC = sensors.getTempCByIndex(0);

  Serial.print("connecting to ");
  Serial.println(host);

  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort))
  {
    Serial.println("connection failed");
    return;
  }

  // We now create a URI for the request
  String url = "/nodemcu-esp8266-with-DS18B20-Sensor-MySQL-Database/add.php?";
  url += "suhu_air=";
  url += suhuC;

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
              "Host: " + host + "\r\n" +
              "Connection: close\r\n\r\n");

  unsigned long timeout = millis();
  while (client.available() == 0)
  {
    if (millis() - timeout > 5000)
    {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  while (client.available())
  {
    String line = client.readStringUntil('\r');
    //Serial.print(line);

    if (line.indexOf("sukses gaes") != -1)
    {
      Serial.println();
      Serial.println("Yes, data masuk");
    }
    else if (line.indexOf("gagal gaes") != -1)
    {
      Serial.println();
      Serial.println("Maaf, data gagal masuk");
      //digitalWrite(alarmPin, HIGH);
    }
  }

  Serial.println();
  Serial.println("closing connection");

  //menampilkan hasil pembacaan sensor Suhu DS18b20
  Serial.print("Suhu = ");
  Serial.print(suhuC);
  Serial.print(" ");
  Serial.println("C");
  delay(1000);
}
Kod PHP:
<?php
  
include('conn.php');

  $sensor $_GET['suhu_air'];

  $sql "INSERT INTO tbl_water (suhu_air) VALUES(:suhu_air)";

  $stmt $PDO->prepare($sql);

  $stmt->bindParam(':suhu_air'$sensor);

  if($stmt->execute()) {
      echo "sukses gaes";
  }else{
      echo "gagal gaes";
  }
?>
 
Odpowiedź
  


Wiadomości w tym wątku
Dodawanie do bazy co 30 min - przez piotrvb - 10-10-2021, 13:29
RE: Dodawanie do bazy co 30 min - przez kaczakat - 10-10-2021, 19:51
RE: Dodawanie do bazy co 30 min - przez piotrvb - 11-10-2021, 17:30
RE: Dodawanie do bazy co 30 min - przez kaczakat - 11-10-2021, 21:15

Skocz do:


Przeglądający: 1 gości