3

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).

  • So the question is which IP address do I have to paste? The ip from my router or the ip from the ESP?
  • 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"

  • HTTP_SERVER_HOSTNAME = "ribisl.bplaced.net"

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
Ribisl
  • 80
  • 1
  • 6

2 Answers2

3

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
seblucas
  • 825
  • 1
  • 7
  • 17
  • What is the difference between HTTP_SERVER_HOSTNAME and HTTP_SERVER_IP, and what do I have to insert in SERIAL_PRINT – Ribisl Nov 26 '16 at 21:02
  • I've added some variable sample. HTTP_SERVER_IP could be a domain name. I use the IP address for performance reason (no DNS query needed) as my nodes are running on battery. – seblucas Nov 27 '16 at 08:19
  • it's not working, but I updated the Question, maybe you can help me with this data little more. – Ribisl Nov 27 '16 at 12:20
  • I'm sure that my code works (used by 4 nodes around my home), did you enable serial_print / debug code to see where it fails, I can't help you without more clues. – seblucas Nov 27 '16 at 19:48
  • Yes I enabled serial_print, but I don't get any output except this: dofile("mysql.lua") > – Ribisl Nov 27 '16 at 20:04
  • Thanks for your second edit, but your code won't work. Nodemcu is event base so you have to wait for the wifi to be up before trying to do a GET : see my answer here : http://stackoverflow.com/questions/33807693/esp8266-send-and-receive-sockets/33813469#33813469 – seblucas Nov 28 '16 at 07:30
  • 1
    Sorry that would be this link http://stackoverflow.com/questions/33288026/the-wifi-sta-module-connects-if-a-loop-is-running/33706338#33706338 – seblucas Nov 28 '16 at 07:38
  • After I changed the filename, ssid and password and started the connect.lua I get this error code (line 28 is the second last) connect.lua:28: attempt to call field 'eventMonReg' (a nil value) > PANIC: unprotected error in call to Lua API (connect.lua:21: attempt to call field 'eventMonStop' (a nil value)) and some other error chars – Ribisl Nov 28 '16 at 18:34
  • but if I manually start the mysql.lua now I can upload the data to the webserver – Ribisl Nov 28 '16 at 18:59
  • My last guess is that your nodemcu build is too old ... besides that I really don't know – seblucas Nov 29 '16 at 14:20
  • I have chosen another connect code now, but so much thanks to you seblucas, I don't think I would have done it nearly so fast without you – Ribisl Nov 29 '16 at 18:00
0

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
Zombro
  • 311
  • 1
  • 9