English Code

第一單元

example_1

--defined pin 1 witch will used 
pin = 1 
--defined state of the pin 1 
state = "off" 
--defined pin 1 become gpio output 
gpio.mode(pin, gpio.OUTPUT)

--1 second alarm the process 0 and do one time 
tmr.alarm(0,1000,1,function()
--if now is "close" open the led and change state 
if state == "off" then 
--make pin output high 
gpio.write(pin, gpio.HIGH)
print("GPIO 1 light on") 
state = "on" 
--if now is "open" close the led and change state 
else
--make pin output low 
gpio.write(pin, gpio.LOW)
print("GPIO 1 light off")
state = "off" 
end 
end)

example_2

print("Connect to AP:")
wifi.setmode(wifi.STATION)
wifi.sta.config("4Clab-2.4G","socad3284")
tmr.alarm(0,1000,1, function()
print(wifi.sta.getip())
if wifi.sta.getip()-=nil then
tmr.stop(0)
end
end)

第二單元

example_1

-- set pwm pin to gpio pin 3 
pin = 3 
-- set pin 3 to be OUTPUT pin 
gpio.mode(pin, gpio.OUTPUT) 
-- set the highest brightness 
width = 1023 
-- set the clock to 1000Hz
clock = 1000 
-- set up pin 3 with 100 Hz 
pwm.setup(pin,clock,width) 
-- start PWM output 
pwm.start(pin) 

-- change the pulse width each 0.1 sec 
tmr.alarm(0,100, 1, function () 
if width < 20 then 
width = 1023 
else 
width = width - 20
end 
pwm.setduty(pin, width) 
print("width = "..width)
end)

example_2

--set PWM pin to gpio pin 3
pin = 3
--set pin 3 to be OUTPUT pin
gpio.mode(pin, gpio.OUTPUT)
--set the highest brightness
width = 1023
--set the clock to 1000Hz
clock = 1000
--set LED dimming step per timer
dim = 50
--set up pin 3 with 1000Hz
pwm.setup(pin,clock,width)
--start PWM output
pwm.start(pin)
--change the pulse width each 0.1 sec
tmr.alarm(0,1000,1,function() 
width = width - dim 
-- LED darknest or LED brightest, change dimming step
if (width <= dim) or (width - clock) then
dim = dim -1
end 
pwm.setduty(pin,width)   --re-start pwm 
print("width =", width)  --show width for LED brightness
end)

example_3

pin = 3
state1 = "down"   --state1 is used to change the value of duty cycle 
state2 = "on"     --state2 is used to set LED blinking
count=0
gpio.mode(pin, gpio.OUTPUT)
width=1023
clock=1000
pwm.setup(pin, clock, width)
pwm.start(pin)
tmr.alarm(0,100, 1, function() 
if count == 2 then 
count= 0 
if state2 == "on" then 
--make pin output high 
pwm.setduty(pin, width) 
print("GPIO 3 light on") 
state2 = "off" 
-- if now is "open" close the led and change state 
else 
--make pin output high 
pwm.setduty(pin,0) 
print("GPIO 3 light off") 
state2 = "on" 
end 
else
count = count + 1 
end 
if state1 =="down" then 
width = width - 20
if width < 0 then 
width = 23 
state1= "up"
end 
else 
width = width + 20
if width > 1023 then 
width = 1023 
state1= "down" 
end 
end 
print("width="..width)
end)

第三單元

example_1

pin = 0 
tmr.alarm(0,200,1,function()
print("adc = "..adc.read(pin))
end)

example_2

pin = 0 
-- set LED to D1 
led = 1 
-- set the brightness of environment 
ambient = 840 
tmr.alarm (0,500,1,function()
bright = adc.read(pin)
print("bright = "..bright)
--if brightness is bigger than ambient, than open the light
if bright >= ambient then 
gpio.write(led, gpio.HIGH) 
-- otherwise, close the light
else 
gpio.write(led, gpio.LOW)
end
end)

example_3

