Introduction: ESP8622 Webinterface for Local Variables

This manual will show you how you can edit ( get /set) local variables and local values , from a browser.

benefit : no need to use uart and change the lua-code to change parameters , like eg : ssid pasword , or whatever you want to edit , in real time..

Tools needed.

Hardware :

  • ESP8622-01
  • usb to TTL adaptor

Software :

Testmovie :

here : youtube

Step 1: Load De Lua Code Into Your Esp

  1. download the code: here :
  2. open ESPlorer
  3. open the code
  4. edit your SSID and SSID pasword to yours
  5. press :"save to esp"
  6. hard reboot , restart your ESPmodule

Step 2: Start Up ESP and Your Browser

In the ESPLORER window you get the DHCP IP address from the ESP.

use this ip into your browser.

press GET , to see the initial values from the ESP to the browser.

press SET , to change the local values from the browser to the ESP

Step 3: Demo Video

code is written to use with firefox

i tested also in chrome , but chrome did 2times a get request. , so tweak the code to use with chrome.

or download firefox here.

Step 4: How the Code Works...

the local value is sended to the html-page , parsed out , and stored in the local.

also

the local is send in the ESP webserver , via a form-tag

local var1 = "initvar1"

the value is set into a input textform :

input type=\"text\" name=\"p1\" value=\"".. var1.."\"

parsed out

positiep1 = string.find(req_data, "p1")

valuep1 = (string.sub(req_data, positiep1+3, positiep2-2))

and saved to the ESP memory as local variable

var1 = valuep1

Step 5: Lua Code

-- v12345vtm youtube https://youtu.be/MzH8zR34x0Y
--https://www.instructables.com/editInstructable/publish/EDRHQW8IG415EAG

-----uart mode uart.setup(0,9600,8,0,1)

-- Your Wifi connection data local SSID = "yourSSID" local SSID_PASSWORD = "yourSSIDpassword" local rtctijd = "15:50"

local var1 = "initvar1" local var2 = "initvar2" local var3 = "initvar3"

local function connect (conn, data) webcode=" "; webcode = webcode .. " " webcode = webcode .. " " webcode = webcode .. "

" webcode = webcode .. " " webcode = webcode .. "p1,ssid:
" webcode = webcode .. "p2,passw:
" webcode = webcode .. "p3,time:
" webcode = webcode .. "

" webcode = webcode .. "

" webcode = webcode .. "

" conn:on ("receive",function (cn, req_data) --print("wat is dat hier") print (req_data) -- print("staat er hier iet boven")

if string.match(req_data, "p1") then

positiep1 = string.find(req_data, "p1") positiep2 = string.find(req_data, "p2") positiep3 = string.find(req_data, "p3") positieEnd = string.find(req_data, "HTTP/")

valuep1 = (string.sub(req_data, positiep1+3, positiep2-2)) valuep2 = (string.sub(req_data, positiep2+3 , positiep3-2)) valuep3 = (string.sub(req_data, positiep3+3 , positieEnd-2)) uart.write (0, valuep1 , "\r\n") uart.write (0, valuep2 , "\r\n") uart.write (0, valuep3 , "\r\n") -- = time-15-10-23-12-27-35-4

var1 = valuep1 var2 = valuep2 var3 = valuep3 --webfomtijd opslaan else -- print ("geen data in form ingevuld gewest of de browser vroeg zijn favicon.ico daarnet ook nog") end cn:send (webcode) -- Close the connection for the request cn:close ( ) end) end

function wait_for_wifi_conn ( ) tmr.alarm (1, 1000, 1, function ( ) if wifi.sta.getip ( ) == nil then print ("Waiting for Wifi connection") else tmr.stop (1) print ("ESP8266 mode is: " .. wifi.getmode ( )) print ("The module MAC address is: " .. wifi.ap.getmac ( )) print ("Config done, IP is " .. wifi.sta.getip ( )) ---gpio.write(led1, gpio.HIGH) end end) end

-- Configure the ESP as a station (client) wifi.setmode (wifi.STATION) wifi.sta.config (SSID, SSID_PASSWORD) wifi.sta.autoconnect (1)

-- Hang out until we get a wifi connection before the httpd server is started. wait_for_wifi_conn ( )

-- Create the httpd server svr = net.createServer (net.TCP, 30)

-- Server listening on port 80, call connect function if a request is received svr:listen (80, connect)

print ("uart.setup(0,9600,8,0,1) finished") print ("waiting for serial data....")

Comments

author
breagan22 (author)2016-05-01

Good job. The video is is great and shows how all this works. Nice!

author
DIY Hacks and How Tos (author)2015-10-24

Very interesting project. Thanks for sharing.

author

your welcome , this is my first instrutables , if you need more information , just ask.