Post topics, source code that relate to the Arduino Platform

User avatar
By blacai
#13668
Suxsem wrote:How is the esp8266 connected to the Arduino? How do you shift from 5v to 3.3v?

How do you capture the output posted in the first post?


I use the following wiring(for softwareserial):
Image

Or the same but pins 1-0 for rx--tx when directly.

I capture the output just by running the sketch and openning the serial monitor with the selected baud rate(19200 in my case). Then it just start running...

I didn't down the 5v to 3.3 for rx-tx. For the + I connect it to the 3.3v from arduino.
User avatar
By Suxsem
#13687 1) you must shift the Arduino tx to 3.3v or you will damage the esp8266
2) the 3.3v pin of arduino can supply 150ma max...The esp8266 can drain up to 300ma.... you will damage the Arduino. Use an external power or a voltage regulator
3) how can you read the output if it's through software serial?
User avatar
By blacai
#13710
Suxsem wrote:1) you must shift the Arduino tx to 3.3v or you will damage the esp8266
2) the 3.3v pin of arduino can supply 150ma max...The esp8266 can drain up to 300ma.... you will damage the Arduino. Use an external power or a voltage regulator
3) how can you read the output if it's through software serial?

Ok, thanks for the answer. I will try modifying the wiring the way you say.

The output is captured by reading the softwareserial (esp8266 is the name in my sketch) and printing it into the Serial. I don't know if this is what you mean.

Check the sendData function, maybe I explain it not clear or I am wrong with what I do.
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.