pin = 0 
-- set LED to D1
led = 1
-- set the brightness of environment 
ambient = 150 
-- set the highest brightness 
width = 0 
-- set the clock to 1000Hz 
clock = 1000 
pwm.setup(led,clock,width) 
-- start PWM output
pwm.start(led) 
tmr.alarm(0,500,1,function()
bright = adc.read(pin) 
--if brightness is bigger than ambient 
if bright <= ambient then 
width = 0
else 
width = bright 
end

pwm.setduty(led, width) 
print("bright = " ..bright ..", width = " .. width) 
end)

第四單元

example_1

PIRPin = 4
i = 1
gpio.mode(PIRPin, gpio.INPUT)
tmr.alarm(0,500, 1, function() 
state = gpio.read (PIRPin) 
print(i .." PIR=" .. state)
i = i + 1
end)

example_2

PIRPin = 4 
pin = 2 
i = 1 
gpio.mode(PIRPin, gpio.INPUT)
gpio.mode(pin, gpio.OUTPUT)
tmr.alarm(0,500,1, function() 
state = gpio.read(PIRPin) 
if state == 1 then 
--make pin output high 
gpio.write(pin, gpio.HIGH )
else
gpio.write(pin, gpio.LOW) end 
print(i .." PIR=".. state) 
i = i + 1
end)

example_3

PIRPin = 4 
pin = 2 
lightpin = 0 
-- set the brightness of environment 
ambient = 400 
i = 1 
gpio.mode(PIRPin, gpio.INPUT) 
tmr.alarm(0,500, 1, function()
state = gpio.read(PIRPin) 
bright = adc.read(lightpin) 
if state == 1 and bright >= ambient then 
--make pin output high 
gpio.write(pin, gpio.HIGH)
else
gpio.write(pin, gpio.LOW) 
end 
print(i .." PIR=" .. state .. " ,bright=" .. bright)
i = i + 1
end)

第五單元

example_1

keys ={{"1","2","3","A"},{"4","5","6","B"},{"7","8","9","C"},{"*","0","#","D"}}
-- define keys' name for 4x4 

for pin = 1, 4 do
gpio.mode(pin, gpio.OUTPUT) --initial gpio D1-D4 output mode 
gpio.mode(pin+4, gpio.OUTPUT) --initial gpio D5-D8 output mode then set to low 
gpio.write(pin+4, gpio.LOW) 
gpio.mode(pink+4, gpio.INPUT) --set pin D5-D8 input mode (Row) 
end 
tmr.alarm(0,200,1,function() --Set timer in 200ms 
for rpin =1,4 do            --Loop for set high level to assigned row pin 
gpio.write(rpin, gpio.HIGH) 
for cpin =1,4 do          -- Loop for read assignedd column pin for keyin detect
hit = gpio.read(cpin+4) 
if hit == gpio.HIGH then 
print("Key["..rpin.."]["..cpin.."]="..keys[rpin][cpin])
-- If there is keyin , show Row/Col pin name and Key name
end
end
gpio.write(rpin, gpio.LOW) -- reset Roy to Low Lee
end 
end)

example_2

keys ={{"1","2","3","A"},{"4","5","6","B"},{"7","8","9","C"},{"*","0","#","D"}}
-- define keys' name for 4x4 

buzzerpin = 12 
gpio.mode(buzzerpin, gpio.OUTPUT) 
gpio.write(buzzerpin, gpio.LOW)   --set buzzerpin to low

for pin = 1, 4 do 
gpio.mode(pin, gpio.OUTPUT)       -- initial gpio D1-D4 output mode
gpio.mode(pin+4, gpio.OUTPUT)     -- initial gpio D5-D8 output mode then set to low
gpio.write(pin+4, gpio.LOW)
gpio.mode(pin+4, gpio.INPUT)      --set pin D5-D8 input mode (Row)
end 

