Hey everyone,
I am trying to retrieve data from a database using the esp8266 module.
I am using a PHP script which echos the needed data, and then I want to retrieve that data using the esp8266 AT commands.
I am using the code below; however, when I connect to the server, I simply get L on the serial monitor.
#include "SoftwareSerial.h"
String ssid ="xx"; //Connect to wifi and IoT
String password="xxx";
SoftwareSerial esp(10, 7);// RX, TX
String server = "xx.000webhostapp.com"; // www.example.com
String uri = "/accounts.php";// our example is /esppost.php
String data;
void setup() {
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
esp.begin(9600);
Serial.println("Resetting...");
reset();
connectWifi();
getData();
}
void loop() {
}
void connectWifi() {
String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\"";
esp.println(cmd);
Serial.println("Connecting to Wifi...");
lcd.clear();
lcd.print("Connecting to Wifi...");
delay(2000);
if(esp.find("OK")) {
Serial.println("Connected!");
}
else {
connectWifi();
Serial.println("Cannot connect to wifi"); }
}
void reset() {
esp.println("AT+RST");
delay(1000);
if(esp.find("OK") ) Serial.println("Module Reset");
}
void getData(){
esp.println("AT+CIPMUX=1");
delay(1000);
printResponse();
esp.println("AT+CIPSTART=4, \"TCP\",\"" + server + "\",80");
delay(1000);
printResponse();
String cmd = "GET /espget.php HTTP/1.1";
esp.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
Serial.print("Hello");
esp.println(cmd);
delay(1000);
esp.println("");
}
void printResponse() {
while (esp.available()) {
Serial.println(esp.readStringUntil('\n'));
}
}