2

I have a ESP8266 simple http server with following Lua script

print("My First Lua program")
--print(adc.readvdd33())
print("Setting Wifi")
wifi.setmode(wifi.STATIONAP)            --[[ STATION + AP --]]
wifi.setphymode(wifi.PHYMODE_N)         --[[ IEEE 802.n --]]
print(wifi.getmode())
print(wifi.getphymode())
wifi.sta.config("srs", "cometomyn/w0")
tmr.delay(5000000)
print("Delay out")
--print(wifi.sta.getip())
srv=net.createServer(net.TCP) 
srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
        print(payload) 
        conn:send("<h1> ESP8266<BR>Server is working!</h1>")
        conn:close()
        end) 
end)

When i connect to server through my laptop with chrome, i am getting "Server is working!" response.

But when i connect through android app i made, it is crashing :( . Following is my app code

public class HttpManager {
public static String downloadUrl(String uri) throws IOException {
    HttpURLConnection con = null;
    InputStream is=null;
    try {
        URL url = new URL(uri);
        con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(10000);
        con.setConnectTimeout(15000);
        con.setRequestMethod("GET");
        //add request header
        //con.setRequestProperty("User-Agent", "Mozilla/5.0");
        //con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
        is = con.getInputStream();
    }catch (IOException e) {
        e.printStackTrace();
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuilder sb =  new StringBuilder();
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();
    String contentOfMyInputStream = sb.toString();
    return contentOfMyInputStream;
}

}

I am calling this HttpManager in an assync task. With this app i am able to get response from sites like google ! .

I am not sure which code is having issue !!! Could anyone can help me to solve this issue ?

Appending crash log too

07-18 11:51:04.122    3710-3710/srs.thebewboston I/First success﹕ http
07-18 11:51:04.312    3710-3985/srs.thebewboston W/System.err﹕ java.io.EOFException
07-18 11:51:04.322    3710-3985/srs.thebewboston W/System.err﹕ at libcore.io.Streams.readAsciiLine(Streams.java:203)
07-18 11:51:04.322    3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:544)
07-18 11:51:04.332    3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
07-18 11:51:04.332    3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
07-18 11:51:04.342    3710-3985/srs.thebewboston W/System.err﹕ at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
07-18 11:51:04.342    3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:38)
07-18 11:51:04.342    3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
07-18 11:51:04.342    3710-3985/srs.thebewboston W/System.err﹕ at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
07-18 11:51:04.382    3710-3985/srs.thebewboston W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-18 11:51:04.382    3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-18 11:51:04.412    3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-18 11:51:04.452    3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-18 11:51:04.472    3710-3985/srs.thebewboston W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-18 11:51:04.592    3710-3985/srs.thebewboston W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
07-18 11:51:04.612    3710-3985/srs.thebewboston W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0x409c01f8)
07-18 11:51:04.652    3710-3985/srs.thebewboston E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:278)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at java.io.Reader.<init>(Reader.java:64)
            at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
            at java.io.InputStreamReader.<init>(InputStreamReader.java:59)
            at srs.thebewboston.HttpManager.downloadUrl(HttpManager.java:43)
            at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:278)
            at srs.thebewboston.MainActivity$MyATask.doInBackground(MainActivity.java:264)
            at android.os.AsyncTask$2.call(AsyncTask.java:264)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

Adding my server response also

