Chat freely about anything...

User avatar
By hudis
#24004 Hello

I’m quite new to arduino and esp8266. Currently I’m trying a simple thing – connect arduino UNO to wifi network using ESP8266 and read a webpage from the internet.
I’m using the AT firmware on ESP8266. I can connect it to wifi and open a TCP connection to web server, pass the URL I want to receive, but I have the following problem:

while (Serial.available()){
char c = Serial.read();
dbgSerial.write(c);
strReceived+=c;
}

This gives me exactly what I need to my debug console – the whole HTTP response together with the content. The moment I comment out the debug output to soft serial, I only get very little data from the server. When I add a delayMicroseconds(50) after Serial.read(), I get a little more data to strReceived, but never as much as when I use the serial debug.

while (Serial.available()){
char c = Serial.read();
strReceived+=c;
delayMicroseconds(50);
}
dbgSerial.println(strReceived);

the above code gives me some data, but never the whole HTTP reply (header and page itself), usually some characters are missing.


any advice?
AT+GMR output:
AT version:0.21.0.0
SDK version:0.9.5


thank you very much
User avatar
By hudis
#24146 I have tried everything between 300bps to 115200 bps on hardware serial and 300bps - 9600 bps on software serial

I tried turning off interrupts at various places in my sketch, I tried different delay times, I tried Mega 2560, Uno...

the best results I have when I dont's use the serial.available() function but read blindly from the serial in a loop such as for (i=1, 1<500, i++) {ch=serial.read(); strReceived+=ch} which fills the strReceived string object with a lot of junk, but catches the most of incoming data.

I can't tell whether it's the cheap chinese arduino's fault, timing problem, buffer problem or what.

if anyone would like to pass me the result of their test I would be very grateful. My current sketch is:
Code: Select all/*
actually 2 sketches in one, controlled by the define statement.
 one part of code only copies characters between the esp8266 and USB serial. I put it here so I don't have two different sketches, one for serial console and one for the actual webclient
*/


#include <SoftwareSerial.h>

#define HOSTNAME    "www.baidu.com"
#define HOSTPORT    80
#define HTTP_HEADER " HTTP/1.1\r\nHost: http://www.baidu.com\r\nConnection: close\r\n\r\n"

//#define esp Serial1
#define dbg Serial
#define esp dbgSerial

#define DTESPEED 9600
//#define PASSTHROUGH
#define WEBCLIENT

#define timeBetweenRequests 500

SoftwareSerial dbgSerial(10, 11); // RX, TX

void setup() {
  dbg.begin(9600);
  delay(100);
  dbg.setTimeout(15000);   
  esp.begin(DTESPEED);
  delay(100);
  esp.setTimeout(15000);   

#ifdef PASSTHROUGH
  dbg.print("Passthrough mode. Our DTE speed is now ");
  dbg.println(DTESPEED);
  dbg.println("reset: AT+RST | firmware: AT+GMR | baudrate: AT+CIOBAUD=9600");
  dbg.println("list AP: AT+CWLAP | IP config: AT+CIFSR | join AP: AT+CWJAP=\"ssid\",\"pass\"");
  dbg.println("where joined: AT+CWJAP? | lcal echo: ETE1, ATE0");
#endif
  delay(1000);
}