tmr.alarm(0,200,1, function ()    --Set Timer in 200ms
for rpin = 1,4 do               --Loop for Set high level to assigned row pin
gpio.write(rpin, gpio.HIGH) 
for cpin=1,4 do               --Loop for read assignedd column pin for keyin detect
hit = gpio.read(cpin+4)
if hit == gpio. HIGH then
print("Key["..rpin.."]["..cpin.."]="..keys[rpin][cpin])
-- If there is keyin, show Row/Col pin name and Key name
gpio.write(buzzerpin, gpio.HIGH)
end 
end 
gpio.write(rpin, gpio.LOW)  -- reset Row to Low Levet
gpio.write(buzzerpin, gpio.LOW)
end 
end)

example_3

keys ={{"1","2","3","A"},{"4","5","6","B"},{"7","8","9","C"},{"*","0","#","D"}}
-- define keys' name for 4x4 

buzzerpin = 12 
gpio.mode(buzzerpin, gpio.OUTPUT) 
gpio.write(buzzerpin, gpio.LOW)   --set buzzerpin to low

for pin = 1, 4 do 
gpio.mode(pin, gpio.OUTPUT)       -- initial gpio D1-D4 output mode
gpio.mode(pin+4, gpio.OUTPUT)     -- initial gpio D5-D8 output mode then set to low
gpio.write(pin+4, gpio.LOW)
gpio.mode(pin+4, gpio.INPUT)      --set pin D5-D8 input mode (Row)
end 

tmr.alarm(0,200,1,function()      --Set Timer in 200ms
for rpin = 1,4 do               -- Loop for Set high level to assigned row pin
gpio.write(rpin, gpio.HIGH) 
for cpin=1,4 do               -- Loop for read assignedd column pin for keyin detect
hit = gpio.read(cpin+4)
if hit == gpio.HIGH then
width = rpin*200 + cpin*50

gpio.write(buzzerpin, gpio. HIGH)
        
pwm.setup(buzzerpin, width, width/2)
pwm.start(buzzerpin) 
pwm.stop(buzzerpin)
  
print("Key["..rpin.."]["..cpin.."]="..keys[rpin][cpin]..", width="..width)
-- If there is key in , show Row/Col pin name and Key name
end 
end 
gpio.write(rpin, gpio.LOW)     -- reset Row to Low Level
end 
end)

第六單元

example_1

analogpin = 0 --set analog pin is 0 
tmr.alarm(0, 1000, 1, function()
print("analog value=" ..adc.read(analogpin)) 
end)

example_2

pin = 2
gpio.mode(pin, gpio.INPUT)
tmr.alarm(0, 1000, 1, function() 
state=gpio.read(pin) 
print("digital value = " ..state)
end)

example_3

analogpin = 0 -- set analog pin is 0 
ledpin = 3 
gpio.mode(ledpin, gpio.OUTPUT)
tmr.alarm(0, 1000, 1, function() 
watervalue = adc.read(analogpin) 
if watervalue > 700 then 
gpio.write(ledpin, gpio.HIGH)
print("analog value=" ..watervalue ..",Light ON")
else
gipio.write(ledpin, gpio.LOW)
print("analog value=" ..watervalue ..",Light OFF")
end
emd)

第七單元

example_1

pin = 1 
gpio.mode(pin, gpio.INPUT) 
tmr.alarm(0,2000, 1, function() 
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
if status == dht.OK then 
-- Float firmware using in this example 
print("DHT Temperature:"..temp..";".." Humidity:".. humi) 
elseif status == dht.ERROR_CHECKSUM then 
print("DHT Checksum error.") 
elseif status == dht.ERROR_TIMEOUT then 
print("DHT time out.")
end
end)

example_2

pin = 1 
soilpin = 0
gpio.mode(pin,gpio.INPUT)
tmr.alarm(0,2000,1,function()
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
soilstate = adc.read(soilpin) 
if status == dht.OK then 
-- Float firmare Lusing in this exampLe 
print("DHT Temperature: "..temp.."; Humidity: "..humi.."; Soil:"..soilstate)
elseif status == dht.ERROR_CHEICKISUM then 
print("DHT Checksum error.")
elseif status == dht.ERROR_TIMEOUT then 
print("DHT time aut.") 
end 
end)

