簡體   English   中英

ESP8266 / NodeMCU RESTful HTTP發布

[英]ESP8266/NodeMCU RESTful HTTP posting

以下代碼在ESP8266微控制器上運行,通過HTTP將基本傳感器讀數流發布到Ubidots Web服務API。 使用了NodeMCU Lua解釋器(我現在想在Lua中使用它,現在不希望使用其他替代方法,例如Arduino IDE或MicroPython。)

“傳感器數據”似乎正常工作(現在是虛擬的),發布閱讀的計時器事件似乎正常,我認為POST的JSON格式和結構基本上是正確的,並且WiFi連接似乎正確,但是數據最終到達雲儀表板。

我認為在設置POST語法方面做得很愚蠢。

我希望能有新的發現該錯誤的眼睛。

WIFI_SSID = "foo_ssid"
WIFI_PASSWORD = "secret_password"
API_TOKEN = "secret_ubidots_token"
update_period = 5 -- seconds

function format_json(variable1, value1, variable2, value2, variable3, value3)
  -- let's allow three different readings in the nested JSON payload.
  data = '{'..variable1..': {"value": '..value1..'},'
            ..variable2..': {"value": '..value2..'},'
            ..variable3..': {"value": '..value3..'}}'
return data
end

function postUbidots(deviceName, name1, value1, name2, value2, name3, value3)
  connection_out = nil
  connection_out =  net.createConnection(net.TCP, 0)

  connection_out:on("receive", function(connection_out, payload_out)
    if (string.find(payload_out, "201 CREATED") ~= nil) then
      print("POST OK");
    end
  end)

  connection_out:on("connection", function(connection_out, payload_out)
    data = format_json(name1, value1, name2, value2, name3, value3)
    local post_string = "POST /api/v1.6/devices/"..deviceName.."/?token="
    ..API_TOKEN.." HTTP/1.1\n"
    .."Host: things.ubidots.com\n"
    .."Content-Type: application/json\n"
    .."Content-Length: "..string.len(data).."\n"
    ..data.."\n"
    connection_out:send(post_string)
  end)

  connection_out:on("disconnection", function(connection_out, payload_out)
    connection_out:close();
    collectgarbage();
  end)

  connection_out:connect(80, 'things.ubidots.com')
end

function readSensors()
  -- don't worry about real sensors right now
  -- let's just make up a few dummy variables.
  sensor1_value = 90
  sensor2_value = 65
  sensor3_value = 30
  postUbidots("deviceName", "sensor1", sensor1_value, "sensor2", sensor2_value, "sensor3", sensor3_value)
end

wifi.setmode(wifi.STATION)
wifi.setphymode(wifi.PHYMODE_G)
station_config={}
station_config.ssid=WIFI_SSID
station_config.pwd=WIFI_PASSWORD
station_config.auto=true
wifi.sta.config(station_config)

tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip() == nil then
    else
        tmr.stop(1)
    end
end)

print("WiFi connected.")
tmr.alarm(1, (update_period*1000), tmr.ALARM_AUTO, function() readSensors() end)

問題是如何格式化HTTP消息。 嘗試這個:

local post_string = "POST /api/v1.6/devices/"..deviceName.."/?token="
..API_TOKEN.." HTTP/1.1\r\n"
.."Host: things.ubidots.com\r\n"
.."Content-Type: application/json\r\n"
.."Content-Length: "..string.len(data).."\r\n\r\n"
..data..

參考: https : //nodemcu.readthedocs.io/en/latest/en/modules/net/#netsocketsend

您是否知道我們有一個專用的HTTP模塊,可以大大簡化事情? -> https://nodemcu.readthedocs.io/en/latest/en/modules/http/#httppost

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM