如何更改发送到数据库的时间 目前数据每 7 秒发送一次,我想最终 15 分钟
Kod:
#include
#include
#include
#包括
const char *ssid = ""; //nama wifi
const char *password = ""; //密码
const char *host = ""; //IP PC
//memasukkan pin
int sensorPinDS18b20 = 4;
//membuat variabel kosong
float suhuC;
OneWire ourWire(sensorPinDS18b20);
达拉斯温度传感器(&ourWire);
void setup() {
Serial.begin(115200);
延迟(1000);
串行。
序列号.println();
Serial.print("连接到");
序列号.println(ssid);
WiFi.begin(ssid, 密码);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
序列号.print(".");
}
Serial.println("");
Serial.println("WiFi 已连接");
Serial.println("IP地址:");
Serial.println(WiFi.localIP());
}
void loop() {
sensors.requestTemperatures();
suhuC = sensors.getTem
PCByIndex(0);
Serial.print("连接到");
Serial.println(主机);
WiFiClient客户端;
const int httpPort = 80;
如果(!客户端连接(主机,
Serial.println("连接失败");
返回;
}
// 我们现在为请求创建一个 URI
String url = "/nodemcu-esp8266-with-DS18B20-Sensor-MySQL-Database/add.php?";
url += "suhu_air=";
网址 += suhuC;
Serial.print("请求地址:");
Serial.println(url);
// 这会将请求发送到服务器
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connec
tion:关闭\r\n\r\n");
无符号长超时=毫秒();
while (client.available() == 0)
{
if (millis() - 超时 >
客户端停止();
返回;
}
}
// 从服务器读取回复的所有行并将它们打印到 Serial
while (client.available())
{
String line = client.readStringUntil('\r');
//串行打印(行);
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();
序列号.println("
//menampilkan hasil pembacaan 传感器 Suhu DS18b20
Serial.print("Suhu = ");
串行打印(suhuC);
序列号.print(" ");
序列号.println("C");
延迟(1000);
}
Kod PHP:
include('conn.php');
$sensor = $_GET['suhu_air'];
$sql = "INSERT INTO tbl_water (suhu_air) VALUES(:suhu_air)";
$stmt = $PDO->准备($sql);
$stmt->bindParam(':suhu_air', $sensor);
if($stmt->execute()) {
echo "sukses gaes";
}else{
echo "gagal gaes";
}
?