example_3

pin = 1
soilpin = 0 
gpio.mode(pin, gpio.INPUT)
buzzerpin = 12 
gpio.mode(buzzerpin, gpio.OUTPUT) 
gpio.write(buzzerpin, gpio.LOW)

function detect_temp_humi() 
if temp > 30 then 
gpio.write(buzzerpin, gpio.HIGH) 
print("The temperature is too high!") 
if humi > 80 then 
--gpio.write(buzzerpin,gpio.HIGH)
print("The humidity is too high!")
elseif humi < 20 then 
print("The humidity is too low!") 
end 
elseif temp < 24 then 
gpio.write(buzzerpin, gpio.HIGH)
print("The temperature is too low !")
if humi > 80 then
print("The humidity is too high!")
elseif humi < 20 then 
print("The humidity is too low !") 
end 
elseif temp>=24 and temp <=30 then 
if humi > 80 then 
gpio.write(buzzerpin,gpio.HIGH)
print("The humidity is too high!")
elseif humi < 20 then 
gpio.write(buzzerpin,gpio.HIGH)
print("The humidity is too low!") 
end 
end 
end

tmr.alarm(0,1000,1,function() 
status, temp, humi, tempo_dec, humi_dec = dht.read(pin)
soilstate = adc.read(soilpin) 
print("DHT Temperature:"..temp.."; Humidity:"..humi.."; Soil:"..soilstate) 
gpio.write(buzzerpin,gpio.LOW) 
if status == dht.OK then 
if soilstate > 700 them 
gpio.write(buzzerpin,gpio.HIGH)
print("Soil humidity isn't enough!") 
detect_temp_humi() 
elseif soilstate <= 700 then 
detect_temp_humi()
end 
elseif status == dht.ERROR_CHECKSUM then 
print("DHT Checksum error.") 
elseif status == dht.ERROR_TIMEOUT then 
print("DHT time out.") 
end 
end)

第八單元

example_1

relayPin = 1 
gpio.mode(relayPin, gpio.OUTPUT) 
-- connect to NC 
gpio.write(relayPin, gpio.HIGH) 
-- connect to NO 
gpio.write(relayPin, gpio.LOW)

example_2

relayPin = 1 
PIRPin = 2 
gpio.mode(relayPin, gpio.OUTPUT) 
gpio.mode(PIRPin, gpio.INPUT) 
tmr.alarm(0,500, 1, function() 
state=gpio.read(PIRPin) 
if state ==1 then 
gpio.write(relayPin, gpio.LOW) 
print("state="..state.."Light open") 
else 
gpio.write(relayPin, gpio.HIGH) 
print("state="..state.."Light close") 
end 
end)

第九單元

example_1

pin = 0 
tmr.alarm(0, 200, 1, function() 
force = adc.read(pin)
print("Force = "..force) 
end)

example_2

pin = 0 
ledpin = 2 
tmr.alarm(0, 200, 1, function() 
force = adc.read(pin) 
if force < 1000 then 
gpio.write(ledpin, gpio.HIGH)
print("force value="..force..",Light ON") 
else
gpio.write(ledpin, gpio.LOW)
print("Force value="..force..",Light OFF")
end 
end)

example_3

pin = 0
ledpin = 2 
width = 0 
clock = 100 
force=1024
pwm.setup(ledpin, clock, width) 
plwm.start(ledpin) 
tmr.alarm(0, 200, 1, function() 
force_lasttime = force
force = adc.read(pin) 
force_diff = force-force_lasttime 
if (force < 950) and (force diffc <=0) then 
if (width < 1000) then 
width = width + 50 
end 
elseif (force < 950) and (force_diff>0) then 
width = width 
else 
if (width>0) then 
width = width-100
end 
end 
pwm.setduty(ledpin, width) 
print("force value=" ..force..",width = "..width)
end)

第十單元

example_1

soundpin = 0
tmr.alarm(0,50,1,function()
print("analog Value=", ado.read(sound pin))
end)

example_2

