I have used ESP8266 wifi module with Arduino UNO for IOT but i want run same program in ARDUINO MEGA 2560 but I i did not get result on serial port.
here is my code:
#include "SoftwareSerial.h"
#include <Adafruit_Sensor.h>
String ssid ="redmi";
String password="1234567890";
SoftwareSerial esp(0, 1);// RX, TX TX 2-> Blue 3 -> Green
String data;
//String server = "localhost";
String server = "inventorymanagement007.000webhostapp.com";
String uri = "/check.php";
void setup() {
esp.begin(115200);
Serial.begin(115200);
reset();
connectWifi();
}
//reset the esp8266 module
void reset() {
esp.println("AT+RST");
delay(1000);
}
//connect to your wifi network
void connectWifi() {
String cmd = "AT+CWJAP="" +ssid+"","" + password + """;
esp.println(cmd);
delay(7000);
if(esp.find("OK")) {
Serial.println("Connected!");
}
//
//else {
//
//connectWifi();
//
//Serial.println("Cannot connect to wifi"); }
}
void loop () {
data = "?dept=1&mach=1";// data sent must be under this form //name1=value1&name2=value2.
httppost();
delay(1000);
}
void httppost () {
esp.println("AT+CIPSTART="TCP","" + server + "",80");//start a TCP connection.
if( esp.find("OK")) {
Serial.println("TCP connection ready");
} delay(1000);delay(1500);
String postRequest =
"GET " + uri + data +" HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Accept: text/html" + "\r\n" +
//"Content-Length: " + data.length() + "\r\n" +
//"Content-Type: application/x-www-form-urlencoded\r\n" +
"\r\n";
String sendCmd = "AT+CIPSEND=";//determine the number of caracters to be sent.
esp.print(sendCmd);
esp.println(postRequest.length() );
if(esp.find(">")) { Serial.println("Sending.."); esp.print(postRequest);
if( esp.find("SEND OK")) { Serial.println("Packet sent");
while (esp.available()) {
String tmpResp = esp.readString();
Serial.println(tmpResp);
}
// close the connection
esp.println("AT+CIPCLOSE");
}
}}