Post topics, source code that relate to the Arduino Platform

User avatar
By blacai
#13937 Some updates...
I flashed the esp8266 with the nodemcu firmware. Now I cannot run AT commands, but LUA. So I adjusted my code to the following:
Code: Select all#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:
Code: Select all  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.
User avatar
By entozoon
#22739
blacai wrote:Some updates...
I flashed the esp8266 with the nodemcu firmware. Now I cannot run AT commands, but LUA. So I adjusted my code to the following:
Code: Select all#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);
}
//////////////////////////////////////////////////////////////////////////////////



Thank you ever so much blacai, that code example was super helpful for me in all kinds of ways, I can't even begin to say!

Although I am having an issue I can't overcome, if anyone has an idea.. Using the code above (but with my own SSID etc), I get a rather truncated serial output from my arduino:

Code: Select allinitializing esp8266 port...
FINISH esp8266 initializing!
wifi.setmode(wifi.STATION)
> wifi.sta.config("MySSID","MyPASS")
> print(wifi.sta.getip())
192.168.1.74   255.255.255.0   192.168.1.254
> conn=net.createConnection(net.TCP, false)
> conn:on("receive", function(conn, pl) print(pl) end)
> conn:connect(80,"121.41.33.127")
> conn:send("GET / HTTP/1.1\\r\\nHost: www.nodemcu.com Connection: keep-alive\r\n\r\n")
> HTTP/1.1 200 OK
Server: nginx
Date: Tue, 07 Jul 2015 22:46:30 GMT
Content-Type: text/html
Content-Length: 545
Last-Modified: Thu, 02 Apr 2015 08:45:06 GMT
Connection: keep-alive
ETag: "551d0192-221"
Accept-Ranges: bytes

<!Doctype html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="icon" type="image/png" href="favicon.png" />
    <title>nodemcu.com</title>
   
    <meta name="keywords" content="NodeMCU,wifi-soc,esp8266" />
    <meta name="description" content="NodeMCU" /


Which ends very abruptly at that point, every time.
However if I give it serialbuffer[700] it will print a bit more of the site .. so I guess it's just truncating at 600 chars or something?
Do you know if it's possible to stop it limiting in this fashion? perhaps with a char* ?
.. I'm receiving json data from an api so it can't be capped like that.