ESP8266通过wifi与Android手机通信

时间:2016-10-12 16:45:15

标签: android sockets kryonet

我正在尝试使用我的应用程序从ESP8266中找到一种读取数据的方法,Chrome上的工作正常。根据{{​​3}}(我正在使用Simple HTTP Server example)ESP使用套接字。我试过了Volley,但它没有用。

我的问题是,KryoNet是我需要的库吗?

我可以从Chrome中读取但不能使用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()

0 个答案:

没有答案
相关问题