我很難用ESP8266-01將數據發送到我的數據庫。Arduino ESP8266 AT GET請求
我從控制檯中的傳感器獲取正確的數據,但沒有在我的數據庫中。 PHP腳本是正確的,我知道,只是可以肯定的是,我也會在這裏添加它。
我的代碼:
// http://playground.arduino.cc/Main/Average
#include <Average.h>
#include <SoftwareSerial.h>
char serialbuffer[100];//serial buffer for request url
SoftwareSerial mySerial(10, 11);
const char* ssid = "Master";
const char* password = "#Bennet99*";
const char* host = "server";
void setup() {
Serial.begin(9600); // Connection to PC
mySerial.begin(9600); // Connection to ESP8266
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
}
void loop() {
float temp = getTemperatureAverage();
Serial.println("Temperature: " + String(temp));
sendTemperature(temp);
delay(10000);
}
void sendTemperature(float temperature) {
digitalWrite(7, HIGH);
delay(2000);
mySerial.println("AT+RST");
WaitForReady(2000);
mySerial.println("AT+CWMODE=1");
WaitForOK(2000);
mySerial.println("AT+RST");
WaitForReady(2000);
mySerial.println("AT+CWJAP=\"Master\",\"#Bennet99*\"");
if (WaitForOK(5000)) {
digitalWrite(13, HIGH); // Connection succesful
}
mySerial.println("AT+CIPSTART=\"TCP\",\"server\",80");
WaitForOK(5000);
mySerial.println("AT+CIPSEND=123");
WaitForOK(5000);
mySerial.print("GET /Intranet/Interface/modules/php/temp/temp.php?sensorid=\"1\"?humidity=\"1\"&temp=" + String(temperature) + " HTTP/1.0\r\n");
mySerial.print("Host: server");
WaitForOK(5000);
mySerial.println("AT+CIPCLOSE");
WaitForOK(5000);
digitalWrite(13, LOW);
digitalWrite(7, LOW);
}
float getTemperatureAverage() {
Average<float> ave(10);
for (int i = 0; i < 10; i++) {
ave.push(getTemperature());
delay(500);
}
float total = 0.0;
delay(50);
float temperature = ave.mean();
return temperature;
}
float getTemperature() {
int sensorVal = analogRead(A0);
float voltage = (sensorVal/1024.0) * 5.0;
float temperature = (voltage - .5) * 100;
return temperature;
}
boolean WaitForOK(long timeoutamount) {
return WaitForResponse("OK", timeoutamount);
}
boolean WaitForReady(long timeoutamount) {
return WaitForResponse("ready", timeoutamount);
}
// Parts used from https://github.com/contractorwolf/ESP8266
boolean WaitForResponse(String response, long timeoutamount) {
unsigned long timeout = millis() + timeoutamount;
while (millis() <= timeout) {
while (mySerial.available() > 0) {
int len = mySerial.readBytesUntil('\n', serialbuffer, sizeof(serialbuffer));
String message = String(serialbuffer).substring(0, len - 1);
if (message == response) {
return true;
}
}
}
return false;
}
PHP:
<?php
$servername = "server";
$username = "root";
$password = "root";
$dbname = "Intranet";
$now = new DateTime();
$field = $_GET['sensorid'];
$value = $_GET['temp'];
$conn = mysql_connect("server","root","root");
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
$con_result = mysql_select_db("some_database", $conn);
if(!$con_result)
{
die('Could not connect to specific database: ' . mysql_error());
}
$datenow = $now->format("Y-m-d H:i:s");
$hvalue = $value;
$sql = "INSERT INTO `DataTable`(`logdata`, `field`, `value`) VALUES (\"$datenow\",\"$field\",$value)";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
//echo "<h1>THE DATA HAS BEEN SENT!!</h1>";
mysql_close($conn);
?>
問候。
'腳本是正確的'沒有。到處都是這些Arduino/ESP自制PHP頁面,它們都是SQL注入漏洞。每一個。請解決安全問題。對於這個問題:你不會正確地終止你的HTTP請求。 'mySerial.print(「Host:server」);'應該是'mySerial.print(「Host:<實際IP地址/域> \ r \ n \ r \ n」)''。然後調整「CIPSEND」中的值。還學會了使用wireshark轉儲發送的HTTP流量,這樣我們就可以比通過幾百行代碼更容易地調試網絡問題**。 –