Lua 带有Nodemcu和http模块的ESP8266:在GET失败后对POST操作进行排序。无回调方法

Lua 带有Nodemcu和http模块的ESP8266:在GET失败后对POST操作进行排序。无回调方法,lua,esp8266,nodemcu,Lua,Esp8266,Nodemcu,我想用我的ESP8266做一个按钮。它需要与我的本地Synapse(Matrix.org)服务器通信。 我需要获取访问密钥和房间id,并需要登录我的ESP8266 所以我有三种http方法。每一个都很好用,但是当我的ESP8266上都有三个时,它就不起作用了。第一个似乎没有得到回应,但是第二个得到了回应。然后SKcript停止,因为某些变量为零。这就是为什么我没有发布第三种方法 My NodeMcu-Firmeware: NodeMCU custom build by frightanic.co

我想用我的ESP8266做一个按钮。它需要与我的本地Synapse(Matrix.org)服务器通信。 我需要获取访问密钥和房间id,并需要登录我的ESP8266

所以我有三种http方法。每一个都很好用,但是当我的ESP8266上都有三个时,它就不起作用了。第一个似乎没有得到回应,但是第二个得到了回应。然后SKcript停止,因为某些变量为零。这就是为什么我没有发布第三种方法

My NodeMcu-Firmeware:
NodeMCU custom build by frightanic.com
   branch: master
   commit: 22e1adc4b06c931797539b986c85e229e5942a5f
   SSL: true
   modules: bit,cjson,crypto,encoder,file,gpio,http,net,node,sntp,tmr,uart,wifi,tls
  build     built on: 2017-05-01 14:03
powered by Lua 5.1.4 on SDK 2.0.0(656edbf)
我的代码:

`ok, jsonheader = pcall(cjson.encode, {Accept="application/json"})
 if ok then
    print(jsonheader)
 else
    print("failed to encode!")
 end

 ok, jsonbody = pcall(cjson.encode, {type="m.login.password",user="admin", password="admin"})
 if ok then
    print(jsonbody)
 else
    print("failed to encode!")
 end


http.post("http://localhost:8008/_matrix/client/r0/login", jsonheader, jsonbody  , function(code, data)
if (code< 0) then
        print("HTTP_GET request failed")

    else

        print(code, data)
        accessToken = cjson.decode(data).access_token
        print(accessToken)
    end
end)






http.get("http://localhost:8008/_matrix/client/r0/directory/room/%23ButtonPrinter%3Ahomeserver", jsonheader, function(code1, data1)
if (code1< 0) then
        print("HTTP_GET request failed")
else
        print("___________________________________")
        print(code1, data1)
        roomId = cjson.decode(data1).room_id
        print(roomId)

    end
end)
服务器的日志文件显示:

2017-05-06 10:09:16,233 - synapse.storage.TIME - 215 - INFO - - Total database time: 0.000% {update_cached_last_access_time(0): 0.000%, store_device(0): 0.000%, get_users_in_room(0): 0.000%} {}
2017-05-06 10:09:18,246 - synapse.access.http.8008 - 59 - INFO - GET-6- 192.168.178.XX - 8008 - Received request: GET /_matrix/client/r0/directory/room/%23ButtonPrinter%3Ahomeserver
2017-05-06 10:09:18,249 - synapse.util.async - 201 - INFO - GET-6- Acquired linearizer lock 'state_resolve_lock' for key frozenset([17, 18])
2017-05-06 10:09:18,250 - synapse.util.async - 208 - INFO - GET-6- Releasing linearizer lock 'state_resolve_lock' for key frozenset([17, 18])
2017-05-06 10:09:18,251 - synapse.access.http.8008 - 91 - INFO - GET-6- 192.168.178.XX - 8008 - {None} Processed request: 4ms (0ms, 0ms) (0ms/2) 69B 200 "GET /_matrix/client/r0/directory/room/%23ButtonPrinter%3Ahomeserver HTTP/1.1" "None"
2017-05-06 10:09:18,253 - synapse.access.http.8008 - 59 - INFO - POST-7- 192.168.178.XX - 8008 - Received request: POST /_matrix/client/r0/login
2017-05-06 10:09:18,545 - synapse.handlers.auth - 433 - INFO - POST-7- Logging in user @admin:homeserver on device DWOVBGCIOD
2017-05-06 10:09:18,548 - synapse.access.http.8008 - 91 - INFO - POST-7- 192.168.178.XX - 8008 - {None} Processed request: 295ms (0ms, 0ms) (3ms/5) 364B 200 "POST /_matrix/client/r0/login HTTP/1.1" "None"
2017-05-06 10:09:21,198 - synapse.handlers.typing - 79 - INFO - - Checking for typing timeouts
2017-05-06 10:09:21,199 - synapse.handlers.presence - 329 - INFO - - Handling presence timeouts
2017-05-06 10:09:26,197 - synapse.handlers.typing - 79 - INFO - - Checking for typing timeouts
2017-05-06 10:09:26,198 - synapse.handlers.presence - 329 - INFO - - Handling presence timeouts
2017-05-06 10:09:26,232 - synapse.storage.TIME - 215 - INFO - - Total database time: 0.042% {store_device(2): 0.017%, add_device_change_to_streams(1): 0.010%, add_access_token_to_user(1): 0.009%} {}
我试了这么多,我把所有的东西都用尾巴包起来,用闹钟试了试。 但什么都不管用。我需要按照正确的顺序做出回应


我做错了什么?

恐怕这有点经典

无法使用此模块执行并发HTTP请求

资料来源:

由于所有的
http.xxx
操作都是异步的(所有带有回调的NodeMCU函数都是…),因此您实际上是在尝试并行运行
http.post
http.get

解决方案1:请求链接

http.post(url, jsonheader, jsonbody, function(code, data)
  if (code < 0) then
    print("HTTP request failed")
  else
    print(code, data)
    -- http.get()
  end
end)
http.post(url, jsonheader, jsonbody, function(code, data)
  if (code < 0) then
    print("HTTP request failed")
  else
    print(code, data)
    node.task.post(function()
      -- http.get()
    end)
  end
end)
http.post(url、jsonheader、jsonbody、函数(代码、数据)
如果(代码<0),则
打印(“HTTP请求失败”)
其他的
打印(代码、数据)
--http.get()
结束
(完)
解决方案2:任务分派

http.post(url, jsonheader, jsonbody, function(code, data)
  if (code < 0) then
    print("HTTP request failed")
  else
    print(code, data)
    -- http.get()
  end
end)
http.post(url, jsonheader, jsonbody, function(code, data)
  if (code < 0) then
    print("HTTP request failed")
  else
    print(code, data)
    node.task.post(function()
      -- http.get()
    end)
  end
end)
http.post(url、jsonheader、jsonbody、函数(代码、数据)
如果(代码<0),则
打印(“HTTP请求失败”)
其他的
打印(代码、数据)
node.task.post(函数()
--http.get()
(完)
结束
(完)

有关详细信息,请参阅。

首先,感谢您的回答。这就是我的想法。我读了NodeMcu文档上的引用。但是当你有一个http方法,并且在回调函数中放置了另一个http方法,就像你在解决方案中给我展示的那样,我明白了这一点。我会尝试你们两个的解决方案,如果它适合我,我会稍后写信。谢谢,我们去年12月增加了链接功能:啊,好的,好的,非常感谢。第一个解决方案现在运行良好。代码结构不是很好,但是现在我有了一个解决方案,有了它,我可以让代码更漂亮,也许可以使用节点模块。非常感谢,这对我很有帮助“代码结构不太好”-显然每个HTTP请求都可以/应该进入它自己的功能。这将给你留下一个相当简洁的结构。在每个HTTP回调中,您只需调用另一个函数(封装了
HTTP.xxx
)。是的,是的,这就是我尝试的方法,起初,这就是我对“tail function”的意思,但我不知道为什么它不起作用。但我会再试一次。