我想用ESP8266制作一个按钮。它需要与我的本地Synapse(Matrix.org)服务器通信。 我需要获取access_key和room_id,需要登录我的ESP8266。
所以我有三个http方法。它的每一个都很好,但是当它们在我的ESP8266上都是三个时它就不起作用了。似乎第一个剂量得到了响应,但第二个得到了响应。然后skript停止因为一些变量是零。这就是为什么我没有发布第三种方法。
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%} {}
我做了很多尝试,我用尾巴方法包裹了所有东西,用报警器试了一遍。 但没有任何作用。我需要以正确的顺序做出所有回应。
我做错了什么?
答案 0 :(得分:0)
我担心这是一种经典之作。
使用此模块执行并发HTTP请求不。
来源:https://nodemcu.readthedocs.io/en/latest/en/modules/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)
解决方案2:任务调度
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。