物聯網教學程式碼

教學內容主的相關程式碼

第一章 NodeMCU 軟硬體平台與感測模組 影片連結

GPIO程式範例

pin = 0                        -- LED pin
gpio.mode(pin, gpio.OUTPUT)    -- D0 set OUTPUT mode
gpio.write(pin, gpio.LOW)      -- D0 set LOW, open LED
tmr.delay(2000000)             -- delay 2s
gpio.write(pin, gpio.HIGH)     -- D0 set HIHT, close LED

Wi-Fi程式範例

wifi.setmode(wifi.STATION)
wifi.sta.config("4Clab-2.4G", "12345678") 

tmr.alarm(0,1000, 1, function() 
print(wifi.sta.getip())
if wifi.sta.getip() ~= nil then 
Stop(0) 
end
end)


第二章 PWM原理與應用 影片連結

LED pwm 程式範例

-- 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)

第三章光敏電阻以及ADC原理與應用 影片連結

光敏阻程式範例

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

第四章移動感測器原理與應用 影片連結

PIR程式範例

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)

第五章 矩陣鍵盤感測器原理與應用 影片連結

鍵盤程式範例

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(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 levet 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 Row to Low Level
end 
end)

第六章土壤濕度感測器原理與應用 影片連結

土壤濕度程式範例

analogPin = 0
tmr.alarm(0,1000,1,function()
print("analog=", adc.read(analogPin)) 
end
)

第七章溫濕度感測器原理與應用 影片連結

溫溼度程式範例

pin = 12 
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 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 timed out.") 
end
end)

第九章壓力感測模組原理與應用 影片連結

壓力感測程式範例

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

第十章聲音感測模組原理與應用 影片連結

analogPin = 0
tmr.alarm (0, 50, 1, function() 
print("analog=", adc.read(analogPin)) 
end)

 

第十三部分 MQTT Message Queuing Telemetry Transport protocol 影片連結

NodeMCU的MQTT訂閱範例

-- init mqtt client with keepalive timer 120sec 
m = mqtt.Client("clientid-sub-01", 120, un, pw) 

-- on publish message receive event 
m:on("message", function(m, topic, data)
print(topic .. " : " )
if data ~= nil then
print (data)
end
end)

-- for secure: m:connect("192.168.11.118", 1880, 1) 
m:connect("iot.eclipse.org", 1883, 0, function(m)
print("connected-2") 
-- subscribe topic with qos = 0 
m:subscribe("lightTopic",0, function(m) 
print("subscribe success")
end)
end)

NodeMCU的MQTT發佈範例

-- init mqtt client with keepalive timer 120sec
m = mqtt.Client("clientid-pub-02", 120, un, pw)

-- for secure: m:connect("192.168.11.118", 1880, 1)
m:connect("iot.eclipse.org", 1883, 0, function (m)
print("connected-2")
-- publish a message with data = hello, QoS = 0, retain = 0 
m:publish("lightTopic","lightness=100",0,0,function(m) 
print("sent")
end)
-- publish a message with data = hello, QoS = 0, retain = 0
m:publish("lightTopic","lightness=200",0,0, function(m) 
print("sent")
end)
end)

MQTT_Sub&Pub範例

--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) 

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)

附錄

進階套件程式碼

程式碼將會陸續更新。


					
Posted in 教學區, 教學檔案, 程式分享.

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *


*