Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua 需要为nodemcu http调用提供代码结构建议吗_Lua_Nodemcu - Fatal编程技术网

Lua 需要为nodemcu http调用提供代码结构建议吗

Lua 需要为nodemcu http调用提供代码结构建议吗,lua,nodemcu,Lua,Nodemcu,我想得到什么 进行http调用以获取身份验证令牌 读取一些传感器数据 使用身份验证令牌通过http发送传感器数据 使节点进入深度睡眠 nodemcu固件(主)的一些限制包括: 无法发出同步http请求 不会调用第二个http.post的回调函数 您不能返回回调函数的值,即使如此(请参见2) 这些限制基本上阻止了我去做我想做的事情 这是我写的一个伪代码示例,由于限制2,这将不起作用 从文件中: 无法使用此模块执行并发HTTP请求 我尝试的另一种方法是返回回调函数,但由于http模块不支持回调函数,

我想得到什么

  • 进行http调用以获取身份验证令牌
  • 读取一些传感器数据
  • 使用身份验证令牌通过http发送传感器数据
  • 使节点进入深度睡眠
  • nodemcu固件(主)的一些限制包括:

  • 无法发出同步http请求
  • 不会调用第二个http.post的回调函数
  • 您不能返回回调函数的值,即使如此(请参见2)
  • 这些限制基本上阻止了我去做我想做的事情

    这是我写的一个伪代码示例,由于限制2,这将不起作用

    从文件中:

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

    我尝试的另一种方法是返回回调函数,但由于http模块不支持回调函数,也不支持同步连接,因此也不起作用

    function auth(user, pass)
        return http.post('https://home.io.moving-bytes.at/api/v1/login',
          'Content-Type: application/x-www-form-urlencoded\r\n',
          'username='..user..'&password='..pass,
          function(code, data)
          if (code < 0) then  
             print("HTTP request failed")
          else
             t = sjson.decode(data)
             token= t.token
             return token
          end
        end) 
    
    end
    
    function post(key, val, token)
    
        return http.post('https://home.io.moving-bytes.at/api/',
          'Authorization: Bearer '..token..'\r\nContent-Type: application/x-www-form-urlencoded\r\n',
    
          'key='..key..'&val='..val,
          function(code, data)
          if (code < 0) then  
             print("HTTP request failed")
          else
            print(data)
          end
        end)
    
    end 
    
    local token=auth("user", "pass");
    post("key", 123, token)
    
    函数身份验证(用户,通过)
    返回http.post('https://home.io.moving-bytes.at/api/v1/login',
    '内容类型:application/x-www-form-urlencoded\r\n',
    用户名=“…用户…”和密码=“…通过,
    功能(代码、数据)
    如果(代码<0),则
    打印(“HTTP请求失败”)
    其他的
    t=sjson.decode(数据)
    token=t.token
    返回令牌
    结束
    (完)
    结束
    功能post(键、val、令牌)
    返回http.post('https://home.io.moving-bytes.at/api/',
    “授权:承载”…令牌”\r\n内容类型:application/x-www-form-urlencoded\r\n“,
    “key=”…key..“&val=”…val,
    功能(代码、数据)
    如果(代码<0),则
    打印(“HTTP请求失败”)
    其他的
    打印(数据)
    结束
    (完)
    结束
    本地令牌=auth(“用户”、“通过”);
    邮政(“钥匙”,123,令牌)
    
    我最新的方法是使用一个回调函数,它也不起作用

    local token=nil
    
    function send_data(key, val, callback)
        callback()
        print("send data")
        print("key "..key)
        print("token in send data"..token)
        http.post('http://home.io.moving-bytes.at/api/sensors/data',
          'Content-Type: application/x-www-form-urlencoded\r\n',
          'sensor_id=5&key='..key..'&value='..val,
          function(code, data)
          print("callback called")
          if (code < 0) then
             print("HTTP request failed")
             print("key: "..key)
             print("val: "..val)
          else
             print(code, data)
          end
        end)
    
    end
    
    
    function auth(user, pass)
        print("auth")
        print("user "..user);
        print("pass "..pass);
        http.post('https://home.io.moving-bytes.at/api/v1/login',
          'Content-Type: application/x-www-form-urlencoded\r\n',
          'username='..user..'&password='..pass,
          function(code, data)
          if (code < 0) then
             print("HTTP request failed")
          else
             t = sjson.decode(data)
             token= t.token
             print("token"..token)
          end
        end)
    
    end
    
    
    send_data("distance", 3, auth("user", "passs"));
    
    localtoken=nil
    函数send_data(键、val、回调)
    回调函数()
    打印(“发送数据”)
    打印(“键”。.键)
    打印(“发送数据中的令牌”。.token)
    http.post('http://home.io.moving-bytes.at/api/sensors/data',
    '内容类型:application/x-www-form-urlencoded\r\n',
    “传感器id=5&key=”…key..“&value=”…val,
    功能(代码、数据)
    打印(“调用回调”)
    如果(代码<0),则
    打印(“HTTP请求失败”)
    打印(“键:…键)
    打印(“val:…val”)
    其他的
    打印(代码、数据)
    结束
    (完)
    结束
    函数身份验证(用户,通过)
    打印(“授权”)
    打印(“用户”。.user);
    打印(“通过”。。通过);
    http.post('https://home.io.moving-bytes.at/api/v1/login',
    '内容类型:application/x-www-form-urlencoded\r\n',
    用户名=“…用户…”和密码=“…通过,
    功能(代码、数据)
    如果(代码<0),则
    打印(“HTTP请求失败”)
    其他的
    t=sjson.decode(数据)
    token=t.token
    打印(“令牌”。.令牌)
    结束
    (完)
    结束
    发送数据(“距离”,3,身份验证(“用户”,“通行证”);
    
    看在上帝的份上,我浪费了很多时间来打两个简单的http电话……

    任何帮助都将不胜感激

    您的第一个代码看起来是正确的方法。运行时会发生什么情况?@EgorSkriptunoff问题在于固件,只会调用第一个http.post回调。从我的服务器日志中,我可以说第二个http.post将被处理,但回调函数永远不会被调用。未测试:
    auth(“user”,“passs”)
    ,然后在该函数的http回调调用中
    node.task.post(send_data(token))
    (与http post无关)。因此,当我想发送三个或更多的key/val paris时,代码看起来像这样:node.task.post(发送_数据(“key”,“val”标记,发送_数据(“key”,“val”标记,发送_数据,发送_数据(“key”,“val”标记)))这样的标准工作流没有更干净的解决方案吗?您的第一个代码看起来是正确的方法。运行时会发生什么情况?@EgorSkriptunoff问题在于固件,只会调用第一个http.post回调。从我的服务器日志中,我可以说第二个http.post将被处理,但回调函数永远不会被调用。未测试:
    auth(“user”,“passs”)
    ,然后在该函数的http回调调用中
    node.task.post(send_data(token))
    (与http post无关)。因此,当我想发送三个或更多的key/val paris时,代码应该是这样的:node.task.post(发送_数据(“key”,“val”标记,发送_数据(“key”,“val”标记,发送_数据,发送_数据(“key”,“val”标记)))对于这样的标准工作流,没有更干净的解决方案吗?
    local token=nil
    
    function send_data(key, val, callback)
        callback()
        print("send data")
        print("key "..key)
        print("token in send data"..token)
        http.post('http://home.io.moving-bytes.at/api/sensors/data',
          'Content-Type: application/x-www-form-urlencoded\r\n',
          'sensor_id=5&key='..key..'&value='..val,
          function(code, data)
          print("callback called")
          if (code < 0) then
             print("HTTP request failed")
             print("key: "..key)
             print("val: "..val)
          else
             print(code, data)
          end
        end)
    
    end
    
    
    function auth(user, pass)
        print("auth")
        print("user "..user);
        print("pass "..pass);
        http.post('https://home.io.moving-bytes.at/api/v1/login',
          'Content-Type: application/x-www-form-urlencoded\r\n',
          'username='..user..'&password='..pass,
          function(code, data)
          if (code < 0) then
             print("HTTP request failed")
          else
             t = sjson.decode(data)
             token= t.token
             print("token"..token)
          end
        end)
    
    end
    
    
    send_data("distance", 3, auth("user", "passs"));