User avatar
faramon
Posts: 123
Joined: Sat Jun 11, 2016 8:36 am
Location: Croatia

NodeMCU with ESP8266 instructions

Thu Mar 30, 2017 9:01 am

Hi all,

Yesterday I did after two days of work (9 hours total) set up my NodeMCU module that works and communicates via mqtt protocol with Raspberray Pi 3 Jessie.

This is the link where i buy this module:
http://www.ebay.com/itm/NEW-Version-Nod ... 1932361662 and these are instructions step by step if someone need to do the same:
1. Buy module NodeMCU from link and connect it to computer to usb port
2. This is page where i order free my new firmware https://nodemcu-build.com/ that is sent to my email free
3. Step 2 was necessary in my case because I try to FLASH Firmware that comes with NodeMCU but it is not the latest and my MQTT didn't work with it, i try many solutions but unsuccessful
3. I have download FLASHER following this link http://www.whatimade.today/loading-the- ... ows-guide/
4. I didn't have problem with flashing my firmware that i download from email link
5. Then go to download ESPlorer (Russian piece of java software that works nice and great)
6. Unpluged the NodeMCU that is connected to computer and Open ESPlorer
7. Connect NodeMCU again to usb on computer
8. Open all menus (panels) in ESPlorer
9. Hit refresh button to get PORT to use (default it is not present here)
10. Pick COM3 (in my case) and 9600 Baud rate
11. Hit Open/Close button
12. upload LUA scripts (I have LUA because it is mother language for NodeMCU.. there is MicroPython firmware if you wish)
14. Create the lua scripts. I have 4 scripts:
application.lua
config.lua
setup.lua
test.lua
15. Run the script test.lua
16. After you set your program to work then rename test to init.lua in youre computer and upload it.

After upload init.lua, this will go on when reboot or unpluged NodeMCU again in power. I read that is better to full finish code to test.lua and after testing it, put init.lua because init-lua is hard to remove, NodeMCU has to be firmwared again.

You can follow this link, it helps me in flashing...
http://www.averagemanvsraspberrypi.com/ ... setup.html

All can be done using Arduino IDE but I did not familiar with it...

Here are this 4 modules.lua
1. test.lua:

Code: Select all

-- ESP8266

print("ESP8266 module start")

app = require("application")
config = require("config") 
setup = require("setup")

setup.start()  
2. setup.lua:

Code: Select all

local module = {}

local function wifi_wait_ip()  
  if wifi.sta.getip()== nil then
    print("IP unavailable, Waiting...")
  else
    tmr.stop(1)
    print("\n====================================")
    print("ESP8266 mode is: " .. wifi.getmode())
    print("MAC address is: " .. wifi.ap.getmac())
    print("IP is "..wifi.sta.getip())
    print("====================================")
    app.start()
  end
end

local function wifi_start(list_aps)  
    if list_aps then
        for key,value in pairs(list_aps) do
            if config.SSID and config.SSID[key] then
                wifi.setmode(wifi.STATION);
                wifi.sta.config(key,config.SSID[key])
                wifi.sta.connect()
                print("Connecting to " .. key .. " ...")
                --config.SSID = nil  -- can save memory
                tmr.alarm(1, 2500, 1, wifi_wait_ip)
				print("Connected!")
            end
        end
    else
        print("Error getting AP list")
    end
end

function module.start()  
  print("Configuring Wifi ...")
  wifi.setmode(wifi.STATION);
  wifi.sta.getap(wifi_start)
end

return module 
3. config.lua:

Code: Select all

local module = {}

module.SSID = {}  
module.SSID["MySSID"] = "MySSIDpass"
module.BROKER = "mybroker_local_or_web"  
module.UN = 'broker_username'
module.PS = 'broker_pass'
module.RECONNECT = 1
module.QOS = 0
module.PORT = 1883 -- Or your broker port
module.KEEPALIVE = 120
module.ID = node.chipid()
module.SUBPOINT = "subscription_topic"
module.PUBPOINT = "publish_topic"
module.RETAIN = 0

return module  
4. application.lua:

Code: Select all

local module = {}  
m = nil

