NodeMCU - Lua - 进行HTTP Post或利用luasocket - 需要指导。

这是我第一次来到这里,我想加入论坛是因为我刚接触Lua编程,几乎放弃了HTTP Post方法。

我正在尝试使用ESP8266(运行在NodeMCU上)进行物联网,并使用ESPlore将Lua程序发送到ESP8266。

因此,我的程序目标是调用API并使用我的运行在ESP8266上的Lua程序发布几个参数。

我尝试了以下方法-

1。使用HTTP Post

conn=net.createConnection(net.TCP, 0)
conn:on("receive", display)
conn:connect(80,HOST)
conn:on("connection",function(obj)
   local post_request = build_post_request(HOST,URI)
   obj:send(post_request)
end

----以下是函数----------------------------------------------------

function build_post_request(host, uri)
    local data = ""
    data = "param1=1&param2=2"
    request = "POST uri HTTP/1.1\r\n"..
      "Host: example.com\r\n"..
      "apiKey: e2sss3af-9ssd-43b0-bfdd-24a1dssssc46\r\n"..
      "Cache-Control: no-cache\r\n"..
      "Content-Type: application/x-www-form-urlencoded\r\n"..data
    return request
end

----------------响应--------------------------------------

HTTP/1.1 400 Bad Request
Date: Sun, 11 Oct 2015 16:10:55 GMT
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 968
Connection: close

Apache Tomcat / 7.0.54 - 错误报告

客户端发送的请求语法有误。

##我不知道它有什么问题。

2。使用Luasocket

我已经在我的程序中包含了以下内容-

local http = require"socket.http"
local ltn12 = require"ltn12"

并且它会抛出以下错误-

script1.lua:3: module 'socket.http' not found:
no field package.preload['socket.http']
no file 'socket/http.lc'
no file 'socket/http.lua'

我不知道如何获取这些库并将其发送到ESP8266,也不确定是否足够。


问题:

使用API将数据发布到服务器的最佳方法是什么。

a。如果是HTTP Post,则代码有什么问题。

b。如果是Luasocket,那么我该如何将其发送到ESP8266,因为我没有在我的笔记本电脑上使用编译器。

点赞
用户1142045
用户1142045
"`Content-Type: application/x-www-form-urlencoded\r\n"..data`"  

我不理解它有什么问题。

在HTTP中,头部总是由`\r\n\r\n`分隔的。没有第二对CR-LF,则以下数据将导致头部错误,如Tomcat报告所述。

第二个问题是您无法在ESP8266上使用标准套接字库。您必须使用net库,该库是Espressif SDK的nodemcu包装器。 
2015-10-11 20:31:45