我无法使用ESP8266-01将数据发送到我的数据库。
我从控制台中的传感器获取了正确的数据,但我的数据库中没有任何内容。 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);
?>
最诚挚的问候。
答案 0 :(得分:1)
问题在于您的算法。 ESP模块有时会快速响应,有时候响应很晚取决于您的互联网连接。
例如,您发送了命令AT+CIPSTART="TCP","server",80
,然后发送了WaitForOK(5000);
。 ESP还没有回复OK
,它仍然连接到服务器,同时你的WaitForOK(5000);
超时并继续下一个命令。
我建议您使用SERIAL
手动输入所有这些命令并检查响应。
感谢。 :)
答案 1 :(得分:0)
我已经在Thingspeak上设计了一个通道,我使用ESP8266通过以下命令发布数据,
AT+CIPSTART="TCP","184.106.153.149",80
其中,AT + CIPSTART启动到具有IP(184.106.153.149)和端口号(80)的物联网的TCP连接。接下来发送HTTP命令
AT+CIPSEND=93
其中93是命令的长度。而命令是
GET /update?api_key=key_of_my_account&field1=1.34&field2=2.89&field3=3.45&field4=4.67\r\n
这已成功将我的数据发布到Thingspeak频道上。