0

I am trying to find a way to read data using my app from an ESP8266, on Chrome works fine by the way. According to the documentation (I am using the Simple HTTP Server example) ESP uses sockets. I tried Volley but it didn't work.

My question is, is KryoNet the library I need?

The code that I can read from chrome but not from my app using Volley:

import machine
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]

html = """<!DOCTYPE html>
<html>
    <head> <title>ESP8266 Pins</title> </head>
    <body> <h1>ESP8266 Pins</h1>
        <table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
    </body>
</html>
"""

import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('listening on', addr)

while True:
    cl, addr = s.accept()
    print('client connected from', addr)
    cl_file = cl.makefile('rwb', 0)
    while True:
        line = cl_file.readline()
        if not line or line == b'\r\n':
            break
    rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
    response = html % '\n'.join(rows)
    cl.send(response)
    cl.close()
traveller
  • 327
  • 1
  • 2
  • 11
  • What does work fine on Crome exactly? – greenapps Oct 12 '16 at 17:07
  • `I am using the Simple HTTP Server example`. Where and how are you using that? Dont assume we read that documentation. – greenapps Oct 12 '16 at 17:08
  • `I tried Volley but it didn't work.`. What did you try to do with Volley. And what did not work exactly? – greenapps Oct 12 '16 at 17:09
  • `a way to read data using my app from an ESP8266`. You should tell what kind of data and how the ESP puts it available. – greenapps Oct 12 '16 at 17:10
  • @greenapps I added the code that runs on the ESP, it's just a table. But it's not over HTTP, it's over sockets. – traveller Oct 12 '16 at 17:12
  • You did not answer my questions at all. Please write a better post so we kniw what you have and want. – greenapps Oct 12 '16 at 17:14
  • `it's just a table. But it's not over HTTP, it's over sockets.`. That looks like code for a server. And a html page containing a table is served by the socket. Pretty normal for a webserver to use sockets. We cannot see if a http protocol is used. And one cannot compare http with a socket. – greenapps Oct 12 '16 at 17:22
  • @greenapps I am trying to read the response of the webserver from my app. First I used Volley but I got an exception of Bad URL. Although it worked for other pages I couldn't read that from the ESP. Then after reading the documentation i realized i was dealing with sockets and i am wondering if KryoNET is the library from my situation. – traveller Oct 12 '16 at 17:51
  • Of course a webserver uses a socket. Without a socket nothing goes. But if you can do it with Chrome then you can do it in an Android app. Now tell what you exactly did in Chrome. – greenapps Oct 12 '16 at 18:45
  • Well, using chrome all I did was to just type the ip, 192.168.4.1 and it worked. What is interesting is that during debugging the error message I receive is `java.net.ProtocolException: Unexpected status line: ` – traveller Oct 12 '16 at 19:02
  • It looks to me that the http protocol is not followed at all. But there are so many ways to retrieve that page. You dont have to use volley for instance. I cannot remember having such problems with that esp component. But i do not understand the posted code either. What is `cl` for instance? What is read from that file? – greenapps Oct 12 '16 at 19:09
  • `The code that I can read from chrome but not from my app using Volley:` ???? What do you mean???? Dont tell us that that is what you get in Chrome. That you get that code in Chrome. You better post what you get in Chrome in another block. – greenapps Oct 12 '16 at 19:16
  • I think I found where the problem lies. Comparing to other examples the lack of `HTTP/1.0 200 OK\r\n\` at the beginning of CONTENT is what causes the problem. Have a look here https://github.com/micropython/micropython/tree/master/examples/network every example begins the response with that line instead of mine. – traveller Oct 12 '16 at 20:03
  • Indeed. It did not follow the http protocol. Then add that line. – greenapps Oct 12 '16 at 20:11

0 Answers0