Arduino Mega 2560 + ESP8266 + PHP问题

时间:2018-05-20 06:16:42

标签: php http arduino esp8266

我使用的是Arduino Mega 2560主板和ESP8266-01 我想将我的ESP8266-01连接到我的PHP网站。我只是想要这样的URL,

" mysite.000webhost.com/index.php出来"

我写了一个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;
  }
}

1 个答案:

答案 0 :(得分:0)

HTTP请求应为:

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