2017-04-10 30 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