java - Android & NodeMCU,从服务器接收响应无法正常工作?

标签 java android lua esp8266 nodemcu

我在Android上编写了一个应用程序,它实现了向服务器发送简单的请求(使用Volley)。服务器建立在 NodeMCU (ESP8266) 微 Controller 上,用 Lua 编写。问题是,发送请求后,应用程序并不总是能够打印响应。如果地址是例如“http://www.google.com ”它正确发送请求并接收并显示响应,但如果它是下面代码中的地址 - 它正确发送请求(服务器使用react)但不(?)接收响应(不显示它,显示:“那不起作用!”)。您有什么想法吗?我该如何解决它并能够打印响应?

Android(负责发送请求的部分):

buttonSynchro.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {


        // Instantiate the RequestQueue.
        String url = "http://192.168.1.12/";


        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        testTextView.setText("Response is: "+ response.substring(0,500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                testTextView.setText("That didn't work!");
            }
        });


        // Add the request to the RequestQueue.
        RequestQueue queue = Volley.newRequestQueue(SettingsActivity.this);
        queue.add(stringRequest);
    }
});  

NodeMCU、Lua:

station_cfg={}
station_cfg.ssid="Dom"
station_cfg.pwd="lalala"
wifi.sta.config(station_cfg)
function receive(conn, request)
        print(request)
        print()
        local buf = "";
        buf = buf.."<!doctype html><html>";
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."</html>";

        conn:send(buf);
        conn:on("sent", function(sck) sck:close() end);     
        collectgarbage();

end

function connection(conn) 
    conn:on("receive", receive) 

end

srv=net.createServer(net.TCP, 30) 
srv:listen(80, connection)

最佳答案

nPn 的代码适用于某些用户代理(macOS 上的 Chrome/Filfox/curl/wget),但不适用于其他用户代理(macOS 和 iOS 上的 Safari、iOS 上的 Firefox Klar)。这可能是由于缺少 HTTP header 造成的。

我建议您坚持使用文档中的示例 https://nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketsend .

srv = net.createServer(net.TCP)

function receiver(sck, data)
  print(data)
  print()

  -- if you're sending back HTML over HTTP you'll want something like this instead
  local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}

  response[#response + 1] = "<!doctype html><html>"
  response[#response + 1] = "<h1> ESP8266 Web Server</h1>"
  response[#response + 1] = "</html>"

  -- sends and removes the first element from the 'response' table
  local function send(localSocket)
    if #response > 0 then
      localSocket:send(table.remove(response, 1))
    else
      localSocket:close()
      response = nil
    end
  end

  -- triggers the send() function again once the first chunk of data was sent
  sck:on("sent", send)

  send(sck)
end

srv:listen(80, function(conn)
  conn:on("receive", receiver)
end)

此外,您的代码(以及 nPn 的代码)会假设 WiFi 在不应使用的地方可用。

wifi.sta.config(station_cfg) (with auto-connect=true) and wifi.stat.connect异步,因此是非阻塞的 - 与许多其他 NodeMCU API 一样。因此,您应该将上述代码放入一个函数中,并且仅在设备连接到 AP 并获​​得 IP 时调用它。你可以通过例如使用 WiFi 事件监视器注册 STA_GOT_IP 事件的回调。您将在 https://nodemcu.readthedocs.io/en/latest/en/upload/#initlua 找到一个非常详细的启动序列示例,该示例监听所有 WiFi 事件。 。对于初学者,您可能需要修剪它并仅监听got-IP。

关于java - Android & NodeMCU,从服务器接收响应无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49203229/

相关文章:

Android RecyclerView ItemTouchHelper 恢复滑动和恢复 View 持有者

lua - Redis 获取排序集的键,其中至少一个成员的分数小于 N

lua - math.sin/cos/tan 是否使用查找表?

java - 如何使用此代码对相同的值进行排序?

java - 我的 RSS 阅读器给出了 java.lang.NullPointerException

java - 在 Docker File 中添加容器启动时启动的脚本

android - 在 Google Play 上发布 2 个 apk。一个用于平板电脑,另一个用于智能手机

java - Android Room 插入所有问题

lua - HMGET 与 lua-resty-redis 中的数组

java - 如何使用 keystore 中存储的自定义 key 执行 AES 加密?