带有 Nodemcu 和 http-modul 的 ESP8266:在 GET 失败后对 POST 操作进行排序。没有回调方法

ESP8266 with Nodemcu and the http-modul: sequencing a POST operation after a GET failed. No callback-method

我想用我的 ESP8266 做一个按钮。它需要与我本地的 Synapse (Matrix.org) 服务器通信。 我需要得到我的 access_key 和一个 room_id 并且需要登录我的 ESP8266。

所以我有三个 http 方法。每一个都工作正常,但是当它们在我的 ESP8266 上都是三个时,它就不起作用了。似乎第一个得到回应,但第二个得到回应。然后由于某些变量为零,脚本停止。这就是为什么我没有 post 第三种方法。

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)

打印输出告诉我,它不会进入 http.post 的第一个回调方法。

pin:6, level:1 
Calling: 00000001
LED On
Push Counter is: 3
___________________________________
 200    {"room_id":"!TpYsyBpFLoxXrbVBZv:homeserver","servers"["homeserver"]}
 !TpYsyBpFLoxXrbVBZv:homeserver

来自服务器的日志文件显示:

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%} {}

我试了很多,我都用tail-method包起来,用一个alarm试了一下。 但没有任何效果。我需要按正确顺序进行所有回复。

我做错了什么?

恐怕这有点经典。

It is not possible to execute concurrent HTTP requests using this module.

来源:https://nodemcu.readthedocs.io/en/latest/en/modules/http/

由于所有 http.xxx 操作都是异步的(所有带有回调的 NodeMCU 函数都是...)您实际上是在尝试 运行 http.posthttp.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)

详情见https://nodemcu.readthedocs.io/en/latest/en/modules/node/#nodetaskpost