void loop() {
#ifdef PASSTHROUGH
 while (dbg.available()){
      char ch=dbg.read();
      esp.write(ch);
  }
  while (esp.available()){
      char ch=esp.read();
      dbg.write(ch);
  }
#endif

#ifdef WEBCLIENT
  String cmd="";
  bool tcpEstablished=false;
  bool readyToSend=false;
  bool readyToReceive;
  dbg.println("Starting");
  delay(timeBetweenRequests);
 
  String URL="GET /robots.txt";
  URL+=HTTP_HEADER; 

  delay(timeBetweenRequests);

  cmd="ATE0";
  esp.println(cmd);
//  dbg.println(cmd);
  delay(100);
  while (esp.available()){
      delayMicroseconds(40);
      char ch=esp.read();
      dbg.write(ch);
  }
 
  delay(timeBetweenRequests);

  cmd="AT+CWJAP?";
  esp.println(cmd);
//  dbg.println(cmd);
  delay(100);
//  esp.find("OK");
  while (esp.available()){
      delayMicroseconds(40);
      char ch=esp.read();
      dbg.write(ch);
  }
  delay(timeBetweenRequests);

  cmd="AT+CIFSR";
  esp.println(cmd);
//  dbg.println(cmd);
  delay(100);
//  esp.find("OK");
  while (esp.available()){
      delayMicroseconds(40);
      char ch=esp.read();
      dbg.write(ch);
  }
  delay(timeBetweenRequests);

  String strReceived="";
  cmd = "AT+CIPSTART=";
  cmd += "\"";
  cmd += "TCP";
  cmd += "\",\"";
  cmd += HOSTNAME;
  cmd += "\",";
  cmd += HOSTPORT;

  esp.println(cmd);
//  dbg.println(cmd);
 
  if(esp.find("CONNECT")){
    dbg.println("\r\nFound CONNECT");
    tcpEstablished=true;
  } else {
    dbg.println("\r\nDid not find CONNECT");
  }
 
  delay(timeBetweenRequests);

  if (tcpEstablished){
    cmd = "AT+CIPSEND=";
    cmd += URL.length();
    esp.print(cmd);
    esp.print("\r\n");
//    dbg.println(cmd);
    if(esp.find(">")){
       dbg.println("Found >");
       readyToSend=true;
    } else {
       dbg.println("Did not find >");
    }
  }
  delay(timeBetweenRequests);

  if (readyToSend){
    cmd=URL;
    esp.print(cmd);
    esp.print("\r\n");
    dbg.println(cmd);
    if(esp.find("+IPD,")){
      dbg.println("Found +IPD,");
      readyToReceive=true;
    }else {
      dbg.println("Did not find +IPD,");
    }
  }
  delay(timeBetweenRequests);
  delay(timeBetweenRequests);
  strReceived="";
  if(readyToReceive){


    while (esp.available()>0){
      char ch=esp.read();
      strReceived+=ch;
 //     dbg.write(ch);
      delayMicroseconds(10);
    }

 
  }
  dbg.println("-=-=-=-=-=-=- Received from URL: -=-=-=-=-=-=-");
  dbg.println(strReceived);
  dbg.println("------------- End of transmission ------------");
  delay(timeBetweenRequests);
  delay(timeBetweenRequests);
  delay(timeBetweenRequests);
  delay(timeBetweenRequests);

#endif
}
User avatar
By hudis
#24162 Ok guys, now I'm really puzzled.

I did the following:

I installed the esp8266-arduino into arduino IDE and re-flashed my ESP8266-1 board with the following sketch (so I don't have the AT firmware on it anymore):

Code: Select all#include <ESP8266WiFi.h>

const char* ssid     = "my_wifi";
const char* password = "my_password";

const char* host = "www.baidu.com";

void setup() {
  Serial.begin(9600);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

int value = 0;

void loop() {
  delay(5000);
  ++value;

  Serial.print("connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
 
  // We now create a URI for the request


  String url="/robots.txt";
 
  Serial.print("Requesting URL: ");
  Serial.println(url);
 
  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(1000);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("closing connection");
  delay(10000);
}


And I uploaded the following to Arduino Uno:
Code: Select all#include <SoftwareSerial.h>
#define dbg Serial
#define esp dbgSerial
#define DTESPEED 9600
#define timeBetweenRequests 500
SoftwareSerial dbgSerial(10, 11); // RX, TX

void setup() {
  dbg.begin(9600);
  delay(100);
  dbg.setTimeout(15000);   
  esp.begin(DTESPEED);
  delay(100);
  esp.setTimeout(15000);   
  delay(1000);
}

void loop() {
  String strReceived="";

  while (esp.available()){
      char ch=esp.read();
      strReceived+=ch;
  }

  if(strReceived.length()>0){
    dbg.print(strReceived);
  }


This time I get the whole robots.txt webpage without any problems, so it seems to me the problem is with AT firmware.