ESP8266 / NodeMCU RESTful HTTP发布

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ESP8266 / NodeMCU RESTful HTTP发布相关的知识,希望对你有一定的参考价值。

以下代码在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
"
    .."Host: things.ubidots.com
"
    .."Content-Type: application/json
"
    .."Content-Length: "..string.len(data).."
"
    ..data.."
"
    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
"
.."Host: things.ubidots.com
"
.."Content-Type: application/json
"
.."Content-Length: "..string.len(data).."

"
..data..

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

您是否知道我们有专门的HTTP模块可以大大简化事情? - > https://nodemcu.readthedocs.io/en/latest/en/modules/http/#httppost

以上是关于ESP8266 / NodeMCU RESTful HTTP发布的主要内容,如果未能解决你的问题,请参考以下文章

ESP8266-NodeMCU项目:ESP8266-NodeMCU+Blinker+DHT11+小爱同学

ESP-NOW ESP8266 NodeMCU单板双向通信

ESP8266 nodemcu

ESP8266 NodeMCU 内存不足

基于MacOS搭建ESP8266 NodeMCU CP2102开发环境

基于MacOS搭建ESP8266 NodeMCU CP2102开发环境