Arduino的ESP8266 AT GET请求(Arduino ESP8266 AT GET Re

2019-09-28 06:37发布

我在将数据发送到我与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);
?>

最好的祝福。

Answer 1:

问题出在你的算法。 ESP模块有时反应迅速,有时晚回应取决于你的互联网连接。 例如,你发送的命令AT+CIPSTART="TCP","server",80然后就WaitForOK(5000); 。 在ESP没有回答OK但它仍然连接到服务器,同时您WaitForOK(5000); 超时并继续下一个命令。

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

谢谢。 :)



Answer 2:

我设计了一个通道在thingspeak,在那里我张贴使用命令使用ESP8266我的数据,

AT+CIPSTART="TCP","184.106.153.149",80

其中AT + CIPSTART启动TCP连接具有IP(184.106.153.149)的thingspeak,与端口号(80)。 接下来发送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频道我的数据。



文章来源: Arduino ESP8266 AT GET Request