GET  Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36
GET  Dalvik/1.6.0 (Linux; U; Android 4.4.4; MI 3W MIUI/V6.5.3.0.KXDMICD)
GET  Mozilla/5.0 (Linux; Android 4.4.4; MI 3W Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.93 Mobile Safari/537.36

1st response when i tried with chrome from Laptop (Worked)

2nd response when i tried with my app from mobile (Not worked!!)

3rd response when i tried with chrome from same mobile (Worked)

My Assynctask calling section

private class MyATask extends AsyncTask<String, String, String>{
        TextView testout = (TextView) findViewById(R.id.testout);
        @Override
        protected void onPreExecute(){
            testout.append("Starting Task" + '\n');
        }
        @Override
        protected String doInBackground(String... params) {
            String httout = null;
            try {
                httout = HttpManager.downloadUrl(params[0]);
            } catch (IOException e) {
                e.printStackTrace();}
            return httout;}
        @Override
        protected void onProgressUpdate(String... values) {
            //testout.append(values[0]+'\n');}
        @Override
        protected void onPostExecute(String result) {
            testout.append(result + '\n');}
    }

Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.1.3"); //This one didn't work Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://www.google.com"); //This one worked

Thanks

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
SREEJITH S
  • 23
  • 3
  • `downloadUrl(HttpManager.java:38)`. Which code is on that line? – greenapps Jul 18 '15 at 06:27
  • `catch (IOException e)`. If you have a catch you should not continue with following code but return. – greenapps Jul 18 '15 at 06:30
  • I am calling HttpManager with the URL i needed to fetch, presently i am giving the IP address of ESP8266 module as below Atask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"http://192.168.1.3"); – SREEJITH S Jul 18 '15 at 06:30
  • `java.lang.NullPointerException`. You have that exception because you continue after that catch. – greenapps Jul 18 '15 at 06:32
  • @SREEJITHS can you log what is returned from server and comment of rest of the code. Is it hitting the server. i doubt that – Raghunandan Jul 18 '15 at 06:33
  • Please react on comments and answer the questions. Your remark serves nothing. – greenapps Jul 18 '15 at 06:33
  • Hi Greenapps, I am a newbie to this Java/android, my doubt is, it working without a crash when i give "google.com" instead of my esp module ip "192.168.1.3" – SREEJITH S Jul 18 '15 at 06:35
  • You already told that. Do not repeat that. Better answer my questinos and react on remarks. Please show complete urls used. – greenapps Jul 18 '15 at 06:36
  • @greenapps I am not sure about adding as remarks :(, I will look into it.... I tried the following Links "http://192.168.1.3" "http://www.google.com" – SREEJITH S Jul 18 '15 at 06:56
  • Those are no links. Please show full code like String url ="google.com";. Now write both down exactly as you used them. So thell the contents/value of uri in `downloadUrl(String uri) ` – greenapps Jul 18 '15 at 07:02
  • @greenapps I added async url calling section also, hope now it give a clear idea – SREEJITH S Jul 18 '15 at 09:31
  • `Adding my server response also`. Those are not your server responses. You found that in the logfiles of your server. Anyhow it looks as if the request is received from your android app too. – greenapps Jul 18 '15 at 09:38
  • You did noty react on my remark: `catch (IOException e). If you have a catch you should not continue with following code but return.`. Why no reaction? Please add following return there: `return "IOException: " + e.getMessage();`. Please report. – greenapps Jul 18 '15 at 09:44
  • @greenapps I tried adding e.getMessage(); in IOException... Now i got message as "IOException:null" – SREEJITH S Jul 18 '15 at 10:20
  • `downloadUrl(String uri) throws IOException {`. Remove the throws IOException. Add a catch block to the try and put it there. Handle all in downloadUrl(). Then you will know the reason. – greenapps Jul 18 '15 at 10:23
  • Check this line carefully. Because it seems your input stream is null. `is = con.getInputStream();` – Mert Gülsoy Sep 08 '15 at 14:45
  • @SREEJITHS Has the problem been solved? Is there any more feedback you need? If not I suggest you consider to accept the answer so that SO can mark this question as closed, http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work. – Marcel Stör Mar 11 '16 at 06:02

1 Answers1

1

There are essentially three issues with this code:

srv:listen(80,function(conn) 
    conn:on("receive",function(conn,payload) 
        print(payload) 
        conn:send("<h1> ESP8266<BR>Server is working!</h1>")
        conn:close()
        end)
  1. conn:send() is asynchronous, see API docs. This means you can't close the connection immediately after calling conn:send() because the data might not have been sent by the time you close the connection.
  2. You reuse the conn variable in the two callback functions. So, change function(conn,payload) to function(whatever,payload) and then do whatever:send accordingly.
  3. What you're sending back is not a valid HTTP response but just some protocol agnostic HTML snippet.

Look at https://github.com/nodemcu/nodemcu-firmware/blob/dev/README.md#programming-model for a complete and working example.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198