Go Down

Topic: Arduino Mega 2560 ESP8266 problem (Read 311 times) previous topic - next topic

chama1993

I'm using Arduino Mega 2560 board and ESP8266-01
I wanna connect my ESP8266-01 to my PHP website. I just wanna request URL like this,

"mysite.000webhost.com/index.php?out"

and I have wrote a PHP code to connect to my firebase. PHP site and firebase both are working fine.
But the problem is how can I load my URL with arduino code?

Please someone help me with this case I'm tired for find a solution with this case.  :smiley-confuse: 

This is my code.

Code: [Select]
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;
  }
}

ieee488

Look for web client examples where the sketch uses AT commands.



.

chama1993

Thanks for your reply. I tried that method but doesn't working on Mega 2560.  :(

chama1993


Go Up