LUA - 使用HTTP GET请求将数据发送到PHP

时间:2016-11-23 17:55:17

标签: sockets lua httprequest nodemcu

我试图制作一个将数据从ESP 8266发送到网络服务器的温度站,问题是我对ESP 8266和Lua不太熟悉(我从中获取了代码)例)。

  • 所以问题是我必须粘贴哪个IP地址?我的路由器的IP或ESP的IP?
  • conn:send("GET /http://mywebsite.net/Temperatur/PHP/insertTemp.php?temp="..temp.."&humi="..humi.." HTTP/1.1\r\n")这条线是否正确?

    SSID="Josennet"
    Password="87E55FA36E"
    IP='10.0.0.1'
    
    temp = 10
    humi = 10
    
    conn=net.createConnection(net.TCP, 0) 
    conn:on("receive", function(conn, payload) print(payload) end)
    conn:connect(80,IP) 
    conn:send("GET /http://mywebsite.net/Temperatur/PHP/insertTemp.php?temp="..temp.."&humi="..humi.." HTTP/1.1\r\n") 
    conn:send("Host: IP\r\n") 
    conn:send("Accept: */*\r\n") 
    conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
    conn:send("\r\n")
    conn:on("sent",function(conn)
    conn:close()
    end)
    conn:on("disconnection", function(conn)
    end)
    

修改 insertTemp.php

<?php    
if($_SERVER["REQUEST_METHOD"]=="GET"){
    include 'connection.php';
    insertTemperatur();
}

function insertTemperatur()
{
    global $connect;
    $Temperatur = $_GET["temp"];

    $query = "INSERT INTO  `Temperatur` (  `Temperatur` ) VALUES ('$Temperatur')";

    mysqli_query($connect,$query);
    mysqli_close($connect);     
}
?>

我为数据后面的常量插入了,但它根本不起作用。 PHP文件是正确的。

  • HTTP_SERVER_SCRIPTNAME =&#34; Temperatur / PHP / insertTemp.php&#34;

  • HTTP_SERVER_IP =&#34; 144.76.167.69&#34;

  • HTTP_SERVER_HOSTNAME =&#34; ribisl.bplaced.net&#34;

编辑2: Mysql.lua:

SSID="Josennet"
Password="87E55FA36E"

wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,Password)



HTTP_SERVER_SCRIPTNAME = "Temperatur/PHP/insertTemp.php"
HTTP_SERVER_IP = "144.76.167.69"
HTTP_SERVER_HOSTNAME = "ribisl.bplaced.net"
SERIAL_PRINT = true
function do_get(data1)
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c)
        if (SERIAL_PRINT) then
            print(c)
        end
    end )
    sk:connect(80, HTTP_SERVER_IP)
    sk:on("connection", function(sck,c)
      -- Wait for connection before sending.
      sk:send("GET /"..HTTP_SERVER_SCRIPTNAME.."?id="..node.chipid()
                                             .."&temp="..data1.." HTTP/1.1\r\n"
            .."Host: "..HTTP_SERVER_HOSTNAME.."\r\n"
            .."Connection: keep-alive\r\n"
            .."Accept: */*\r\n\r\n")
    end)
end
do_get("12") --just for testing

2 个答案:

答案 0 :(得分:2)

你的问题有点令人困惑,但我想我理解你的问题(所以我会试试)。这是我用来对HTTP服务器进行GET的函数:

function do_get(data1, data2)
    sk=net.createConnection(net.TCP, 0)
    sk:on("receive", function(sck, c)
        if (SERIAL_PRINT) then
            print(c)
        end
    end )
    sk:connect(80, HTTP_SERVER_IP)
    sk:on("connection", function(sck,c)
      -- Wait for connection before sending.
      sk:send("GET /"..HTTP_SERVER_SCRIPTNAME.."?id="..node.chipid()
                                             .."&t="..data1
                                             .."&h="..data2.." HTTP/1.1\r\n"
            .."Host: "..HTTP_SERVER_HOSTNAME.."\r\n"
            .."Connection: keep-alive\r\n"
            .."Accept: */*\r\n\r\n")
    end)
end

您只需要定义所有大写变量(SERIAL_PRINT,HTTP_SERVER_IP,HTTP_SERVER_SCRIPTNAME和HTTP_SERVER_HOSTNAME)。

编辑:请注意,您的脚本名称不应包含您的主机名,在您的情况下它应该只是'Temperatur / PHP / insertTemp.php'

EDIT2:以下是我的配置变量样本

-- HTTP Server informations
HTTP_SERVER_IP = "192.168.0.107"
HTTP_SERVER_HOSTNAME = "subdomain.mydomain.com"
HTTP_SERVER_SCRIPTNAME = "mydir/sensor.php"

-- Serial output (True for debug messages, false otherwise)
SERIAL_PRINT = true

答案 1 :(得分:0)

是否有可能

 -- Start a simple http server
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive",function(conn,payload)
    print(payload)
    conn:send("hi! data:")
    FUNCTION_HERE()
  end)
  conn:on("sent",function(conn) conn:close() end)
end)

然后

ESP8266_IP="192.168.1.20"
ESP8266_NETMASK="255.255.255.0"
ESP8266_GATEWAY="192.168.1.1"
if ESP8266_IP ~= "" then
 wifi.sta.setip({ip=ESP8266_IP,netmask=ESP8266_NETMASK,gateway=ESP8266_GATEWAY})
end

并在wifi上使用中央(管理型节点)SoC /计算机:

$ curl $ESP8266_IP