As the title says... Chat on...

User avatar
By Sille
#57887 Hey,

I´m looking for some advice hopefully someone can guide me. In the last weeks I tried a lot with my ESP to get some things work but at some point things got stucked. My ESP is connected to a sensor but it´s a bit different from the DHT-22 which people often use to figure out how LUA or this module works. This sensor clocks out 1024 values (floats) and I like to store them into a string or file and send them to a local webserver and then to database. I installed the Apache server and everything works fine as long I´m not using POST or I´m trying to send the whole data string with 1024 float values. I also noticed that i can´t loop requests more than five times and that the ESP is running out of spiffs very fast (I do things basically wrong). First I like to know if there is a appropriate LUA-module or a philosophy for this purpose. I need an idea or advice for further researches. I´m not looking for examples or a code primarly but for guidance.

best regards

Sille
User avatar
By Sille
#57937 Maybe i should spedify my request a bit more. I tried some things that basically worked but I have to get more information about sending long data strings with the esp. I know there a lot of examples around here but I don´t want to send one or two values with a delay about 10 minutes and I noticed that I have to request the server maybe more than once. If I loop a GET request with the http-module my ESP runs out of spiffs - error. I cannot store the whole value into one string so I would like to put the data into file and want to send one line after another but I do not know how. I tried to use the net-module but I was not able to figure out how to transfer a whole file with multiple send-commands. Maybe I can modify my code in that way that I´m able to transfer the whole sensor-data in chunks. For example I tested it with an array (a) and i attached 80 float values if I attache more values I get some errors like "string overflow" or something. If I splited the array into more parts and attache them additionaly to "x_axis=..." I get errors because the payload size is too big. I tried to loop "conn:send..." without success. Can somebody help me out with some information what I´m doing wrong? Help would be appreciated, thx.

Code: Select allarray = {}
b = 80

for i = 0, b do
array[i] = 70.1111
end

a = table.concat(array, ",")

conn = nil
conn = net.createConnection(net.TCP, 0)

-- show the retrieved web page

conn:on("receive", function(conn, payload)
                       success = true
                       --print(payload)
                       end)

-- once connected, request page (send parameters to a php script)

conn:on("connection", function(conn, payload)
                       print('\nConnected')
                       
                       conn:send("GET /Test/FWRITE.php?"       
                        .."x_axis="..a
                        .."&index="..b
                        .." HTTP/1.1\r\n"
                        .."Host: "MY-IP"\r\n"
                        .."Connection: close\r\n"
                        .."Accept: */*\r\n"
                        .."User-Agent: Mozilla/4.0 "
                        .."(compatible; esp8266 Lua; "
                        .."Windows NT 5.1)\r\n"
                        .."\r\n")
                       end)
conn:close()

-- when disconnected, let it be known
conn:on("disconnection", function(conn, payload) print('\nDisconnected') end)
                                             
conn:connect(80,'MY-IP')
tmr.delay(10)


User avatar
By Sille
#58376 In case somebody is interested in my progress or giving me some further information I managed some things in the meanwhile. I had a look at some examples with the ESP as a webserver but I would like to use it as a client but anyway. I recognized that the request header information "keep-alive" is keeping alive the connection :) what I was basically doing wrong is that I did not understood what "event driven" really means. I implemented a additional event driven function into the conn:on("connection"...) function called "conn:on("sent"...). The result was that I can now send the 1024 float numbers in multiple chunks and close the connection after a few cycles when the string is sent.

Now I would like to establish a new connection directly after I closed the old one but if I put a conn:connect(...) into the event conn:on("disconnect"...) the esp reboots. I do not want to have a additional or multpile connections I only want to have only one. Hopefully somebody can tell help me out with this because I do not understand what is the problem when I want to reconnect after a disconnect. Maybe someone can make a suggestion about other solutions websockets or whatesver. Thanks a lot

bets regards

sille

Code: Select allarray = {}
b = 100

function send_string(value, index)                       
    conn:send("GET /Test/FWRITE.php?"        --ändern in measure.php
    .."x_axis="..value
    .."&y_axis="..index
    .." HTTP/1.1\r\n"
    .."Host: 192.168.4.\r\n"
    --.."Keep-Alive: timeout=5 max=2\r\n"
    .."Connection: keep-alive\r\n"
    --.."Connection: close\r\n"
    .."Accept: */*\r\n"
    .."User-Agent: Mozilla/4.0 "
    .."(compatible; esp8266 Lua; "
    .."Windows NT 5.1)\r\n"
    .."\r\n")
end


conn = nil

conn = net.createConnection(net.TCP, 0)

conn:on("receive", function(conn, payload) success = true print(payload) end)

conn:on("connection", function(conn, payload) print('\nConnected')

    send_string("new_data_line", 0)
   
    j = 0

        conn:on("sent", function(conn, payload) print ("\nsent")


            for i = 0, b do
            array[i] = 70.1111
            end

            a = table.concat(array, ",")
            print(node.heap())
           
            send_string(a, b)
            j = j + 1
            if j > 5 then conn:close() end

        end)
end)                   

conn:on("disconnection", function(conn, payload) print('\nDisconnected')
conn:connect(80, "my_IP")
end)
                                             
conn:connect(80,'my_IP')
 
User avatar
By marcelstoer
#58397 To interact with a web server over HTTP I suggest you use the NodeMCU HTTP module. Makes things a lot easier.
Last edited by marcelstoer on Sun Nov 20, 2016 7:34 am, edited 1 time in total.