CRTC Electronics

Click here to edit subtitle

Esp Lua Sample Code

timer to blink LEDs

print("Useing tmr.alarm to blink led")

lightOn=false
ledPin=2

gpio.mode(ledPin,gpio.OUTPUT)
tmr.alarm(1,1000,1,function()  --  timer id (0-6), interval_ms, mode timer: tmr.ALARM_SINGLE or tmr.ALARM_SEMI or tmr.ALARM_AUTO, add a funtion() 
    if lightOn==false then
        lightOn = true
        gpio.write(ledPin,1)
   else
        lightOn=false
        gpio.write(ledPin,0)
   end
end) 

Read a pin

-- READ a pin

    while 1 do
        if gpio.read(inPin)==1 then
        gpio.write(outPin, 0)
        else 
        gpio.write(outPin, 1)
        end
        tmr.delay(3000)
    end


Random

--Random

while 1 do
gpio.write(0, 1)
tmr.delay(math.random(5000,500000))
gpio.write(0, 0)
tmr.delay(math.random(5000,500000))
end

Server on internet router needed

-- ESP8266 as a Server

wifi.setmode(wifi.STATION)
wifi.sta.config("BrownHouse","")--"CJSD_Guest", "")
print(wifi.sta.getip())

-- a simple http server
srv = net.createServer(net.TCP)
srv:listen(80, function(conn)
    conn:on("receive", function(sck, payload)
        print(payload)
        sck:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1> Hello, NodeMCU.</h1>")
    end)
    conn:on("sent", function(sck) sck:close() end)
end)

Asseses Point

--*** Soft AP  *************    
     print("Ready to start soft ap")
     cfg={}
     cfg.ssid="ESP8266_HI";
     cfg.pwd="12345678"
     wifi.ap.config(cfg)

     --You can mannuley set IP
     --cfg={}
     --cfg.ip="192.168.1.1";
     --cfg.netmask="255.255.255.0";
     --cfg.gateway="192.168.1.1";
     --wifi.ap.setip(cfg);
     wifi.setmode(wifi.SOFTAP)
     print("Soft AP started")
     print("SSID: ".. cfg.ssid)
     print("Password: ".. cfg.pwd)
     print("Heep:(bytes)"..node.heap());
     print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());

Soft Ap

--*** Soft AP  *************    
     print("Ready to start soft ap")
     cfg={}
     cfg.ssid="ESP8266_HI";
     cfg.pwd="12345678"
     wifi.ap.config(cfg)

     --You can mannuley set IP
     --cfg={}
     --cfg.ip="192.168.1.1";
     --cfg.netmask="255.255.255.0";
     --cfg.gateway="192.168.1.1";
     --wifi.ap.setip(cfg);
     wifi.setmode(wifi.SOFTAP)
     print("Soft AP started")
     print("SSID: ".. cfg.ssid)
     print("Password: ".. cfg.pwd)
     print("Heep:(bytes)"..node.heap());
     print("MAC:"..wifi.ap.getmac().."\r\nIP:"..wifi.ap.getip());

IO

--******* set up the IO ports *******
gpio.mode(2,gpio.OUTPUT)
gpio.write(2,0)

Server

--*** Server ***********

sv=net.createServer(net.TCP,30)
sv:listen(80,function(c)
    c:on("receive", function(c, pl)
        print(pl)

        --*** turn on/off gpios 
        _, j = string.find(pl,'GPIO_2_switch=')
         if j ~= nil then   
            print(string.sub(pl,j+1))
            if string.sub(pl,j+1) == "on" then
            gpio.write(2,1)
            else
            gpio.write(2,0)
            end
         end
        --*** HTMP web page header ***---
        c:send('<!DOCTYPE html>') 
       -- c:send("HTTP/1.1 200 OK\r\n") 
       -- c:send("Connection: close\r\n\r\n")
        c:send("<html lang='en'> ")
        c:send("<body> ")
        c:send("<h1>ESP8266 Wireless control setup</h1> ")

        --*** HTML buttons  
        c:send('<p>GPIO 2 is: '..gpio.read(2)..'</p>')
        c:send('<form method="post" >')        
        c:send('<input type="radio" name="GPIO_2_switch" value="on"> ON </input> <br />')
        c:send('<input type="radio" name="GPIO_2_switch" value="off"> OFF </input> <br />')
        c:send('<input type="submit" value="Light Switch"  />')  
        c:send('</form></body></html>')
        c:close()
    end)
end)