Skip to content
jollen edited this page Dec 8, 2015 · 11 revisions

2015.12.8 ESP8266 工作坊說明

node-wot 修改了 nodemcu-firmware,以做為 WoT 的實驗用 firmware。變更如下:

  1. 將 libcoap 更換為 Contiki OS 的 er-coap-13 實作,並且只保留了 CoAP client
  • 目前僅將 ESP8266 做為 data push 裝置,不做為 server 端
  • 未來可加入基於 er-coap-13 的 LWM2M 實作 (wakaama)
  • 未來可引入 protothreads 機制,取代 FreeRTOS multitasking
  1. LWM2M 只佈署在 connected device 上(具對外網路能力的裝置),現階段可使用 Notebook、LinktIt Smart 7688 或 Intel Edison 做為 connected device
  • ESP8266 做為 physical data object(IoT data device),並且將 CoAP blocking request 修改為 protothread 機制
  • Physical data object 不具備 control 能力
  1. 上述修改不影響 Lua programming model,因此 Lua 程式可通用(同時在 nodemcu-firmware 與 node-wot 執行)

工作坊議程請見 https://www.mokoversity.com/events/esp8266

Hello, HTTPD

-- Select IO - GPIO4
outpin=4
gpio.mode(outpin,gpio.OUTPUT)
gpio.write(outpin,gpio.HIGH)    

function power(stat)
  if stat=="ON"  then gpio.write(outpin,gpio.HIGH) 
    return 
   end
  if stat=="OFF" then gpio.write(outpin,gpio.LOW) 
    return 
   end
end

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

-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)  
wifi.sta.config("JY", "1234567654321")  
wifi.sta.autoconnect(1)

-- 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, "GET /power/on") ~= nil) then
            power("ON")
        elseif (string.find(payload, "GET /power/off") ~= nil) then
            power("OFF")
        end
        
        conn:send("HTTP/1.1 200 OK\n\n")
        conn:close()
    end)
end)

Hello, CoAP

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

-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)  
wifi.sta.config("JY", "1234567654321")  
wifi.sta.autoconnect(1)

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

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

-- Make a POST request
uri="coap://127.0.0.1:8000/object/12345678/send"

tmr.alarm(0, 1000, 1, function() 
    cc:post(uri, "{\"temp\":20}\r\n")
end)

Hello, LWM2M

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

-- Configure the ESP as a station (client)
wifi.setmode(wifi.STATION)  
wifi.sta.config("JY", "1234567654321")  
wifi.sta.autoconnect(1)

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

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

-- Make a POST request
uri="coap://172.20.10.4:8000/object/12345/send"
cc:post(uri, "{\"temp\":30}\r\n")

-- Register LWM2M object
registry="coap://172.20.10.4:8000/75000/1/create"
cc:post(registry, "{}\r\n")
Clone this wiki locally