conn:send conn:on trying to find info on how to use.

Thread Starter

be80be

Joined Jul 5, 2008
2,072
I been playing with Lua script and I run into conn:send and conn on . But I can't find and thing about it
How it is used. I read up On Lua and and It look like Lua naming of a function.
Code:
conn:on("receive",function(conn,payload)
    --next row is for debugging output only
    print(payload)
I think it's just being used as variable
Code:
srv=net.createServer(net.TCP) srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload)
    --next row is for debugging output only
    print(payload)
But where would I find information On how to write code like it.
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
Well I played with some code figued it out on my own.
The code makes a web server using the esp2866 I changed the code to learn about Lua code on the esp2866 names of functions and global and local name in Lua
Code:
    --wifi.setmode(wifi.STATION)
    --wifi.sta.config("NameHere","PassWordHere")
    outpin=3 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20150318)

    srv=net.createServer(net.TCP) srv:listen(80,function(connek)
    connek:on("receive",function(connek,payload)
    --next row is for debugging output only
    print(payload)

    function ctrlpower()
    pwm.close(outpin)
    gpio.mode(outpin,gpio.OUTPUT)
    dopress=string.sub(payload,getinput[2]+1,#payload)
    if dopress=="ON"  then gpio.write(outpin,gpio.LOW)return end
    if dopress=="OFF" then gpio.write(outpin,gpio.HIGH)return end
    if dopress=="Blink" then pwm.setup(outpin,2,512)pwm.start(outpin)return end
    pwm.setup(outpin,1000,dopress*10)
    pwm.start(outpin)
    end
    --parse position POST value from header
    getinput={string.find(payload,"pwmi=")}
    --If POST value exist, set LED power
    if getinput[2]~=nil then ctrlpower()end

    connek:send('HTTP/1.1 200 OK\n\n')
    connek:send('<!DOCTYPE HTML>\n')
    connek:send('<html>\n')
    connek:send('<head><meta  content="text/html; charset=utf-8">\n')
    connek:send('<title>ESP8266 Wifi</title></head>\n')
    connek:send('<body><h1>GPIO controls</h1>\n')
    connek:send('<h3>This shows pwm and led switching !!!</h3>\n')
    connek:send('<h3>Tested with every thing</h3>\n')
    connek:send('<IMG SRC="http://sites.google.com/site/be80be/be80be-pic-ing-howto-s/pic/led.jpg" WIDTH="150" HEIGHT="101" BORDER="1"><br><br>\n')
    connek:send('<form action="" method="POST">\n')
    connek:send('<input type="submit" name="pwmi" value="ON">\n')
    connek:send('<input type="submit" name="pwmi" value="10">\n')
    connek:send('<input type="submit" name="pwmi" value="20">\n')
    connek:send('<input type="submit" name="pwmi" value="30">\n')
    connek:send('<input type="submit" name="pwmi" value="40">\n')
    connek:send('<input type="submit" name="pwmi" value="50">\n')
    connek:send('<input type="submit" name="pwmi" value="60">\n')
    connek:send('<input type="submit" name="pwmi" value="70">\n')
    connek:send('<input type="submit" name="pwmi" value="80">\n')
    connek:send('<input type="submit" name="pwmi" value="90">\n')
    connek:send('<input type="submit" name="pwmi" value="OFF"> % of power<br><br>\n')
    connek:send('<input type="submit" name="pwmi" value="Blink"> Led blinker</form>\n')
    connek:send('</body></html>\n')
    connek:on("sent",function(connek) connek:close() end)
    end)
end)
Cool turn things on with the ESP2866 the above code shows up as pic when run on the esp2866
 
Last edited:

xtal

Joined Oct 21, 2008
13
this appears to work OK using wired connection to router with wireless to ESP8266
When I use wireless to router and wireless to ESP8266 , I initially get 1 page display ,,when I click button I get 2 displays
I have the same problem [using similiar code] with 2 other simple pgms

mike w

msw101144@charter.net
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
The whole documentation for using LUA with the esp 2866 is not good at all. You get a sample that half works with no comments. I spent a week digging and never found much.
Code:
connek:on("sent",function(connek)
the "sent" is a variable name but it's a built in function there's no doc on how there used.
 

xtal

Joined Oct 21, 2008
13
be80be
do you get double display when using wireless to router and wireless to ESP ??

I also used Blinker Thing from allaboutcircuits with same result..

Also I can send data from serial port to a VAR that the web page will display,
but I must manually click a button to get the VAR to display the new value ..
function si(sIN) ~~~~~load var~~~~end
tried uart:eek:n + code, but restart caused looping!!!

I can not figure out how to get the page to update automatically, any ideas???
 

xtal

Joined Oct 21, 2008
13
there is a new ESP-basic starting esp8266basic.com
it may get interesting
I tried it , it worked 1st time , then had problems, havn't tried since,, little doc's
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
double display
Is the web browser I only had the problem with apple stuff safari
That's fixed in the web page html code so is the refresh that's this
con.on("sent",function(con)
that should send updated data to the sever and it should reply back if there any change.
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
I didn't put a smiley in there .on This the problem I have the stuff they call LUA is not Lua it and it don't add up to how I am learning LUA pure LUA so when i write something I LUA and it don't work I hop over to esp2866 and look at some of the Lua scripts and I'm like what the heck is that. But I read today that the esp2866 can handle pure LUA code and to write it that way is better because you can find how to word it.

I installed LUA 5.4.1 on my computer next time i use a vm but you can learn to write LUA code and test it because at the rate I was going with the esp2866 it would be trash because it can only have so many flashes till the Program memory is trash
 
Last edited:

xtal

Joined Oct 21, 2008
13
Heres my code I'm trying to use -- it also 2 displays when totally wireless
Code:
collectgarbage()
tmr.alarm(0, 1000, 1, function(QQ)  
   print("\nTry Connecting:")
   ip, nm, gw=wifi.sta.getip()
  if ip ~= nil then        
      print("\nIP Info: \nIP Address: ",ip,"\n Netmask: ",nm,"\n Gateway: ",gw)        
      tmr.stop(0)
   end 
end)
collectgarbage() 
for xx=1,10000 do
  tmr.delay(50)  
end;                
collectgarbage()                        
led_pin = 1
sw_pin = 2
eye_opn=0
adc_id = 0 
adc_value = 512
k1=0.11
p00='P'
apc0='11111'
ppm0='1770'
cdeg0='25.1'
fdeg0='77.1'
apc1='22222' 
ppm1='1772'
apc2='33333'
ppm2='1773'
kta0='0.01'
lo0='12'
hi0='20'
prb='01.0'
ph='7.0'

blink_open = "http://i.imgur.com/kzt3tO8.png"
blink_close = "http://i.imgur.com/KS1dPa7.png"
site_image = blink_open
collectgarbage()
uart.setup(0,9600,8,0,1) 
srv = net.createServer(net.TCP, 30)
srv:listen(80,function(conn)
    conn:on("receive", function(conn, payload)             
        function esp_update()                  
            mcu_do=string.sub(payload,postparse[2]+1,#payload)            
            if mcu_do == "Update+LED" then print("Noda") end                  
            if mcu_do == "Read+ADC" then
                adc_value = adc.read(adc_id)
                if adc_value > 1023 then
                    adc_value = 1023
                elseif adc_value < 0 then
                    adc_value = 0
                end
                print("ADC: ", adc_value)                    
            end
            if mcu_do == "Blink+my+EYEs" then
               if eye_opn==0 then 
                  site_image = blink_close
                  eye_opn=1
               else
                  site_image = blink_open
                  eye_opn=0
              end
            end            
            if mcu_do == "TEST+12345" then
                apc0 = apc0 + 100                            
            end            
        end
       
        postparse={string.find(payload,"mcu_do=")}
        if postparse[2]~=nil then esp_update()end
      --  conn:send('HTTP/1.1 200 OK\n\n')
        conn:send('<!DOCTYPE HTML>\n')
        conn:send('<html>\n')
      --  conn:send('<head><meta  content="text/html; charset=utf-8">\n')
      --  conn:send('<title>ESP8266 Blinker Thing</title></head>\n')
        conn:send('<body><h1>ESP8266 Blinker Thing!</h1>\n')          
      --   conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br><br>\n')
        conn:send('Temperature__Sensor___DS18B20 <input style="text-align: center" type="text" value="'..p00..'" size="5" maxlength="5"><br>' 
                  ..'   Probe__K <input style="text-align: center" type="text" value="'..k1..'" size="5" maxlength="5">' 
                  ..'   Computed_K <input style="text-align: center" type="text" value="'..k1..'" size="5" maxlength="5"><br>'
                  ..'   Pool__PH <input style="text-align: center" type="text" value="'..ph..'" size="5" maxlength="5">' 
                  ..'   PH___@25C <input style="text-align: center" type="text" value="'..ph..'" size="5" maxlength="5"><br>')  
        conn:send('PulseCount@CurrentT <input style="text-align: center" type="text" value="'..apc0..'" size="5" maxlength="5">'
                  ..'   ppm_currentT <input style="text-align: center" type="text" value="'..ppm0..'" size="5" maxlength="5">'
                  ..'   TempC <input style="text-align: center" type="text" value="'..cdeg0..'" size="5" maxlength="5"><br>')                                  
        conn:send('PulseCountAdj1@25C <input style="text-align: center" type="text" value="'..apc1..'" size="5" maxlength="5">'
                  ..'   ppm@25Adj1 <input style="text-align: center" type="text" value="'..ppm1..'" size="5" maxlength="5">'
                  ..'   TempF <input style="text-align: center" type="text" value="'..fdeg0..'" size="5" maxlength="5"><br>')                        
        conn:send('PulseCountAdj2@25C <input style="text-align: center" type="text" value="'..apc2..'" size="5" maxlength="5">'
                  ..'   ppm@25Adj2 <input style="text-align: center" type="text" value="'..ppm2..'" size="5" maxlength="5"><br>')                
        conn:send('<p>ADC Value: '..adc_value..'</p><br>')
        conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br><br>\n')      
        conn:send('<form action="" method="POST">\n')
        conn:send('<input type="submit" name="mcu_do" value="Blink my EYEs">\n')
        conn:send('<input type="submit" name="mcu_do" value="Read ADC">\n')
        conn:send('<input type="submit" name="mcu_do" value="Update LED">\n')        
        conn:send('<input type="submit" name="mcu_do" value="TEST 12345">\n')        
        conn:send('</form>\n')        
        conn:send('</body></html>\n')
        conn:on("sent", function(conn) conn:close() end)
    end) 
       
end)
function round2(num, idp)
  return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end 
function si(sIN)
 buffer=sIN
 print("buf--",buffer) 
 if string.find(sIN,"co") then
    cx=(string.find(sIN,"co"))+2
    cdeg0 = string.sub(sIN,cx,6)   --cx startof data  6 end of data
    fdeg0 = round2((cdeg0*1.8) +32,1) 
    print("cd-",cx,cdeg0,fdeg0)
 end
 if string.find(sIN,"ao") then
    cx=(string.find(sIN,"ao"))+2 
    apc0 = string.sub(sIN,cx,7)
    apc1=round2(apc0/(((cdeg0-25)*.021)+1),1) 
    apc2=round2((apc1*46.5)/(cdeg0+21.5),1) 
    print("apc",cx,apc0,apc1,apc2)
 end
 if string.find(sIN,"bo") then
   cx=(string.find(sIN,"bo"))+2              
   ppm0=string.sub(sIN,cx,6)
   print("ppm0",cx,ppm0)
   ppm1=round2(ppm0/(((cdeg0-25)*.021)+1),1)
   ppm2=round2((ppm0*46.5)/(cdeg0+21.5),1 )
   print("ppm",ppm1,ppm2)
 end  
    tmr.alarm(0,500,0,function()  
                        if eye_opn==0 then 
                           site_image = blink_close
                           eye_opn=1
                        else
                           site_image = blink_open
                           eye_opn=0
                         end
              end)
end                      
--apc0=string.sub(sIN,string.find(sIN,"pc0=")+4,string.find(sIN,"pm0=")-2) -- 1st
 

xtal

Joined Oct 21, 2008
13
using chrome will try others ...

will try the con.on("sent",function(con)
if I can figure it out how to implement into my code...
I've been playing about 2 weeks now .....

have you checked nodeLUA, I think nodeMCU may be an offshoot...
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
Try this
Code:
--  conn:send('<head><meta  content="text/html; charset=utf-8">\n')
the semi-colon is the problem have to debug it with chrome some where after that semi-colon there a extra “;”
 

xtal

Joined Oct 21, 2008
13
I've discovered another problem , I can onlt click the buttons about 25 times then out of memory!!!!

Will start looking into AT+ stuff, just saw server code a couple of days ago, but unable to locate it now...

I will uncomment the line and try some more --- thx
 

xtal

Joined Oct 21, 2008
13
changed the html code to this -- still get double display when totally wireless-- will keep plugging on for a while
I get double with IE, Chrome, Opera

Code:
conn:send('HTTP/1.1 200 OK\n\n')
        conn:send('<!DOCTYPE HTML>\n')
        conn:send('<html>\n')
        conn:send('<head><meta  content="text/html charset=utf-8">')
        conn:send('<title>ESP8266 Blinker Thing</title></head>')
        conn:send('<body><h1>ESP8266 Blinker Thing!</h1>')               
        conn:send('<p>ADC Value: '..adc_value..'</p><br>')
        conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br>')     
        conn:send('<form action="" method="POST">')
        conn:send('<input type="submit" name="mcu_do" value="Blink my EYEs">')
        conn:send('<input type="submit" name="mcu_do" value="Read ADC">')
        conn:send('<input type="submit" name="mcu_do" value="Update LED">')       
        conn:send('<input type="submit" name="mcu_do" value="TEST 12345">')       
        conn:send('</form>')       
        conn:send('</body></html>')
        conn:on("sent", function(conn) conn:close() end)
 

xtal

Joined Oct 21, 2008
13
I tried my wifes laptop and it displays AOK
3 DT's fail with WUSB600N
possible fix for double display = new driver for WUSB600N ???
will try finding a new driver ?
 

Thread Starter

be80be

Joined Jul 5, 2008
2,072
If I new what model I'd try to dig you one up. On a side note have you tried arduino on the esp2866.
I kind of like Lua but the lack of documentation on some of the functions they made is pain and there forum is a waste of time. I ordered 4 of the esp2866 -E the next one coming will be using the esp as wifi to serial and some kind of 8 bit uC for I/O. Maybe the best way to go a nice big pic and the esp2866 sending data out the esp is fast at sending and receiving data.
 

xtal

Joined Oct 21, 2008
13
I found a driver for windows 7 rt2870 v3.2.9.0 and it appears to work ok... I only show 1 screen...
I went back to the XP machine discovered that I get double screen wired and wireless ....
using chrome and IE , I'll let that sleeping dog alone .....my w2000 laptop displays ok with opera

any ideas how to ? trigger event to update web page from serial input
ie ao12345 on serial input updates var with 12345 ,but web page does not display
the new value until I click a button.....
 

xtal

Joined Oct 21, 2008
13
I sort of like LUA, but would like to understand the function stuff better, and would like
better monitor async IO, not enough memory is the main problem now [ I assume ram]
and yes the doc's are not that great- examples with no comments..
I'm a great believer that 1 picture is worth 1000 words....
I have ordered some ESP-12E [china express 15-49 days]


I have seen some serial bridge code available , but have not played with it.
 
Top