soundpin = 0 
ledyellow=1 
ledgreen=2 
ledred=3
tmr.alarm(0,50,1,function() 
sound = adc.read(soundpin) 
print("analog Value="..sound) 
if sound >= 800 then 
gpio.write(ledyellow, gpio.HIGH) 
elseif sound < 800 and sound >= 790 then 
gpio.write(ledred, gpio.HIGH) 
elseif sound < 790 then 
gpio.write(ledgreen, gpio.HIGH) 
end 
gpio.write(ledyellow, gpio.LOW) 
gpio.write(ledgreen, gpio.LOW) 
gpio.write(ledred, gipio.LOW)
end)

第十一單元

example_1_Wifi連線

pin = 0 
gpio.mode(pin, gpio.OUTPUT) 
gpio.write(pin, gpio.LOW) 
wifi.setmode(wifi.STATION)
wifi.sta.config("4Clab-2.4G", "socad3284")
tmr.alarm(0,1000, 1, function() 
print(wifi.sta.getip()) 
if wifi.sta.getip()~= nil then 
gpio.write(pin, gpio.HIGH)
tmr.stop(0) 
end 
end)

example_1_A_subscribe

--clientid cannot be the same with publisher  
m = mqtt.Client("clientid_A", 120, nil, nil)
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("connect to MQTT Broker success")
m:subscribe("topicA",0, function(m)
print("subscribe success")
end)
end)

m:on("message", function(m, topic, data) 
print("Topic ["..topic.."]:")
if data ~= nil then
print(data)
end
end)

example_1_B_Publisher

m = mqtt.Client("clientid_B", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("Publisher connect...")
end)

m:on ("connect", function (m)
print("connect")
msg="Hello, I am B"
m:publish("topicA", msg, 0,0, function(m) 
print("\n clientid B sent \""..msg.. "\" success")
end)
end)

example_1_B_Subscribe

--clientid cannot be the same with publisher 
m = mqtt.Client("clientid_B", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function(m) 
print("clientid B connect to MOTT Broker success") 
m:subscribe("topicB",0, function (m) 
print("clientid_B subscribe topicB success")
end) 
end) 

m:on("message",function(m, topic, data) 
print("Topic [".. topic .. "]:") 
if data ~= nil then 
print(data)
end
end)

example_1_A_Publisher

m = mqtt.Client("clientid_A", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("clientid_A connect to MQTT Broker success")
end)

m:on ("connect", function (m)
print("clientid_A connected")
msg="Hello B"
m:publish("topicB", msg, 0,0, function(m) 
print("\n clientid_A sent \""..msg.. "\" success")
end)
end)

 

 

 

example_2

m = mqtt.Client("clientid_A", 120, nil, nil)
m:connect("iot.eclipse.org", 1883, 0, function (m)
print("clientid_A connect to MQTT Broken success") 
m:subscribe("topicA",0, function (m)
print("clientid_A subscribe topicA success")
end)
end)

m:on("message", function (m, topic, data) 
print("Topic ["..topic.."]:") 
if data ~= nil and data == "on" then 
print("Light on") 
gpio.write(0, gpio.LOW) 
elseif data ~= nil and data == "off" then 
print("Light off") 
gpio.write(0, gpio.HIGH) 
end 
end)

example_2_A

m = mqtt.Client("clientid_A", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function (m)
  print("clientid_A connect to MQTT Broker success")
end)

msg="on"
m:on("connect", function(m) 
print("clientid_A connected") 
tmr.alarm(0,3000, 1, function() 
if msg== "on" then 
m:publish("topicB", msg, 0,0, function(m) 
print("\n clientid_A sent \""..msg.."\" success") 
msg="off" 
end) 
else 
m:publish("topicB", msg, 0,0, function (m) 
print("\n clientid_A sent \""..msg.."\" success") 
msg="on"
end) 
end 
end)
end)

m = mqtt.Client("clientid_B", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function(m) 
print("clientid B connect to MQTT Broker success")
m:subscribe("topicB",0, function(m)
print("clientid_B subscribe topic B success") 
end)
end)

m:on("message", function(m, topic, data)
print("Topic [".. topic .. "]:" ) 
if data ~= nil and data == "on" then 
print("Light on") 
gpio.write(0,gpio.LOW) 
elseif data ~= nil and data=="off" then 
print("Light off") 
gpio.write(0, gpio.HIGH) 
end 
end)

example_2_B

m = mqtt.Client("clientid_B", 120, nil, nil) 
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("clientid_B connect to MQTT Broker success")
end)

msg="on"
m:on("connect", function (m)
print("clientid_B connected")
tmr.alarm(0,3000, 1, function()
if msg == "on" then 
m:publish("topicA", msg, 0,0, function(m)
print("\n clientid_B sent \""..msg.. "\" success")
msg="off"
end)
else 
m:publish("topicA", msg, 0,0, function(m) 
print("\n clientid_B sent \""..msg.."\" success")
msg="on"
end) 
end
end)
end)

example_3_A

m = mqtt.Client("clientid_A", 120, nil, nil)
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("clientid_A connect to MQTT Broken success") 
m:subscribe("topicA",0, function (m)
print("clientid. A subscribe topicA success")
end)
end)

m:on("message", function(m, topic, data)
print("Topic ["..topic.."]: ") 
if data ~= nil and data == "on" then 
print("Light on") 
gpio.write(0, gpio.LOW) 
elseif data ~= nil and data == "off" then 
print("Light off") 
gpio.write(0, gpio.HIGH) 
end 
end)

example_3_B

m = mqtt.Client("clientid_B", 120, nil, nil)
m:connect("iot.eclipse,org", 1883, 0, function(m)
print("clientid B connect to MQTT Broker success")
end)

