http — Arduino Mega 2560 + ESP8266 + PHP выпуск

Я использую плату Arduino Mega 2560 и ESP8266-01
Я хочу подключить мой ESP8266-01 к моему сайту PHP. Я просто хочу запросить URL, как это,

«Mysite.000webhost.com/index.php?out»

и я написал PHP-код для подключения к моей базе. PHP-сайт и Firebase оба работают нормально.
Но проблема в том, как я могу загрузить свой URL с кодом Arduino?

Пожалуйста, кто-нибудь, помогите мне с этим делом. Я устал искать решение с этим делом.

Это мой код

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;
}
}

2

Решение

HTTP-запрос должен быть:

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector