Reputation: 80
I'm trying to make a Temperature station which sends data from an ESP 8266 to a webserver, the problem is that I'm not very familiar with the ESP 8266 and Lua (I took the code from an example).
conn:send("GET /http://mywebsite.net/Temperatur/PHP/insertTemp.php?temp="..temp.."&humi="..humi.." HTTP/1.1\r\n")
is this line correct?
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)
Edit: 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);
}
?>
I inserted for the constants following data, but it's not working at all. The PHP file is correct.
HTTP_SERVER_SCRIPTNAME = "Temperatur/PHP/insertTemp.php"
HTTP_SERVER_IP = "144.76.167.69"
Edit 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
Upvotes: 3
Views: 3444
Reputation: 863
Your question is a little confusing but I think I understand your question (so I'll try). Here is a function i use to do a GET to a HTTP server :
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
You just have to define all upper case variables (SERIAL_PRINT, HTTP_SERVER_IP, HTTP_SERVER_SCRIPTNAME and HTTP_SERVER_HOSTNAME).
EDIT : Note that your script name should not contain your host name, it should only be 'Temperatur/PHP/insertTemp.php' in your case
EDIT2 : Here is sample of my configuration variables
-- 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
Upvotes: 3
Reputation: 355
Would it be possible to
-- 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)
and then
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
and use a central (admin-type node) SoC / computer on wifi to:
$ curl $ESP8266_IP
Upvotes: 0