有人可以告诉我如何编写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”对的值?
答案 0 :(得分:2)
我不明白究竟你的问题是什么,抱歉。它是read GPIO values的方法,是处理ADC,是sending data in an interval还是Lua中的字符串连接,还是?
所以,这是一个修补代码的简短代码段:
test "hello world"
如果您需要编码更多JSON数据,那么dedicated module就可以了。
还值得注意的是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)
是异步的(与许多NodeMCU功能一样),您需要暂停网络呼叫,直到获得IP地址。我们也有template for that in our docs。