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

Discussion in 'Embedded Systems and Microcontrollers' started by be80be, Sep 1, 2015.

  1. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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 (Text):
    1. conn:on("receive",function(conn,payload)
    2.     --next row is for debugging output only
    3.     print(payload)
    I think it's just being used as variable
    Code (Text):
    1. srv=net.createServer(net.TCP) srv:listen(80,function(conn)
    2.     conn:on("receive",function(conn,payload)
    3.     --next row is for debugging output only
    4.     print(payload)
    But where would I find information On how to write code like it.
     
  2. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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 (Text):
    1.     --wifi.setmode(wifi.STATION)
    2.     --wifi.sta.config("NameHere","PassWordHere")
    3.     outpin=3 -- Select right IO index !! Here is settings for GPIO2 (Lua build 20150318)
    4.  
    5.     srv=net.createServer(net.TCP) srv:listen(80,function(connek)
    6.     connek:on("receive",function(connek,payload)
    7.     --next row is for debugging output only
    8.     print(payload)
    9.  
    10.     function ctrlpower()
    11.     pwm.close(outpin)
    12.     gpio.mode(outpin,gpio.OUTPUT)
    13.     dopress=string.sub(payload,getinput[2]+1,#payload)
    14.     if dopress=="ON"  then gpio.write(outpin,gpio.LOW)return end
    15.     if dopress=="OFF" then gpio.write(outpin,gpio.HIGH)return end
    16.     if dopress=="Blink" then pwm.setup(outpin,2,512)pwm.start(outpin)return end
    17.     pwm.setup(outpin,1000,dopress*10)
    18.     pwm.start(outpin)
    19.     end
    20.     --parse position POST value from header
    21.     getinput={string.find(payload,"pwmi=")}
    22.     --If POST value exist, set LED power
    23.     if getinput[2]~=nil then ctrlpower()end
    24.  
    25.     connek:send('HTTP/1.1 200 OK\n\n')
    26.     connek:send('<!DOCTYPE HTML>\n')
    27.     connek:send('<html>\n')
    28.     connek:send('<head><meta  content="text/html; charset=utf-8">\n')
    29.     connek:send('<title>ESP8266 Wifi</title></head>\n')
    30.     connek:send('<body><h1>GPIO controls</h1>\n')
    31.     connek:send('<h3>This shows pwm and led switching !!!</h3>\n')
    32.     connek:send('<h3>Tested with every thing</h3>\n')
    33.     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')
    34.     connek:send('<form action="" method="POST">\n')
    35.     connek:send('<input type="submit" name="pwmi" value="ON">\n')
    36.     connek:send('<input type="submit" name="pwmi" value="10">\n')
    37.     connek:send('<input type="submit" name="pwmi" value="20">\n')
    38.     connek:send('<input type="submit" name="pwmi" value="30">\n')
    39.     connek:send('<input type="submit" name="pwmi" value="40">\n')
    40.     connek:send('<input type="submit" name="pwmi" value="50">\n')
    41.     connek:send('<input type="submit" name="pwmi" value="60">\n')
    42.     connek:send('<input type="submit" name="pwmi" value="70">\n')
    43.     connek:send('<input type="submit" name="pwmi" value="80">\n')
    44.     connek:send('<input type="submit" name="pwmi" value="90">\n')
    45.     connek:send('<input type="submit" name="pwmi" value="OFF"> % of power<br><br>\n')
    46.     connek:send('<input type="submit" name="pwmi" value="Blink"> Led blinker</form>\n')
    47.     connek:send('</body></html>\n')
    48.     connek:on("sent",function(connek) connek:close() end)
    49.     end)
    50. end)
    51.  
    52.  
    Cool turn things on with the ESP2866 the above code shows up as pic when run on the esp2866
    [​IMG]
     
    Last edited: Sep 1, 2015
  3. xtal

    Member

    Oct 21, 2008
    13
    0
    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
     
  4. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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 (Text):
    1. 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.
     
  5. xtal

    Member

    Oct 21, 2008
    13
    0
    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???
     
  6. xtal

    Member

    Oct 21, 2008
    13
    0
    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
     
  7. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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
    that should send updated data to the sever and it should reply back if there any change.
     
  8. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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: Sep 7, 2015
  9. xtal

    Member

    Oct 21, 2008
    13
    0
    Heres my code I'm trying to use -- it also 2 displays when totally wireless
    Code (Text):
    1.  
    2. collectgarbage()
    3. tmr.alarm(0, 1000, 1, function(QQ)  
    4.    print("\nTry Connecting:")
    5.    ip, nm, gw=wifi.sta.getip()
    6.   if ip ~= nil then        
    7.       print("\nIP Info: \nIP Address: ",ip,"\n Netmask: ",nm,"\n Gateway: ",gw)        
    8.       tmr.stop(0)
    9.    end
    10. end)
    11. collectgarbage()
    12. for xx=1,10000 do
    13.   tmr.delay(50)  
    14. end;                
    15. collectgarbage()                        
    16. led_pin = 1
    17. sw_pin = 2
    18. eye_opn=0
    19. adc_id = 0
    20. adc_value = 512
    21. k1=0.11
    22. p00='P'
    23. apc0='11111'
    24. ppm0='1770'
    25. cdeg0='25.1'
    26. fdeg0='77.1'
    27. apc1='22222'
    28. ppm1='1772'
    29. apc2='33333'
    30. ppm2='1773'
    31. kta0='0.01'
    32. lo0='12'
    33. hi0='20'
    34. prb='01.0'
    35. ph='7.0'
    36.  
    37. blink_open = "http://i.imgur.com/kzt3tO8.png"
    38. blink_close = "http://i.imgur.com/KS1dPa7.png"
    39. site_image = blink_open
    40. collectgarbage()
    41. uart.setup(0,9600,8,0,1)
    42. srv = net.createServer(net.TCP, 30)
    43. srv:listen(80,function(conn)
    44.     conn:on("receive", function(conn, payload)            
    45.         function esp_update()                  
    46.             mcu_do=string.sub(payload,postparse[2]+1,#payload)            
    47.             if mcu_do == "Update+LED" then print("Noda") end                  
    48.             if mcu_do == "Read+ADC" then
    49.                 adc_value = adc.read(adc_id)
    50.                 if adc_value > 1023 then
    51.                     adc_value = 1023
    52.                 elseif adc_value < 0 then
    53.                     adc_value = 0
    54.                 end
    55.                 print("ADC: ", adc_value)                    
    56.             end
    57.             if mcu_do == "Blink+my+EYEs" then
    58.                if eye_opn==0 then
    59.                   site_image = blink_close
    60.                   eye_opn=1
    61.                else
    62.                   site_image = blink_open
    63.                   eye_opn=0
    64.               end
    65.             end            
    66.             if mcu_do == "TEST+12345" then
    67.                 apc0 = apc0 + 100                            
    68.             end            
    69.         end
    70.        
    71.         postparse={string.find(payload,"mcu_do=")}
    72.         if postparse[2]~=nil then esp_update()end
    73.       --  conn:send('HTTP/1.1 200 OK\n\n')
    74.         conn:send('<!DOCTYPE HTML>\n')
    75.         conn:send('<html>\n')
    76.       --  conn:send('<head><meta  content="text/html; charset=utf-8">\n')
    77.       --  conn:send('<title>ESP8266 Blinker Thing</title></head>\n')
    78.         conn:send('<body><h1>ESP8266 Blinker Thing!</h1>\n')          
    79.       --   conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br><br>\n')
    80.         conn:send('Temperature__Sensor___DS18B20 <input style="text-align: center" type="text" value="'..p00..'" size="5" maxlength="5"><br>'
    81.                   ..'   Probe__K <input style="text-align: center" type="text" value="'..k1..'" size="5" maxlength="5">'
    82.                   ..'   Computed_K <input style="text-align: center" type="text" value="'..k1..'" size="5" maxlength="5"><br>'
    83.                   ..'   Pool__PH <input style="text-align: center" type="text" value="'..ph..'" size="5" maxlength="5">'
    84.                   ..'   PH___@25C <input style="text-align: center" type="text" value="'..ph..'" size="5" maxlength="5"><br>')  
    85.         conn:send('PulseCount@CurrentT <input style="text-align: center" type="text" value="'..apc0..'" size="5" maxlength="5">'
    86.                   ..'   ppm_currentT <input style="text-align: center" type="text" value="'..ppm0..'" size="5" maxlength="5">'
    87.                   ..'   TempC <input style="text-align: center" type="text" value="'..cdeg0..'" size="5" maxlength="5"><br>')                                  
    88.         conn:send('PulseCountAdj1@25C <input style="text-align: center" type="text" value="'..apc1..'" size="5" maxlength="5">'
    89.                   ..'   ppm@25Adj1 <input style="text-align: center" type="text" value="'..ppm1..'" size="5" maxlength="5">'
    90.                   ..'   TempF <input style="text-align: center" type="text" value="'..fdeg0..'" size="5" maxlength="5"><br>')                        
    91.         conn:send('PulseCountAdj2@25C <input style="text-align: center" type="text" value="'..apc2..'" size="5" maxlength="5">'
    92.                   ..'   ppm@25Adj2 <input style="text-align: center" type="text" value="'..ppm2..'" size="5" maxlength="5"><br>')                
    93.         conn:send('<p>ADC Value: '..adc_value..'</p><br>')
    94.         conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br><br>\n')      
    95.         conn:send('<form action="" method="POST">\n')
    96.         conn:send('<input type="submit" name="mcu_do" value="Blink my EYEs">\n')
    97.         conn:send('<input type="submit" name="mcu_do" value="Read ADC">\n')
    98.         conn:send('<input type="submit" name="mcu_do" value="Update LED">\n')        
    99.         conn:send('<input type="submit" name="mcu_do" value="TEST 12345">\n')        
    100.         conn:send('</form>\n')        
    101.         conn:send('</body></html>\n')
    102.         conn:on("sent", function(conn) conn:close() end)
    103.     end)
    104.        
    105. end)
    106. function round2(num, idp)
    107.   return tonumber(string.format("%." .. (idp or 0) .. "f", num))
    108. end
    109. function si(sIN)
    110.  buffer=sIN
    111.  print("buf--",buffer)
    112.  if string.find(sIN,"co") then
    113.     cx=(string.find(sIN,"co"))+2
    114.     cdeg0 = string.sub(sIN,cx,6)   --cx startof data  6 end of data
    115.     fdeg0 = round2((cdeg0*1.8) +32,1)
    116.     print("cd-",cx,cdeg0,fdeg0)
    117.  end
    118.  if string.find(sIN,"ao") then
    119.     cx=(string.find(sIN,"ao"))+2
    120.     apc0 = string.sub(sIN,cx,7)
    121.     apc1=round2(apc0/(((cdeg0-25)*.021)+1),1)
    122.     apc2=round2((apc1*46.5)/(cdeg0+21.5),1)
    123.     print("apc",cx,apc0,apc1,apc2)
    124.  end
    125.  if string.find(sIN,"bo") then
    126.    cx=(string.find(sIN,"bo"))+2              
    127.    ppm0=string.sub(sIN,cx,6)
    128.    print("ppm0",cx,ppm0)
    129.    ppm1=round2(ppm0/(((cdeg0-25)*.021)+1),1)
    130.    ppm2=round2((ppm0*46.5)/(cdeg0+21.5),1 )
    131.    print("ppm",ppm1,ppm2)
    132.  end  
    133.     tmr.alarm(0,500,0,function()  
    134.                         if eye_opn==0 then
    135.                            site_image = blink_close
    136.                            eye_opn=1
    137.                         else
    138.                            site_image = blink_open
    139.                            eye_opn=0
    140.                          end
    141.               end)
    142. end                      
    143. --apc0=string.sub(sIN,string.find(sIN,"pc0=")+4,string.find(sIN,"pm0=")-2) -- 1st
    144.  
     
  10. xtal

    Member

    Oct 21, 2008
    13
    0
    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...
     
  11. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    Try this
    Code (Text):
    1. --  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 “;”
     
  12. xtal

    Member

    Oct 21, 2008
    13
    0
    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
     
  13. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    Get rid of the collectgarbage()
    It's filling up more then likely
     
  14. xtal

    Member

    Oct 21, 2008
    13
    0
    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 (Text):
    1.  
    2. conn:send('HTTP/1.1 200 OK\n\n')
    3.         conn:send('<!DOCTYPE HTML>\n')
    4.         conn:send('<html>\n')
    5.         conn:send('<head><meta  content="text/html charset=utf-8">')
    6.         conn:send('<title>ESP8266 Blinker Thing</title></head>')
    7.         conn:send('<body><h1>ESP8266 Blinker Thing!</h1>')              
    8.         conn:send('<p>ADC Value: '..adc_value..'</p><br>')
    9.         conn:send('<IMG SRC="'..site_image..'" WIDTH="98" HEIGHT="49" BORDER="1"><br>')    
    10.         conn:send('<form action="" method="POST">')
    11.         conn:send('<input type="submit" name="mcu_do" value="Blink my EYEs">')
    12.         conn:send('<input type="submit" name="mcu_do" value="Read ADC">')
    13.         conn:send('<input type="submit" name="mcu_do" value="Update LED">')      
    14.         conn:send('<input type="submit" name="mcu_do" value="TEST 12345">')      
    15.         conn:send('</form>')      
    16.         conn:send('</body></html>')
    17.         conn:on("sent", function(conn) conn:close() end)
    18.  
     
  15. xtal

    Member

    Oct 21, 2008
    13
    0
    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 ?
     
  16. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    That's a network adapter you plug into the usb
     
  17. xtal

    Member

    Oct 21, 2008
    13
    0
    its a usb wireless adapter 2G/5G ,, finding good drivers for xp/w7 is a problem..
     
  18. be80be

    Thread Starter Senior Member

    Jul 5, 2008
    431
    57
    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.
     
  19. xtal

    Member

    Oct 21, 2008
    13
    0
    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.....
     
  20. xtal

    Member

    Oct 21, 2008
    13
    0
    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.
     
Loading...