Reading Requirement
-
Struktural Koneksi

-
Persiapan ESP8266
Setelah memahami struktural koneksi, supaya pengerjaan berjalan lancar, tahap pertama , kita harus meng-flash modul ESP-12 tersebut seperti pada reading requirement ESP8266
- Alat dan Bahan
- ESP8266
- FTDI
- Kabel Konektor
- Protoboard
- LED
- Resistor 220 Ohm
-
ESP8266 Wiring
Pada tahap selanjutnya, hubungkanlah ESP-12 ke PC/Laptop menggunakan kabel FTDI yang telah dipersiapkan untuk programming, kemudian komponen lain seperti LED ke ESP-12 seperti gambar di bawah ini.
-
Programming
Buatlah script program menggunakan LUA Uploader seperti script contoh init.lua dan mqtt.lua, kemudian upload program ke ESP8266
Program Lua Uploader
untuk melihat fungsi toolbar klik link Tabel Lua Uploader.
Test (Monitoring)
Buka serial monitor atau output screen pada sebelah kanan layar PC/Laptop untuk melihat hasilnya.
Sample code ESP8266
init.lua
-- Constants
SSID = "Your SSID"
APPWD = "Password"
-- File that is executed after connection
CMDFILE = "mqtt.lua"
-- Some control variables
wifiTrys = 0 -- Counter of trys to connect to wifi
NUMWIFITRYS = 200 -- Maximum number of WIFI Testings while waiting for connection
-- Change the code of this function that it calls your code.
function launch()
print("Connected to WIFI!")
print("IP Address: " .. wifi.sta.getip())
-- Call our command file every minute.
tmr.alarm(0, 1000, 0, function() dofile(CMDFILE) end )
end
function checkWIFI()
if ( wifiTrys > NUMWIFITRYS ) then
print("Sorry. Not able to connect")
else
ipAddr = wifi.sta.getip()
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
-- lauch() -- Cannot call directly the function from here the timer... NodeMcu crashes...
tmr.alarm( 1 , 500 , 0 , launch )
else
-- Reset alarm again
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
print("Checking WIFI..." .. wifiTrys)
wifiTrys = wifiTrys + 1
end
end
end
print("-- Starting up! ")
-- Lets see if we are already connected by getting the IP
ipAddr = wifi.sta.getip()
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
-- We aren't connected, so let's connect
print("Configuring WIFI....")
wifi.setmode( wifi.STATION )
wifi.sta.config( SSID , APPWD)
print("Waiting for connection")
tmr.alarm( 0 , 2500 , 0 , checkWIFI ) -- Call checkWIFI 2.5S in the future.
else
-- We are connected, so just run the launch code.
launch()
end
-- Drop through here to let NodeMcu run
File init.lua ini merupakan script penting dari project ini, karena script ini berfungsi untuk menghubungkan ESP8266-12 ke server dengan men-setting SSID Wifi dan Password Wifi yang anda gunakan.
mqtt.lua
m = mqtt.Client("device-8d7dc14bf6cf01xxxxxxx",120,"user","password")
--inisialisasi pin (gpio)
pin=2
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.HIGH)
--Fungsi di bawah ini berfugsi untuk mengkonekkan device ke ip
geeknesia.com
function connect()
m:connect("geeknesia.com", 1883, 0, function(conn)
print("connected")
m:lwt("iot/will", "device-8d7dc14bf6cf01xxxxxx", 0, 0)
m:subscribe("topic-8d7dc14bf6cf010dxxxxxx",0, function(conn)
print("subscribe success")
end)
end)
print("selesai connect")
end
connect()
--Script dibawah bertujuan supaya jika koneksi terputus maka akan auto
reconnect
m:on("offline", function(conn)
print("Reconnect" )
connect()
end)
--Pesan yang diterima dari topic yang di subscribe
--Jika Esp-12 menerima “on” maka pin akan HIGH dan sebaliknya jika
menerima
--“off” maka pin akan LOW (Tergantung Wiring Esp ke led)
m:on("message", function(conn, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
if data=="on" then
gpio.write(2, gpio.HIGH)
end
if data=="off" then
gpio.write(2, gpio.LOW)
end
end
end)
Dan untuk File mqtt.lua adalah program utama dalam project ini, anda dapat membuat script aplikasi yang anda inginkan atau aplikasi project yang kita bahas, dan memasukan Device Id,Username,Password yang sudah anda dapatkan pada saat anda membuat gateway dan device di akun geeknesia.com, yang dimana ketiga data tersebut berfungsi untuk menghubungkan device (ESP8266-12) ke cloud (geeknesia.com).
Note: Pastikan file init.lua dan mqtt.lua ter-upload kedalam ESP8266.