I flashed the esp8266 with the nodemcu firmware. Now I cannot run AT commands, but LUA. So I adjusted my code to the following:
#include <SoftwareSerial.h>
SoftwareSerial esp8266(3, 2); // RX | TX
#define DEBUG true
int ERROR_PIN = 7;
int OK_PIN = 6;
char serialbuffer[600];//serial buffer for request url
const String ssid = "Windows Phone7080";
const String pw = "12345678";
int state = 0;
void setup()
{
delay(1000);
/**
// init leds
pinMode(ERROR_PIN, OUTPUT);
pinMode(OK_PIN, OUTPUT);
state = 0;
digitalWrite(ERROR_PIN, HIGH);
digitalWrite(OK_PIN, LOW);
/**/
// init ports
Serial.begin(9600);
Serial.println("initializing esp8266 port...");
esp8266.begin(9600);
delay(400);
// init WIFI
/**/
while(!esp8266.available())
{
Serial.print("...");
delay(300);
}
Serial.println();
Serial.println("FINISH esp8266 initializing!");
//
/**
digitalWrite(ERROR_PIN, LOW);
digitalWrite(OK_PIN, HIGH);
state = 1;
/**/
/**/
// Setup connection
sendData("print(wifi.sta.getip())\r\n",2000,DEBUG);
sendData("wifi.setmode(wifi.STATION)\r\n",1000,DEBUG);
sendData("wifi.sta.config(\"" + ssid + "\",\""+ pw +"\")\r\n",4000,DEBUG);
sendData("print(wifi.sta.getip())\r\n",4000,DEBUG);
delay(2000);
// test output
Createconnection("");
/**/
/**/
}
void loop()
{
if (esp8266.available())
{
char c = esp8266.read() ;
Serial.print(c);
}
if (Serial.available())
{
char c = Serial.read();
esp8266.print(c);
}
}
//////////////////////////////////////////////////////////////////////////////
char* sendData(String command, const int timeout, boolean debug)
{
String response = "";
int idx = 0;
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
serialbuffer[idx] = c;
//response+=c;
idx++;
}
}
serialbuffer[idx] = '\0';
if(debug)
{
Serial.print(serialbuffer);
}
return serialbuffer;
}
//////////////////////////////////////////////////////////////////////////////////
String Createconnection(String url)
{
String tmpCreateConnection = "conn=net.createConnection(net.TCP, false)\r\n";
String tmpReceive = "conn:on(\"receive\", function(conn, pl) print(pl) end)\r\n";
String tmpConnect = "conn:connect(80,\"121.41.33.127\")\r\n";
String tmpSend = "conn:send(\"GET / HTTP/1.1\r\nHost: www.nodemcu.com Connection: keep-alive\r\n\r\n\")\r\n";
sendData(tmpCreateConnection, 4000, DEBUG);
delay(1000);
sendData(tmpReceive, 4000, DEBUG);
delay(1000);
sendData(tmpConnect, 4000, DEBUG);
delay(1000);
sendData(tmpSend, 6000, DEBUG);
}
//////////////////////////////////////////////////////////////////////////////////
Everything works but the GET... It says about "error on end of line". Probably because of the \r\n and the way "send" collapses with the esp8266.print ...
If I run the commands directly one by one the way they say here http://nodemcu.com/index_en.html it is ok.
Now I don't know if I should keep this firmware LUA version and try to rewrite the sendData function or just try to find another firmware AT version up to date and check my previous code
Suggestions?
Edit.:
It works like this:
String tmpSend = "conn:send(\"GET / HTTP/1.1\\r\\nHost: www.nodemcu.com Connection: keep-alive\\r\\n\\r\\n\")\r\n";
with the \\r\\n because the send command expects a "real" \r\n.