Hello Folks,
I'm using a Arduino Nano with an onboard ESP8266 Module.
I set up a webserver with php and parsed some JSON data for the value I would like to get. (It's a temperature value from the openweather api). On the php Site there is only the value to see.
Setting up the ESP Module works fine, connecting to the Wifi etc, but I am not able to retrieve the value from the Software Serial Port.
I already tried a couple of libraries for the esp Module. With some I was able to get a stable Wifi Connection, but wasn't able to parse / get the data, with other libraries it was vice versa, so I decided to go with the AT Commands by myself.
But now I'm stuck and hope someone can put me in the right direction.
Here is the code:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(11, 12);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(19200);
// set the data rate for the SoftwareSerial port
esp8266.begin(19200);
delay(500);
wificonnect();
delay(500);
getip();
delay(500);
configTCP();
delay(500);
}
void loop() {
String request = "GET /weather_temp.php HTTP/1.0\r\nHost: 192.168.2.211\r\n";
TCPconnect();
esp8266.println("AT+CIPSEND=" + String(request.length() + 2));
delay(1000);
// Serial.println(esp8266.readString());
if (esp8266.find(">"))
{
Serial.println("CIPSEND OK, Sending Request");
esp8266.println(request);
delay(5000);
while (esp8266.available() > 0)
{
Serial.println(esp8266.readString());
}
esp8266.println("AT+CIPCLOSE");
}
else
{
Serial.println("gethttp ERROR");
}
//---------- first approach via a funtion, connection works, but returns always "IPD not found"
// gethttp("GET /weather_temp.php HTTP/1.0\r\nHost: 192.168.2.211\r\n");
delay(10000);
}
//-----------------------Custom functions-------------------
//--------------------getHTTP-----------------
String gethttp(String request)
{
esp8266.println("AT+CIPSEND=" + String(request.length() + 2));
delay(1000);
// Serial.println(esp8266.readString());
if (esp8266.find(">"))
{
Serial.println("CIPSEND OK, Sending Request");
esp8266.println(request);
delay(5000);
if (esp8266.find("+IPD"))
{
Serial.println("IPD found");
}
else
{
Serial.println("IPD not found");
}
esp8266.println("AT+CIPCLOSE");
}
else
{
Serial.println("gethttp ERROR");
}
}
//-----------------------Connect to TCP (single TCP Connection)-------------------
void TCPconnect()
{
esp8266.println("AT+CIPSTART=\"TCP\",\"192.168.2.211\",80");
delay(500);
if (esp8266.find("OK"))
{
Serial.println("TCPconnect OK");
}
else
{
Serial.println("TCPconnect ERROR");
}
}
//-----------------------Cipmux=0 (single TCP Connection)-------------------
void configTCP()
{
esp8266.println("AT+CIPMODE=0");
if (esp8266.find("OK"))
{
Serial.println("Set CIPMode OK");
}
else
{
Serial.println("Set CIPMode ERROR");
}
esp8266.println("AT+CIPMUX=0");
if (esp8266.find("OK"))
{
Serial.println("Set CIPMUX OK");
}
else
{
Serial.println("Set CIPMUX ERROR");
}
}
//-----------------------Wifi Connect-------------------
void wificonnect()
{
esp8266.println("AT+CWMODE=1");
delay(500);
esp8266.println("AT+CWJAP=\"SSID\",\"Password\"");
delay(4000);
if (esp8266.find("OK"))
{
Serial.println("Wifi Connect OK");
}
else
{
Serial.println("Wifi Connect ERROR");
}
}
//----------------------set CWMode---------------------
void setcwmode()
{
esp8266.println("AT+CWMODE=1");
delay(5000);
if (esp8266.find("OK"))
{
Serial.println("CWMode Set OK");
}
else
{
Serial.println("CWMode Set ERROR");
}
}
//-----------------------Return IP config-------------------
void getip()
{
esp8266.println("AT+CIFSR");
Serial.println(esp8266.readString());
}
And here ist the output :
Wifi Connect OK
AT+CWJAP="SSID","Password"
OK
AT+CIFSR
+CIFSR:STAIP,"192.168.2.204"
+CIFSR:STAMAC,"18:fe:34:a4:5c:6a"
OK
Set CIPMode OK
Set CIPMUX OK
TCPconnect OK
CIPSEND OK, Sending Request
GET /weather_temp.php HTTP/1.0
Host: 192.168.2.211
SEND OK
Thank you in advance
Triscus