2017-04-10 63 views
0

有人可以告诉我如何编写http.post()请求的主体以从ESP8266上连接的传感器读取值?NodeMCU HTTP模块HTTP.POST()请求正文参数

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引脚连接的传感器值,并把那个作为“钥匙”的价值:在POST请求的身体“值”对?

+0

你需要更多的反馈吗? –

回答

1

我不明白究竟是什么你的问题是,对不起。它是如何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