PIRPin = 2
gpio.mode(PIRPin, gpio.INPUT) 
m:on("connect", function(m)
print("clientid_B connected") 
tmr.alarm(0,3000,1, function()
state=gpio.read(PIRPin) 
if state ==1 then 
msg="on" 
m:publish("topicA",msg,0,0,function(m) 
print("state="..state.." clientid_B sent \""..msg.."\" success" 
end) 
else 
msg="off" 
m:publish("topicA", msg, 0,0, function(m) 
print("state="..state.." clientid_B sent \""..msg.. "\" success") 
end)
end
end) 
end)
 

第十二單元

example_1

print("Running DHT CoAP server...") 
dhtPin = 1 
temperature = -127 
humidity = -1 
tmr.alarm(0,1000, 1,function() 
status, temp, humi, temp_decimial, humi_decimial = dht.read(dhtPin) 
if( status == dht.OK ) then 
temperature = temp 
humidity = humi 
print("Temperature= "..temp,"Humidity= "..humi) 
elseif ( status == dht.ERROR_CHECKSUM ) then 
print("status ERROR CHECKSUM") 
elseif ( status == dht.ERROR_TIMEOUT ) then 
print("status ERROR TIMEOUT") 
end 
end)
function typeID(p) 
if p~=nil then 
deviceInfo.deviceID = p 
end 
return deviceInfo.deviceID 
end

function flags(p) 
if pro~=nil then 
deviceInfo.flags = p
end
return deviceInfo.flags 
end 

cv = require "C4lab_CoAP210-ob"       --load CoAP observer 
cv:startCoapServer(5683)              --define CoAP port
cv:addActuator("/typeID","typeID")
cv:addActuator("/flags", "flags")
cv:addSensor("/121/1", "temperature") --Add temperature sensor
cv:addSensor("/121/2", "humidity")    --Add humidity sensor
cv:postResource()
deviceInfo = {                        --CoAP device information 
flags = 0x00000001,                 --IoT device can be power off 
classID = 0,                        -- 0 for home appliance
deviceID = 120                      -- devices with sensors and actuators
}

example_2

print("Running DHT CoAP server...") 
dhtPin = 1
temperature = -127
humidity = -1
ledPin = 2                           -- define LED control pin 
gpio.mode(ledPin, gpio.OUTPUT)       -- Set LED GPTIO 
gpio.write(ledPin, gpio.LOW)
led = 0
tmr.alarm(0,1000,1,function() 
status, temp, humi, temp_decimial, hurni_decimial = dhit.read(dhtPin) 
if( status == dht.OK ) then 
temperature = temp 
humidity = humi 
elseif( status == dht.ERROR_CHECKSU) then 
print("Status ERROR_CHECKSUM") 
elseif( status == dht.ERROR_TIMEOUT ) then 
print("status ERROR TIMEOUT")
end 
end) 

function setLed(p)                    -- LED IOT control function 
if p=="0" then                      -- if command "0", LEO off 
led = 0 
gpio.write(ledPin, gpio.LOW) 
elseif p=="1" then                  --if command "1", LED on 
led = 1 
gpio.write(ledPin, gpio.HIGH) 
end 
return led                          -- return LED states 
end

function typeID(p) 
if p ~= nil then 
deviceInfo.deviceID = p 
end 
return deviceInfo.deviceID 
end

function flags(p) 
if p ~= nil then 
deviceInfo.flags = p
end
return deviceInfo.flags 
end 

cv = require "C4lab_CoAP210-ob"       --load Coap observer 
cv:startCoapserver(5683)              --define Co4 P port
cv:addActuator("/typeID", "typeID") 
cv:addActuator("/flags", "flags")

cv:addSensor("/121/1", "temperature") --Add temperature sensor 
cv:addSensor("/121/2", "humidity")    --Add humidity sensor 
cv:addActuator("/122/200", "setLed") --Add LED Actuator
cv:postResource()

deviceInfo = {                        --CoAP device information 
flags = 0x00000001,                 --IoT device can be power off 
classID = 0,                        -- 0 for home appliance
deviceID = 120                      -- devices with sensors and actuators
}

example_3

print("Running DHT CoAP server...") 
dhtPin = 1
temperature = -127
humidity = -1
ledPin = 2                           -- define LED control pin 
gpio.mode(ledPin, gpio.OUTPUT)       -- Set LED GPTIO 
gpio.write(ledPin, gpio.LOW)
led = 0
tmr.alarm(0,1000,1,function() 
status, temp, humi, temp_decimial, hurni_decimial = dhit.read(dhtPin) 
illuminance = adc.read(0)
if( status == dht.OK ) then 
temperature = temp 
humidity = humi 
print("Temperature= "..temp,"Humidity= "..humi,"illuminace= "..illuminace,"","LED= "..led) 
elseif( status == dht.ERROR_CHECKSU) then 
print("Status ERROR_CHECKSUM") 
elseif( status == dht.ERROR_TIMEOUT ) then 
print("status ERROR TIMEOUT")
end 
end) 

function setLed(p)                    -- LED IOT control function 
if p=="0" then                      -- if command "0", LEO off 
led = 0 
gpio.write(ledPin, gpio.LOW) 
elseif p=="1" then                  --if command "1", LED on 
led = 1 
gpio.write(ledPin, gpio.HIGH) 
end 
return led                          -- return LED states 
end

function typeID(p) 
if p ~= nil then 
deviceInfo.deviceID = p 
end 
return deviceInfo.deviceID 
end

function flags(p) 
if p ~= nil then 
deviceInfo.flags = p
end
return deviceInfo.flags 
end 

cv = require "C4lab_CoAP210-ob"       --load Coap observer 
cv:startCoapserver(5683)              --define Co4 P port
cv:addActuator("/typeID", "typeID") 
cv:addActuator("/flags", "flags")
cv:addSensor("/121/1", "temperature") --Add temperature sensor 
cv:addSensor("/121/2", "humidity")    --Add humidity sensor 
cv:addSensor("/121/3", "illuminace")    --Add Light sensor
cv:addActuator("/122/200", "setLed") --Add LED Actuator
cv:postResource()

deviceInfo = {                        --CoAP device information 
flags = 0x00000001,                 --IoT device can be power off 
classID = 0,                        -- 0 for home appliance
deviceID = 120                      -- devices with sensors and actuators
}