Arduino ESP8266 AT GET 请求 [英] Arduino ESP8266 AT GET Request

查看:48
本文介绍了Arduino ESP8266 AT GET 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 ESP8266-01 将数据发送到我的数据库.

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

我从控制台中的传感器获取了正确的数据,但在我的数据库中没有任何数据.PHP 脚本是正确的,我知道,只是为了确定,我也要把它添加到这里.

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.

我的代码:

 // 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);
?>

最好的问候.

推荐答案

问题出在你的算法上.ESP 模块有时响应快有时响应晚取决于您的互联网连接.例如,您发送命令 AT+CIPSTART="TCP","server",80 然后您 WaitForOK(5000);.ESP 还没有回复 OK 并且仍在连接到服务器,同时您的 WaitForOK(5000); 超时并继续执行下一个命令.

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.

我建议您使用 SERIAL 手动输入所有这些命令并检查响应.

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

谢谢.:)

这篇关于Arduino ESP8266 AT GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