-->
Page 3 of 12

Re: ESP8266 and Arduino

PostPosted: Sun Sep 14, 2014 10:55 pm
by RichardS
So you did add a voltage divider and it worked? I assume you picked values to give you 3.3V out when 5V is applied :lol:

Thanks.

Re: ESP8266 and Arduino

PostPosted: Mon Sep 15, 2014 1:05 am
by Patch
I did, and it did! :D

Image

Vin == Arduino TX
Vout == ESP RX
R1 == 1K
R2 == 2K

I'm inclined to try 10 and 20K...

Re: ESP8266 and Arduino

PostPosted: Mon Sep 15, 2014 2:32 am
by SirGeorg
I also had problems, but that was not connected to the voltage divider but to the sloppy example sketch. This is what worked for me

Be aware that I tested this on the Leonardo which has 2 serial interfaces: Serial, which is the software serial over USB and Serial one which is the hardware serial on pins 0,1. Serial 1 is used for communication with the wifi module

Code: Select all#include <SoftwareSerial.h>
#define SSID        "Your SSID here"
#define PASS        "Your Password here"
#define DST_IP      "220.181.111.85"   

void setup()
{
  // Open serial communications and wait for port to open:
  Serial1.begin(115200);
  Serial1.setTimeout(5000);
  Serial.begin(9600);  //can't be faster than 19200 for softserial
  while(!Serial);
  while(!Serial1);
  Serial.println("ESP8266 Demo");
  //test if the module is ready
  while(Serial1.available()>0)
  Serial1.read();
 
  Serial1.println("AT+RST");
  Serial.println("Resetting module");
  Serial1.flush();
 
  //while(Serial1.available()==0) {
  // Serial.println("Waiting for module to answer!");
   //delay(100);
  //}
 
  //while(Serial1.available()) {
  //Serial.write(Serial1.read());
  //}
  //Serial.println(Serial1.available());
  //delay(1000);
  if (Serial1.find("ready"))
  {
    Serial.println("Module is ready");
  }
  else
  {
    Serial.println("Module have no response.");
    //while (1);
  }
  delay(1000);
  //connect to the wifi
  boolean connected = false;
  for (int i = 0; i < 5; i++)
  {
    if (connectWiFi())
    {
      connected = true;
      break;
    }
  }
  if (!connected) {
    while (1);
  }
  //delay(5000);
  //print the ip addr
  /*Serial.println("AT+CIFSR");
  dbgSerial.println("ip address:");
  while (Serial.available())
    dbgSerial.write(Serial.read());*/
  while(Serial1.available())
    Serial1.read();
  //set the single connection mode
  Serial1.println("AT+CIPMUX=0");
  Serial1.flush();
}

void loop()
{
  while(Serial.available())
    Serial.read();
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";
  Serial1.println(cmd);
  Serial.println(cmd);
  Serial1.flush();
  if (Serial1.find("ERROR")) return;
 
  while(Serial.available())
    Serial.read();

  cmd = "GET / HTTP/1.0\r\n\r\n";
  Serial1.print("AT+CIPSEND=");
  Serial.print("AT+CIPSEND=");
  //Serial1.flush();
  Serial1.println(cmd.length());
  Serial.println(cmd.length());
  Serial1.flush();
  if (Serial1.find(">"))
  {
    Serial.print(">");
  } else
  {
    Serial1.println("AT+CIPCLOSE");
    Serial.println("connect timeout");
    Serial1.flush();
    return;
  }
  Serial1.print(cmd);
  Serial1.flush();
  //Serial.find("+IPD");
  while (Serial1.available())
  {
    char c = Serial1.read();
    Serial.write(c);
    if (c == '\r') Serial.print('\n');
  }
  Serial.println("====");
  delay(1000);
}

boolean connectWiFi()
{
  while(Serial1.available())
    Serial1.read();
 
  //while(Serial1.find("OK")) {
    Serial1.println("AT+CWMODE=1");
    Serial.println("AT+CWMODE=1");
    Serial1.flush();
  //}
  while(Serial1.available())
    Serial1.read();
 
 //Serial.println("!!!");

  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  Serial.println(cmd);
  Serial1.println(cmd);
  Serial1.flush();
 
  //while(Serial1.available()==0) {
  // Serial.println("Waiting for module to answer!");
   //delay(100);
  //}
  //delay(2000);
  if (Serial1.find("OK"))
  {
    Serial.println("OK, Connected to WiFi.");
    return true;
  } else
  {
    Serial.println("Can not connect to the WiFi.");
    return false;
  }
}


In principle, before I issue a serial command, I read all chars waiting on the serial interface (probably not necessary) and after I issue a serial command, I wait until everything is transmitted

Code: Select allSerial1.flush()


This way I get rid of the delay, which in my case solved the communication problems (and is better coding style imho). The module is connected to my wifi, however, after I submitt the TCP packacke, I son't get an answer. Any ideas?

Re: ESP8266 and Arduino

PostPosted: Mon Sep 15, 2014 6:57 pm
by Samighi11
Several questions

I just go my modules. Should I try 57600 or 115200?
I am using 8 MHz for 3.3 compatibility. Should I switch to an arduino ino and level shift? Both rx and tx?

I have been trying all day and I get no response from the three modules I have (2 more have not been opened yet for safe keeping)

Please let me know. I am very curious to get these to work.

Thanks, saman