-- Sends a simple ping to the broker
local function send_test_ping()
    --print("Publishing...")
    m:publish(config.PUBPOINT, "Just ping " .. config.ID, config.QOS, config.RETAIN, function(conn) 
      print("Ping published!") 
    end)
end

-- Sends my id to the broker for registration
local function register_myself()  
    print("Subscribing...")
    --m:subscribe(config.SUBPOINT .. config.ID,0,function(conn)
    m:subscribe(config.SUBPOINT, config.QOS, function(conn)
        print("Successfully subscribed to data subscription point " .. config.SUBPOINT)
    end)
end

local function mqtt_start()  
    print("Starting MQTT module...")
	-- initiate the mqtt client and set keepalive timer to 120sec
    m = mqtt.Client(config.ID, config.KEEPALIVE,  config.UN, config.PS)
		
	-- Connect to broker
    m:connect(config.BROKER, config.PORT, config.QOS, config.RECONNECT, function(con) 
		--print("Connected!")
        register_myself()
        -- And then pings each 1000 milliseconds
        tmr.stop(6)
        tmr.alarm(6, 10000, 1, send_test_ping)
    end,
    function(client, reason)
        print("failed reason: " .. reason)
    end)
		
    -- on receive message
    m:on("message", function(conn, topic, data)
		  print(topic .. ":" )
      if data ~= nil then
        print(data)
        -- do something, we have received a message
      end
    end)
		
    print("Connecting to broker...")
    --m:on("connect", function(con) print ("Connected!") end)

end

function module.start()  
  mqtt_start()
  --print(config.SUBPOINT)
end

return module  
Thanx for reading this,
Faramon
Last edited by faramon on Mon May 01, 2017 9:58 am, edited 1 time in total.

User avatar
faramon
Posts: 123
Joined: Sat Jun 11, 2016 8:36 am
Location: Croatia

Re: NodeMCU with ESP8266 instructions

Thu Mar 30, 2017 8:39 pm

I connect LED and now I'am able to light my LED On and Off over MQTT with my smartphone or Raspberry Pi.

This is my code with this stuff:

Code: Select all

local module = {}  
m = nil

-- TogglingLED
local pin = 4  --> GPIO2
local value = gpio.LOW
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, value)

local function toggleLedOn ()
    value = gpio.HIGH
    gpio.write(pin, value)
end

local function toggleLedOff ()
    value = gpio.LOW
    gpio.write(pin, value)
end
-- TogglingLED

-- Sends a simple ping to the broker
local function send_test_ping()
    --print("Publishing...")
    m:publish(config.PUBPOINT, "Just ping " .. config.ID, config.QOS, config.RETAIN, function(conn) 
      print("Ping published!") 
    end)
end

-- Sends my id to the broker for registration
local function register_myself()  
    print("Subscribing...")
    --m:subscribe(config.SUBPOINT .. config.ID,0,function(conn)
    m:subscribe(config.SUBPOINT, config.QOS, function(conn)
        print("Successfully subscribed to data subscription point " .. config.SUBPOINT)
    end)
end

local function mqtt_start()  
    print("Starting MQTT module...")
	-- initiate the mqtt client and set keepalive timer to 120sec
    m = mqtt.Client(config.ID, config.KEEPALIVE,  config.UN, config.PS)
		
	-- Connect to broker
    m:connect(config.BROKER, config.PORT, config.QOS, config.RECONNECT, function(con) 
		--print("Connected!")
        register_myself()
        -- And then pings each 1000 milliseconds
        --tmr.stop(6)
        --tmr.alarm(6, 10000, 1, send_test_ping)
    end,
    function(client, reason)
        print("failed reason: " .. reason)
    end)
		
    -- on receive message
    m:on("message", function(conn, topic, data)
		  print(topic .. ":" )
      if data ~= nil then
        print(data)
        -- do something, we have received a message
        if data == "1" then
            print("On")
            toggleLedOn()
        end

        if data == "0" then
            print("Off")
            toggleLedOff()
        end
      end
    end)
		
    print("Connecting to broker...")
    --m:on("connect", function(con) print ("Connected!") end)
end

function module.start()  
  mqtt_start()
  --print(config.SUBPOINT)
end

return module   
Faramon

Return to “General discussion”