Skip to content

Lua Examples

jollen chen edited this page May 29, 2018 · 3 revisions

CoAP Client

-- Print IP address
ip = wifi.sta.getip()  
print(ip)

-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)  
station_cfg={}
station_cfg.ssid="JY"
station_cfg.pwd="1234567654321"
station_cfg.save=true
wifi.sta.config(station_cfg)
wifi.sta.autoconnect(1)

-- Print IP address
ip = wifi.sta.getip()  
print(ip)

-- Create a CoAP client
cc = coap.Client()

-- Make a POST request
-- Visit https://wotcity.com
uri="coap://wot.city:8147/object/5550937980d51931b3000009/send"

tmr.alarm(0, 3000, 1, function() 
    buf = 
          "{" ..
          "\"quality\":" ..
          adc.read(0) ..
          "}"
    
    cc:post(coap.CON, uri, buf)
    print(buf)
end)

HTTP Server

-- Create a server
-- and set 30s time out for a inactive client
sv = net.createServer(net.TCP, 30)

-- Server listen on 80
-- Print HTTP headers to console
sv:listen(80,function(c)  
    c:on("receive", function(conn, payload)
        print(payload)

        if (string.find(payload, "/1/quality") ~= nil) then
            buf = "HTTP/1.1 200 OK\n\n" ..
                  "{" ..
                  "\"quality\":" ..
                  adc.read(0) ..
                  "}"
            conn:send(buf)
        end
        
        conn:close()
    end)
end)
Clone this wiki locally