我正在做功课,将传感器数据发送到PHP。我正在使用Wamp服务器和phpMyAdmin。我已连接NodeMCU WIFI
并看到了一个数据,但是无法将数据保存到数据库中。
错误提示:
GET FAILED, error no: -1, error: connection refused
这是我的代码:
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
int value=analogRead(A0);
int heartbeat=int(60/(float(value)/1000));
float heart = heartbeat;
float temp = mlx.readObjectTempC();
String phpHost =host;
phpHost+="/DogSensor_pre.php?temp=" + String(temp) + "&heart=" + String(heart);
Serial.print("Connect to ");
Serial.println(phpHost);
http.begin(phpHost);
int httpCode = http.GET();
http.setTimeout(3000);
if(httpCode > 0) {
Serial.printf("GET code : %d\n\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println(payload);
}
}
else {
Serial.printf("GET failed, error no: %d, error: %s\n", httpCode , http.errorToString(httpCode).c_str());
}
http.end();
}
}
<?php
$servername="*****";
$username="******";
$password="*****";
$dbname="*****";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(mysqli_connect_errno()){
echo "Failed to connect to MySql: ";
mysqli_connect_error();
}
$temp = $_GET["temp"];
$heart = $_GET["heart"];
$sql = "INSERT INTO ****.doghealth_pre(temp, heart) VALUES($temp, $heart)";
mysqli_query($conn, $sql);
mysqli_close($conn);
?>
http.GET()
连续具有-1值。我认为这就是问题所在,但我不是Web编程专家。
如何解决此问题?如何解决connection refused
错误?