arduino get请求_Arduino ESP8266 AT GET请求

I'm having trouble to send data to my database with the ESP8266-01.

I'm getting the correct data from the sensor in the Console but nothing in my Database. The PHP Script is correct, I know that, just to be sure, I'm gonna add it in here too.

My Code:

// http://playground.arduino.cc/Main/Average

#include

#include

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 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:

$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 "

THE DATA HAS BEEN SENT!!

";

mysql_close($conn);

?>

Best Regards.

解决方案

The problem is in your algorithm. ESP module sometimes respond quickly and and sometimes respond late depends on your internet connection.

for example you sent the command AT+CIPSTART="TCP","server",80 and then you WaitForOK(5000);. The ESP didn't reply OK yet and it is still connecting to the server, meanwhile your WaitForOK(5000); timed out and proceed for the next command.

I will suggest you to manually enter all those commands using SERIAL and check for the response.

Thanks. :)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值