如何解决ESP8266到在线mysql数据库
我试图将数据从带有LM35的ESP8266传输到在线MySQL数据库。我遵循了教程,但是没有用。
Arduino代码
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "password";
const char* host = "ftp.jasperator.be";
const int sensor=A0; // Assigning analog pin A0 to variable 'sensor'
float tempc; //variable to store temperature in degree Celsius
float tempf; //variable to store temperature in Fahreinheit
float vout; //temporary variable to hold sensor reading
void setup()
{
Serial.begin(115200);
// We start by connecting to a WiFi network
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());
pinMode(sensor,INPUT); // Configuring pin A1 as input
}
void loop()
{
vout=analogRead(sensor);
vout=(vout*500)/1023;
tempc=vout; // Storing value in Degree Celsius
tempf=(vout*1.8)+32; // Converting to Fahrenheit
Serial.print("in DegreeC=");
Serial.print("\t");
Serial.print(tempc);
Serial.println();
delay(1000); //Delay of 1 second for ease of viewing
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 21;
if (!client.connect(host,httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET http://myserver.mysuffix/index.php?") +
("&realtime_data=") + tempc +
" 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 > 1000) {
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);
}
Serial.println();
Serial.println("closing connection");
}
PHP代码
下一个代码是我的PHP文件,在这里我尝试获取温度传感器数据并将其放入我的在线数据库中。
$temperature = $_GET["tempc"];
$query = "INSERT INTO mydatabasename (realtime_data) VALUES ('$temperature')";
$conn->exec($query);
echo "Insertion Success!<br>";
我在ESP8266和我的互联网之间以及我的Web主机之间建立了连接,但是它只给我数据库中的空输入。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。