首页 文章

NodeMCU HTTP模块HTTP.POST()请求正文参数

提问于
浏览
0

有人可以告诉我如何编写http.post()请求的主体来读取ESP8266上附加传感器的值吗?

wifi.setmode(wifi.STATION);
wifi.sta.config("ssid","pwd")
local sensorPin = adc.read(0)

http.post('url',
  'Content-Type: application/json\r\n', 
  '"humidity":sensorPin'
  ,function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
  end)

如何从GPIO引脚上的附加传感器读取值,并将其作为后置请求正文中“key”:“value”对的值?

1 回答

  • 2

    我不明白你的问题到底是什么,对不起 . 它是如何read GPIO values,是处理ADC,是sending data in an interval,还是在Lua中使用字符串连接,还是?

    所以,这是一个简短的片段,可以修复你的代码:

    url = 'url'
    jsonContentTypeHeader = 'Content-Type: application/json\r\n'
    
    http.post(url, jsonContentTypeHeader,
      '{"humidity":' .. adc.read(0) .. '}', function(code, data)
        if (code < 0) then
          print("HTTP request failed")
        else
          print(code, data)
        end
      end)
    

    如果你需要编码更多的JSON数据,那么就有dedicated module .

    同样值得注意的是 wifi.sta.config("ssid", "pwd") 是异步的(和很多NodeMCU功能一样),你需要阻止网络呼叫,直到你得到一个IP地址 . 我们也有template for that in our docs .

相关问题