changes
This commit is contained in:
parent
9e634e3ecb
commit
01e23e6608
22
init.lua
22
init.lua
@ -1,18 +1,16 @@
|
|||||||
-- Constants
|
-- Constants
|
||||||
SSID = "MYWIWIF"
|
SSID = "MessWLAN"
|
||||||
APPWD = "QuantumPassword"
|
APPWD = "UNVmpwbr6heQnMQ7ykXT"
|
||||||
CMDFILE = "mqtt.lua" -- File that is executed after connection
|
CMDFILE = "mqtt.lua"
|
||||||
|
|
||||||
-- Some control variables
|
-- Some control variables
|
||||||
wifiTrys = 0 -- Counter of trys to connect to wifi
|
wifiTrys = 0
|
||||||
NUMWIFITRYS = 200 -- Maximum number of WIFI Testings while waiting for connection
|
NUMWIFITRYS = 200
|
||||||
|
|
||||||
-- Change the code of this function that it calls your code.
|
|
||||||
function launch()
|
function launch()
|
||||||
print("Connected to WIFI!")
|
print("Connected to WIFI!")
|
||||||
print("IP Address: " .. wifi.sta.getip())
|
print("IP Address: " .. wifi.sta.getip())
|
||||||
-- Call our command file every minute.
|
tmr.alarm(0, 5000, 0, function() dofile(CMDFILE) end )
|
||||||
tmr.alarm(0, 60000, 0, function() dofile(CMDFILE) end )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function checkWIFI()
|
function checkWIFI()
|
||||||
@ -21,10 +19,8 @@ function checkWIFI()
|
|||||||
else
|
else
|
||||||
ipAddr = wifi.sta.getip()
|
ipAddr = wifi.sta.getip()
|
||||||
if ( ( ipAddr ~= nil ) and ( ipAddr ~= "0.0.0.0" ) )then
|
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 )
|
tmr.alarm( 1 , 500 , 0 , launch )
|
||||||
else
|
else
|
||||||
-- Reset alarm again
|
|
||||||
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
|
tmr.alarm( 0 , 2500 , 0 , checkWIFI)
|
||||||
print("Checking WIFI..." .. wifiTrys)
|
print("Checking WIFI..." .. wifiTrys)
|
||||||
wifiTrys = wifiTrys + 1
|
wifiTrys = wifiTrys + 1
|
||||||
@ -34,17 +30,13 @@ end
|
|||||||
|
|
||||||
print("-- Starting up! ")
|
print("-- Starting up! ")
|
||||||
|
|
||||||
-- Lets see if we are already connected by getting the IP
|
|
||||||
ipAddr = wifi.sta.getip()
|
ipAddr = wifi.sta.getip()
|
||||||
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
|
if ( ( ipAddr == nil ) or ( ipAddr == "0.0.0.0" ) ) then
|
||||||
-- We aren't connected, so let's connect
|
|
||||||
print("Configuring WIFI....")
|
print("Configuring WIFI....")
|
||||||
wifi.setmode( wifi.STATION )
|
wifi.setmode( wifi.STATION )
|
||||||
wifi.sta.config( SSID , APPWD)
|
wifi.sta.config( SSID , APPWD)
|
||||||
print("Waiting for connection")
|
print("Waiting for connection")
|
||||||
tmr.alarm( 0 , 2500 , 0 , checkWIFI ) -- Call checkWIFI 2.5S in the future.
|
tmr.alarm( 0 , 2500 , 0 , checkWIFI )
|
||||||
else
|
else
|
||||||
-- We are connected, so just run the launch code.
|
|
||||||
launch()
|
launch()
|
||||||
end
|
end
|
||||||
-- Drop through here to let NodeMcu run
|
|
77
mqtt.lua
77
mqtt.lua
@ -1,92 +1,73 @@
|
|||||||
-- Configuration to connect to the MQTT broker.
|
BROKER = "172.16.2.15"
|
||||||
BROKER = "172.16.2.15" -- Ip/hostname of MQTT broker
|
BRPORT = 1883
|
||||||
BRPORT = 1883 -- MQTT broker port
|
BRUSER = ""
|
||||||
BRUSER = "" -- If MQTT authenitcation is used then define the user
|
BRPWD = ""
|
||||||
BRPWD = "" -- The above user password
|
CLIENTID = "ESP8266y-" .. node.chipid()
|
||||||
CLIENTID = "ESP8266-" .. node.chipid() -- The MQTT ID. Change to something you like
|
|
||||||
|
|
||||||
SWITCH_PIN = 0
|
SWITCH_PIN = 0
|
||||||
|
|
||||||
SWITCH_ID = 0
|
SWITCH_ID = 0
|
||||||
|
|
||||||
-- MQTT topics to subscribe
|
topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID}
|
||||||
topics = {"IoT/Watchdog", "IoT/Switch" .. SWITCH_ID} -- Add/remove topics to the array
|
pub_sem = 0
|
||||||
|
current_topic = 1
|
||||||
-- Control variables.
|
topicsub_delay = 50
|
||||||
pub_sem = 0 -- MQTT Publish semaphore. Stops the publishing whne the previous hasn't ended
|
|
||||||
current_topic = 1 -- variable for one currently being subscribed to
|
|
||||||
topicsub_delay = 50 -- microseconds between subscription attempts, worked for me (local network) down to 5...YMMV
|
|
||||||
id1 = 0
|
|
||||||
id2 = 0
|
id2 = 0
|
||||||
|
|
||||||
switch_state = 0
|
switch_state = 0
|
||||||
|
|
||||||
|
|
||||||
gpio.mode(SWITCH_PIN, gpio.OUTPUT)
|
-- gpio.mode(SWITCH_PIN, gpio.OUTPUT)
|
||||||
gpio.write(SWITCH_PIN, gpio.LOW)
|
-- gpio.write(SWITCH_PIN, gpio.LOW)
|
||||||
|
|
||||||
-- connect to the broker
|
print("Connecting to MQTT broker. Please wait...")
|
||||||
print "Connecting to MQTT broker. Please wait..."
|
|
||||||
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
|
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
|
||||||
|
print("step 1")
|
||||||
|
tmr.alarm(4, 15, 1 mqtt_watchdog_expired)
|
||||||
|
print("step 2")
|
||||||
m:connect( BROKER , BRPORT, 0, function(conn)
|
m:connect( BROKER , BRPORT, 0, function(conn)
|
||||||
print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID )
|
print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID )
|
||||||
mqtt_sub() --run the subscription function
|
mqtt_sub()
|
||||||
end)
|
end)
|
||||||
|
print("step 3")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function mqtt_watchdog_expired()
|
||||||
|
print("mqtt watchdog expired")
|
||||||
|
end
|
||||||
|
|
||||||
function mqtt_sub()
|
function mqtt_sub()
|
||||||
if table.getn(topics) < current_topic then
|
if table.getn(topics) < current_topic then
|
||||||
-- if we have subscribed to all topics in the array, run the main prog
|
|
||||||
run_main_prog()
|
run_main_prog()
|
||||||
else
|
else
|
||||||
--subscribe to the topic
|
|
||||||
m:subscribe(topics[current_topic] , 0, function(conn)
|
m:subscribe(topics[current_topic] , 0, function(conn)
|
||||||
print("Subscribing topic: " .. topics[current_topic - 1] )
|
print("Subscribing topic: " .. topics[current_topic - 1] )
|
||||||
end)
|
end)
|
||||||
current_topic = current_topic + 1 -- Goto next topic
|
current_topic = current_topic + 1
|
||||||
--set the timer to rerun the loop as long there is topics to subscribe
|
|
||||||
tmr.alarm(5, topicsub_delay, 0, mqtt_sub )
|
tmr.alarm(5, topicsub_delay, 0, mqtt_sub )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sample publish functions:
|
|
||||||
function publish_heartbeat()
|
|
||||||
if pub_sem == 0 then -- Is the semaphore set=
|
|
||||||
pub_sem = 1 -- Nop. Let's block it
|
|
||||||
local uptime = tmr.time()
|
|
||||||
local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. "}}"
|
|
||||||
m:publish("IoT/Heartbeat/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
|
|
||||||
-- Callback function. We've sent the data
|
|
||||||
print("Heartbeat sent: " .. id1)
|
|
||||||
pub_sem = 0 -- Unblock the semaphore
|
|
||||||
id1 = id1 +1 -- Let's increase our counter
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function publish_status()
|
function publish_status()
|
||||||
if pub_sem == 0 then -- Is the semaphore set=
|
if pub_sem == 0 then
|
||||||
pub_sem = 1 -- Nop. Let's block it
|
pub_sem = 1
|
||||||
local uptime = tmr.time()
|
local uptime = tmr.time()
|
||||||
local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. ", \"state\":" .. switch_state .. "}}"
|
local msg = "{\"metadata\":{\"device\":\"WiFiSwitch" .. SWITCH_ID .. "\"}, \"data\":{\"uptime\":" .. uptime .. ", \"state\":" .. switch_state .. "}}"
|
||||||
m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
|
m:publish("IoT/Status/WiFiSwitch" .. SWITCH_ID, msg ,0,0, function(conn)
|
||||||
-- Callback function. We've sent the data
|
|
||||||
print("Status sent: " .. id2)
|
print("Status sent: " .. id2)
|
||||||
print("State: " .. switch_state)
|
print("State: " .. switch_state)
|
||||||
pub_sem = 0 -- Unblock the semaphore
|
pub_sem = 0
|
||||||
id2 = id2 +1 -- Let's increase our counter
|
id2 = id2 +1
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--main program to run after the subscriptions are done
|
|
||||||
function run_main_prog()
|
function run_main_prog()
|
||||||
print("Main program")
|
tmr.alarm(3, 1000, 1, publish_status )
|
||||||
|
|
||||||
tmr.alarm(2, 1000, 1, publish_heartbeat )
|
|
||||||
tmr.alarm(3, 1346, 1, publish_status )
|
|
||||||
-- Callback to receive the subscribed topic messages.
|
|
||||||
m:on("message", function(conn, topic, data)
|
m:on("message", function(conn, topic, data)
|
||||||
print(topic .. ":" )
|
print(topic .. ":" )
|
||||||
if (data ~= nil ) then
|
if (data ~= nil ) then
|
||||||
|
Loading…
x
Reference in New Issue
Block a user