-1

I really need all the help I can get... It's been a week now and I'm still not able to solve the problem.

I have text file on a server. The whole address is www.example.com/test.txt I want to read the file using a GET request every 3-5 secs and parse it for further actions. But the arduino codes that I've tried till now use software serial and it keeps misbehaving. Sometimes it skips a few serial-print commands and loops back sometimes it reads the file 3-4 times and then hangs. Basically I'm getting the response only a few times but then it stops working properly.

I want to solve this...I need a working arduino code to work with ESP8266 so that I can successfully poll a text file from server without any problems.

Here is my current code -

 #include<SoftwareSerial.h>

    #define led 13
    SoftwareSerial esp(11, 12);
    String domain = "www.example.com";
    String request = "GET /test.txt HTTP/1.0\r\n\r\n";


    void SerialDo(String command, float time, bool debug) {
      delay(100);
      esp.print(command);

      int i = millis();
      while (millis() - i < (time * 1000)) {
        while ( (esp.available())) {
          if (debug) {
            Serial.print((char)esp.read());
          }
        }
      }

      Serial.flush();
    }

    void setup() {
      // put your setup code here, to run once:
      digitalWrite(led, OUTPUT);
      Serial.begin(9600);
      esp.begin(9600);
      esp.setTimeout(3000);
      digitalWrite(led, LOW);
      Serial.println("Hello");
      //delay(2000);
      SerialDo("AT+RST\r\n", 5, true);
      Serial.flush();
      SerialDo("AT+CWMODE=1\r\n", 0.5, true);

      delay(1000);
      //SerialDo("AT+CWLAP\r\n", 5, true);
      SerialDo("AT+CWJAP=\"Sherlock\",\"Watson@11\"\r\n", 10, true);
      SerialDo("AT+CIPMUX=0\r\n", 0.5, true);

    }

    void loop() {


      delay(1000);
      Serial.print("loop\r\n");

      SerialDo("AT+CIPMUX=0\r\n", 0.5, true);
      SerialDo("AT+CIPSTART=\"TCP\",\"" + domain + "\",80\r\n", 1.5, true);
      esp.flush();

      esp.print("AT+CIPSEND=" + String(request.length()) + "\r\n");
      delay(100);
      esp.print(request);

      String t = "";
      int i = millis();
      while (millis() - i < 1500) {
        while ((esp.available() > 0)) {
          if (true) {
            char c = (char)esp.read();
            t += c;
            Serial.print(c);
          }
        }
      }

      esp.print("AT+CIPCLOSE\r\n");

      if (t.indexOf("hello world") > -1) {
        digitalWrite(led, !digitalRead(led));
      }
  }
  • Which Serial.print()s are being omitted? By response, you mean the complete text file, right? How large is the text file? – TisteAndii Jan 9 '16 at 16:28
0

It seems that you're trying to do the same thing:

https://www.youtube.com/watch?v=q02f4sPghSo

Maybe it will help, I'm tryin' to do that (a POST), only that I don't have the server on a text file.

  • Welcome to Arduino SE. Link-only answers are generally discouraged because it relies on the link's destination staying online. Could you expand your answer to include some of the essential information please? Thanks. – Peter Bloomfield May 20 '15 at 1:26
0

You aren't implementing the timeouts well and I don't see the point of this if:

if (true) {
   char c = (char)esp.read();
   t += c;
   Serial.print(c);
}

It will always be executed. You have allotted 1500 ms to the whole process of reading the whole file, whether the ESP is done or not. Inside the loop, you are calling Serial.print(), as well as concatenating chars to a string, which takes a significant portion of the 1.5 secs. Get rid of the Serial.print() and use the timeout this way:

unsigned long lastRead = millis();
while (millis() - lastRead < 1500) {
    while (esp.available() {
        t += (char)esp.read();
        lastRead = millis();
    }
}
Serial.print(t);

Storing the value of millis() with an int isn't a good idea since after about 33 seconds, it'll overflow. The code above will ensure that the last time a character was read is always stored in lastRead; the end result is that no character is available from the ESP after a continuous 1500ms, we conclude that there's nothing more to be read or some error has occurred. Repeat this for the loop in SerialDo as well. The setTimeout in your setup() is also unneeded, in this case; you didn't call any SoftwareSerial method that makes use of it, so remove the line. Also if your server supports HTTP 1.1, then try this request:

    String request = "GET /test.txt HTTP/1.1\r\nHost: www.example.com\r\n\r\n"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.