php - Arduino Mega 2560 + ESP8266 + PHP 问题

标签 php http arduino esp8266

我正在使用 Arduino Mega 2560 开发板和 ESP8266-01 我想将我的 ESP8266-01 连接到我的 PHP 网站。我只想请求这样的 URL,

“mysite.000webhost.com/index.php?out”

并且我已经编写了一个 PHP 代码来连接到我的 firebase。 PHP 站点和 firebase 都运行良好。 但问题是如何使用 arduino 代码加载我的 URL?

请有人帮我处理这个案子,我已经厌倦了为这个案子找到解决方案。

这是我的代码。

int ledPin = 13;      // LED+
// int temp_sensor = A0; // LM35 Output


void setup() {
  // put your setup code here, to run once: 
  pinMode(ledPin, OUTPUT); // Set ledPin as Output
  Serial.begin(9600);      // PC Arduino Serial Monitor
  Serial1.begin(115200);   // Arduino to ESP01 Communication
  connectWiFi();           // To connect to Wifi
 } 

void loop() { 
  // put your main code here, to run repeatedly:
 // float analog_val = analogRead(temp_sensor);     // Read Analog Temperature

  //j=tempc;

  digitalWrite(ledPin, HIGH); // LED ON
  delay(500);                 // wait for 500 mSec
  digitalWrite(ledPin, LOW);  // LED OFF

  Serial1.println("AT+CIPMUX=0\r\n");      // To Set MUX = 0
  delay(500);                             // Wait for 2 sec

  // TCP connection 
  String cmd = "AT+CIPSTART=\"TCP\",\"";   // TCP connection with https://thingspeak.com server
 // cmd += "184.106.153.149";                // IP addr of api.thingspeak.com
  cmd += "mywebsite.000webhostapp.com";                       // ip address of my site
  cmd += "\",80\r\n\r\n";                  // Port No. = 80

  Serial1.println(cmd);                    // Display above Command on PC
  Serial.println(cmd);                     // Send above command to Rx1, Tx1

  delay(2000);                            // Wait for 20 Sec

  if(Serial1.find("ERROR"))                // If returns error in TCP connection
  { 
    Serial.println("AT+CIPSTART error");   // Display error msg to PC
    //return; 
  }

  // prepare GET string 
  String getStr = "POST /index.php?in HTTP/1.0 ";
  getStr += "Host: mywebsite.000webhostapp.com";
//  getStr += apiKey;
//  getStr +="&field1=";
//  getStr += analog_val;
  getStr += "\r\n\r\n";

  Serial.println(getStr);                 // Display GET String on PC

  cmd = "AT+CIPSEND=";                    // send data length 
  cmd += String(getStr.length());
  cmd+="\r\n";

  Serial.println(cmd);                   // Display Data length on PC
  Serial1.println(cmd);                  // Send Data length command to Tx1, Rx1

  delay(1000);                          // wait for 20sec

  if(Serial1.find(">"))                    // If prompt opens //verify connection with cloud
  {
    Serial.println("connected to Cloud");  // Display confirmation msg to PC
    Serial1.print(getStr);                 // Send GET String to Rx1, Tx1
  }
  else
  { 
    Serial1.println("AT+CIPCLOSE\r\n");    // Send Close Connection command to Rx1, Tx1
    Serial.println("AT+CIPCLOSE");         // Display Connection closed command on PC
  } 

  // thingspeak free version needs 16 sec delay between updates 
 // delay(1000);                            // wait for 16sec

 }

boolean connectWiFi() {               // Connect to Wifi Function
  Serial1.println("AT+CWMODE=1\r\n"); // Setting Mode = 1 
  delay(100);                         // wait for 100 mSec

  String cmd = "AT+CWJAP=\"";         // Connect to WiFi
  cmd += "Silver-WiFi";                   // ssid_name
  cmd += "\",\"";
  cmd += "MyWifi123";                // password
  cmd += "\"\r\n";              

  Serial.println(cmd);                // Display Connect Wifi Command on PC
  Serial1.println(cmd);               // send Connect WiFi command to Rx1, Tx1 

  delay(500);                       // wait for 10 sec

  Serial1.println("AT+CWJAP?");       // Verify Connected WiFi

  if(Serial1.find("+CWJAP"))        
  {
    Serial.println("OK, Connected to WiFi.");         // Display Confirmation msg on PC
    return true;
  }
  else
  {
    Serial.println("Can not connect to the WiFi.");   // Display Error msg on PC
    return false;
  }
}

最佳答案

HTTP 请求应该是:

String getStr = "POST /index.php?in HTTP/1.0\r\n";
getStr += "Host: mywebsite.000webhostapp.com\r\n";
getStr += "\r\n";

关于php - Arduino Mega 2560 + ESP8266 + PHP 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432204/

相关文章:

php - Laravel 5.5 Console TV Bar Chart Highcharts 多个数据集和标签

php - 基本连接表和外键

php - php-搜索查询

php - 合并两个表,按日期排序,有限制

Java 正则表达式 : How to replace double or more slashes with a single slash but ignoring http://or https://

javascript - 如何使用javascript多次访问一个页面?

php - 如何让 Guzzle 6 在 Laravel 中出现 503 错误时重试请求

c++ - Arduino serial.available 奇怪的错误

arduino - 在英特尔伽利略 Gen 2 板上卸载或停止 Arduino Sketch

c++ - 使用 4 个 16 位定时器实现 400hz PWM