2017-04-10 4 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에서 문자열 연결을 사용하고 있습니까? 더 많은 JSON 데이터를 인코딩해야하는 경우

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) 

가 그에 대한 dedicated module있다 :

그래서, 여기에 코드를 수정합니다 짧은 단편이다.

또한 wifi.sta.config("ssid", "pwd")은 비동기 적이기 때문에 (많은 NodeMCU 기능과 마찬가지로) IP 주소가있을 때까지 네트워크 호출을 보류해야합니다. 우리는 template for that in our docs도 가지고 있습니다.